-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add version command to the default framework CLI commands (#458)
* feat(framework): add version command * feat(framework): use go binary build info to resolve application version * feat(framework): use go binary build info to resolve application version * feat(framework): use cmd Print * feat(framework): edit implementation * feat(framework): fix linter errors --------- Co-authored-by: Kirill Grigorev <kirill.grigorev@omnevo.net>
- Loading branch information
Showing
6 changed files
with
130 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,65 @@ | ||
package flamingo | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"runtime" | ||
"runtime/debug" | ||
) | ||
|
||
var appVersion = "develop" | ||
|
||
const ( | ||
vcsRevisionSettingKey = "vcs.revision" | ||
) | ||
|
||
type ( | ||
AppInfo struct { | ||
AppVersion string | ||
VCSRevision string | ||
RuntimeVersion string | ||
MainPackagePath string | ||
FlamingoVersion string | ||
} | ||
) | ||
|
||
// AppVersion returns the application version | ||
// set this during build with `go build -ldflags "-X flamingo.me/flamingo/v3/framework/flamingo.appVersion=1.2.3"`. | ||
func AppVersion() string { | ||
return appVersion | ||
} | ||
|
||
// GetAppInfo provides basic application information like runtime version, flamingo version etc. | ||
func GetAppInfo() AppInfo { | ||
appInfo := AppInfo{ | ||
AppVersion: AppVersion(), | ||
RuntimeVersion: runtime.Version(), | ||
} | ||
|
||
if info, ok := debug.ReadBuildInfo(); ok { | ||
appInfo.MainPackagePath = info.Main.Path | ||
|
||
for _, module := range info.Deps { | ||
if module.Path == "flamingo.me/flamingo/v3" { | ||
appInfo.FlamingoVersion = module.Version | ||
} | ||
} | ||
|
||
for _, setting := range info.Settings { | ||
if setting.Key == vcsRevisionSettingKey { | ||
appInfo.VCSRevision = setting.Value | ||
} | ||
} | ||
} | ||
|
||
return appInfo | ||
} | ||
|
||
// PrintAppInfo prints application info to the writer | ||
func PrintAppInfo(writer io.Writer, appInfo AppInfo) { | ||
_, _ = fmt.Fprintf(writer, "%20s\t%s\n", "App version:", appInfo.AppVersion) | ||
_, _ = fmt.Fprintf(writer, "%20s\t%s\n", "Go runtime version:", appInfo.RuntimeVersion) | ||
_, _ = fmt.Fprintf(writer, "%20s\t%s\n", "VCS revision:", appInfo.VCSRevision) | ||
_, _ = fmt.Fprintf(writer, "%20s\t%s\n", "Path:", appInfo.MainPackagePath) | ||
_, _ = fmt.Fprintf(writer, "%20s\t%s\n", "Flamingo version:", appInfo.FlamingoVersion) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package flamingo_test | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"flamingo.me/flamingo/v3/framework/flamingo" | ||
) | ||
|
||
func TestPrintAppInfo(t *testing.T) { | ||
t.Parallel() | ||
|
||
type args struct { | ||
appInfo flamingo.AppInfo | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
args args | ||
wantWriter string | ||
}{ | ||
{ | ||
name: "", | ||
args: args{ | ||
appInfo: flamingo.AppInfo{ | ||
AppVersion: "v1.2.3", | ||
VCSRevision: "c9ce01204a18ff2f3e9ed999fbf7f3eb8e70b614", | ||
RuntimeVersion: "go1.23.3", | ||
MainPackagePath: "go.aoe.com/whitelabel-airline/flamingo", | ||
FlamingoVersion: "v3.11.0", | ||
}, | ||
}, | ||
wantWriter: " App version:\tv1.2.3\n Go runtime version:\tgo1.23.3\n VCS revision:\tc9ce01204a18ff2f3e9ed999fbf7f3eb8e70b614\n Path:\tgo.aoe.com/whitelabel-airline/flamingo\n Flamingo version:\tv3.11.0\n", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
writer := &bytes.Buffer{} | ||
flamingo.PrintAppInfo(writer, tt.args.appInfo) | ||
assert.Equalf(t, tt.wantWriter, writer.String(), "PrintAppInfo(%v, %v)", writer, tt.args.appInfo) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package flamingo | ||
|
||
import ( | ||
"bytes" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
func VersionCmd() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "version", | ||
Short: "Application version", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
var buffer bytes.Buffer | ||
|
||
appInfo := GetAppInfo() | ||
PrintAppInfo(&buffer, appInfo) | ||
|
||
cmd.Println(buffer.String()) | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |
"testing" | ||
|
||
"flamingo.me/dingo" | ||
|
||
"flamingo.me/flamingo/v3/framework" | ||
) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters