Skip to content

Commit 1ca7eeb

Browse files
authored
Merge pull request #34 from WilliamK112/agent/fabricopsctl-version
Add fabricopsctl version command
2 parents 638904c + 99275d8 commit 1ca7eeb

7 files changed

Lines changed: 108 additions & 5 deletions

File tree

.github/workflows/release.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ jobs:
100100
- name: Build binaries
101101
run: |
102102
make build
103-
make build-fabricopsctl
103+
make build-fabricopsctl-release VERSION="${VERSION}"
104+
test "$(bin/fabricopsctl version)" = "fabricopsctl ${VERSION}"
104105
105106
- name: Build release artifacts
106107
run: |

.github/workflows/test.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,10 @@ jobs:
3232
run: |
3333
make test
3434
35+
- name: Verify fabricopsctl release version
36+
run: |
37+
make build-fabricopsctl-release VERSION=1.2.3
38+
test "$(bin/fabricopsctl version)" = "fabricopsctl 1.2.3"
39+
3540
- name: Verify generated files are committed
3641
run: git diff --exit-code

Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ IMAGE_REGISTRY ?= ghcr.io/lf-decentralized-trust-labs
66
IMAGE_REPOSITORY ?= fabricops
77
VERSION ?= 0.2.0
88
RELEASE_IMG ?= $(IMAGE_REGISTRY)/$(IMAGE_REPOSITORY):$(VERSION)
9+
FABRICOPSCTL_VERSION ?= development
10+
FABRICOPSCTL_LDFLAGS ?= -X main.version=$(FABRICOPSCTL_VERSION)
911
SAMPLE_CHAINCODE_IMAGES ?= $(IMAGE_REGISTRY)/fabricops-node-settlement:$(VERSION) $(IMAGE_REGISTRY)/fabricops-go-settlement:$(VERSION) $(IMAGE_REGISTRY)/fabricops-java-settlement:$(VERSION)
1012
RELEASE_CHECK_IMAGES ?= $(RELEASE_IMG) $(SAMPLE_CHAINCODE_IMAGES)
1113
# YEAR defines the year value used for substituting the YEAR placeholder in the boilerplate header.
@@ -177,7 +179,11 @@ build: manifests generate fmt vet ## Build manager binary.
177179

178180
.PHONY: build-fabricopsctl
179181
build-fabricopsctl: fmt vet ## Build fabricopsctl helper binary.
180-
go build -o bin/fabricopsctl ./cmd/fabricopsctl
182+
go build -ldflags "$(FABRICOPSCTL_LDFLAGS)" -o bin/fabricopsctl ./cmd/fabricopsctl
183+
184+
.PHONY: build-fabricopsctl-release
185+
build-fabricopsctl-release: ## Build fabricopsctl with VERSION embedded for a release.
186+
$(MAKE) build-fabricopsctl FABRICOPSCTL_VERSION=$(VERSION)
181187

182188
.PHONY: run
183189
run: manifests generate fmt vet ## Run a controller from your host.

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ Install the CLI with Go:
115115
```bash
116116
go install github.com/LF-Decentralized-Trust-labs/FabricOps/cmd/fabricopsctl@latest
117117
export PATH="$(go env GOPATH)/bin:$PATH"
118+
fabricopsctl version
118119
fabricopsctl status -n default fabricnetwork-sample
119120
fabricopsctl wait -n default --timeout 20m fabricnetwork-sample
120121
fabricopsctl status --participant -n default bankb-participant
@@ -143,6 +144,7 @@ When building from source:
143144

144145
```bash
145146
make build-fabricopsctl
147+
bin/fabricopsctl version
146148
bin/fabricopsctl status -n default fabricnetwork-sample
147149
bin/fabricopsctl wait -n default --timeout 20m fabricnetwork-sample
148150
bin/fabricopsctl status --participant -n default bankb-participant
@@ -162,6 +164,10 @@ bin/fabricopsctl query --participant -n default --org BankB \
162164
--args '["settlement-001"]' bankb-participant
163165
```
164166

167+
Local source builds report `fabricopsctl development`. Release builds use
168+
`make build-fabricopsctl-release VERSION=<version>` to inject the release
169+
version into the binary with Go linker flags.
170+
165171
Tools that render or apply FabricOps resources, including a future Fablo
166172
Kubernetes engine, can use the same CLI surface after applying the
167173
`FabricNetwork` or `FabricParticipant`: `wait` for readiness or a named
@@ -398,6 +404,9 @@ generates `install.yaml` and the Helm chart package, verifies GHCR public
398404
visibility, commits the release-prep changes, tags the commit, and creates the
399405
GitHub release with the generated assets.
400406

407+
The workflow also builds `fabricopsctl` with the release version embedded and
408+
verifies the binary through the normal build gate.
409+
401410
The local release helpers remain useful for debugging individual steps. For
402411
example:
403412

cmd/fabricopsctl/main.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ import (
4040
)
4141

4242
const (
43-
defaultNamespace = "default"
44-
defaultWaitFor = "condition=Ready"
45-
defaultCondition = "Ready"
43+
defaultNamespace = "default"
44+
defaultWaitFor = "condition=Ready"
45+
defaultCondition = "Ready"
46+
developmentVersion = "development"
4647

4748
connectionProfileJSONKey = "connection.json"
4849
connectionProfileYAMLKey = "connection.yaml"
@@ -51,6 +52,7 @@ const (
5152
var (
5253
errUsage = errors.New("usage error")
5354
cliScheme = runtime.NewScheme()
55+
version = developmentVersion
5456
)
5557

5658
func init() {
@@ -83,6 +85,8 @@ func run(args []string, stdout, stderr io.Writer) error {
8385
case "help", "-h", "--help":
8486
printUsage(stdout)
8587
return nil
88+
case "version":
89+
return runVersion(args[1:], stdout, stderr)
8690
case "status":
8791
return runStatus(args[1:], stdout, stderr)
8892
case "wait":
@@ -101,6 +105,15 @@ func run(args []string, stdout, stderr io.Writer) error {
101105
}
102106
}
103107

108+
func runVersion(args []string, stdout, stderr io.Writer) error {
109+
if len(args) != 0 {
110+
printLine(stderr, "Usage: fabricopsctl version")
111+
return errUsage
112+
}
113+
printf(stdout, "fabricopsctl %s\n", version)
114+
return nil
115+
}
116+
104117
func runStatus(args []string, stdout, stderr io.Writer) error {
105118
var kube kubeOptions
106119
var output string
@@ -856,6 +869,7 @@ func printLine(out io.Writer, args ...any) {
856869

857870
func printUsage(out io.Writer) {
858871
printLine(out, `Usage:
872+
fabricopsctl version
859873
fabricopsctl status [flags] <fabricnetwork>
860874
fabricopsctl status --participant [flags] <fabricparticipant>
861875
fabricopsctl wait [flags] <fabricnetwork>
@@ -878,6 +892,7 @@ Common flags:
878892
--context string Kubeconfig context override
879893
880894
Examples:
895+
fabricopsctl version
881896
fabricopsctl status fabricnetwork-sample
882897
fabricopsctl status --participant bankb-participant
883898
fabricopsctl status -n default -o json fabricnetwork-sample

