intake/cmd/actionList.go

64 lines
1.3 KiB
Go

package cmd
import (
"fmt"
"log"
"slices"
"github.com/Jaculabilis/intake/core"
"github.com/spf13/cobra"
)
var actionListCmd = &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
Short: "List actions on a source",
Long: `List actions on a source.
`,
Run: func(cmd *cobra.Command, args []string) {
actionList(stringArg(cmd, "source"), boolArg(cmd, "argv"))
},
}
func init() {
actionCmd.AddCommand(actionListCmd)
actionListCmd.Flags().StringP("source", "s", "", "Source to list actions")
actionListCmd.MarkFlagRequired("source")
actionListCmd.Flags().BoolP("argv", "a", false, "Include action command")
}
func actionList(source string, argv bool) {
if source == "" {
log.Fatal("error: --source is empty")
}
db := openAndMigrateDb()
actions, err := core.GetActionsForSource(db, source)
if err != nil {
log.Fatal(err)
}
slices.SortFunc(actions, actionSort)
if argv {
actionArgv := make(map[string][]string)
for _, name := range actions {
argv, err := core.GetArgvForAction(db, source, name)
if err != nil {
log.Fatalf("error: could not get argv for source %s action %s: %v", source, name, err)
}
actionArgv[name] = argv
}
for _, name := range actions {
fmt.Printf("%s %v\n", name, actionArgv[name])
}
} else {
for _, action := range actions {
fmt.Println(action)
}
}
}