Skip to content

Commit 285640a

Browse files
authored
Merge pull request #17 from liquidweb/version-cmd
add 'version' command
2 parents bf3151e + 7169933 commit 285640a

File tree

8 files changed

+110
-25
lines changed

8 files changed

+110
-25
lines changed

.goreleaser.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ builds:
1111
- windows
1212
- linux
1313
- darwin
14+
ldflags: -X github.com/liquidweb/liquidweb-cli/cmd.Version={{ .Env.VERSION }} -X github.com/liquidweb/liquidweb-cli/cmd.BuildTime={{ .Env.BUILDTIME }} -X github.com/liquidweb/liquidweb-cli/cmd.GitCommit={{ .Env.GITCOMMIT }}
1415
archives:
1516
- replacements:
1617
darwin: Darwin

Makefile

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,21 @@
11
SHELL=/bin/bash
22

3-
GO_LINKER_SYMBOL := "main.version"
4-
53
default:
64
$(MAKE) install
75

8-
%:
9-
@:
10-
11-
args = `arg="$(filter-out $@,$(MAKECMDGOALS))" && echo $${arg:-${1}}`
12-
13-
all: build
14-
156
clean:
167
rm -rf _exe/
178

189
static:
19-
CGO_ENABLED=0 go build -x -ldflags '-w -extldflags "-static"' -o _exe/liquidweb-cli github.com/liquidweb/liquidweb-cli
10+
scripts/build/static
2011

2112
build:
22-
go build -ldflags="-s -w" -o _exe/liquidweb-cli github.com/liquidweb/liquidweb-cli
13+
scripts/build/dynamic
2314

2415
install:
25-
go install
26-
@echo ""
27-
@echo "liquidweb-cli has been installed, and it should now be in your PATH."
28-
@echo ""
29-
@echo "Executables are installed in the directory named by the GOBIN environment"
30-
@echo "variable, which defaults to GOPATH/bin or HOME/go/bin if the GOPATH"
31-
@echo "environment variable is not set. Executables in GOROOT"
32-
@echo "are installed in GOROOT/bin or GOTOOLDIR instead of GOBIN."
33-
@echo ""
34-
35-
run:
36-
go run main.go $(call args,)
16+
scripts/build/install
3717

3818
release-build:
39-
goreleaser --snapshot --skip-publish --rm-dist
19+
scripts/build/release-build
4020

41-
.PHONY: clean static all build run
21+
.PHONY: clean static all build

cmd/version.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Copyright © LiquidWeb
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package cmd
17+
18+
import (
19+
"fmt"
20+
21+
"github.com/spf13/cobra"
22+
)
23+
24+
// default build-time variables; these are overridden via ldflags
25+
var (
26+
Version = "unknown-version"
27+
GitCommit = "unknown-commit"
28+
BuildTime = "unknown-buildtime"
29+
)
30+
31+
var versionCmd = &cobra.Command{
32+
Use: "version",
33+
Short: "show build information",
34+
Long: `Show build information.
35+
36+
This information should be provided with any bug report.`,
37+
Run: func(cmd *cobra.Command, args []string) {
38+
fmt.Printf("LiquidWeb CLI Build Details\n\n")
39+
fmt.Printf(" Build Time: %s\n", BuildTime)
40+
fmt.Printf(" Version: %s\n", Version)
41+
fmt.Printf(" Git commit: %s\n\n", GitCommit)
42+
},
43+
}
44+
45+
func init() {
46+
rootCmd.AddCommand(versionCmd)
47+
}

scripts/build/.variables

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env bash
2+
3+
unset LDFLAGS
4+
5+
GITCOMMIT=$(git rev-parse --short HEAD 2> /dev/null)
6+
BUILDTIME=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
7+
8+
latest_tag=$(git describe --abbrev=0 --tags)
9+
gitcommit_match_tag=$(git describe --exact-match ${GITCOMMIT} 2> /dev/null)
10+
11+
if [[ "${gitcommit_match_tag}" == "${latest_tag}" ]]; then
12+
# building on official tag
13+
VERSION=${latest_tag}
14+
else
15+
# -dirty because its a dev build, not on a official tag.
16+
VERSION="${latest_tag}-dirty"
17+
fi
18+
19+
export LDFLAGS="\
20+
-w \
21+
-X \"github.com/liquidweb/liquidweb-cli/cmd.GitCommit=${GITCOMMIT}\" \
22+
-X \"github.com/liquidweb/liquidweb-cli/cmd.BuildTime=${BUILDTIME}\" \
23+
-X \"github.com/liquidweb/liquidweb-cli/cmd.Version=${VERSION}\" \
24+
${LDFLAGS:-} \
25+
"

scripts/build/dynamic

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env bash
2+
3+
source scripts/build/.variables
4+
set -e
5+
go build -x --ldflags "${LDFLAGS}" -o _exe/liquidweb-cli github.com/liquidweb/liquidweb-cli
6+
exit 0

scripts/build/install

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env bash
2+
3+
source scripts/build/.variables
4+
set -e
5+
go install -x --ldflags "${LDFLAGS}"
6+
echo ""
7+
echo "liquidweb-cli has been installed, and it should now be in your PATH."
8+
echo ""
9+
echo "Executables are installed in the directory named by the GOBIN environment"
10+
echo "variable, which defaults to GOPATH/bin or HOME/go/bin if the GOPATH"
11+
echo "environment variable is not set. Executables in GOROOT"
12+
echo "are installed in GOROOT/bin or GOTOOLDIR instead of GOBIN."
13+
echo ""
14+
exit 0

scripts/build/release-build

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env bash
2+
3+
source scripts/build/.variables
4+
set -e
5+
VERSION=${VERSION} GITCOMMIT=${GITCOMMIT} BUILDTIME=${BUILDTIME} goreleaser --snapshot --skip-publish --rm-dist
6+
exit 0

scripts/build/static

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env bash
2+
3+
source scripts/build/.variables
4+
set -e
5+
CGO_ENABLED=0 go build -x -ldflags '-w -extldflags "-static"' --ldflags "${LDFLAGS}" -o _exe/liquidweb-cli github.com/liquidweb/liquidweb-cli
6+
exit 0

0 commit comments

Comments
 (0)