package cmd

import (
	"fmt"
	"runtime"

	"code.icod.de/dalu/gomanager/ent"
	"code.icod.de/dalu/gomanager/runner"
	"github.com/spf13/cobra"

	_ "github.com/mattn/go-sqlite3"
)

// cronCmd represents the cron command
var cronCmd = &cobra.Command{
	Use:   "cron",
	Short: "this command is meant to be ran via cron",
	RunE: func(cmd *cobra.Command, args []string) error {
		runtime.GOMAXPROCS(1)
		client, err := ent.Open("sqlite3", fmt.Sprintf("file:%s?mode=rwc&cache=private&_fk=1", sqliteFilename))
		if err != nil {
			return fmt.Errorf("failed opening connection to sqlite: %v", err)
		}
		defer client.Close()
		r := runner.NewRunner(client)
		return r.Run()
	},
}

func init() {
	rootCmd.AddCommand(cronCmd)

	// Here you will define your flags and configuration settings.

	// Cobra supports Persistent Flags which will work for this command
	// and all subcommands, e.g.:
	// cronCmd.PersistentFlags().String("foo", "", "A help for foo")

	// Cobra supports local flags which will only run when this command
	// is called directly, e.g.:
	// cronCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
