diff --git a/cmd/logo.txt b/cmd/logo.txt new file mode 100644 index 000000000..bfe325fba --- /dev/null +++ b/cmd/logo.txt @@ -0,0 +1,5 @@ + ____ ___ _ _ _____ _ ___ _ _ _____ ____ _ _  + / ___/ _ \| \ | |_ _|/ \ |_ _| \ | | ____| _ \| | __ _| |__  +| | | | | | \| | | | / _ \ | || \| | _| | |_) | |/ _` | '_ \  +| |__| |_| | |\ | | |/ ___ \ | || |\ | |___| _ <| | (_| | |_) | + \____\___/|_| \_| |_/_/ \_\___|_| \_|_____|_| \_\_|\__,_|_.__/  diff --git a/cmd/version.go b/cmd/version.go index c12694871..7504916e2 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -5,6 +5,7 @@ package cmd import ( + _ "embed" "fmt" "net/http" "os" @@ -30,28 +31,29 @@ func init() { rootCmd.AddCommand(versionCmd) } -var slug = ` - _ _ _ - _ (_) | | | | - ____ ___ ____ | |_ ____ _ ____ ____ ____| | ____| | _ -/ ___) _ \| _ \| _)/ _ | | _ \ / _ )/ ___) |/ _ | || \ -( (__| |_|| | | | |_( ( | | | | | ( (/ /| | | ( ( | | |_) ) -\____)___/|_| |_|\___)_||_|_|_| |_|\____)_| |_|\_||_|____/ -` +// this a note to self how color codes work +// https://stackoverflow.com/questions/4842424/list-of-ansi-color-escape-sequences +// https://patorjk.com/software/taag/#p=display&f=Ivrit&t=CONTAINERlab +// +//go:embed logo.txt +var projASCIILogo string // versionCmd represents the version command. var versionCmd = &cobra.Command{ Use: "version", Short: "show containerlab version or upgrade", - Run: func(cmd *cobra.Command, args []string) { - fmt.Println(slug) + RunE: func(cmd *cobra.Command, args []string) error { + fmt.Println(projASCIILogo) + verSlug := docsLinkFromVer(version) fmt.Printf(" version: %s\n", version) fmt.Printf(" commit: %s\n", commit) fmt.Printf(" date: %s\n", date) fmt.Printf(" source: %s\n", repoUrl) fmt.Printf(" rel. notes: https://containerlab.dev/rn/%s\n", verSlug) + + return nil }, } diff --git a/cmd/version_test.go b/cmd/version_test.go new file mode 100644 index 000000000..91c710b7b --- /dev/null +++ b/cmd/version_test.go @@ -0,0 +1,35 @@ +package cmd + +import ( + "testing" + + "github.com/google/go-cmp/cmp" +) + +func TestDocsLinkFromVer(t *testing.T) { + tests := []struct { + name string + version string + expected string + }{ + { + name: "major and minor only", + version: "0.47.0", + expected: "0.47/", + }, + { + name: "major, minor, and patch version", + version: "0.47.2", + expected: "0.47/#0472", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := docsLinkFromVer(tt.version) + if diff := cmp.Diff(got, tt.expected); diff != "" { + t.Fatalf("docsLinkFromVer() mismatch (-want +got):\n%s", diff) + } + }) + } +} diff --git a/get.sh b/get.sh index be2c6b9de..5b8919e3d 100644 --- a/get.sh +++ b/get.sh @@ -128,6 +128,24 @@ setDesiredVersion() { fi } +# docsLinkFromVer returns the url portion for release notes +# based on the Go docsLinkFromVer() function in version.go +docsLinkFromVer() { + ver=$1 + IFS='.' read -ra segments <<< "$ver" + maj=${segments[0]} + min=${segments[1]} + patch=${segments[2]} + + relSlug="$maj.$min/" + if [ -n "$patch" ]; then + if [ "$patch" -ne 0 ]; then + relSlug="$relSlug#$maj$min$patch" + fi + fi + echo "$relSlug" +} + # checkInstalledVersion checks which version is installed and # if it needs to be changed. checkInstalledVersion() { @@ -138,10 +156,11 @@ checkInstalledVersion() { return 0 else if [ "$(printf '%s\n' "$TAG_WO_VER" "$version" | sort -V | head -n1)" = "$TAG_WO_VER" ]; then + RN_VER=$(docsLinkFromVer $TAG_WO_VER) echo "A newer ${BINARY_NAME} version $version is already installed" echo "You are running ${BINARY_NAME} version $version" echo "You are trying to downgrade to ${BINARY_NAME} version ${TAG_WO_VER}" - echo "Release notes: https://containerlab.dev/rn/${TAG_WO_VER}" + echo "Release notes: https://containerlab.dev/rn/${RN_VER}" UPGR_NEEDED="Y" # check if stdin is open (i.e. capable of getting users input) if [ -t 0 ]; then @@ -152,7 +171,8 @@ checkInstalledVersion() { fi return 0 else - echo "A newer ${BINARY_NAME} ${TAG_WO_VER} is available. Release notes: https://containerlab.dev/rn/${TAG_WO_VER}" + RN_VER=$(docsLinkFromVer $TAG_WO_VER) + echo "A newer ${BINARY_NAME} ${TAG_WO_VER} is available. Release notes: https://containerlab.dev/rn/${RN_VER}" echo "You are running containerlab $version version" UPGR_NEEDED="Y" # check if stdin is open (i.e. capable of getting users input)