-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.go
47 lines (35 loc) · 1003 Bytes
/
root.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
package watch
import (
"strings"
"github.com/samber/lo"
"github.com/spf13/cobra"
"github.com/xuender/kit/logs"
"github.com/xuender/kit/los"
"github.com/youthlin/t"
)
func NewCmd(cmd *cobra.Command) *cobra.Command {
cmd.Short = t.T("Watch and Run")
cmd.Long = t.T("Watch to the directory and run a command. If the directory is modified, restart the command.")
cmd.Example = t.T(" # Watch dir\n go-cli watch [command]")
cmd.Flags().StringP("path", "p", "", t.T("watch path, default is current directory"))
cmd.Run = run
return cmd
}
func run(cmd *cobra.Command, args []string) {
if len(args) == 0 {
lo.Must0(cmd.Usage())
return
}
logs.D.Println(strings.Join(args, " "))
if strings.Contains(args[0], " ") {
args = append(los.SplitStr(args[0], ' ', '\t'), args[1:]...)
}
watch := NewService()
defer watch.Close()
if path, err := cmd.Flags().GetString("path"); err == nil && path != "" {
watch.Add(path)
} else {
watch.Add(".")
}
watch.Run(args[0], args[1:])
}