diff --git a/config.go b/config.go index 68a3554..af3aa22 100644 --- a/config.go +++ b/config.go @@ -27,6 +27,7 @@ type Config struct { // Settings Config string `json:"config,omitempty" help:"Base configuration file or template." short:"c" group:"Settings" default:"default" placeholder:"base"` + Version bool `hidden:"" json:",omitempty" help:"Print the version number" short:"v" group:"Settings"` Interactive bool `hidden:"" json:",omitempty" help:"Use an interactive form for configuration options." short:"i" group:"Settings"` Language string `json:"language,omitempty" help:"Language of code file." short:"l" group:"Settings" placeholder:"go"` Theme string `json:"theme" help:"Theme to use for syntax highlighting." short:"t" group:"Settings" placeholder:"charm"` diff --git a/help.go b/help.go index ed695ff..bd9f3ae 100644 --- a/help.go +++ b/help.go @@ -41,7 +41,8 @@ func helpPrinter(_ kong.HelpOptions, ctx *kong.Context) error { fmt.Println() for _, f := range flags { - if f.Name == "interactive" { + switch f.Name { + case "version", "interactive": printFlag(f) } } diff --git a/main.go b/main.go index 5a710d7..854af03 100644 --- a/main.go +++ b/main.go @@ -60,25 +60,19 @@ func main() { printErrorFatal("Invalid Usage", err) } - //nolint: nestif - if len(ctx.Args) > 0 { - switch ctx.Args[0] { - case "version": - if Version == "" { - if info, ok := debug.ReadBuildInfo(); ok && info.Main.Sum != "" { - Version = info.Main.Version - } else { - Version = "unknown (built from source)" - } + if config.Version { + if Version == "" { + Version = "unknown (built from source)" + if info, ok := debug.ReadBuildInfo(); ok && (info.Main.Version != "" && info.Main.Version != "(devel)") { + Version = info.Main.Version } - version := fmt.Sprintf("freeze version %s", Version) - if len(CommitSHA) >= shaLen { - version += " (" + CommitSHA[:shaLen] + ")" - } - - fmt.Println(version) - os.Exit(0) } + version := fmt.Sprintf("freeze version: %s", Version) + if len(CommitSHA) > shaLen { // if the commit SHA is longer than `shaLen` characters, show the first `shaLen` number of characters. + version += fmt.Sprintf(" (%s)", CommitSHA[:shaLen]) + } + fmt.Println(version) + return } // Copy the pty output to buffer