Skip to content

Commit

Permalink
Merge pull request #45 from tfadeyi/fix-version-command
Browse files Browse the repository at this point in the history
Fix version command
  • Loading branch information
tfadeyi committed Jun 17, 2023
2 parents 1e29807 + ac29c7d commit 2a5cb3d
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 25 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ go.work
kodata/
builds/
demo/data

dist/
4 changes: 2 additions & 2 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ builds:
flags:
- -trimpath
ldflags:
- -s -w -X "internal/version.version={{.Version}}" -X "internal/version.commit={{.Commit}}" -X "internal/version.date={{.Date}}"
- -s -w -X "github.com/tfadeyi/slotalk/internal/version.Platform={{ .Os }}/{{ .Arch }}" github.com/tfadeyi/slotalk/internal/version.Version={{.Version}}" -X "github.com/tfadeyi/slotalk/internal/version.Commit={{.Commit}}" -X "github.com/tfadeyi/slotalk/internal/version.Date={{.Date}}"
binary: "{{ .ProjectName }}"
goos:
- linux
Expand All @@ -25,7 +25,7 @@ builds:
flags:
- -trimpath
ldflags:
- -s -w -X "internal/version.version={{.Version}}" -X "internal/version.commit={{.Commit}}" -X "internal/version.date={{.Date}}"
- -s -w -X "github.com/tfadeyi/slotalk/internal/version.Version={{.Version}}" -X "github.com/tfadeyi/slotalk/internal/version.Commit={{.Commit}}" -X "github.com/tfadeyi/slotalk/internal/version.Date={{.Date}}"
binary: "{{ .ProjectName }}"
goos:
- darwin
Expand Down
10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@ SHELL := /bin/bash
COMMIT:=$(shell git rev-list -1 HEAD)
VERSION:=$(COMMIT)
DATE:=$(shell date -uR)
GOOS:=$(shell go env GOOS)
GOARCH:=$(shell go env GOARCH)

define LDFLAGS
-X "github.com/tfadeyi/slotalk/internal/version.Platform=$(GOOS)/$(GOARCH)" \
-X "github.com/tfadeyi/slotalk/internal/version.Commit=$(COMMIT)" \
-X "github.com/tfadeyi/slotalk/internal/version.BuildDate=$(DATE)"
endef

BIN_NAME:=slotalk
GOFLAGS:=-mod=readonly
GO_BUILD:=go build $(GOFLAGS)
GO_BUILD:=go build $(GOFLAGS) -ldflags '$(LDFLAGS)'

# include files with the `// +build mock` annotation
TEST_TAGS:=-tags mock -coverprofile cover.out
Expand Down
3 changes: 2 additions & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var versionCmd = &cobra.Command{
ctx := cmd.Context()
log := logging.LoggerFromContext(ctx)
log = log.WithName("version")
log.Info(version.BuildInfo())
log.Debug(version.BuildInfo())
log.Info(version.Info())
},
}
18 changes: 9 additions & 9 deletions internal/logging/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Package logging uses the logr.Logger interface to integrate with different loggi


<a name="ContextWithLogger"></a>
## func [ContextWithLogger](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/internal/logging/logging.go#L21>)
## func [ContextWithLogger](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/internal/logging/logging.go#L25>)

```go
func ContextWithLogger(ctx context.Context, l Logger) context.Context
Expand All @@ -32,7 +32,7 @@ func ContextWithLogger(ctx context.Context, l Logger) context.Context
ContextWithLogger wraps the logr NewContext function

<a name="IsValidLevel"></a>
## func [IsValidLevel](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/internal/logging/logging.go#L83>)
## func [IsValidLevel](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/internal/logging/logging.go#L87>)

```go
func IsValidLevel(lvl string) bool
Expand All @@ -53,7 +53,7 @@ type Logger struct {
```

<a name="LoggerFromContext"></a>
### func [LoggerFromContext](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/internal/logging/logging.go#L26>)
### func [LoggerFromContext](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/internal/logging/logging.go#L30>)

```go
func LoggerFromContext(ctx context.Context) Logger
Expand All @@ -62,7 +62,7 @@ func LoggerFromContext(ctx context.Context) Logger
LoggerFromContext wraps the LoggerFromContext or creates a Zap production logger

<a name="NewStandardLogger"></a>
### func [NewStandardLogger](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/internal/logging/logging.go#L35>)
### func [NewStandardLogger](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/internal/logging/logging.go#L39>)

```go
func NewStandardLogger() Logger
Expand All @@ -71,7 +71,7 @@ func NewStandardLogger() Logger
NewStandardLogger wraps the creation of a Golang standard logger

<a name="Logger.Debug"></a>
### func \(\*Logger\) [Debug](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/internal/logging/logging.go#L64>)
### func \(\*Logger\) [Debug](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/internal/logging/logging.go#L68>)

```go
func (l *Logger) Debug(msg string, keysAndValues ...interface{})
Expand All @@ -80,7 +80,7 @@ func (l *Logger) Debug(msg string, keysAndValues ...interface{})
Debug logs the message using the info debug level \(1\)

<a name="Logger.Info"></a>
### func \(\*Logger\) [Info](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/internal/logging/logging.go#L54>)
### func \(\*Logger\) [Info](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/internal/logging/logging.go#L58>)

```go
func (l *Logger) Info(msg string, keysAndValues ...interface{})
Expand All @@ -89,7 +89,7 @@ func (l *Logger) Info(msg string, keysAndValues ...interface{})
Info logs the message using the info log level \(2\)

<a name="Logger.SetLevel"></a>
### func \(\*Logger\) [SetLevel](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/internal/logging/logging.go#L46>)
### func \(\*Logger\) [SetLevel](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/internal/logging/logging.go#L50>)

```go
func (l *Logger) SetLevel(lvl string) Logger
Expand All @@ -98,7 +98,7 @@ func (l *Logger) SetLevel(lvl string) Logger
SetLevel sets the global level against which all info logs will be compared. If this is greater than or equal to the "V" of the logger, the message will be logged. A higher value here means more logs will be written

<a name="Logger.Warn"></a>
### func \(\*Logger\) [Warn](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/internal/logging/logging.go#L59>)
### func \(\*Logger\) [Warn](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/internal/logging/logging.go#L63>)

```go
func (l *Logger) Warn(err error, keysAndValues ...interface{})
Expand All @@ -107,7 +107,7 @@ func (l *Logger) Warn(err error, keysAndValues ...interface{})
Warn logs the error using the warning log level \(3\)

<a name="Logger.WithName"></a>
### func \(\*Logger\) [WithName](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/internal/logging/logging.go#L39>)
### func \(\*Logger\) [WithName](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/internal/logging/logging.go#L43>)

```go
func (l *Logger) WithName(name string) Logger
Expand Down
8 changes: 6 additions & 2 deletions internal/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ type (
}
)

