-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adding version command * Added supporting packages * Added prettylogs * Updated goreleaser to get version * Updated makefile to add version * update makefile, update go.mod
- Loading branch information
Showing
8 changed files
with
173 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/massdriver-cloud/airlock/pkg/prettylogs" | ||
"github.com/massdriver-cloud/airlock/pkg/version" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewCmdVersion() *cobra.Command { | ||
versionCmd := &cobra.Command{ | ||
Use: "version", | ||
Aliases: []string{"v"}, | ||
Short: "Version of Airlock", | ||
Run: runVersion, | ||
} | ||
return versionCmd | ||
} | ||
|
||
func runVersion(cmd *cobra.Command, args []string) { | ||
latestVersion, err := version.GetLatestVersion() | ||
if err != nil { | ||
fmt.Errorf("could not check for newer version, skipping.\nurl: %s\nerror: %w\n", version.LatestReleaseURL, err) | ||
} | ||
|
||
isOld, _ := version.CheckForNewerVersionAvailable(latestVersion) | ||
if isOld { | ||
fmt.Printf("A newer version of Airlock is available, you can download it here: %v\n", version.LatestReleaseURL) | ||
} | ||
airlockVersion := prettylogs.Green(version.AirlockVersion()) | ||
fmt.Printf("Airlock version: %v (git SHA: %v)\n", airlockVersion, version.AirlockGitSHA()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package prettylogs | ||
|
||
import "github.com/charmbracelet/lipgloss" | ||
|
||
func Underline(word string) lipgloss.Style { | ||
return lipgloss.NewStyle().SetString(word).Underline(true).Foreground(lipgloss.Color("#7D56f4")) | ||
} | ||
|
||
func Green(word string) lipgloss.Style { | ||
return lipgloss.NewStyle().SetString(word).Foreground(lipgloss.Color("#00FF00")) | ||
} | ||
|
||
func Orange(word string) lipgloss.Style { | ||
return lipgloss.NewStyle().SetString(word).Foreground(lipgloss.Color("#FFA500")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package version | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"strings" | ||
|
||
"golang.org/x/mod/semver" | ||
) | ||
|
||
const LatestReleaseURL = "https://github.com/massdriver-cloud/airlock/releases/latest" | ||
|
||
var ( | ||
version = "unknown" | ||
gitSHA = "unknown" | ||
) | ||
|
||
func AirlockVersion() string { | ||
return version | ||
} | ||
|
||
func AirlockGitSHA() string { | ||
return gitSHA | ||
} | ||
|
||
func SetVersion(setVersion string) { | ||
version = setVersion | ||
} | ||
|
||
func GetLatestVersion() (string, error) { | ||
ctx := context.Background() | ||
req, reqErr := http.NewRequestWithContext(ctx, http.MethodGet, LatestReleaseURL, nil) | ||
if reqErr != nil { | ||
return "", reqErr | ||
} | ||
|
||
resp, respErr := http.DefaultClient.Do(req) | ||
if respErr != nil { | ||
return "", respErr | ||
} | ||
defer resp.Body.Close() | ||
|
||
redirectURL := resp.Request.URL.String() | ||
parts := strings.Split(redirectURL, "/") | ||
latestVersion := parts[len(parts)-1] | ||
return latestVersion, nil | ||
} | ||
|
||
func CheckForNewerVersionAvailable(latestVersion string) (bool, string) { | ||
currentVersion := version | ||
|
||
// semver requires "v" for version (e.g., v1.0.0 not 1.0.0). Adds "v" if missing | ||
if !strings.HasPrefix(currentVersion, "v") { | ||
currentVersion = "v" + currentVersion | ||
} | ||
|
||
if !strings.HasPrefix(latestVersion, "v") { | ||
latestVersion = "v" + latestVersion | ||
} | ||
|
||
if semver.Compare(currentVersion, latestVersion) < 0 { | ||
return true, latestVersion | ||
} | ||
|
||
return false, latestVersion | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package version_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/massdriver-cloud/airlock/pkg/version" | ||
) | ||
|
||
func TestCheckForNewerVersionAvailable(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
current string | ||
latest string | ||
wantIsOld bool | ||
}{ | ||
{ | ||
name: "unknown version should be considered old", | ||
current: "unknown", | ||
latest: "v1.2.0", | ||
wantIsOld: true, | ||
}, | ||
{ | ||
name: "current version is the latest version", | ||
current: "1.2.0", | ||
latest: "v1.2.0", | ||
wantIsOld: false, | ||
}, | ||
{ | ||
name: "current version is older than the latest version", | ||
current: "1.0.0", | ||
latest: "v1.2.0", | ||
wantIsOld: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
version.SetVersion(tt.current) | ||
|
||
got, latestVersion := version.CheckForNewerVersionAvailable(tt.latest) | ||
if got != tt.wantIsOld { | ||
t.Errorf("CheckForNewerVersionAvailable() got = %v, want %v", got, tt.wantIsOld) | ||
} | ||
if latestVersion != tt.latest { | ||
t.Errorf("CheckForNewerVersionAvailable() latestVersion = %v, want %v", latestVersion, tt.latest) | ||
} | ||
}) | ||
} | ||
} |