|
| 1 | +package command |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "net/url" |
| 8 | + "os/exec" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/gotify/cli/v2/config" |
| 13 | + "github.com/gotify/cli/v2/utils" |
| 14 | + "github.com/gotify/go-api-client/v2/models" |
| 15 | + "gopkg.in/urfave/cli.v1" |
| 16 | +) |
| 17 | + |
| 18 | +func Watch() cli.Command { |
| 19 | + return cli.Command{ |
| 20 | + Name: "watch", |
| 21 | + Usage: "watch the result of a command and pushes output difference", |
| 22 | + ArgsUsage: "<cmd>", |
| 23 | + Flags: []cli.Flag{ |
| 24 | + cli.Float64Flag{Name: "interval,n", Usage: "watch interval (sec)", Value: 2}, |
| 25 | + cli.IntFlag{Name: "priority,p", Usage: "Set the priority"}, |
| 26 | + cli.StringFlag{Name: "exec,x", Usage: "Pass command to exec", Value: "sh -c"}, |
| 27 | + cli.StringFlag{Name: "title,t", Usage: "Set the title (empty for command)"}, |
| 28 | + cli.StringFlag{Name: "token", Usage: "Override the app token"}, |
| 29 | + cli.StringFlag{Name: "url", Usage: "Override the Gotify URL"}, |
| 30 | + cli.StringFlag{Name: "output,o", Usage: "Output verbosity (short|default|long)", Value: "default"}, |
| 31 | + }, |
| 32 | + Action: doWatch, |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func doWatch(ctx *cli.Context) { |
| 37 | + conf, confErr := config.ReadConfig(config.GetLocations()) |
| 38 | + |
| 39 | + cmdArgs := ctx.Args() |
| 40 | + cmdStringNotation := strings.Join(cmdArgs, " ") |
| 41 | + execArgs := strings.Split(ctx.String("exec"), " ") |
| 42 | + cmdArgs = append(execArgs[1:], cmdStringNotation) |
| 43 | + execCmd := execArgs[0] |
| 44 | + |
| 45 | + outputMode := ctx.String("output") |
| 46 | + if !(outputMode == "default" || outputMode == "long" || outputMode == "short") { |
| 47 | + utils.Exit1With("output mode should be short|default|long") |
| 48 | + return |
| 49 | + } |
| 50 | + interval := ctx.Float64("interval") |
| 51 | + priority := ctx.Int("priority") |
| 52 | + title := ctx.String("title") |
| 53 | + if title == "" { |
| 54 | + title = cmdStringNotation |
| 55 | + } |
| 56 | + token := ctx.String("token") |
| 57 | + if token == "" { |
| 58 | + if confErr != nil { |
| 59 | + utils.Exit1With("token is not configured, run 'gotify init'") |
| 60 | + return |
| 61 | + } |
| 62 | + token = conf.Token |
| 63 | + } |
| 64 | + stringURL := ctx.String("url") |
| 65 | + if stringURL == "" { |
| 66 | + if confErr != nil { |
| 67 | + utils.Exit1With("url is not configured, run 'gotify init'") |
| 68 | + return |
| 69 | + } |
| 70 | + stringURL = conf.URL |
| 71 | + } |
| 72 | + parsedURL, err := url.Parse(stringURL) |
| 73 | + if err != nil { |
| 74 | + utils.Exit1With("invalid url", stringURL) |
| 75 | + return |
| 76 | + } |
| 77 | + |
| 78 | + watchInterval := time.Duration(interval*1000) * time.Millisecond |
| 79 | + |
| 80 | + evalCmdOutput := func() (string, error) { |
| 81 | + cmd := exec.Command(execCmd, cmdArgs...) |
| 82 | + timeOut := time.After(watchInterval) |
| 83 | + outputBuf := bytes.NewBuffer([]byte{}) |
| 84 | + cmd.Stdout = outputBuf |
| 85 | + cmd.Stderr = outputBuf |
| 86 | + err := cmd.Start() |
| 87 | + if err != nil { |
| 88 | + return "", fmt.Errorf("command failed to invoke: %v", err) |
| 89 | + } |
| 90 | + done := make(chan error) |
| 91 | + go func() { |
| 92 | + err := cmd.Wait() |
| 93 | + if err != nil { |
| 94 | + done <- fmt.Errorf("command failed to invoke: %v", err) |
| 95 | + } |
| 96 | + done <- nil |
| 97 | + }() |
| 98 | + select { |
| 99 | + case err := <-done: |
| 100 | + return outputBuf.String(), err |
| 101 | + case <-timeOut: |
| 102 | + cmd.Process.Kill() |
| 103 | + return outputBuf.String(), errors.New("command timed out") |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + lastOutput, err := evalCmdOutput() |
| 108 | + if err != nil { |
| 109 | + utils.Exit1With("first run failed", err) |
| 110 | + } |
| 111 | + for range time.NewTicker(watchInterval).C { |
| 112 | + output, err := evalCmdOutput() |
| 113 | + if err != nil { |
| 114 | + output += fmt.Sprintf("\n!== <%v> ==!", err) |
| 115 | + } |
| 116 | + if output != lastOutput { |
| 117 | + msgData := bytes.NewBuffer([]byte{}) |
| 118 | + |
| 119 | + switch outputMode { |
| 120 | + case "long": |
| 121 | + fmt.Fprintf(msgData, "command output for \"%s\" changed:\n\n", cmdStringNotation) |
| 122 | + fmt.Fprintln(msgData, "== BEGIN OLD OUTPUT ==") |
| 123 | + fmt.Fprint(msgData, lastOutput) |
| 124 | + fmt.Fprintln(msgData, "== END OLD OUTPUT ==") |
| 125 | + fmt.Fprintln(msgData, "== BEGIN NEW OUTPUT ==") |
| 126 | + fmt.Fprint(msgData, output) |
| 127 | + fmt.Fprintln(msgData, "== END NEW OUTPUT ==") |
| 128 | + case "default": |
| 129 | + fmt.Fprintf(msgData, "command output for \"%s\" changed:\n\n", cmdStringNotation) |
| 130 | + fmt.Fprintln(msgData, "== BEGIN NEW OUTPUT ==") |
| 131 | + fmt.Fprint(msgData, output) |
| 132 | + fmt.Fprintln(msgData, "== END NEW OUTPUT ==") |
| 133 | + case "short": |
| 134 | + fmt.Fprintf(msgData, output) |
| 135 | + } |
| 136 | + |
| 137 | + msgString := msgData.String() |
| 138 | + fmt.Println(msgString) |
| 139 | + pushMessage(parsedURL, token, models.MessageExternal{ |
| 140 | + Title: title, |
| 141 | + Message: msgString, |
| 142 | + Priority: priority, |
| 143 | + }, true) |
| 144 | + lastOutput = output |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | +} |
0 commit comments