Skip to content

Commit 3e20e9e

Browse files
authored
1 parent ec6befd commit 3e20e9e

File tree

7 files changed

+75
-55
lines changed

7 files changed

+75
-55
lines changed

Makefile

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,47 @@
11
# We're going to be using this Makefile as a sort of task runner, for all sorts of operations in this project
22

33
# first we'll grab the current version from our ENV VAR (added by our CI) - see here: https://github.com/marketplace/actions/version-increment
4+
BINARY_NAME := otdfctl
45
CURR_VERSION := ${SEM_VER}
6+
COMMIT_SHA := ${GITHUB_SHA}
7+
BUILD_TIME := $(shell date -u '+%Y-%m-%dT%H:%M:%SZ')
8+
9+
GO_MOD_LINE = $(shell head -n 1 go.mod | cut -c 8-)
10+
GO_MOD_NAME = $(word 1,$(subst /, ,$(GO_MOD_LINE)))
11+
APP_CFG = $(GO_MOD_LINE)/pkg/config
12+
13+
GO_BUILD_FLAGS=-ldflags "-X $(APP_CFG).Version=${CURR_VERSION} -X $(APP_CFG).CommitSha=${COMMIT_SHA} -X $(APP_CFG).BuildTime=${BUILD_TIME}"
14+
GO_BUILD_PREFIX=$(TARGET_DIR)/$(BINARY_NAME)-${CURR_VERSION}
15+
16+
# If commit sha is not available try git
17+
ifndef COMMIT_SHA
18+
COMMIT_SHA := $(shell git rev-parse HEAD)
19+
endif
20+
21+
# If current version is not available try git
22+
ifndef CURR_VERSION
23+
CURR_VERSION := $(shell git describe --tags --always)
24+
endif
525

626
# Default target executed when no arguments are given to make.
727
# NOTE: .PHONY is used to indicate that the target is not a file (e.g. there is no file called 'build-darwin-amd64', instead the .PHONY directive tells make that the proceeding target is a command to be executed, not a file to be generated)
828
.PHONY: all
929
all: run
1030
.DEFAULT_GOAL := run
1131

12-
13-
14-
15-
# Binary name: Change this to your actual binary name
16-
BINARY_NAME=${BIN_NAME}
17-
18-
1932
# Target directory for compiled binaries
2033
TARGET_DIR=target
2134

2235
# Output directory for the zipped artifacts
2336
OUTPUT_DIR=output
2437

25-
# Targets for building the project for different platforms
26-
.PHONY: build-darwin-amd64 build-darwin-arm64 build-linux-amd64 build-linux-arm build-linux-arm64 build-windows-amd64 build-windows-arm build-windows-arm64
27-
build: clean build-darwin-amd64 build-darwin-arm64 build-linux-amd64 build-linux-arm build-linux-arm64 build-windows-amd64 build-windows-arm build-windows-arm64
28-
2938
# Build commands for each platform
30-
build-darwin-amd64:
31-
GOOS=darwin GOARCH=amd64 go build -o $(TARGET_DIR)/$(BINARY_NAME)-${CURR_VERSION}-darwin-amd64 .
32-
33-
build-darwin-arm64:
34-
GOOS=darwin GOARCH=arm64 go build -o $(TARGET_DIR)/$(BINARY_NAME)-${CURR_VERSION}-darwin-arm64 .
35-
36-
build-linux-amd64:
37-
GOOS=linux GOARCH=amd64 go build -o $(TARGET_DIR)/$(BINARY_NAME)-${CURR_VERSION}-linux-amd64 .
38-
39-
build-linux-arm:
40-
GOOS=linux GOARCH=arm go build -o $(TARGET_DIR)/$(BINARY_NAME)-${CURR_VERSION}-linux-arm .
41-
42-
build-linux-arm64:
43-
GOOS=linux GOARCH=arm64 go build -o $(TARGET_DIR)/$(BINARY_NAME)-${CURR_VERSION}-linux-arm64 .
44-
45-
build-windows-amd64:
46-
GOOS=windows GOARCH=amd64 go build -o $(TARGET_DIR)/$(BINARY_NAME)-${CURR_VERSION}-windows-amd64.exe .
39+
PLATFORMS := darwin-amd64 darwin-arm64 linux-amd64 linux-arm linux-arm64 windows-amd64-.exe windows-arm-.exe windows-arm64-.exe
4740

48-
build-windows-arm:
49-
GOOS=windows GOARCH=arm go build -o $(TARGET_DIR)/$(BINARY_NAME)-${CURR_VERSION}-windows-arm.exe .
41+
build: clean $(addprefix build-,$(PLATFORMS))
5042

51-
build-windows-arm64:
52-
GOOS=windows GOARCH=arm64 go build -o $(TARGET_DIR)/$(BINARY_NAME)-${CURR_VERSION}-windows-arm64.exe .
43+
build-%:
44+
GOOS=$(word 1,$(subst -, ,$*)) GOARCH=$(word 2,$(subst -, ,$*)) go build $(GO_BUILD_FLAGS) -o $(GO_BUILD_PREFIX)-$(word 1,$(subst -, ,$*))-$(word 2,$(subst -, ,$*))$(word 3,$(subst -, ,$*))
5345

5446
# Target for running the project (adjust as necessary for your project)
5547
.PHONY: run

cmd/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package cmd
33
import (
44
"fmt"
55

6-
"github.com/opentdf/otdfctl/internal/config"
76
"github.com/opentdf/otdfctl/pkg/cli"
7+
"github.com/opentdf/otdfctl/pkg/config"
88
"github.com/opentdf/otdfctl/pkg/man"
99
"github.com/spf13/cobra"
1010
)