cmd/fabricopsctl/main_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,63 @@ import (
2929
fabricopsv1alpha1 "github.com/LF-Decentralized-Trust-labs/FabricOps/api/v1alpha1"
3030
)
3131

32+
func TestRunVersionPrintsDevelopmentVersion(t *testing.T) {
33+
var stdout, stderr bytes.Buffer
34+
if err := run([]string{"version"}, &stdout, &stderr); err != nil {
35+
t.Fatalf("run(version) error = %v", err)
36+
}
37+
if got, want := strings.TrimSpace(stdout.String()), "fabricopsctl development"; got != want {
38+
t.Fatalf("stdout = %q, want %q", got, want)
39+
}
40+
if stderr.Len() != 0 {
41+
t.Fatalf("stderr = %q, want empty", stderr.String())
42+
}
43+
}
44+
45+
func TestRunVersionPrintsInjectedVersion(t *testing.T) {
46+
originalVersion := version
47+
t.Cleanup(func() { version = originalVersion })
48+
version = "0.2.0"
49+
50+
var stdout, stderr bytes.Buffer
51+
if err := run([]string{"version"}, &stdout, &stderr); err != nil {
52+
t.Fatalf("run(version) error = %v", err)
53+
}
54+
if got, want := strings.TrimSpace(stdout.String()), "fabricopsctl 0.2.0"; got != want {
55+
t.Fatalf("stdout = %q, want %q", got, want)
56+
}
57+
if stderr.Len() != 0 {
58+
t.Fatalf("stderr = %q, want empty", stderr.String())
59+
}
60+
}
61+
62+
func TestRunVersionRejectsArguments(t *testing.T) {
63+
var stdout, stderr bytes.Buffer
64+
err := run([]string{"version", "extra"}, &stdout, &stderr)
65+
if !errors.Is(err, errUsage) {
66+
t.Fatalf("run(version extra) error = %v, want errUsage", err)
67+
}
68+
if stdout.Len() != 0 {
69+
t.Fatalf("stdout = %q, want empty", stdout.String())
70+
}
71+
if got, want := strings.TrimSpace(stderr.String()), "Usage: fabricopsctl version"; got != want {
72+
t.Fatalf("stderr = %q, want %q", got, want)
73+
}
74+
}
75+
76+
func TestHelpListsVersionCommand(t *testing.T) {
77+
var stdout, stderr bytes.Buffer
78+
if err := run([]string{"--help"}, &stdout, &stderr); err != nil {
79+
t.Fatalf("run(--help) error = %v", err)
80+
}
81+
if !strings.Contains(stdout.String(), "fabricopsctl version") {
82+
t.Fatalf("stdout does not list version command:\n%s", stdout.String())
83+
}
84+
if stderr.Len() != 0 {
85+
t.Fatalf("stderr = %q, want empty", stderr.String())
86+
}
87+
}
88+
3289
func TestWaitForFabricNetworkReadyReturnsOnReadyCondition(t *testing.T) {
3390
var stdout, stderr bytes.Buffer
3491
err := waitForFabricNetworkReady(

docs/first-release-checklist.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ The workflow performs these release gates:
1616
- Validate the release tag and ensure the release does not already exist.
1717
- Update release-version files.
1818
- Run Go module tidy verification, unit/envtest tests, lint, and binary builds.
19+
- Build `fabricopsctl` with the release version embedded through Go linker flags.
1920
- Build and push the multi-platform manager image.
2021
- Build and push sample chaincode images.
2122
- Build `dist/install.yaml` and `dist/fabricops-<version>.tgz`.
@@ -26,6 +27,15 @@ The workflow performs these release gates:
2627
The commands below mirror the automated workflow and are useful for local
2728
debugging or release dry runs.
2829

30+
## Verify The CLI Version
31+
32+
Build the CLI with the same linker-injected version used by the release workflow:
33+
34+
```bash
35+
make build-fabricopsctl-release VERSION=0.2.0
36+
test "$(bin/fabricopsctl version)" = "fabricopsctl 0.2.0"
37+
```
38+
2939
## Build And Publish Images
3040

3141
```bash

0 commit comments

Comments
 (0)