// Package cli implements the proxy command line. package cli import ( "errors" "fmt" "net/http" "os" "strings" "github.com/spf13/cobra" ) const ( exitError = 1 exitUsage = 2 ) // Build identifies the running binary. type Build struct { Version string Commit string } // flagError marks a flag parsing failure so Run can exit with exitUsage. type flagError struct{ err error } func (e *flagError) Error() string { return e.err.Error() } func (e *flagError) Unwrap() error { return e.err } // Run executes the CLI and returns its exit code. func Run(args []string, build Build) int { root := Root(build) root.SetArgs(prepareArgs(root, args)) cmd, err := root.ExecuteC() if err == nil { return 0 } fmt.Fprintln(os.Stderr, err) var fe *flagError if errors.As(err, &fe) { fmt.Fprint(os.Stderr, cmd.UsageString()) return exitUsage } return exitError } // Root returns the full command tree. The root command is serve, so proxy // with no command starts the server. func Root(build Build) *cobra.Command { root := newServeCmd(build) root.Use = "proxy" root.Short = "Package registry caching proxy" root.Long = "git-pkgs proxy caches package artifacts from upstream registries.\n\n" + "With no command, proxy runs serve.\n\n" + serveEnvHelp root.Args = nil root.SilenceErrors = true root.SilenceUsage = true root.SetFlagErrorFunc(func(_ *cobra.Command, err error) error { return &flagError{err: err} }) root.AddCommand( newServeCmd(build), newStatsCmd(), newMirrorCmd(), newHostedCmd(os.Getenv, os.Stdout, &http.Client{Timeout: hostedClientTimeout}), ) return root } // prepareArgs finds the command the leading words of args name and rewrites // the single-dash long flags that follow to the two-dash form cobra parses. func prepareArgs(root *cobra.Command, args []string) []string { cmd, rest := root, args for len(rest) > 0 { next := subcommand(cmd, rest[0]) if next == nil { break } cmd, rest = next, rest[1:] } out := make([]string, 0, len(args)) out = append(out, args[:len(args)-len(rest)]...) return append(out, normalizeArgs(cmd, rest)...) } func subcommand(cmd *cobra.Command, name string) *cobra.Command { for _, sub := range cmd.Commands() { if sub.Name() == name { return sub } } return nil } // normalizeArgs rewrites -name to --name for every long flag cmd defines. // The value following a non-boolean flag is copied through untouched. func normalizeArgs(cmd *cobra.Command, args []string) []string { cmd.InitDefaultHelpFlag() flags := cmd.Flags() out := make([]string, 0, len(args)) for i := 0; i < len(args); i++ { arg := args[i] if arg == "--" { return append(out, args[i:]...) } name, hasValue := longFlagName(arg) f := flags.Lookup(name) if f == nil { out = append(out, arg) continue } out = append(out, "--"+strings.TrimLeft(arg, "-")) if !hasValue && f.NoOptDefVal == "" && i+1 < len(args) { i++ out = append(out, args[i]) } } return out } // longFlagName returns the flag name in -name, --name, -name=v or --name=v, // and whether a value is attached. It returns "" for anything else, // including single-letter shorthands. func longFlagName(arg string) (name string, hasValue bool) { rest, ok := strings.CutPrefix(arg, "-") if !ok { return "", false } rest, doubleDash := strings.CutPrefix(rest, "-") name, _, hasValue = strings.Cut(rest, "=") if !doubleDash && len(name) == 1 { return "", false } return name, hasValue }