cmd/dev.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99
"strings"
1010

1111
"github.com/evertras/bubble-table/table"
12-
"github.com/opentdf/otdfctl/internal/config"
1312
"github.com/opentdf/otdfctl/pkg/cli"
13+
"github.com/opentdf/otdfctl/pkg/config"
1414
"github.com/opentdf/otdfctl/pkg/handlers"
1515
"github.com/opentdf/otdfctl/pkg/man"
1616
"github.com/opentdf/platform/protocol/go/common"

cmd/execute.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"fmt"
55
"os"
66

7-
"github.com/opentdf/otdfctl/internal/config"
7+
"github.com/opentdf/otdfctl/pkg/config"
88
"github.com/spf13/cobra"
99
)
1010

cmd/root.go

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ Copyright © 2023 NAME HERE <EMAIL ADDRESS>
44
package cmd
55

66
import (
7-
"github.com/opentdf/otdfctl/internal/config"
7+
"fmt"
8+
9+
"github.com/opentdf/otdfctl/pkg/cli"
10+
"github.com/opentdf/otdfctl/pkg/config"
811
"github.com/opentdf/otdfctl/pkg/man"
912
"github.com/spf13/cobra"
1013
)
@@ -20,39 +23,55 @@ var (
2023

2124
// RootCmd represents the base command when called without any subcommands.
2225
var (
23-
defaultCmdName = "otdfctl"
24-
RootCmd = &man.Docs.GetDoc("<root>").Command
26+
RootCmd = &man.Docs.GetDoc("<root>").Command
2527
)
2628

2729
func init() {
28-
doc := man.Docs.GetDoc("<root>")
29-
RootCmd = &doc.Command
30+
rootCmd := man.Docs.GetCommand("<root>", man.WithRun(func(cmd *cobra.Command, args []string) {
31+
flaghelper := cli.NewFlagHelper(cmd)
32+
33+
if flaghelper.GetOptionalBool("version") {
34+
fmt.Println(config.AppName + " version " + config.Version + " (" + config.BuildTime + ") " + config.CommitSha)
35+
return
36+
}
37+
38+
cmd.Help()
39+
}))
40+
41+
RootCmd = &rootCmd.Command
42+
43+
RootCmd.Flags().Bool(
44+
rootCmd.GetDocFlag("version").Name,
45+
rootCmd.GetDocFlag("version").DefaultAsBool(),
46+
rootCmd.GetDocFlag("version").Description,
47+
)
48+
3049
RootCmd.PersistentFlags().String(
31-
doc.GetDocFlag("host").Name,
32-
doc.GetDocFlag("host").Default,
33-
doc.GetDocFlag("host").Description,
50+
rootCmd.GetDocFlag("host").Name,
51+
rootCmd.GetDocFlag("host").Default,
52+
rootCmd.GetDocFlag("host").Description,
3453
)
3554
RootCmd.PersistentFlags().Bool(
36-
doc.GetDocFlag("tls-no-verify").Name,
37-
doc.GetDocFlag("tls-no-verify").DefaultAsBool(),
38-
doc.GetDocFlag("tls-no-verify").Description,
55+
rootCmd.GetDocFlag("tls-no-verify").Name,
56+
rootCmd.GetDocFlag("tls-no-verify").DefaultAsBool(),
57+
rootCmd.GetDocFlag("tls-no-verify").Description,
3958
)
4059
RootCmd.PersistentFlags().String(
41-
doc.GetDocFlag("log-level").Name,
42-
doc.GetDocFlag("log-level").Default,
43-
doc.GetDocFlag("log-level").Description,
60+
rootCmd.GetDocFlag("log-level").Name,
61+
rootCmd.GetDocFlag("log-level").Default,
62+
rootCmd.GetDocFlag("log-level").Description,
4463
)
4564
RootCmd.PersistentFlags().StringVar(
4665
&clientCredsFile,
47-
doc.GetDocFlag("with-client-creds-file").Name,
48-
doc.GetDocFlag("with-client-creds-file").Default,
49-
doc.GetDocFlag("with-client-creds-file").Description,
66+
rootCmd.GetDocFlag("with-client-creds-file").Name,
67+
rootCmd.GetDocFlag("with-client-creds-file").Default,
68+
rootCmd.GetDocFlag("with-client-creds-file").Description,
5069
)
5170
RootCmd.PersistentFlags().StringVar(
5271
&clientCredsJSON,
53-
doc.GetDocFlag("with-client-creds").Name,
54-
doc.GetDocFlag("with-client-creds").Default,
55-
doc.GetDocFlag("with-client-creds").Description,
72+
rootCmd.GetDocFlag("with-client-creds").Name,
73+
rootCmd.GetDocFlag("with-client-creds").Default,
74+
rootCmd.GetDocFlag("with-client-creds").Description,
5675
)
5776
RootCmd.AddGroup(&cobra.Group{ID: "tdf"})
5877
}

docs/man/_index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ title: otdfctl - OpenTDF Control Tool
44
command:
55
name: otdfctl
66
flags:
7+
- name: version
8+
description: show version
9+
default: false
710
- name: host
811
description: Hostname of the platform (i.e. https://localhost)
912
default:

internal/config/config.go renamed to pkg/config/config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ import (
1111
"github.com/spf13/viper"
1212
)
1313

14+
var AppName = "otdfctl"
15+
16+
var Version = "0.0.0"
17+
var BuildTime = "1970-01-01T00:00:00Z"
18+
var CommitSha = "0000000"
19+
1420
type Output struct {
1521
Format string `yaml:"format" default:"styled"`
1622
}

0 commit comments

Comments
 (0)