Default Cobra CMD Setup

This commit is contained in:
2026-05-26 15:31:44 +02:00
commit 3465dfc95f
9 changed files with 185 additions and 0 deletions
+23
View File
@@ -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)
}
+43
View File
@@ -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)
}
+17
View File
@@ -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)
}
+29
View File
@@ -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)
}