Skip to content

Commit

Permalink
Update the version command to return more details (#71)
Browse files Browse the repository at this point in the history
Signed-off-by: oluwole fadeyi <[email protected]>
  • Loading branch information
tfadeyi authored Aug 3, 2023
1 parent ba4aad0 commit f318280
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 35 deletions.
24 changes: 2 additions & 22 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ builds:
flags:
- -trimpath
ldflags:
- -s -w -X "pkg/version.version={{.Version}}" -X "pkg/version.commit={{.Commit}}" -X "pkg/version.date={{.Date}}"
- '-s -w -X "github.com/tfadeyi/{{ .ProjectName }}/pkg/version.Platform=linux/unknown" -X "github.com/tfadeyi/{{ .ProjectName }}/pkg/version.Version={{ .Version }}" -X "github.com/tfadeyi/{{ .ProjectName }}/pkg/version.Commit={{ .Commit }}" -X "github.com/tfadeyi/{{ .ProjectName }}/pkg/version.Date={{ .Date }}"'
binary: "{{ .ProjectName }}"
goos:
- linux
Expand All @@ -25,27 +25,13 @@ builds:
flags:
- -trimpath
ldflags:
- -s -w -X "pkg/version.version={{.Version}}" -X "pkg/version.commit={{.Commit}}" -X "pkg/version.date={{.Date}}"
- '-s -w -X "github.com/tfadeyi/{{ .ProjectName }}/pkg/version.Platform=darwin/unknown" -X "github.com/tfadeyi/{{ .ProjectName }}/pkg/version.Version={{ .Version }}" -X "github.com/tfadeyi/{{ .ProjectName }}/pkg/version.Commit={{ .Commit }}" -X "github.com/tfadeyi/{{ .ProjectName }}/pkg/version.Date={{ .Date }}"'
binary: "{{ .ProjectName }}"
goos:
- darwin
goarch:
- amd64
- arm64
- id: "windows"
env:
- CGO_ENABLED=0
mod_timestamp: "{{ .CommitTimestamp }}"
flags:
- -trimpath
ldflags:
- -s -w -X "pkg/version.version={{.Version}}" -X "pkg/version.commit={{.Commit}}" -X "pkg/version.date={{.Date}}"
binary: "{{ .ProjectName }}"
goos:
- windows
goarch:
- amd64
- arm64
archives:
- id: linux
format: tar.gz
Expand All @@ -59,12 +45,6 @@ archives:
wrap_in_directory: true
builds:
- "darwin"
- id: windows
format: tar.gz
name_template: "{{ .ProjectName }}-{{ .Os }}-{{ .Arch }}"
wrap_in_directory: true
builds:
- "windows"

kos:
- working_dir: .
Expand Down
45 changes: 45 additions & 0 deletions cmd/options/version/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package version

import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

type (
// Options is the list of options/flag available to the application,
// plus the clients needed by the application to function.
Options struct {
Verbose bool
}
)

// New creates a new instance of the application's options
func New() *Options {
return new(Options)
}

// Prepare assigns the applications flag/options to the cobra cli
func (o *Options) Prepare(cmd *cobra.Command) *Options {
o.addAppFlags(cmd.Flags())
return o
}

// Validate validates the flag values given to the application
func (o *Options) Validate() error {
return nil
}

// Complete initialises the components needed for the application to function given the options,
func (o *Options) Complete() error {
return nil
}

func (o *Options) addAppFlags(fs *pflag.FlagSet) {
fs.BoolVarP(
&o.Verbose,
"version",
"v",
false,
"Verbose build information",
)
}
37 changes: 28 additions & 9 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,39 @@ package cmd

import (
"github.com/spf13/cobra"
versionoptions "github.com/tfadeyi/auth0-simple-exporter/cmd/options/version"
"github.com/tfadeyi/auth0-simple-exporter/pkg/logging"
"github.com/tfadeyi/auth0-simple-exporter/pkg/version"
)

var versionCmd = &cobra.Command{
Use: "version",
Short: "Returns the binary build information.",
Run: func(cmd *cobra.Command, args []string) {
ctx := cmd.Context()
log := logging.LoggerFromContext(ctx)
log.Info(version.BuildInfo())
},
func versionCmd() *cobra.Command {
opts := versionoptions.New()
cmd := &cobra.Command{
Use: "version",
Short: "Returns the binary build information.",
SilenceErrors: true,
SilenceUsage: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
err := opts.Validate()
if err != nil {
return err
}
err = opts.Complete()
return err
},
Run: func(cmd *cobra.Command, args []string) {
ctx := cmd.Context()
log := logging.LoggerFromContext(ctx)
if opts.Verbose {
log.Info(version.BuildInfo())
}
log.Info(version.Info())
},
}
opts = opts.Prepare(cmd)
return cmd
}

func init() {
rootCmd.AddCommand(versionCmd)
rootCmd.AddCommand(versionCmd())
}
20 changes: 16 additions & 4 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,23 @@ import "fmt"

// Values injected at build-time
var (
version string = "dev"
commit string = "unknown"
date string = "unknown"
Platform string = "unknown"
Version string = "dev"
Commit string = "unknown"
Date string = "unknown"
)

// BuildInfo returns the binary build information
func BuildInfo() string {
return fmt.Sprintf("%s, commit %s, built at %s", version, commit, date)
return fmt.Sprintf(`
Host machine: %s
Version: %s
Commit: %s
Built at: %s`, Platform, Version, Commit, Date)
}

// Info returns simple version information of the binary
func Info() string {
return Version
}

0 comments on commit f318280

Please sign in to comment.