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

View File

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

View File

@ -15,10 +15,11 @@ import (
) )
var ( 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+ // Available at go version 1.18+
BuildRevision = context.BuildRevision 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+ // Available at go version 1.18+
BuildTime = context.BuildTime BuildTime = context.BuildTime
) )

View File

@ -46,10 +46,10 @@ import (
) )
var ( 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+ // Available at go version 1.18+
BuildRevision string 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+ // Available at go version 1.18+
BuildTime string BuildTime string
) )

View File

@ -27,15 +27,6 @@ type Options struct {
TimeLocation *time.Location TimeLocation *time.Location
} }
type modRevision struct {
options Options
buildTime string
buildRevision string
contents []byte
}
// New returns an Iris Handler which renders // New returns an Iris Handler which renders
// the server name (env), build information (if available) // 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. // 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), // TimeLocation: time.FixedZone("Greece/Athens", 10800),
// })) // }))
func New(opts Options) context.Handler { func New(opts Options) context.Handler {
bTime, bRevision := context.BuildTime, context.BuildRevision buildTime, buildRevision := context.BuildTime, context.BuildRevision
if opts.UnixTime { if opts.UnixTime {
if t, err := time.Parse(time.RFC3339, bTime); err == nil { if t, err := time.Parse(time.RFC3339, buildTime); err == nil {
bTime = fmt.Sprintf("%d", t.Unix()) buildTime = fmt.Sprintf("%d", t.Unix())
} }
} else if opts.TimeLocation != nil { } else if opts.TimeLocation != nil {
if t, err := time.Parse(time.RFC3339, bTime); err == nil { if t, err := time.Parse(time.RFC3339, buildTime); err == nil {
bTime = t.In(opts.TimeLocation).String() buildTime = t.In(opts.TimeLocation).String()
} }
} }
m := &modRevision{ var buildInfo string
options: opts, if buildInfo = opts.ServerName; buildInfo != "" {
if env := opts.Env; env != "" {
buildTime: bTime, buildInfo += fmt.Sprintf(" (%s)", env)
buildRevision: bRevision, }
} }
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 { if len(contents) > 0 {
contents = append(contents, []byte("\n\nOK")...) contents = append(contents, []byte("\n\nOK")...)
} else { } else {
@ -79,24 +77,3 @@ func New(opts Options) context.Handler {
ctx.Write(contents) 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 ""
}