intake/cmd/sourceAdd.go

40 lines
726 B
Go
Raw Normal View History

2025-01-21 16:42:59 +00:00
package cmd
import (
"log"
2025-01-23 15:55:11 +00:00
"github.com/Jaculabilis/intake/core"
2025-01-21 16:42:59 +00:00
"github.com/spf13/cobra"
)
var sourceAddCmd = &cobra.Command{
Use: "add",
Short: "Create a source",
2025-01-23 15:55:11 +00:00
Long: `Create 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
sourceAdd(stringArg(cmd, "source"))
2025-01-21 16:42:59 +00:00
},
}
func init() {
sourceCmd.AddCommand(sourceAddCmd)
2025-01-23 15:55:11 +00:00
2025-01-30 15:54:13 +00:00
sourceAddCmd.Flags().StringP("source", "s", "", "Source name")
2025-01-23 15:55:11 +00:00
sourceAddCmd.MarkFlagRequired("source")
}
2025-01-30 15:54:13 +00:00
func sourceAdd(source string) {
if source == "" {
2025-01-23 15:55:11 +00:00
log.Fatal("error: --source is empty")
}
db := openAndMigrateDb()
2025-01-30 15:54:13 +00:00
if err := core.AddSource(db, source); err != nil {
2025-01-23 21:22:38 +00:00
log.Fatalf("error: failed to add source: %v", err)
2025-01-23 15:55:11 +00:00
}
2025-01-30 15:54:13 +00:00
log.Printf("Added source %s", source)
2025-01-21 16:42:59 +00:00
}