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()
	},
}

var sourceAddSource string

func init() {
	sourceCmd.AddCommand(sourceAddCmd)

	sourceAddCmd.Flags().StringVarP(&sourceAddSource, "source", "s", "", "Source name")
	sourceAddCmd.MarkFlagRequired("source")
}

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

	db := openAndMigrateDb()

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

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