Skip to content

Commit

Permalink
Add version command (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
Evertras authored Jan 2, 2023
1 parent c8d26b9 commit faf3db7
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/docker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ jobs:
with:
push: true
tags: evertras/cynomys:${{ env.EVERTRAS_PUBLISHED_VERSION }},evertras/cynomys:latest
build-args: |
BUILD_VERSION=${{ env.EVERTRAS_PUBLISHED_VERSION }}
2 changes: 2 additions & 0 deletions .github/workflows/goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@ jobs:
distribution: goreleaser
version: latest
args: release --rm-dist
ldflags:
- github.com/evertras/cynomys/cmd/cyn/cmds.BuildVersion=v{{ .Version }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
7 changes: 5 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Builder image
FROM golang:1.19.3 AS builder
ARG BUILD_VERSION=docker-dev

WORKDIR /app
COPY go.mod .
Expand All @@ -8,13 +9,15 @@ RUN go mod download

COPY cmd/ cmd/
COPY pkg/ pkg/
RUN CGO_ENABLED=0 go build -o /cyn ./cmd/cyn/*.go
RUN CGO_ENABLED=0 go build \
-o /cyn \
-ldflags="-X github.com/evertras/cynomys/cmd/cyn/cmds.BuildVersion=$BUILD_VERSION" \
./cmd/cyn/*.go

# We want to access some basic shell tools for debugging, but we want to be
# as tiny as possible...
FROM alpine:3.17.0
RUN apk add strace
RUN which strace
COPY --from=builder /cyn /usr/local/bin/cyn

ENTRYPOINT ["/usr/local/bin/cyn"]
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build: bin/cyn
build-all: bin/cyn bin/cyn-linux bin/cyn-mac bin/cyn-windows

# Build for local
bin/cyn: ./cmd/cyn/*.go ./pkg/listener/*.go ./pkg/sender/*.go
bin/cyn: ./cmd/cyn/*.go ./pkg/listener/*.go ./pkg/sender/*.go ./cmd/cyn/cmds/*.go
CGO_ENABLED=0 go build -o bin/cyn ./cmd/cyn/*.go

# Build for other OSes
Expand Down
6 changes: 5 additions & 1 deletion cmd/cyn/root.go → cmd/cyn/cmds/root.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cmds

import (
"fmt"
Expand Down Expand Up @@ -150,3 +150,7 @@ var rootCmd = &cobra.Command{
return eg.Wait()
},
}

func Execute() error {
return rootCmd.Execute()
}
21 changes: 21 additions & 0 deletions cmd/cyn/cmds/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cmds

import (
"fmt"

"github.com/spf13/cobra"
)

var BuildVersion string = "dev"

func init() {
rootCmd.AddCommand(versionCmd)
}

var versionCmd = &cobra.Command{
Use: "version",
Short: "Displays the version",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(BuildVersion)
},
}
4 changes: 3 additions & 1 deletion cmd/cyn/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import (
"fmt"
"log"
"os"

"github.com/evertras/cynomys/cmd/cyn/cmds"
)

func main() {
log.SetOutput(os.Stdout)
err := rootCmd.Execute()
err := cmds.Execute()

if err != nil {
fmt.Println("Failed to run:", err)
Expand Down
9 changes: 7 additions & 2 deletions tests/features/cli-help.feature
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Feature: run to trigger CLI help
Feature: run to trigger CLI information
In order to use cyn more easily
Cyn should provide help text when run with incorrect configurations
Cyn should provide help text when run with incorrect configurations or
when prompted to do so

Scenario: run with no flags or config
Given cyn is run with no flags or config
Expand All @@ -12,3 +13,7 @@ Feature: run to trigger CLI help
Given cyn is run with an unknown flag
When I wait a moment
Then the stderr contains "Usage"

Scenario: get version
Given cyn is run with the version command
Then the stdout contains "dev"
4 changes: 4 additions & 0 deletions tests/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ func (t *testContext) cynIsRunWithAnUnknownFlag() error {
return t.startCynInBackground("--thisdoesntexist")
}

func (t *testContext) cynIsRunWithTheVersionCommand() error {
return t.startCynInBackground("version")
}

func (t *testContext) cynIsRunWithTheConfigFile() error {
return t.startCynInBackground("--config-file", configFileLocation)
}
Expand Down
1 change: 1 addition & 0 deletions tests/steps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func InitializeScenario(sc *godog.ScenarioContext) {
sc.Step(`^cyn is sending (UDP|TCP) to (.*)$`, t.cynIsSendingTo)
sc.Step(`^cyn is run with no flags or config$`, t.cynIsRunWithoutFlagsOrConfig)
sc.Step(`^cyn is run with an unknown flag$`, t.cynIsRunWithAnUnknownFlag)
sc.Step(`^cyn is run with the version command$`, t.cynIsRunWithTheVersionCommand)
sc.Step(`^cyn is started with the config file$`, t.cynIsRunWithTheConfigFile)
sc.Step(`^I wait (\d+) seconds?$`, t.waitSeconds)
sc.Step(`^I wait a moment$`, t.waitAMoment)
Expand Down

0 comments on commit faf3db7

Please sign in to comment.