Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -31,8 +30,5 @@ func Execute() {
}

func init() {
rootCmd.SetVersionTemplate(fmt.Sprintf(
"tmpo version %s\ncommit: %s\nbuilt: %s\n",
Version, Commit, Date,
))
rootCmd.SetVersionTemplate(GetVersionOutput())
}
57 changes: 57 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package cmd

import (
"fmt"
"regexp"
"strings"
"time"

"github.com/spf13/cobra"
)

var versionCmd = &cobra.Command{
Use: "version",
Short: "Show version information",
Long: "Display the current version information including date and release URL.",
Hidden: true,
Run: func(cmd *cobra.Command, args []string) {
fmt.Print(GetVersionOutput())
},
}

// GetVersionOutput returns the formatted version string used by both
// the version subcommand and the -v/--version flags
func GetVersionOutput() string {
return fmt.Sprintf("\ntmpo version %s %s\n%s\n\n", Version, GetFormattedDate(Date), GetChangelogUrl(Version))
}

// GetFormattedDate parses inputDate as an RFC3339 timestamp and returns the date
// formatted as "YYYY-MM-DD" wrapped in parentheses (for example "(2006-01-02)").
// If inputDate is empty or cannot be parsed as RFC3339, it returns an empty string.
func GetFormattedDate(inputDate string) string {
if inputDate == "" {
return ""
}

date, err := time.Parse(time.RFC3339, inputDate)
if err != nil {
return ""
}

return fmt.Sprintf("(%s)", date.Format("2006-01-02"))
}

func GetChangelogUrl(version string) string {
path := "https://github.com/DylanDevelops/tmpo"

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

return fmt.Sprintf("%s/releases/tag/v%s", path, strings.TrimPrefix(version, "v"))
}

func init() {
rootCmd.AddCommand(versionCmd)
}
Loading