fleet/cmd/fleetctl/trigger.go

63 lines
1.4 KiB
Go
Raw Normal View History

2022-12-06 15:56:54 +00:00
package main
import (
"fmt"
"strings"
"github.com/fleetdm/fleet/v4/server/contexts/ctxerr"
"github.com/fleetdm/fleet/v4/server/service"
"github.com/urfave/cli/v2"
)
func triggerCommand() *cli.Command {
var name string
return &cli.Command{
Name: "trigger",
Usage: "Trigger an ad hoc run of all jobs in a specified cron schedule",
UsageText: `fleetctl trigger [options]`,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "name",
EnvVars: []string{"NAME"},
Value: "",
Destination: &name,
Usage: "Name of the cron schedule to trigger",
},
configFlag(),
contextFlag(),
debugFlag(),
},
Action: func(c *cli.Context) error {
client, err := clientFromCLI(c)
if err != nil {
return err
}
if err := client.TriggerCronSchedule(name); err != nil {
root := ctxerr.Cause(err)
switch root.(type) {
case service.NotFoundErr, service.ConflictErr:
fmt.Println(fmt.Sprintf("[!] %s", formatTriggerErrMsg(name, root.Error())))
return nil
default:
return err
}
}
fmt.Println(fmt.Sprintf("[+] Sent request to trigger %s schedule", name))
return nil
},
}
}
func formatTriggerErrMsg(name string, msg string) string {
formatted := msg
if name == "" {
formatted = strings.Replace(strings.ToLower(msg), "invalid name", "name must be specified", 1)
}
if len(formatted) >= 1 {
formatted = strings.ToUpper(formatted[:1]) + formatted[1:]
}
return formatted
}