diff --git a/cmd/rootCmd.go b/cmd/rootCmd.go index a907a52..e31d68e 100644 --- a/cmd/rootCmd.go +++ b/cmd/rootCmd.go @@ -20,6 +20,10 @@ var rootCmd = &cobra.Command{ Version: setlist.VERSION, } +func init() { + rootCmd.SetVersionTemplate(fmt.Sprintf("{{.Name}} version %s (%s)\n", setlist.VERSION, setlist.COMMIT)) +} + func loadAWSConfig(ctx context.Context) (aws.Config, error) { if profile != "" { slog.Info("Loading AWS config with profile", "profile", profile) diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 0000000..9b98a4a --- /dev/null +++ b/cmd/version.go @@ -0,0 +1,22 @@ +package main + +import ( + "fmt" + + "github.com/scottbrown/setlist" + + "github.com/spf13/cobra" +) + +var versionCmd = &cobra.Command{ + Use: "version", + Short: "Printing the current version", + Long: "Printing the current version and commit information", + Run: func(cmd *cobra.Command, args []string) { + fmt.Printf("%s version %s (%s)\n", AppName, setlist.VERSION, setlist.COMMIT) + }, +} + +func init() { + rootCmd.AddCommand(versionCmd) +} diff --git a/taskfile.yml b/taskfile.yml index 1c4e8b1..57da777 100644 --- a/taskfile.yml +++ b/taskfile.yml @@ -12,16 +12,22 @@ vars: build_dir: "{{ .pwd }}/.build" dist_dir: "{{ .pwd }}/.dist" + git_version: + sh: git tag --points-at HEAD | head -1 | grep . || git rev-parse --abbrev-ref HEAD + git_commit: + sh: git rev-parse --short HEAD + ldflags: "-X {{ .app_repo }}.VERSION={{ .git_version }} -X {{ .app_repo }}.COMMIT={{ .git_commit }}" + tasks: build: desc: "Builds a local artifact" cmds: - - go build -o {{ .build_dir }}/{{ .bin_name }} {{ .pkg_name }} + - go build -ldflags "{{ .ldflags }}" -o {{ .build_dir }}/{{ .bin_name }} {{ .pkg_name }} lambda-build: desc: "Builds the Lambda artifact" cmds: - - GOOS=linux GOARCH=arm64 go build -tags lambda.norpc -o {{ .build_dir }}/lambda/bootstrap {{ .app_repo }}/cmd/lambda + - GOOS=linux GOARCH=arm64 go build -ldflags "{{ .ldflags }}" -tags lambda.norpc -o {{ .build_dir }}/lambda/bootstrap {{ .app_repo }}/cmd/lambda fmt: desc: "Formats the code" @@ -79,7 +85,7 @@ tasks: desc: "Builds a linux artifact" cmds: - for: [ amd64, arm64 ] - cmd: GOOS=$GOOS GOARCH={{ .ITEM }} go build -o {{ .build_dir }}/linux-{{ .ITEM }}/{{ .bin_name }} {{ .app_repo }}/cmd + cmd: GOOS=$GOOS GOARCH={{ .ITEM }} go build -ldflags "{{ .ldflags }}" -o {{ .build_dir }}/linux-{{ .ITEM }}/{{ .bin_name }} {{ .app_repo }}/cmd env: GOOS: linux @@ -87,7 +93,7 @@ tasks: desc: "Builds a Windows artifact" cmds: - for: [ amd64, arm64 ] - cmd: GOOS=$GOOS GOARCH={{ .ITEM }} go build -o {{ .build_dir }}/windows-{{ .ITEM }}/{{ .bin_name }}.exe {{ .app_repo }}/cmd + cmd: GOOS=$GOOS GOARCH={{ .ITEM }} go build -ldflags "{{ .ldflags }}" -o {{ .build_dir }}/windows-{{ .ITEM }}/{{ .bin_name }}.exe {{ .app_repo }}/cmd env: GOOS: windows @@ -95,7 +101,7 @@ tasks: desc: "Builds a MacOS artifact" cmds: - for: [ amd64, arm64 ] - cmd: GOOS=darwin GOARCH={{ .ITEM }} go build -o {{ .build_dir }}/darwin-{{ .ITEM }}/{{ .bin_name }} {{ .app_repo }}/cmd + cmd: GOOS=darwin GOARCH={{ .ITEM }} go build -ldflags "{{ .ldflags }}" -o {{ .build_dir }}/darwin-{{ .ITEM }}/{{ .bin_name }} {{ .app_repo }}/cmd env: GOOS: darwin diff --git a/update.go b/update.go index 1f9114f..dac3f76 100644 --- a/update.go +++ b/update.go @@ -31,6 +31,10 @@ type ReleaseInfo struct { // CheckForUpdates compares the current version with the latest release // and returns information about a newer version if available func CheckForUpdates(ctx context.Context, client HTTPDoer) (*UpdateInfo, error) { + if VERSION == "dev" { + return nil, fmt.Errorf("cannot check for updates: running a dev build") + } + // Create request with context req, err := http.NewRequestWithContext(ctx, http.MethodGet, GithubAPI, nil) if err != nil { diff --git a/update_test.go b/update_test.go index d8e3aaa..eaee467 100644 --- a/update_test.go +++ b/update_test.go @@ -198,6 +198,25 @@ func TestCheckForUpdatesHTTPError(t *testing.T) { } } +func TestCheckForUpdatesDevBuild(t *testing.T) { + originalVersion := VERSION + defer func() { VERSION = originalVersion }() + + VERSION = "dev" + + mockClient := &mockHTTPClient{} + + _, err := CheckForUpdates(context.Background(), mockClient) + if err == nil { + t.Error("Expected error for dev build, got nil") + } + + expected := "cannot check for updates: running a dev build" + if err.Error() != expected { + t.Errorf("Expected error %q, got %q", expected, err.Error()) + } +} + func TestCheckForUpdatesWithContext(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) cancel() diff --git a/version.go b/version.go index 495c2f4..9dbc928 100644 --- a/version.go +++ b/version.go @@ -1,5 +1,4 @@ package setlist -// VERSION defines the current version of the Setlist application. -// This is displayed in the CLI help output and used in release artifacts. -var VERSION = "1.2.1" +var VERSION = "dev" +var COMMIT = "unknown"