Skip to content

Commit

Permalink
feat: Add new version subcommand (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinmichaelchen authored Sep 6, 2023
1 parent 4ef8c8c commit 8f5b5be
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
12 changes: 11 additions & 1 deletion cmd/graphql-schema-picker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}
13 changes: 12 additions & 1 deletion internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,25 @@ var rootCmd = &cobra.Command{
}

var (
ldFlags LDFlags
sdlFile string
debug bool
dryRun bool
desiredDefinitions []string
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")
Expand All @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions internal/cli/cli_version.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 8f5b5be

Please sign in to comment.