Skip to content

Commit

Permalink
Add version cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
loicsapone committed Nov 15, 2021
1 parent dbb14fd commit 0b6dd11
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
8 changes: 5 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,17 @@ func Execute(version string) {
},
}

rootCmd.PersistentFlags().Bool("help", false, "Show help for command")
rootCmd.SetHelpFunc(root.HelpFunc)
rootCmd.SetHelpCommand(&cobra.Command{Hidden: true})

rootCmd.PersistentFlags().Bool("version", false, "Show ae version")
rootCmd.SetVersionTemplate(versionFormat(version))

rootCmd.AddCommand(newVersionCommand(version))
rootCmd.AddCommand(newCompletionCommand())
rootCmd.AddCommand(newUserCommands()...)

rootCmd.PersistentFlags().Bool("help", false, "Show help for command")
rootCmd.PersistentFlags().Bool("version", false, "Show ae version")

if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down
37 changes: 37 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package cmd

import (
"fmt"
"regexp"

"github.com/spf13/cobra"
)

func newVersionCommand(version string) *cobra.Command {
cmd := &cobra.Command{
Use: "version",
Hidden: true,

RunE: func(cmd *cobra.Command, args []string) error {
fmt.Fprint(cmd.OutOrStdout(), versionFormat(version))
return nil
},
}

return cmd
}

func versionFormat(version string) string {
return fmt.Sprintf("ae version %s\n%s\n", version, changelogURL(version))
}

func changelogURL(version string) string {
path := "https://github.com/IQ2i/aergie"

r := regexp.MustCompile(`^\d+\.\d+\.\d+$`)
if !r.MatchString(version) {
return fmt.Sprintf("%s/releases/latest", path)
}

return fmt.Sprintf("https://github.com/IQ2i/aergie/releases/tag/v%s", version)
}
28 changes: 28 additions & 0 deletions cmd/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cmd

import (
"testing"
)

func TestFormat(t *testing.T) {
expects := "ae version 1.0.0\nhttps://github.com/IQ2i/aergie/releases/tag/v1.0.0\n"
if got := versionFormat("1.0.0"); got != expects {
t.Errorf("versionFormat() = %q, wants %q", got, expects)
}
}

func TestChangelogURL(t *testing.T) {
tag := "1.0.0"
url := "https://github.com/IQ2i/aergie/releases/tag/v1.0.0"
result := changelogURL(tag)
if result != url {
t.Errorf("expected %s to create url %s but got %s", tag, url, result)
}

tag = "DEV"
url = "https://github.com/IQ2i/aergie/releases/latest"
result = changelogURL(tag)
if result != url {
t.Errorf("expected %s to create url %s but got %s", tag, url, result)
}
}

0 comments on commit 0b6dd11

Please sign in to comment.