modrevision:minor

This commit is contained in:
Gerasimos (Makis) Maropoulos 2022-04-10 01:25:19 +03:00
parent 193de00426
commit 73dfabf412
No known key found for this signature in database
GPG Key ID: 66FCC29BD385FCA6
5 changed files with 28 additions and 54 deletions

View File

@ -3,6 +3,7 @@ package cmd
import (
"github.com/username/project/api"
"github.com/kataras/iris/v12"
"github.com/spf13/cobra"
)
@ -13,7 +14,7 @@ var serverConfig api.Configuration
// New returns a new CLI app.
// Build with:
// $ go build -ldflags="-s -w"
func New(buildRevision, buildTime string) *cobra.Command {
func New() *cobra.Command {
configFile := defaultConfigFilename
rootCmd := &cobra.Command{
@ -35,8 +36,8 @@ func New(buildRevision, buildTime string) *cobra.Command {
}
helpTemplate := HelpTemplate{
BuildRevision: buildRevision,
BuildTime: buildTime,
BuildRevision: iris.BuildRevision,
BuildTime: iris.BuildTime,
ShowGoRuntimeVersion: true,
}
rootCmd.SetHelpTemplate(helpTemplate.String())

View File

@ -7,13 +7,8 @@ import (
"github.com/username/project/cmd"
)
var (
buildRevision string
buildTime string
)
func main() {
app := cmd.New(buildRevision, buildTime)
app := cmd.New()
if err := app.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)

View File

@ -15,10 +15,11 @@ import (
)
var (
// BuildRevision holds the vcs commit id information.
// BuildRevision holds the vcs commit id information of the program's build.
// To display the Iris' version please use the iris.Version constant instead.
// Available at go version 1.18+
BuildRevision = context.BuildRevision
// BuildTime holds the vcs commit time information.
// BuildTime holds the vcs commit time information of the program's build.
// Available at go version 1.18+
BuildTime = context.BuildTime
)

View File

@ -46,10 +46,10 @@ import (
)
var (
// BuildRevision holds the vcs commit id information.
// BuildRevision holds the vcs commit id information of the program's build.
// Available at go version 1.18+
BuildRevision string
// BuildTime holds the vcs commit time information.
// BuildTime holds the vcs commit time information of the program's build.
// Available at go version 1.18+
BuildTime string
)

View File

@ -27,15 +27,6 @@ type Options struct {
TimeLocation *time.Location
}
type modRevision struct {
options Options
buildTime string
buildRevision string
contents []byte
}
// New returns an Iris Handler which renders
// the server name (env), build information (if available)
// and an OK message. The handler displays simple debug information such as build commit id and time.
@ -50,25 +41,32 @@ type modRevision struct {
// TimeLocation: time.FixedZone("Greece/Athens", 10800),
// }))
func New(opts Options) context.Handler {
bTime, bRevision := context.BuildTime, context.BuildRevision
buildTime, buildRevision := context.BuildTime, context.BuildRevision
if opts.UnixTime {
if t, err := time.Parse(time.RFC3339, bTime); err == nil {
bTime = fmt.Sprintf("%d", t.Unix())
if t, err := time.Parse(time.RFC3339, buildTime); err == nil {
buildTime = fmt.Sprintf("%d", t.Unix())
}
} else if opts.TimeLocation != nil {
if t, err := time.Parse(time.RFC3339, bTime); err == nil {
bTime = t.In(opts.TimeLocation).String()
if t, err := time.Parse(time.RFC3339, buildTime); err == nil {
buildTime = t.In(opts.TimeLocation).String()
}
}
m := &modRevision{
options: opts,
buildTime: bTime,
buildRevision: bRevision,
var buildInfo string
if buildInfo = opts.ServerName; buildInfo != "" {
if env := opts.Env; env != "" {
buildInfo += fmt.Sprintf(" (%s)", env)
}
}
contents := []byte(m.String())
if buildRevision != "" && buildTime != "" {
buildTitle := ">>>> build"
tab := strings.Repeat(" ", len(buildTitle))
buildInfo += fmt.Sprintf("\n\n%s\n%[2]srevision %[3]s\n%[2]sbuildtime %[4]s\n%[2]sdeveloper %[5]s",
buildTitle, tab, buildRevision, buildTime, opts.Developer)
}
contents := []byte(buildInfo)
if len(contents) > 0 {
contents = append(contents, []byte("\n\nOK")...)
} else {
@ -79,24 +77,3 @@ func New(opts Options) context.Handler {
ctx.Write(contents)
}
}
// String returns the server name and its running environment or an empty string
// of the given server name is empty.
func (m *modRevision) String() string {
if name := m.options.ServerName; name != "" {
if env := m.options.Env; env != "" {
name += fmt.Sprintf(" (%s)", env)
}
if m.buildRevision != "" && m.buildTime != "" {
buildTitle := ">>>> build" // if we ever want an emoji, there is one: \U0001f4bb
tab := strings.Repeat(" ", len(buildTitle))
name += fmt.Sprintf("\n\n%[1]s\n%srevision %s\n[1]sbuildtime %s\n[1]sdeveloper %s", tab,
buildTitle, m.buildRevision, m.buildTime, m.options.Developer)
}
return name
}
return ""
}