Add -config flag using cobra to specify config file path

This commit is contained in:
2026-01-08 04:58:05 -08:00
parent 271527679e
commit 6a20406b0f
4 changed files with 82 additions and 23 deletions

View File

@@ -2,6 +2,10 @@
package main
import (
"fmt"
"os"
"github.com/spf13/cobra"
"go.uber.org/fx"
"sneak.berlin/go/pixa/internal/config"
"sneak.berlin/go/pixa/internal/database"
@@ -19,11 +23,33 @@ var (
Buildarch string //nolint:gochecknoglobals // set by ldflags
)
var configPath string //nolint:gochecknoglobals // cobra flag
func main() {
rootCmd := &cobra.Command{
Use: "pixad",
Short: "Pixa image caching proxy server",
Run: run,
}
rootCmd.Flags().StringVarP(&configPath, "config", "c", "", "path to config file")
if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func run(_ *cobra.Command, _ []string) {
globals.Appname = Appname
globals.Version = Version
globals.Buildarch = Buildarch
// Set config path in environment if specified via flag
if configPath != "" {
_ = os.Setenv("PIXA_CONFIG_PATH", configPath)
}
fx.New(
fx.Provide(
config.New,