Skip to content

Commit 8f5b5be

Browse files
feat: Add new version subcommand (#10)
1 parent 4ef8c8c commit 8f5b5be

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

cmd/graphql-schema-picker/main.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ import (
44
"github.com/kevinmichaelchen/graphql-schema-picker/internal/cli"
55
)
66

7+
var (
8+
version = "dev"
9+
commit = "none"
10+
date = "unknown"
11+
)
12+
713
func main() {
8-
cli.Main()
14+
cli.Main(cli.LDFlags{
15+
Version: version,
16+
Commit: commit,
17+
Date: date,
18+
})
919
}

internal/cli/cli.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,25 @@ var rootCmd = &cobra.Command{
2020
}
2121

2222
var (
23+
ldFlags LDFlags
2324
sdlFile string
2425
debug bool
2526
dryRun bool
2627
desiredDefinitions []string
2728
output string
2829
)
2930

31+
// LDFlags contain fields that get linked and compiled into the final binary
32+
// program at build time.
33+
type LDFlags struct {
34+
Version string
35+
Commit string
36+
Date string
37+
}
38+
3039
func init() {
3140
rootCmd.AddCommand(pick)
41+
rootCmd.AddCommand(versionCmd)
3242

3343
rootCmd.PersistentFlags().StringVarP(&sdlFile, "sdl-file", "f", "", "path to an SDL file")
3444
rootCmd.PersistentFlags().BoolVarP(&debug, "debug", "", false, "verbose debug logging")
@@ -38,7 +48,8 @@ func init() {
3848
pick.Flags().StringVarP(&output, "output", "o", "", "where the resulting schema/SDL file is written")
3949
}
4050

41-
func Main() {
51+
func Main(ldf LDFlags) {
52+
ldFlags = ldf
4253
if err := rootCmd.Execute(); err != nil {
4354
log.Error("execution failed", "err", err)
4455
os.Exit(1)

internal/cli/cli_version.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package cli
2+
3+
import (
4+
"fmt"
5+
"github.com/spf13/cobra"
6+
)
7+
8+
var versionCmd = &cobra.Command{
9+
Use: "version",
10+
Short: "Prints CLI version info",
11+
Long: `Prints CLI version info`,
12+
Run: fnVersion,
13+
}
14+
15+
func fnVersion(cmd *cobra.Command, args []string) {
16+
fmt.Printf("version %s\n", ldFlags.Version)
17+
}

0 commit comments

Comments
 (0)