diff --git a/_examples/project/cmd/cmd.go b/_examples/project/cmd/cmd.go index 20f3d441..ba4e29f6 100644 --- a/_examples/project/cmd/cmd.go +++ b/_examples/project/cmd/cmd.go @@ -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()) diff --git a/_examples/project/main.go b/_examples/project/main.go index 89d00f59..0e8ce255 100644 --- a/_examples/project/main.go +++ b/_examples/project/main.go @@ -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) diff --git a/aliases.go b/aliases.go index 554c9660..76968a66 100644 --- a/aliases.go +++ b/aliases.go @@ -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 ) diff --git a/context/context.go b/context/context.go index ef7d99f9..257d0bbe 100644 --- a/context/context.go +++ b/context/context.go @@ -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 ) diff --git a/middleware/modrevision/modrevision.go b/middleware/modrevision/modrevision.go index ca40b620..198339c8 100644 --- a/middleware/modrevision/modrevision.go +++ b/middleware/modrevision/modrevision.go @@ -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 "" -}