Skip to content

Commit 4499d87

Browse files
committed
feat(version): print version
- When first argument is `version` or `-version` or `--version` - Print release tag version, or latest-<commithash> otherwise
1 parent e676983 commit 4499d87

File tree

3 files changed

+31
-18
lines changed

3 files changed

+31
-18
lines changed

cmd/updater/main.go

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -98,21 +98,27 @@ func main() {
9898

9999
func _main(ctx context.Context, reader *reader.Reader, args []string, logger log.LoggerInterface,
100100
buildInfo models.BuildInformation, timeNow func() time.Time) (err error) {
101-
if health.IsClientMode(args) {
102-
// Running the program in a separate instance through the Docker
103-
// built-in healthcheck, in an ephemeral fashion to query the
104-
// long running instance of the program about its status
105-
106-
var healthSettings config.Health
107-
healthSettings.Read(reader)
108-
healthSettings.SetDefaults()
109-
err = healthSettings.Validate()
110-
if err != nil {
111-
return fmt.Errorf("health settings: %w", err)
101+
if len(args) > 1 {
102+
switch args[1] {
103+
case "version", "-version", "--version":
104+
fmt.Println(buildInfo.VersionString())
105+
return nil
106+
case "healthcheck":
107+
// Running the program in a separate instance through the Docker
108+
// built-in healthcheck, in an ephemeral fashion to query the
109+
// long running instance of the program about its status
110+
111+
var healthSettings config.Health
112+
healthSettings.Read(reader)
113+
healthSettings.SetDefaults()
114+
err = healthSettings.Validate()
115+
if err != nil {
116+
return fmt.Errorf("health settings: %w", err)
117+
}
118+
119+
client := health.NewClient()
120+
return client.Query(ctx, *healthSettings.ServerAddress)
112121
}
113-
114-
client := health.NewClient()
115-
return client.Query(ctx, *healthSettings.ServerAddress)
116122
}
117123

118124
announcementExp, err := time.Parse(time.RFC3339, "2023-07-15T00:00:00Z")

internal/health/client.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ import (
1010
"time"
1111
)
1212

13-
func IsClientMode(args []string) bool {
14-
return len(args) > 1 && args[1] == "healthcheck"
15-
}
16-
1713
type Client struct {
1814
*http.Client
1915
}

internal/models/build.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,14 @@ type BuildInformation struct {
55
Commit string `json:"commit"`
66
Date string `json:"buildDate"`
77
}
8+
9+
func (b BuildInformation) VersionString() string {
10+
if b.Version != "latest" {
11+
return b.Version
12+
}
13+
const commitShortHashLength = 7
14+
if len(b.Commit) != commitShortHashLength {
15+
return "latest"
16+
}
17+
return b.Version + "-" + b.Commit[:7]
18+
}

0 commit comments

Comments
 (0)