2
0
mirror of https://github.com/skanehira/tson synced 2024-11-03 09:40:22 +00:00

add log flag

This commit is contained in:
skanehira 2019-10-30 10:49:33 +09:00
parent 55a32b05ed
commit d91c9a55e5
3 changed files with 32 additions and 0 deletions

1
go.mod
View File

@ -4,6 +4,7 @@ go 1.13
require ( require (
github.com/gdamore/tcell v1.3.0 github.com/gdamore/tcell v1.3.0
github.com/mitchellh/go-homedir v1.1.0
github.com/rivo/tview v0.0.0-20191018125527-685bf6da76c2 github.com/rivo/tview v0.0.0-20191018125527-685bf6da76c2
golang.org/x/crypto v0.0.0-20191029031824-8986dd9e96cf golang.org/x/crypto v0.0.0-20191029031824-8986dd9e96cf
) )

2
go.sum
View File

@ -8,6 +8,8 @@ github.com/lucasb-eyer/go-colorful v1.0.2 h1:mCMFu6PgSozg9tDNMMK3g18oJBX7oYGrC09
github.com/lucasb-eyer/go-colorful v1.0.2/go.mod h1:0MS4r+7BZKSJ5mw4/S5MPN+qHFF1fYclkSPilDOKW0s= github.com/lucasb-eyer/go-colorful v1.0.2/go.mod h1:0MS4r+7BZKSJ5mw4/S5MPN+qHFF1fYclkSPilDOKW0s=
github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y= github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y=
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/rivo/tview v0.0.0-20191018125527-685bf6da76c2 h1:GVXSfgXOMAeLvFH7IrpY3yYM8H3YekZEFcZ14q9gQXM= github.com/rivo/tview v0.0.0-20191018125527-685bf6da76c2 h1:GVXSfgXOMAeLvFH7IrpY3yYM8H3YekZEFcZ14q9gQXM=
github.com/rivo/tview v0.0.0-20191018125527-685bf6da76c2/go.mod h1:/rBeY22VG2QprWnEqG57IBC8biVu3i0DOIjRLc9I8H0= github.com/rivo/tview v0.0.0-20191018125527-685bf6da76c2/go.mod h1:/rBeY22VG2QprWnEqG57IBC8biVu3i0DOIjRLc9I8H0=
github.com/rivo/uniseg v0.1.0 h1:+2KBaVoUmb9XzDsrx/Ct0W/EYOSFf/nWTauy++DprtY= github.com/rivo/uniseg v0.1.0 h1:+2KBaVoUmb9XzDsrx/Ct0W/EYOSFf/nWTauy++DprtY=

29
main.go
View File

@ -2,15 +2,44 @@ package main
import ( import (
"encoding/json" "encoding/json"
"flag"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
"path/filepath"
"github.com/mitchellh/go-homedir"
"github.com/skanehira/tson/gui" "github.com/skanehira/tson/gui"
"golang.org/x/crypto/ssh/terminal" "golang.org/x/crypto/ssh/terminal"
) )
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)
}
}
func run() int { func run() int {
if !terminal.IsTerminal(0) { if !terminal.IsTerminal(0) {
b, err := ioutil.ReadAll(os.Stdin) b, err := ioutil.ReadAll(os.Stdin)