Skip to content

Commit

Permalink
Add version flag, installation script and ldflags
Browse files Browse the repository at this point in the history
  • Loading branch information
viktigpetterr committed Jan 30, 2023
1 parent ebfaf45 commit 5e98413
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ builds:
- linux
- windows
- darwin
ldflags:
- -X main.version={{.Version}}

archives:
- replacements:
darwin: macOS
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
install:
go install ./cmd/debricked
bash scripts/install.sh

lint:
bash scripts/test_lint.sh
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,20 @@ Check out the [releases](https://github.com/debricked/cli/releases/latest) page.
Bellow follow some common ways to install the CLI.
### Linux
```sh
curl -L https://github.com/debricked/cli/releases/download/v0.0.7/cli_0.0.7_linux_x86_64.tar.gz | tar -xz debricked
curl -L https://github.com/debricked/cli/releases/download/v0.0.8/cli_0.0.8_linux_x86_64.tar.gz | tar -xz debricked
```
```sh
./debricked
```
### Mac
```sh
curl -L https://github.com/debricked/cli/releases/download/v0.0.7/cli_0.0.7_macOS_arm64.tar.gz | tar -xz debricked
curl -L https://github.com/debricked/cli/releases/download/v0.0.8/cli_0.0.8_macOS_arm64.tar.gz | tar -xz debricked
```
```sh
./debricked
```
### Windows
1. [Download zip](https://github.com/debricked/cli/releases/download/v0.0.7/cli_0.0.7_windows_x86_64.tar.gz)
1. [Download zip](https://github.com/debricked/cli/releases/download/v0.0.8/cli_0.0.8_windows_x86_64.tar.gz)
2. Unpack zip
```sh
.\debricked
Expand Down
4 changes: 3 additions & 1 deletion cmd/debricked/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"os"
)

var version string // Set at compile time

func main() {
if err := root.NewRootCmd().Execute(); err != nil {
if err := root.NewRootCmd(version).Execute(); err != nil {
os.Exit(1)
}
}
5 changes: 4 additions & 1 deletion pkg/cmd/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var accessToken string

const AccessTokenFlag = "access-token"

func NewRootCmd() *cobra.Command {
func NewRootCmd(version string) *cobra.Command {
rootCmd := &cobra.Command{
Use: "debricked",
Short: "Debricked CLI - Keep track of your dependencies!",
Expand All @@ -22,6 +22,7 @@ Complete documentation is available at https://debricked.com/docs/integrations/c
PreRun: func(cmd *cobra.Command, _ []string) {
_ = viper.BindPFlags(cmd.PersistentFlags())
},
Version: version,
}
viper.SetEnvPrefix("DEBRICKED")
viper.MustBindEnv(AccessTokenFlag)
Expand All @@ -41,5 +42,7 @@ Read more: https://debricked.com/docs/administration/access-tokens.html`,

rootCmd.CompletionOptions.DisableDefaultCmd = true

//rootCmd.SetVersionTemplate()

return rootCmd
}
2 changes: 1 addition & 1 deletion pkg/cmd/root/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func TestNewRootCmd(t *testing.T) {
cmd := NewRootCmd()
cmd := NewRootCmd("v0.0.0")
commands := cmd.Commands()
nbrOfCommands := 3
if len(commands) != nbrOfCommands {
Expand Down
8 changes: 8 additions & 0 deletions scripts/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
if ! command -v git &> /dev/null
then
echo -e "Failed to find git, thus also the version. Version will be set to v0.0.0"
fi
version=$(git symbolic-ref -q --short HEAD || git describe --tags --exact-match)
ldFlags="-X main.version=${version}"
go install -ldflags "${ldFlags}" ./cmd/debricked
1 change: 0 additions & 1 deletion scripts/test_lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[0;33m'
SET='\033[0m'
echo "Lint"
if ! command -v golangci-lint &> /dev/null
then
echo -e "${YELLOW}golangci-lint${SET} could not be found. Make sure it is installed"
Expand Down

0 comments on commit 5e98413

Please sign in to comment.