check / check (push) Waiting to run
`mfer version`, `mfer --version` and the banner print the revision the build stamps into main.Gitrev or, when it stamped none (as after `go install` of a tag), the main module's version from runtime/debug.ReadBuildInfo. The banner no longer shows a release date. mfer.Version and mfer.ReleaseDate go, with main.Version and the version field and parameter in internal/cli that nothing read. A program using the library reads the module version from its own build information. The README says where the version comes from. Judgement call: a binary with no build information, or none holding a module version, prints "unknown", the word script/build already uses when git describe gives nothing. Model: opus-5-5
72 lines
2.0 KiB
Go
72 lines
2.0 KiB
Go
package cli
|
|
|
|
import (
|
|
"io"
|
|
"os"
|
|
|
|
"github.com/spf13/afero"
|
|
"sneak.berlin/go/mfer/mfer"
|
|
)
|
|
|
|
// NoColor disables colored output when set. Automatically true if the
|
|
// NO_COLOR environment variable is present (per https://no-color.org/).
|
|
//
|
|
//nolint:gochecknoglobals // process-wide setting derived from the environment
|
|
var NoColor = noColorEnvSet()
|
|
|
|
// noColorEnvSet reports whether the NO_COLOR environment variable is
|
|
// present.
|
|
func noColorEnvSet() bool {
|
|
_, exists := os.LookupEnv("NO_COLOR")
|
|
|
|
return exists
|
|
}
|
|
|
|
// RunOptions contains all configuration for running the CLI application.
|
|
// Use DefaultRunOptions for standard CLI execution, or construct manually for testing.
|
|
type RunOptions struct {
|
|
Appname string // Application name displayed in help and version output
|
|
Gitrev string // Revision stamped in at build time, or empty
|
|
Args []string // Command-line arguments (typically os.Args)
|
|
Stdin io.Reader // Standard input stream
|
|
Stdout io.Writer // Standard output stream
|
|
Stderr io.Writer // Standard error stream
|
|
Fs afero.Fs // Filesystem abstraction for file operations
|
|
}
|
|
|
|
// DefaultRunOptions returns RunOptions configured for normal CLI execution.
|
|
func DefaultRunOptions(appname, gitrev string) *RunOptions {
|
|
return &RunOptions{
|
|
Appname: appname,
|
|
Gitrev: gitrev,
|
|
Args: os.Args,
|
|
Stdin: os.Stdin,
|
|
Stdout: os.Stdout,
|
|
Stderr: os.Stderr,
|
|
Fs: afero.NewOsFs(),
|
|
}
|
|
}
|
|
|
|
// Run creates and runs the CLI application with default options.
|
|
func Run(appname, gitrev string) int {
|
|
return RunWithOptions(DefaultRunOptions(appname, gitrev))
|
|
}
|
|
|
|
// RunWithOptions creates and runs the CLI application with the given options.
|
|
func RunWithOptions(opts *RunOptions) int {
|
|
m := &CLIApp{
|
|
appname: opts.Appname,
|
|
gitrev: opts.Gitrev,
|
|
exitCode: 0,
|
|
maxManifestSize: mfer.MaxManifestSize,
|
|
Stdin: opts.Stdin,
|
|
Stdout: opts.Stdout,
|
|
Stderr: opts.Stderr,
|
|
Fs: opts.Fs,
|
|
}
|
|
|
|
m.run(opts.Args)
|
|
|
|
return m.exitCode
|
|
}
|