From 6a8a2e7043783b034a83b13c4ebd4d1e73299eb3 Mon Sep 17 00:00:00 2001 From: bohendo Date: Thu, 5 Oct 2023 13:37:08 -0400 Subject: [PATCH 01/11] init source-of-truth version file --- VERSION | 1 + 1 file changed, 1 insertion(+) create mode 100644 VERSION diff --git a/VERSION b/VERSION new file mode 100644 index 0000000..6e8bf73 --- /dev/null +++ b/VERSION @@ -0,0 +1 @@ +0.1.0 From 9c46b7480764ce76f0eabd779ddb86734944bfd5 Mon Sep 17 00:00:00 2001 From: bohendo Date: Thu, 5 Oct 2023 13:56:24 -0400 Subject: [PATCH 02/11] adjust 1password error reporting --- cmd/cloudexec/configure.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/cloudexec/configure.go b/cmd/cloudexec/configure.go index d800e22..7e36deb 100644 --- a/cmd/cloudexec/configure.go +++ b/cmd/cloudexec/configure.go @@ -121,8 +121,8 @@ func processOpValue(value string) (string, error) { cmd.Stderr = &stderr output, err := cmd.Output() if err != nil { - fmt.Println(stderr.String()) - return "", fmt.Errorf("Failed to process 1password reference for %s: %w", value, err) + // err says "exit status 1" so not very helpful, omit it from the following message + return "", fmt.Errorf("Failed to process 1password reference for %s: %s", value, stderr.String()) } return strings.TrimSpace(string(output)), nil } From c47ef02217c19a4aa1e73bf5ad8471e111d13cf5 Mon Sep 17 00:00:00 2001 From: bohendo Date: Thu, 5 Oct 2023 14:32:17 -0400 Subject: [PATCH 03/11] add linker flags to make build recipe --- Makefile | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 57b2956..217f4cb 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,12 @@ +# Capture version, commit, and date +GIT_COMMIT=$(shell git rev-list -1 HEAD) +GIT_DATE=$(shell git log -1 --format=%cd --date=format:'%Y-%m-%d %H:%M:%S') +VERSION="$(shell cat VERSION | tr -d '\n\r')" + build: + # Build the Go app with ldflags @mkdir -p dist - @cd cmd/cloudexec && go build -o ../../dist/cloudexec + cd cmd/cloudexec && go build -ldflags "-X 'main.version=$(VERSION)' -X 'main.commit=$(GIT_COMMIT)' -X 'main.date=$(GIT_DATE)'" -o ../../dist/cloudexec format: trunk fmt From 2ef66cf00cb3d69446c33a1333cd42468e1ab830 Mon Sep 17 00:00:00 2001 From: bohendo Date: Thu, 5 Oct 2023 16:18:31 -0400 Subject: [PATCH 04/11] add link flags to flake.nix --- flake.nix | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/flake.nix b/flake.nix index d9ad5ce..37bc3c0 100644 --- a/flake.nix +++ b/flake.nix @@ -18,7 +18,16 @@ default = cloudexec; - cloudexec = pkgs.buildGoModule { + cloudexec = + let + gitInfo = pkgs.runCommand "get-git-info" {} '' + echo "commit=$(git rev-parse HEAD)" > $out + echo "date=$(git log -1 --format=%cd --date=format:'%Y-%m-%d %H:%M:%S')" >> $out + ''; + + gitCommit = builtins.head (builtins.match "commit=(.*)" (builtins.readFile gitInfo)); + gitDate = builtins.head (builtins.match "date=(.*)" (builtins.readFile gitInfo)); + in pkgs.buildGoModule { pname = "cloudexec"; version = "0.0.1"; # TBD src = ./.; From e5a0a092354369a556d8f4abee309bc0b995176c Mon Sep 17 00:00:00 2001 From: bohendo Date: Thu, 5 Oct 2023 16:46:38 -0400 Subject: [PATCH 05/11] export version-related variables --- cmd/cloudexec/main.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/cmd/cloudexec/main.go b/cmd/cloudexec/main.go index 939bfdb..07b3c6f 100644 --- a/cmd/cloudexec/main.go +++ b/cmd/cloudexec/main.go @@ -15,11 +15,11 @@ import ( ) var ( - version = "dev" - commit = "none" - date = "unknown" - configFilePath = fmt.Sprintf("%s/.config/cloudexec/config.toml", os.Getenv("HOME")) - launchConfigFilePath = "./cloudexec.toml" + Version = "dev" + Commit = "none" + Date = "unknown" + ConfigFilePath = fmt.Sprintf("%s/.config/cloudexec/config.toml", os.Getenv("HOME")) + LaunchConfigFilePath = "./cloudexec.toml" ) func main() { @@ -34,7 +34,7 @@ func main() { dropletName := fmt.Sprintf("cloudexec-%v", username) // Attempt to load the configuration - config, configErr := LoadConfig(configFilePath) + config, configErr := LoadConfig(ConfigFilePath) app := &cli.App{ Name: "cloudexec", @@ -112,16 +112,16 @@ func main() { } // Check if a local cloudexec.toml exists - if _, err := os.Stat(launchConfigFilePath); os.IsNotExist(err) { + if _, err := os.Stat(LaunchConfigFilePath); os.IsNotExist(err) { // Check if the path to a launch config is provided if c.Args().Len() < 1 { return fmt.Errorf("please provide a path to a cloudexec.toml file or create one in the current directory") } - launchConfigFilePath = c.Args().Get(0) + LaunchConfigFilePath = c.Args().Get(0) } // Load the launch configuration - lc, err := LoadLaunchConfig(launchConfigFilePath) + lc, err := LoadLaunchConfig(LaunchConfigFilePath) if err != nil { return err } @@ -503,7 +503,7 @@ func main() { Usage: "Gets the version of the app", Aliases: []string{"v"}, Action: func(*cli.Context) error { - fmt.Printf("cloudexec %s, commit %s, built at %s", version, commit, date) + fmt.Printf("cloudexec %s, commit %s, built at %s", Version, Commit, Date) return nil }, }, From f15545e3eb0c6ea61dbaab0b6a0a352429022998 Mon Sep 17 00:00:00 2001 From: bohendo Date: Fri, 6 Oct 2023 14:25:31 -0400 Subject: [PATCH 06/11] fix build-time ldflag for version info --- .gitignore | 1 + Makefile | 15 +++++++++++---- VERSION | 2 +- flake.nix | 38 ++++++++++++++++++++++++++++---------- 4 files changed, 41 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index a243a30..297ef26 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +*.backup .direnv/ .env .vscode diff --git a/Makefile b/Makefile index 217f4cb..9fab12f 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,8 @@ +SHELL=bash + # Capture version, commit, and date GIT_COMMIT=$(shell git rev-list -1 HEAD) -GIT_DATE=$(shell git log -1 --format=%cd --date=format:'%Y-%m-%d %H:%M:%S') +GIT_DATE=$(shell git log -1 --format=%cd --date=format:'%Y-%m-%d-%H:%M:%S') VERSION="$(shell cat VERSION | tr -d '\n\r')" build: @@ -23,8 +25,13 @@ lint: go fmt pkg/state/*.go shellcheck cmd/cloudexec/user_data.sh.tmpl -existing_installation=$(shell nix profile list | grep cloudexec | cut -d " " -f 1) nix-install: - nix build - nix profile remove $(existing_installation) + rm -f ./.VERSION.backup + cp ./VERSION ./.VERSION.backup + echo "commit=$(GIT_COMMIT)" >> ./VERSION + echo "date=$(GIT_DATE)" >> ./VERSION + nix build || (cp -f ./.VERSION.backup ./VERSION && false) + rm -f ./VERSION + cp ./.VERSION.backup ./VERSION + nix profile remove $(shell nix profile list | grep cloudexec | cut -d " " -f 1) nix profile install ./result diff --git a/VERSION b/VERSION index 6e8bf73..de55ab6 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.1.0 +version=0.1.0 diff --git a/flake.nix b/flake.nix index 37bc3c0..70a9179 100644 --- a/flake.nix +++ b/flake.nix @@ -18,23 +18,41 @@ default = cloudexec; - cloudexec = - let - gitInfo = pkgs.runCommand "get-git-info" {} '' - echo "commit=$(git rev-parse HEAD)" > $out - echo "date=$(git log -1 --format=%cd --date=format:'%Y-%m-%d %H:%M:%S')" >> $out - ''; - - gitCommit = builtins.head (builtins.match "commit=(.*)" (builtins.readFile gitInfo)); - gitDate = builtins.head (builtins.match "date=(.*)" (builtins.readFile gitInfo)); - in pkgs.buildGoModule { + cloudexec = let + versionFileContent = builtins.readFile ./VERSION; + version = let + result = builtins.match ".*version=([^\n]*).*" versionFileContent; + in if result != null then builtins.head result else "unknown"; + gitCommit = let + result = builtins.match ".*commit=([^\n]*).*" versionFileContent; + in if result != null then builtins.head result else "unknown"; + gitDate = let + result = builtins.match ".*date=([^\n]*).*" versionFileContent; + in if result != null then builtins.head result else "unknown"; + testString = '' + version=0.1.0 + foo=bar + ''; + result = builtins.match ".*version=([^\n]*).*" testString; + in pkgs.buildGoModule { + mytest = builtins.trace result "test"; + debug1 = builtins.trace "VERSION values: <${versionFileContent}>" "debug"; + debug2 = builtins.trace "version value: <${version}>" "debug"; + debug3 = builtins.trace "gitCommit value: <${gitCommit}>" "debug"; + debug4 = builtins.trace "gitDate value: <${gitDate}>" "debug"; pname = "cloudexec"; version = "0.0.1"; # TBD src = ./.; vendorSha256 = "sha256-xiiMcjo+hRllttjYXB3F2Ms2gX43r7/qgwxr4THNhsk="; nativeBuildInputs = [ + pkgs.git pkgs.go_1_20 ]; + ldflags = [ + "-X main.Version=${version}" + "-X main.Commit=${gitCommit}" + "-X main.Date=${gitDate}" + ]; }; vscode = pkgs.vscode-with-extensions.override { From 7bba4d5c1b01199937c43eecf86d4d175235ac46 Mon Sep 17 00:00:00 2001 From: bohendo Date: Fri, 6 Oct 2023 14:56:01 -0400 Subject: [PATCH 07/11] clean up logs and fix vanilla build --- Makefile | 18 +++++++++--------- VERSION | 2 +- flake.nix | 19 ++++--------------- 3 files changed, 14 insertions(+), 25 deletions(-) diff --git a/Makefile b/Makefile index 9fab12f..f3c6e33 100644 --- a/Makefile +++ b/Makefile @@ -5,10 +5,10 @@ GIT_COMMIT=$(shell git rev-list -1 HEAD) GIT_DATE=$(shell git log -1 --format=%cd --date=format:'%Y-%m-%d-%H:%M:%S') VERSION="$(shell cat VERSION | tr -d '\n\r')" +# Build the Go app with ldflags build: - # Build the Go app with ldflags @mkdir -p dist - cd cmd/cloudexec && go build -ldflags "-X 'main.version=$(VERSION)' -X 'main.commit=$(GIT_COMMIT)' -X 'main.date=$(GIT_DATE)'" -o ../../dist/cloudexec + cd cmd/cloudexec && go build -ldflags "-X 'main.Version=$(VERSION)' -X 'main.Commit=$(GIT_COMMIT)' -X 'main.Date=$(GIT_DATE)'" -o ../../dist/cloudexec format: trunk fmt @@ -25,13 +25,13 @@ lint: go fmt pkg/state/*.go shellcheck cmd/cloudexec/user_data.sh.tmpl +# smuggles git info in the VERSION file to work around nix flake hermicity nix-install: - rm -f ./.VERSION.backup - cp ./VERSION ./.VERSION.backup - echo "commit=$(GIT_COMMIT)" >> ./VERSION - echo "date=$(GIT_DATE)" >> ./VERSION - nix build || (cp -f ./.VERSION.backup ./VERSION && false) - rm -f ./VERSION - cp ./.VERSION.backup ./VERSION + @cp -f ./VERSION ./.VERSION.backup + @echo "commit=$(GIT_COMMIT)" >> ./VERSION + @echo "date=$(GIT_DATE)" >> ./VERSION + @echo "nix build" + @nix build || (cp -f ./.VERSION.backup ./VERSION && false) + @mv -f ./.VERSION.backup ./VERSION nix profile remove $(shell nix profile list | grep cloudexec | cut -d " " -f 1) nix profile install ./result diff --git a/VERSION b/VERSION index de55ab6..6e8bf73 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -version=0.1.0 +0.1.0 diff --git a/flake.nix b/flake.nix index 70a9179..94f1cab 100644 --- a/flake.nix +++ b/flake.nix @@ -19,29 +19,18 @@ default = cloudexec; cloudexec = let - versionFileContent = builtins.readFile ./VERSION; version = let - result = builtins.match ".*version=([^\n]*).*" versionFileContent; + result = builtins.match "([^\n]*).*" (builtins.readFile ./VERSION); in if result != null then builtins.head result else "unknown"; gitCommit = let - result = builtins.match ".*commit=([^\n]*).*" versionFileContent; + result = builtins.match ".*commit=([^\n]*).*" (builtins.readFile ./VERSION); in if result != null then builtins.head result else "unknown"; gitDate = let - result = builtins.match ".*date=([^\n]*).*" versionFileContent; + result = builtins.match ".*date=([^\n]*).*" (builtins.readFile ./VERSION); in if result != null then builtins.head result else "unknown"; - testString = '' - version=0.1.0 - foo=bar - ''; - result = builtins.match ".*version=([^\n]*).*" testString; in pkgs.buildGoModule { - mytest = builtins.trace result "test"; - debug1 = builtins.trace "VERSION values: <${versionFileContent}>" "debug"; - debug2 = builtins.trace "version value: <${version}>" "debug"; - debug3 = builtins.trace "gitCommit value: <${gitCommit}>" "debug"; - debug4 = builtins.trace "gitDate value: <${gitDate}>" "debug"; pname = "cloudexec"; - version = "0.0.1"; # TBD + version = "${version}"; src = ./.; vendorSha256 = "sha256-xiiMcjo+hRllttjYXB3F2Ms2gX43r7/qgwxr4THNhsk="; nativeBuildInputs = [ From ddc6ce12b6faff97de1416c785ea83cd0bd459d8 Mon Sep 17 00:00:00 2001 From: bohendo Date: Fri, 6 Oct 2023 15:13:45 -0400 Subject: [PATCH 08/11] reformat --- cmd/cloudexec/configure.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/cloudexec/configure.go b/cmd/cloudexec/configure.go index 7e36deb..d536559 100644 --- a/cmd/cloudexec/configure.go +++ b/cmd/cloudexec/configure.go @@ -121,7 +121,7 @@ func processOpValue(value string) (string, error) { cmd.Stderr = &stderr output, err := cmd.Output() if err != nil { - // err says "exit status 1" so not very helpful, omit it from the following message + // err says "exit status 1" so not very helpful, omit it from the following message return "", fmt.Errorf("Failed to process 1password reference for %s: %s", value, stderr.String()) } return strings.TrimSpace(string(output)), nil From d5b4070d2639cce5514fd39d75c1c3b642b54167 Mon Sep 17 00:00:00 2001 From: bohendo Date: Fri, 6 Oct 2023 16:53:54 -0400 Subject: [PATCH 09/11] update ldflags in goreleaser --- .goreleaser.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.goreleaser.yml b/.goreleaser.yml index b48c57c..0dd3a37 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -3,9 +3,9 @@ project_name: cloudexec builds: - ldflags: - -s -w - - -X main.version={{.Version}} - - -X main.commit={{.FullCommit}} - - -X main.date={{.CommitDate}} + - -X main.Version={{.Version}} + - -X main.Commit={{.FullCommit}} + - -X main.Date={{.CommitDate}} env: - CGO_ENABLED=0 - GOFLAGS=-mod=readonly -trimpath From db14f69db0deb765499c2f5e2c502fd1b4e0e104 Mon Sep 17 00:00:00 2001 From: bohendo Date: Fri, 6 Oct 2023 16:54:16 -0400 Subject: [PATCH 10/11] robustify make rule --- Makefile | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index f3c6e33..609bc2d 100644 --- a/Makefile +++ b/Makefile @@ -28,10 +28,15 @@ lint: # smuggles git info in the VERSION file to work around nix flake hermicity nix-install: @cp -f ./VERSION ./.VERSION.backup - @echo "commit=$(GIT_COMMIT)" >> ./VERSION - @echo "date=$(GIT_DATE)" >> ./VERSION - @echo "nix build" - @nix build || (cp -f ./.VERSION.backup ./VERSION && false) - @mv -f ./.VERSION.backup ./VERSION - nix profile remove $(shell nix profile list | grep cloudexec | cut -d " " -f 1) - nix profile install ./result + @trap 'mv -f ./.VERSION.backup ./VERSION' EXIT; \ + SUCCESS=0; \ + echo "commit=$(GIT_COMMIT)" >> ./VERSION; \ + echo "date=$(GIT_DATE)" >> ./VERSION; \ + echo "nix build"; \ + nix build && SUCCESS=1; \ + if [ $$SUCCESS -eq 1 ]; then \ + echo nix profile remove $(shell nix profile list | grep cloudexec | cut -d " " -f 1); \ + nix profile remove $(shell nix profile list | grep cloudexec | cut -d " " -f 1); \ + echo nix profile install ./result; \ + nix profile install ./result; \ + fi From 9c5ed695124305927bad2a13417051c4dc227a03 Mon Sep 17 00:00:00 2001 From: bohendo Date: Fri, 6 Oct 2023 16:59:01 -0400 Subject: [PATCH 11/11] cleanup Makefile --- Makefile | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index 609bc2d..5469289 100644 --- a/Makefile +++ b/Makefile @@ -27,16 +27,14 @@ lint: # smuggles git info in the VERSION file to work around nix flake hermicity nix-install: - @cp -f ./VERSION ./.VERSION.backup - @trap 'mv -f ./.VERSION.backup ./VERSION' EXIT; \ - SUCCESS=0; \ + @set -e; \ + cp -f ./VERSION ./.VERSION.backup; \ + trap 'mv -f ./.VERSION.backup ./VERSION' EXIT; \ echo "commit=$(GIT_COMMIT)" >> ./VERSION; \ echo "date=$(GIT_DATE)" >> ./VERSION; \ echo "nix build"; \ - nix build && SUCCESS=1; \ - if [ $$SUCCESS -eq 1 ]; then \ - echo nix profile remove $(shell nix profile list | grep cloudexec | cut -d " " -f 1); \ - nix profile remove $(shell nix profile list | grep cloudexec | cut -d " " -f 1); \ - echo nix profile install ./result; \ - nix profile install ./result; \ - fi + nix build; \ + echo nix profile remove $(shell nix profile list | grep cloudexec | cut -d " " -f 1); \ + nix profile remove $(shell nix profile list | grep cloudexec | cut -d " " -f 1); \ + echo nix profile install ./result; \ + nix profile install ./result