const (
debug = stdr.MessageClass(5)
)

// ContextWithLogger wraps the logr NewContext function
func ContextWithLogger(ctx context.Context, l Logger) context.Context {
return logr.NewContext(ctx, l.Logger)
Expand Down Expand Up @@ -62,15 +66,15 @@ func (l *Logger) Warn(err error, keysAndValues ...interface{}) {

// Debug logs the message using the info debug level (1)
func (l *Logger) Debug(msg string, keysAndValues ...interface{}) {
l.V(int(stdr.All)).Info(msg, keysAndValues...)
l.V(int(debug)).Info(msg, keysAndValues...)
}

func findLogLevel(lvl string) stdr.MessageClass {
switch strings.TrimSpace(strings.ToLower(lvl)) {
case "info":
return stdr.Info
case "debug":
return stdr.All
return debug
case "warn":
return stdr.Error
case "none":
Expand Down
6 changes: 3 additions & 3 deletions internal/logging/logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ func TestFindLevel(t *testing.T) {
t.Parallel()

t.Run("Successfully return stdr.All for debug as input", func(t *testing.T) {
assert.Equal(t, stdr.All, findLogLevel("debug"))
assert.Equal(t, debug, findLogLevel("debug"))
})

t.Run("Successfully return stdr.All for debug(with whitespace) as input", func(t *testing.T) {
assert.Equal(t, stdr.All, findLogLevel(" debug "))
assert.Equal(t, debug, findLogLevel(" debug "))
})

t.Run("Successfully return stdr.All for DEBUG as input", func(t *testing.T) {
assert.Equal(t, stdr.All, findLogLevel("DEBUG"))
assert.Equal(t, debug, findLogLevel("DEBUG"))
})

t.Run("Successfully return stdr.Error for warn as input", func(t *testing.T) {
Expand Down
26 changes: 25 additions & 1 deletion internal/version/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,40 @@ Package version, returns the build info of the binary

## Index

- [Variables](<#variables>)
- [func BuildInfo\(\) string](<#BuildInfo>)
- [func Info\(\) string](<#Info>)


## Variables

<a name="Platform"></a>Values injected at build\-time

```go
var (
Platform string = "unknown"
Version string = "dev"
Commit string = "unknown"
Date string = "unknown"
)
```

<a name="BuildInfo"></a>
## func [BuildInfo](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/internal/version/version.go#L13>)
## func [BuildInfo](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/internal/version/version.go#L16>)

```go
func BuildInfo() string
```

BuildInfo returns the binary build information

<a name="Info"></a>
## func [Info](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/internal/version/version.go#L26>)

```go
func Info() string
```

Info returns simple version information of the binary

Generated by [gomarkdoc](<https://github.com/princjef/gomarkdoc>)
23 changes: 18 additions & 5 deletions internal/version/version.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
package version

import "fmt"
import (
"fmt"
)

// Values injected at build-time
var (
version string = "dev"
commit string = "unknown"
date string = "unknown"
Platform string = "unknown"
Version string = "dev"
Commit string = "unknown"
Date string = "unknown"
)

// BuildInfo returns the binary build information
func BuildInfo() string {
return fmt.Sprintf("%s, commit %s, built at %s", version, commit, date)
return fmt.Sprintf(`
Host machine: %s
Version: %s
Commit: %s
Built at: %s`, Platform, Version, Commit, Date)
}

// Info returns simple version information of the binary
func Info() string {
return Version
}
12 changes: 11 additions & 1 deletion internal/version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,15 @@ import (

func TestBuildInfo(t *testing.T) {
info := BuildInfo()
assert.Equal(t, fmt.Sprintf("%s, commit %s, built at %s", version, commit, date), info)
assert.Equal(t, fmt.Sprintf(`
Host machine: %s
Version: %s
Commit: %s
Built at: %s`, Platform, Version, Commit, Date), info)
}

func TestInfo(t *testing.T) {
info := Info()
assert.Equal(t, Version, info)
}

0 comments on commit 2a5cb3d

Please sign in to comment.