From 8f5b5beef3783bc906f1f06542ccef21ed7e7966 Mon Sep 17 00:00:00 2001 From: Kevin Chen Date: Tue, 5 Sep 2023 20:03:42 -0400 Subject: [PATCH] feat: Add new version subcommand (#10) --- cmd/graphql-schema-picker/main.go | 12 +++++++++++- internal/cli/cli.go | 13 ++++++++++++- internal/cli/cli_version.go | 17 +++++++++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 internal/cli/cli_version.go diff --git a/cmd/graphql-schema-picker/main.go b/cmd/graphql-schema-picker/main.go index cf767ab..f1a2a57 100644 --- a/cmd/graphql-schema-picker/main.go +++ b/cmd/graphql-schema-picker/main.go @@ -4,6 +4,16 @@ import ( "github.com/kevinmichaelchen/graphql-schema-picker/internal/cli" ) +var ( + version = "dev" + commit = "none" + date = "unknown" +) + func main() { - cli.Main() + cli.Main(cli.LDFlags{ + Version: version, + Commit: commit, + Date: date, + }) } diff --git a/internal/cli/cli.go b/internal/cli/cli.go index 723ba55..acd760d 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -20,6 +20,7 @@ var rootCmd = &cobra.Command{ } var ( + ldFlags LDFlags sdlFile string debug bool dryRun bool @@ -27,8 +28,17 @@ var ( output string ) +// LDFlags contain fields that get linked and compiled into the final binary +// program at build time. +type LDFlags struct { + Version string + Commit string + Date string +} + func init() { rootCmd.AddCommand(pick) + rootCmd.AddCommand(versionCmd) rootCmd.PersistentFlags().StringVarP(&sdlFile, "sdl-file", "f", "", "path to an SDL file") rootCmd.PersistentFlags().BoolVarP(&debug, "debug", "", false, "verbose debug logging") @@ -38,7 +48,8 @@ func init() { pick.Flags().StringVarP(&output, "output", "o", "", "where the resulting schema/SDL file is written") } -func Main() { +func Main(ldf LDFlags) { + ldFlags = ldf if err := rootCmd.Execute(); err != nil { log.Error("execution failed", "err", err) os.Exit(1) diff --git a/internal/cli/cli_version.go b/internal/cli/cli_version.go new file mode 100644 index 0000000..ab658db --- /dev/null +++ b/internal/cli/cli_version.go @@ -0,0 +1,17 @@ +package cli + +import ( + "fmt" + "github.com/spf13/cobra" +) + +var versionCmd = &cobra.Command{ + Use: "version", + Short: "Prints CLI version info", + Long: `Prints CLI version info`, + Run: fnVersion, +} + +func fnVersion(cmd *cobra.Command, args []string) { + fmt.Printf("version %s\n", ldFlags.Version) +}