feat: base implementation

This commit is contained in:
2025-07-20 13:47:16 +02:00
parent ad082a3f12
commit 7d561ea6ea
21 changed files with 635 additions and 0 deletions

14
cmd/config.go Normal file
View File

@ -0,0 +1,14 @@
package cmd
type AppConfig struct {
OutputVerbose bool
OutputMode string
}
type OutputMode string
const (
Go OutputMode = "go"
Json OutputMode = "json"
Yaml OutputMode = "yaml"
)

5
cmd/output.go Normal file
View File

@ -0,0 +1,5 @@
package cmd
type OutputProvider interface {
Convert(item any) (string, error)
}

19
cmd/resources.go Normal file
View File

@ -0,0 +1,19 @@
package cmd
import (
"git.bissendorf.co/bissendorf/unifood/m/v2/core/handler/menu"
"git.bissendorf.co/bissendorf/unifood/m/v2/core/interfaces"
"git.bissendorf.co/bissendorf/unifood/m/v2/core/services/stwhbclient"
"git.bissendorf.co/bissendorf/unifood/m/v2/model/external/stwbremen"
)
var availableResources = []interfaces.ResourceCommand[any]{
{
Name: "menu",
Aliases: []string{"m"},
Verbs: []interfaces.Verb{interfaces.VerbGet},
Handler: &menu.MenuHandler{
QueryClient: stwhbclient.New[[]stwbremen.Dish](),
},
},
}

42
cmd/root.go Normal file
View File

@ -0,0 +1,42 @@
package cmd
import (
"context"
"fmt"
"log/slog"
"os"
"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) {
},
}
func Execute() {
initRootCmd()
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func initRootCmd() {
var appConfig AppConfig
rootCmd.PersistentFlags().BoolVarP(&appConfig.OutputVerbose, "verbose", "v", false, "Enable verbose output")
rootCmd.PersistentFlags().StringVarP(&appConfig.OutputMode, "output", "o", string(Json), "Set output format")
logger := jlog.New(slog.LevelDebug)
ctx := jlog.ContextWith(context.Background(), logger)
logger.Debug("Register verb commands")
rootCmd.AddCommand(getVerbs(ctx)...)
logger.Debug("Verb commands registered successfully")
}

99
cmd/verbs.go Normal file
View File

@ -0,0 +1,99 @@
package cmd
import (
"context"
"encoding/json"
"fmt"
"log/slog"
"os"
"slices"
"strings"
"git.bissendorf.co/bissendorf/unifood/m/v2/core/interfaces"
"git.bissendorf.co/bissendorf/unifood/m/v2/core/interfaces/params"
"git.bissendorf.co/bissendorf/unifood/m/v2/core/services/jlog"
"github.com/spf13/cobra"
)
type VerbItem struct {
Name interfaces.Verb
Aliases []string
Description string
RunFn func(ctx context.Context, handler interfaces.ResourceHandler, params params.Container) error
}
var verbs = []VerbItem{
{
Name: interfaces.VerbGet,
Description: "Retrieve resource information",
RunFn: func(ctx context.Context, handler interfaces.ResourceHandler, params params.Container) error {
h, ok := handler.(interfaces.GetHandler)
if !ok {
return fmt.Errorf("resource does not support GET")
}
item, err := h.Get(ctx, params)
if err != nil {
return fmt.Errorf("retrieving item failed: %w", err)
}
json.NewEncoder(os.Stdout).Encode(item)
return nil
},
},
}
func getVerbs(ctx context.Context) (commands []*cobra.Command) {
logger := jlog.FromContext(ctx)
for _, v := range verbs {
verbCommand := &cobra.Command{
Use: strings.ToLower(string(v.Name)),
Aliases: v.Aliases,
Short: v.Description,
}
// Add all resources that can handle this verb
for _, r := range availableResources {
if !slices.Contains(r.Verbs, v.Name) {
continue
}
var params params.Container
resourceCommand := &cobra.Command{
Use: r.Name,
Aliases: r.Aliases,
Short: r.Description,
Run: func(cmd *cobra.Command, args []string) {
logger := jlog.New(slog.LevelInfo)
ctx := jlog.ContextWith(context.Background(), logger)
err := v.RunFn(ctx, r.Handler, params)
if err != nil {
logger.ErrorContext(ctx, fmt.Sprintf("%s %s failed", strings.ToUpper(string(v.Name)), r.Name), "error", err.Error())
os.Exit(1)
}
},
}
// Register parameters
for _, param := range r.Handler.GetParametersForVerb(v.Name) {
resourceCommand.Flags().StringVarP(
params.Register(param.Name, param.ParseFunc),
param.Name,
param.ShortHand,
param.DefaultFunc(),
param.Description,
)
}
verbCommand.AddCommand(resourceCommand)
logger.Debug(fmt.Sprintf("Registered %s %s", strings.ToUpper(string(v.Name)), r.Name))
}
commands = append(commands, verbCommand)
}
return
}