2
0
mirror of https://github.com/skanehira/tson synced 2024-11-17 09:25:31 +00:00
tson/main.go

72 lines
1.1 KiB
Go
Raw Normal View History

2019-10-29 14:01:24 +00:00
package main
import (
"encoding/json"
2019-10-30 01:49:33 +00:00
"flag"
2019-10-29 14:01:24 +00:00
"fmt"
"io/ioutil"
"log"
"os"
2019-10-30 01:49:33 +00:00
"path/filepath"
2019-10-29 14:01:24 +00:00
2019-10-30 01:49:33 +00:00
"github.com/mitchellh/go-homedir"
2019-10-29 17:28:25 +00:00
"github.com/skanehira/tson/gui"
2019-10-29 14:01:24 +00:00
"golang.org/x/crypto/ssh/terminal"
)
2019-10-30 01:49:33 +00:00
var (
enableLog = flag.Bool("log", false, "enable log")
)
func init() {
flag.Parse()
if *enableLog {
home, err := homedir.Dir()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
logWriter, err := os.OpenFile(filepath.Join(home, "tson.log"), os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
log.SetOutput(logWriter)
log.SetFlags(log.Lshortfile)
} else {
log.SetOutput(ioutil.Discard)
}
}
2019-10-29 14:01:24 +00:00
func run() int {
if !terminal.IsTerminal(0) {
b, err := ioutil.ReadAll(os.Stdin)
if err != nil {
2019-10-29 17:28:25 +00:00
fmt.Fprintln(os.Stderr, err)
2019-10-29 14:01:24 +00:00
return 1
}
2019-10-29 17:28:25 +00:00
if len(b) == 0 {
fmt.Println("json is empty")
return 0
}
2019-10-29 14:01:24 +00:00
var t interface{}
if err := json.Unmarshal(b, &t); err != nil {
2019-10-29 17:28:25 +00:00
fmt.Fprintln(os.Stderr, err)
2019-10-29 14:01:24 +00:00
return 1
}
2019-10-29 17:28:25 +00:00
if err := gui.New().Run(t); err != nil {
log.Println(err)
return 1
}
2019-10-29 14:01:24 +00:00
}
return 0
}
func main() {
os.Exit(run())
}