-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(cmd): Clean up versioning with functional argument
- Loading branch information
Showing
10 changed files
with
77 additions
and
25 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
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
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
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,12 @@ | ||
package options | ||
|
||
import "github.com/spf13/cobra" | ||
|
||
type Option func(cmd *cobra.Command) | ||
|
||
func WithVersion(version string) Option { | ||
return func(cmd *cobra.Command) { | ||
cmd.Version = buildVersion(version) | ||
cmd.InitDefaultVersionFlag() | ||
} | ||
} |
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,35 @@ | ||
package options | ||
|
||
import "runtime/debug" | ||
|
||
func buildVersion(version string) string { | ||
var commit string | ||
var modified bool | ||
if info, ok := debug.ReadBuildInfo(); ok { | ||
for _, setting := range info.Settings { | ||
switch setting.Key { | ||
case "vcs.revision": | ||
commit = setting.Value | ||
case "vcs.modified": | ||
if setting.Value == "true" { | ||
modified = true | ||
} | ||
} | ||
} | ||
} | ||
|
||
if commit != "" { | ||
if len(commit) > 8 { | ||
commit = commit[:8] | ||
} | ||
if modified { | ||
commit = "*" + commit | ||
} | ||
if version == "" { | ||
version = commit | ||
} else { | ||
version += " (" + commit + ")" | ||
} | ||
} | ||
return version | ||
} |
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
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