Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Color logo and fixed rel notes link in bash function #1680

Merged
merged 2 commits into from
Oct 28, 2023
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
5 changes: 5 additions & 0 deletions cmd/logo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
____ ___ _ _ _____ _ ___ _ _ _____ ____ _ _ 
/ ___/ _ \| \ | |_ _|/ \ |_ _| \ | | ____| _ \| | __ _| |__ 
| | | | | | \| | | | / _ \ | || \| | _| | |_) | |/ _` | '_ \ 
| |__| |_| | |\ | | |/ ___ \ | || |\ | |___| _ <| | (_| | |_) |
\____\___/|_| \_| |_/_/ \_\___|_| \_|_____|_| \_\_|\__,_|_.__/ 
22 changes: 12 additions & 10 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package cmd

import (
_ "embed"
"fmt"
"net/http"
"os"
Expand All @@ -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
},
}

Expand Down
35 changes: 35 additions & 0 deletions cmd/version_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
24 changes: 22 additions & 2 deletions get.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
Expand All @@ -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)
Expand Down