53 lines
1.2 KiB
Go
53 lines
1.2 KiB
Go
package cmd
|
|
|
|
import (
|
|
"log"
|
|
|
|
"github.com/Jaculabilis/intake/core"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var actionAddCmd = &cobra.Command{
|
|
Use: "add [flags] -- argv...",
|
|
Short: "Add an action to a source",
|
|
Long: `Add an action to a source.
|
|
`,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
actionAdd(getArgv(cmd, args))
|
|
},
|
|
}
|
|
|
|
var actionAddSource string
|
|
var actionAddAction string
|
|
|
|
func init() {
|
|
actionCmd.AddCommand(actionAddCmd)
|
|
|
|
actionAddCmd.Flags().StringVarP(&actionAddSource, "source", "s", "", "Source to add action")
|
|
actionAddCmd.MarkFlagRequired("source")
|
|
|
|
actionAddCmd.Flags().StringVarP(&actionAddAction, "action", "a", "", "Action name")
|
|
actionAddCmd.MarkFlagRequired("action")
|
|
}
|
|
|
|
func actionAdd(argv []string) {
|
|
if actionAddSource == "" {
|
|
log.Fatal("error: --source is empty")
|
|
}
|
|
if actionAddAction == "" {
|
|
log.Fatal("error: --action is empty")
|
|
}
|
|
if len(argv) == 0 {
|
|
log.Fatal("error: no argv provided")
|
|
}
|
|
|
|
db := openAndMigrateDb()
|
|
|
|
err := core.AddAction(db, actionAddSource, actionAddAction, argv)
|
|
if err != nil {
|
|
log.Fatalf("error: failed to add action: %v", err)
|
|
}
|
|
|
|
log.Printf("Added action %s to source %s", actionAddAction, actionAddSource)
|
|
}
|