aboutsummaryrefslogtreecommitdiff
path: root/cmd/version.go
diff options
context:
space:
mode:
authorJoseph Richey <joerichey94@gmail.com>2017-10-16 10:50:53 -0700
committerJoseph Richey <joerichey94@gmail.com>2017-10-19 02:22:25 -0700
commitb5cc60b2b974645f0d09721c292cd243d049cbcf (patch)
treefbc44d87ba8dd83e2238ff8e217ea560f56b3091 /cmd/version.go
parentb4299090c3e503ba0c49a6086b1a46c218ca45f4 (diff)
Refactor almost complete
Diffstat (limited to 'cmd/version.go')
-rw-r--r--cmd/version.go121
1 files changed, 99 insertions, 22 deletions
diff --git a/cmd/version.go b/cmd/version.go
index 99097b5..219684d 100644
--- a/cmd/version.go
+++ b/cmd/version.go
@@ -21,33 +21,89 @@ package cmd
import (
"fmt"
+ "time"
"github.com/blang/semver"
+ "github.com/pkg/errors"
)
-// Templates for use with the version command, which both parse the Info var.
-var (
- VersionTemplate = "{{.FullName}} {{.Info.Version}}\n"
- VersionLongTemplate = `{{if .Info.BuildTime}}
-Compiled:
- {{.Info.BuildTime}}
-{{end}}
+// Info contains global information about the program.
+var Info = &InfoData{}
-{{with $length := len .Info.Authors}}
-{{if $length}}
-Author{{if ne 1 $length}}s{{end}}:
-{{range .Info.Authors}}
- {{.Name}}{{if .Email}} <{{.Email}}>{{end}}
-{{end}}
-{{end}}
-{{end}}
+// InfoData describes the structure of our global program information
+type InfoData struct {
+ // Version (if set) will be displayed in both the short and long version
+ // output. This can be set directly or using VersionTag at link time.
+ Version semver.Version
+ // BuildTime (if set) will be displayed in the long version output. This
+ // can be set directory or by setting cmd.BuildTimeTag in a linking
+ // flag. s
+ //
+ BuildTime time.Time
+ // Authors (if non-empty) are displayed in the long version output.
+ Authors []Author
+ // Copyright (if set) is displayed in the long version output.
+ Copyright string
+}
-{{if .Info.Copyright}}
-Copyright:
-{{.Info.Copyright}}
-{{end}}`
+// Author contains the contact information for a contributor.
+type Author struct {
+ Name string
+ Email string
+}
+
+// We have to use separate Tag variables, because build tags of the form:
+// "-X cmd.Info.Version=foo"
+// are invalid.
+var (
+ // VersionTag can be set via the linker, and its value will be used to
+ // set Info.Version. Format this tag using Semver (http://semver.org/).
+ // Example:
+ // "-X cmd.VersionTag=1.2.3-beta"
+ VersionTag string
+ // BuildTimeTag can be set via the linker, and its value will be used to
+ // set Info.BuildTime. Format this tag like the output of UNIX's `date`.
+ // Example:
+ // "-X cmd.BuildTimeTag=Thu Oct 12 21:32:02 PDT 2017"
+ BuildTimeTag string
)
+func init() {
+ var err error
+ // parse the tag variables
+ if VersionTag != "" {
+ Info.Version, err = semver.ParseTolerant(VersionTag)
+ if err != nil {
+ panic(errors.Wrapf(err, "semver: parsing %q", VersionTag))
+ }
+ }
+ if BuildTimeTag != "" {
+ Info.BuildTime, err = time.Parse(time.UnixDate, BuildTimeTag)
+ if err != nil {
+ panic(err)
+ }
+ }
+
+ // fscrypt specific initialization
+ Info.Authors = []Author{{
+ Name: "Joe Richey",
+ Email: "joerichey@google.com",
+ }}
+ Info.Copyright = `Copyright 2017 Google, Inc.
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.`
+}
+
// VersionCommand is a command which will display either the VersionTag (by
// default) or the full version information: version, copyright, authors, etc...
var VersionCommand = &Command{
@@ -67,13 +123,34 @@ var longFlag = &BoolFlag{
Usage: "Print the detailed version, build, and copyright information.",
}
+// TemplateVersionShort describes the format of the one line version command.
+var TemplateVersionShort = "{{.FullName}} {{.Info.Version}}\n"
+
+// TemplateVersionLong describes the format of the additional version data.
+var TemplateVersionLong = `{{if not .Info.BuildTime.IsZero}}
+Compiled:
+ {{.Info.BuildTime}}
+{{end -}}
+
+{{with $length := len .Info.Authors}}
+Author{{if ne 1 $length}}s{{end}}:
+{{- range $.Info.Authors}}
+ {{.Name}}{{if .Email}} <{{.Email}}>{{end -}}
+{{end}}
+{{end -}}
+
+{{if .Info.Copyright}}
+Copyright:
+ {{.Info.Copyright}}
+{{end}}`
+
func versionAction(ctx *Context) error {
- if ctx.Info.Version.Equals(semver.Version{}) {
+ if Info.Version.Equals(semver.Version{}) {
return ErrUnknownVersion
}
- ctx.executeTemplate(Output, VersionTemplate)
+ ExecuteTemplate(Output, TemplateVersionShort, ctx)
if longFlag.Value {
- ctx.executeTemplate(Output, VersionLongTemplate)
+ ExecuteTemplate(Output, TemplateVersionLong, ctx)
}
return nil
}