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

Adding version command #17

Merged
merged 2 commits into from
Oct 15, 2024
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
7 changes: 3 additions & 4 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ builds:
goarch:
- amd64
- arm64
# commented these out so i work on implementing them with airlock
# ldflags: &build-ldflags |
# -X github.com/massdriver-cloud/mass/pkg/version.version={{.Version}}
# -X github.com/massdriver-cloud/mass/pkg/version.gitSHA={{.FullCommit}}
ldflags: &build-ldflags |
-X github.com/massdriver-cloud/airlock/pkg/version.version={{.Version}}
-X github.com/massdriver-cloud/airlock/pkg/version.gitSHA={{.FullCommit}}

- id: darwin-build
binary: airlock
Expand Down
8 changes: 5 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
INSTALL_PATH ?= /usr/local/bin
GIT_SHA := $(shell git log -1 --pretty=format:"%H")
LD_FLAGS := "-X github.com/massdriver-cloud/airlock/pkg/version.version=dev -X github.com/massdriver-cloud/airlock/pkg/version.gitSHA=local-dev-${GIT_SHA}"

MKFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST)))
MKFILE_DIR := $(dir $(MKFILE_PATH))
Expand All @@ -19,7 +21,7 @@ generate: ${API_DIR}/zz_generated.go

.PHONY: test
test:
go test ./... -cover
go clean -testcache && go test ./... -cover

bin:
mkdir bin
Expand All @@ -30,11 +32,11 @@ lint:

.PHONY: build.macos
build.macos: bin
GOOS=darwin GOARCH=arm64 go build -o bin/airlock-darwin-arm64
GOOS=darwin GOARCH=arm64 go build -o bin/airlock-darwin-arm64 -ldflags=${LD_FLAGS}

.PHONY: build.linux
build.linux: bin
GOOS=linux GOARCH=amd64 go build -o bin/airlock-linux-amd64
GOOS=linux GOARCH=amd64 go build -o bin/airlock-linux-amd64 -ldflags=${LD_FLAGS}

.PHONY: install.macos
install.macos: build.macos
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func Execute() {
rootCmd.AddCommand(NewCmdHelm())
rootCmd.AddCommand(NewCmdOpenTofu())
rootCmd.AddCommand(NewCmdValidate())
rootCmd.AddCommand(NewCmdVersion())
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
Expand Down
33 changes: 33 additions & 0 deletions cmd/version.go
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())
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ toolchain go1.23.2
require (
github.com/Checkmarx/kics/v2 v2.1.3
github.com/charmbracelet/glamour v0.8.0
github.com/charmbracelet/lipgloss v0.13.0
github.com/hashicorp/hcl/v2 v2.22.0
github.com/massdriver-cloud/terraform-config-inspect v0.0.1
github.com/spf13/cobra v1.8.1
github.com/stretchr/testify v1.9.0
github.com/wk8/go-ordered-map/v2 v2.1.8
github.com/xeipuuv/gojsonschema v1.2.0
github.com/zclconf/go-cty v1.15.0
golang.org/x/mod v0.21.0
gopkg.in/yaml.v3 v3.0.1
)

Expand All @@ -26,7 +28,6 @@ require (
github.com/aymerick/douceur v0.2.0 // indirect
github.com/bahlo/generic-list-go v0.2.0 // indirect
github.com/buger/jsonparser v1.1.1 // indirect
github.com/charmbracelet/lipgloss v0.13.0 // indirect
github.com/charmbracelet/x/ansi v0.3.2 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dlclark/regexp2 v1.11.4 // indirect
Expand Down Expand Up @@ -55,7 +56,6 @@ require (
github.com/yuin/goldmark-emoji v1.0.3 // indirect
golang.org/x/crypto v0.28.0 // indirect
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 // indirect
golang.org/x/mod v0.21.0 // indirect
golang.org/x/net v0.30.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.26.0 // indirect
Expand Down
15 changes: 15 additions & 0 deletions pkg/prettylogs/main.go
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"))
}
66 changes: 66 additions & 0 deletions pkg/version/version.go
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
}
48 changes: 48 additions & 0 deletions pkg/version/version_test.go
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)
}
})
}
}
Loading