55 lines
1.5 KiB
Go
55 lines
1.5 KiB
Go
package cmd
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
"maps"
|
|
"os"
|
|
"slices"
|
|
"strings"
|
|
|
|
"git.bissendorf.co/bissendorf/unifood/m/v2/core/output"
|
|
"git.bissendorf.co/bissendorf/unifood/m/v2/core/services/jlog"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var rootCmd = &cobra.Command{
|
|
Use: "unifood",
|
|
Short: "Unifood is a CLI for retrieving restaurant information",
|
|
Long: ``,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
_ = cmd.Help()
|
|
},
|
|
}
|
|
|
|
func Execute() {
|
|
initRootCmd()
|
|
|
|
if err := rootCmd.Execute(); err != nil {
|
|
fmt.Println(err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func initRootCmd() {
|
|
var appConfig AppConfig
|
|
|
|
// Compile list of available formatters
|
|
formatters := slices.AppendSeq([]string{}, maps.Keys(output.Formatters))
|
|
formattersList := fmt.Sprintf("(available: %s)", strings.Join(formatters, ", "))
|
|
|
|
// Add persistent flags
|
|
rootCmd.PersistentFlags().BoolVarP(&appConfig.OutputVerbose, "verbose", "v", false, "Enable verbose output")
|
|
rootCmd.PersistentFlags().StringVarP(&appConfig.OutputFormatter, "output", "o", "table", "Set output format "+formattersList)
|
|
rootCmd.PersistentFlags().BoolVar(&appConfig.OutputOrderReverse, "reverse", false, "Reverses output item order")
|
|
rootCmd.PersistentFlags().BoolVar(&appConfig.PrintConfig, "print-config", false, "Enable printing the application config")
|
|
|
|
// Create logger and add child commands
|
|
logger := jlog.New(slog.LevelDebug)
|
|
ctx := jlog.ContextWith(context.Background(), logger)
|
|
|
|
rootCmd.AddCommand(&versionCmd)
|
|
rootCmd.AddCommand(getVerbs(ctx, &appConfig)...)
|
|
}
|