package cmd

import (
	"log"

	"github.com/Jaculabilis/intake/core"
	"github.com/spf13/cobra"
)

var sourceAddCmd = &cobra.Command{
	Use:   "add",
	Short: "Create a source",
	Long: `Create a source.
`,
	Run: func(cmd *cobra.Command, args []string) {
		sourceAdd(stringArg(cmd, "source"))
	},
}

func init() {
	sourceCmd.AddCommand(sourceAddCmd)

	sourceAddCmd.Flags().StringP("source", "s", "", "Source name")
	sourceAddCmd.MarkFlagRequired("source")
}

func sourceAdd(source string) {
	if source == "" {
		log.Fatal("error: --source is empty")
	}

	db := openAndMigrateDb()

	if err := core.AddSource(db, source); err != nil {
		log.Fatalf("error: failed to add source: %v", err)
	}

	log.Printf("Added source %s", source)
}