From 3465dfc95f92e363be7c54f98153bcab4c2410c7 Mon Sep 17 00:00:00 2001 From: simon Date: Tue, 26 May 2026 15:31:44 +0200 Subject: [PATCH] Default Cobra CMD Setup --- cmd/generate.go | 23 +++++++++++++++++++++ cmd/root.go | 43 +++++++++++++++++++++++++++++++++++++++ cmd/serve.go | 17 ++++++++++++++++ cmd/version.go | 29 ++++++++++++++++++++++++++ config.example.json | 4 ++++ go.mod | 10 +++++++++ go.sum | 10 +++++++++ internal/config/config.go | 36 ++++++++++++++++++++++++++++++++ main.go | 13 ++++++++++++ 9 files changed, 185 insertions(+) create mode 100644 cmd/generate.go create mode 100644 cmd/root.go create mode 100644 cmd/serve.go create mode 100644 cmd/version.go create mode 100644 config.example.json create mode 100644 go.mod create mode 100644 go.sum create mode 100644 internal/config/config.go create mode 100644 main.go diff --git a/cmd/generate.go b/cmd/generate.go new file mode 100644 index 0000000..12a44e0 --- /dev/null +++ b/cmd/generate.go @@ -0,0 +1,23 @@ +package cmd + +import ( + "github.com/spf13/cobra" +) + +var generateCmd = &cobra.Command{ + Use: "generate", + Short: "Generate project artifacts", +} + +var generateTemplateCmd = &cobra.Command{ + Use: "template", + Short: "Generate a template", + Run: func(cmd *cobra.Command, args []string) { + // TODO: implement template generation + }, +} + +func init() { + generateCmd.AddCommand(generateTemplateCmd) + rootCmd.AddCommand(generateCmd) +} diff --git a/cmd/root.go b/cmd/root.go new file mode 100644 index 0000000..fe0f7a6 --- /dev/null +++ b/cmd/root.go @@ -0,0 +1,43 @@ +package cmd + +import ( + "fmt" + "os" + + "github.com/spf13/cobra" + "printer.backend/internal/config" +) + +var ( + cfgFile string + cfg *config.Config +) + +var rootCmd = &cobra.Command{ + Use: "printer-backend", + Short: "Alox printer backend CLI", + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + if cfgFile == "" { + cfg = config.Default() + return nil + } + var err error + cfg, err = config.Load(cfgFile) + return err + }, + SilenceUsage: true, +} + +// Execute runs the root command. +func Execute() error { + return rootCmd.Execute() +} + +func init() { + rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "path to JSON config file") +} + +func exitWithError(err error) { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) +} diff --git a/cmd/serve.go b/cmd/serve.go new file mode 100644 index 0000000..06757f7 --- /dev/null +++ b/cmd/serve.go @@ -0,0 +1,17 @@ +package cmd + +import ( + "github.com/spf13/cobra" +) + +var serveCmd = &cobra.Command{ + Use: "serve", + Short: "Start the HTTP server", + Run: func(cmd *cobra.Command, args []string) { + // TODO: implement server + }, +} + +func init() { + rootCmd.AddCommand(serveCmd) +} diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 0000000..c21a827 --- /dev/null +++ b/cmd/version.go @@ -0,0 +1,29 @@ +package cmd + +import ( + "fmt" + "runtime" + + "github.com/spf13/cobra" +) + +var ( + version = "dev" + commit = "none" + date = "unknown" +) + +var versionCmd = &cobra.Command{ + Use: "version", + Short: "Print version information", + Run: func(cmd *cobra.Command, args []string) { + fmt.Printf("printer-backend %s\n", version) + fmt.Printf(" commit: %s\n", commit) + fmt.Printf(" built: %s\n", date) + fmt.Printf(" go: %s\n", runtime.Version()) + }, +} + +func init() { + rootCmd.AddCommand(versionCmd) +} diff --git a/config.example.json b/config.example.json new file mode 100644 index 0000000..15fd0fc --- /dev/null +++ b/config.example.json @@ -0,0 +1,4 @@ +{ + "host": "127.0.0.1", + "port": 8080 +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..e7c584d --- /dev/null +++ b/go.mod @@ -0,0 +1,10 @@ +module printer.backend + +go 1.26.3 + +require github.com/spf13/cobra v1.10.2 + +require ( + github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/spf13/pflag v1.0.9 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..a6ee3e0 --- /dev/null +++ b/go.sum @@ -0,0 +1,10 @@ +github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU= +github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTUUS4= +github.com/spf13/pflag v1.0.9 h1:9exaQaMOCwffKiiiYk6/BndUBv+iRViNW+4lEMi0PvY= +github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100644 index 0000000..2638d9e --- /dev/null +++ b/internal/config/config.go @@ -0,0 +1,36 @@ +package config + +import ( + "encoding/json" + "fmt" + "os" +) + +// Config is the application configuration loaded from JSON. +type Config struct { + Host string `json:"host"` + Port int `json:"port"` +} + +// Default returns sensible defaults when no config file is used. +func Default() *Config { + return &Config{ + Host: "127.0.0.1", + Port: 8080, + } +} + +// Load reads and parses a JSON config file from path. +func Load(path string) (*Config, error) { + data, err := os.ReadFile(path) + if err != nil { + return nil, fmt.Errorf("read config: %w", err) + } + + cfg := Default() + if err := json.Unmarshal(data, cfg); err != nil { + return nil, fmt.Errorf("parse config: %w", err) + } + + return cfg, nil +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..3bc9fc2 --- /dev/null +++ b/main.go @@ -0,0 +1,13 @@ +package main + +import ( + "os" + + "printer.backend/cmd" +) + +func main() { + if err := cmd.Execute(); err != nil { + os.Exit(1) + } +}