intake/cmd/actionAdd.go

52 lines
1.2 KiB
Go
Raw Normal View History

2025-01-21 16:42:59 +00:00
package cmd
import (
"log"
"github.com/Jaculabilis/intake/core"
2025-01-21 16:42:59 +00:00
"github.com/spf13/cobra"
)
var actionAddCmd = &cobra.Command{
Use: "add [flags] -- argv...",
2025-01-21 16:42:59 +00:00
Short: "Add an action to a source",
Long: `Add an action to a source.
2025-01-21 16:42:59 +00:00
`,
Run: func(cmd *cobra.Command, args []string) {
2025-01-30 15:54:13 +00:00
actionAdd(stringArg(cmd, "source"), stringArg(cmd, "action"), getArgv(cmd, args))
2025-01-21 16:42:59 +00:00
},
}
func init() {
actionCmd.AddCommand(actionAddCmd)
2025-01-30 15:54:13 +00:00
actionAddCmd.Flags().StringP("source", "s", "", "Source to add action")
actionAddCmd.MarkFlagRequired("source")
2025-01-30 15:54:13 +00:00
actionAddCmd.Flags().StringP("action", "a", "", "Action name")
actionAddCmd.MarkFlagRequired("action")
}
// TODO: This is a duplicate of `action edit`, the action CLI should be simplified
2025-01-30 15:54:13 +00:00
func actionAdd(source string, action string, argv []string) {
if source == "" {
log.Fatal("error: --source is empty")
}
2025-01-30 15:54:13 +00:00
if action == "" {
log.Fatal("error: --action is empty")
}
if len(argv) == 0 {
log.Fatal("error: no argv provided")
}
db := openAndMigrateDb()
err := core.SetAction(db, source, action, argv)
if err != nil {
2025-01-23 21:22:38 +00:00
log.Fatalf("error: failed to add action: %v", err)
}
2025-01-30 15:54:13 +00:00
log.Printf("Added action %s to source %s", action, source)
2025-01-21 16:42:59 +00:00
}