forked from mkideal/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cliutil.go
126 lines (115 loc) · 2.78 KB
/
cliutil.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package cli
import (
"bufio"
"encoding/json"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/labstack/gommon/color"
"github.com/mattn/go-isatty"
)
func colorSwitch(clr *color.Color, w io.Writer, fds ...uintptr) {
clr.Disable()
if len(fds) > 0 {
if isatty.IsTerminal(fds[0]) {
clr.Enable()
}
} else if w, ok := w.(*os.File); ok && isatty.IsTerminal(w.Fd()) {
clr.Enable()
}
}
// HelpCommandFn implements buildin help command function
func HelpCommandFn(ctx *Context) error {
var (
args = ctx.NativeArgs()
parent = ctx.Command().Parent()
)
if len(args) == 0 {
ctx.String(parent.Usage(ctx))
return nil
}
var (
child = parent.Route(args)
clr = ctx.Color()
)
if child == nil {
return fmt.Errorf("command %s not found", clr.Yellow(strings.Join(args, " ")))
}
ctx.String(child.Usage(ctx))
return nil
}
// HelpCommand returns a buildin help command
func HelpCommand(desc string) *Command {
return &Command{
Name: "help",
Desc: desc,
CanSubRoute: true,
NoHook: true,
Fn: HelpCommandFn,
}
}
// Daemon startup app as a daemon process, success if result from stderr has prefix successPrefix
func Daemon(ctx *Context, successPrefix string) error {
cmd := exec.Command(os.Args[0], ctx.NativeArgs()...)
serr, err := cmd.StderrPipe()
if err != nil {
return err
}
if err := cmd.Start(); err != nil {
return err
}
reader := bufio.NewReader(serr)
line, err := reader.ReadString('\n')
if err != nil {
return err
}
if strings.HasPrefix(line, successPrefix) {
ctx.String(line)
cmd.Process.Release()
} else {
cmd.Process.Kill()
line = strings.TrimSuffix(line, "\n")
return fmt.Errorf(line)
}
return nil
}
// DaemonResponse output response to stderr
func DaemonResponse(resp string) {
fmt.Fprintln(os.Stderr, resp)
}
// ReadJSON reads data as a json structure into argv
func ReadJSON(r io.Reader, argv interface{}) error {
return json.NewDecoder(r).Decode(argv)
}
// ReadJSONFromFile is similar to ReadJSON, but read from file
func ReadJSONFromFile(filename string, argv interface{}) error {
file, err := os.Open(filename)
if err == nil {
defer file.Close()
err = ReadJSON(file, argv)
}
return err
}
// ReadJSONConfigFromFile is similar to ReadJSONFromFile, but allows reading file from where the executable file resides as well
func ReadJSONConfigFromFile(filename string, argv interface{}) error {
file, err := os.Open(filename)
if err == nil {
defer file.Close()
err = ReadJSON(file, argv)
} else {
exe, e := os.Executable()
if e != nil {
return e
}
// allow self-config .json files to go with the executable file, #40
file, err = os.Open(filepath.Dir(exe) + string(filepath.Separator) + filename)
if err == nil {
defer file.Close()
err = ReadJSON(file, argv)
}
}
return err
}