From b4299090c3e503ba0c49a6086b1a46c218ca45f4 Mon Sep 17 00:00:00 2001 From: "Joe Richey joerichey@google.com" Date: Thu, 12 Oct 2017 17:59:45 -0700 Subject: Command, Context, command line splitting setup --- cmd/info.go | 88 +++++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 56 insertions(+), 32 deletions(-) (limited to 'cmd/info.go') diff --git a/cmd/info.go b/cmd/info.go index 6257ec1..96079ea 100644 --- a/cmd/info.go +++ b/cmd/info.go @@ -22,51 +22,75 @@ package cmd import ( "time" - "github.com/urfave/cli" + "github.com/blang/semver" + "github.com/pkg/errors" ) -// Info contains the global info for the functions. -var Info struct { - // Program is the name of the top-level program being executed. If not - // set it is set in cmd.RunArgs(). - Program string +var ( // VersionTag (if set) will be displayed in both the short and long - // version output. VersionTag is not parsed, so any string will work. + // version output and can be accessed though Context.Info.Version. + // VersionTag must be formatted using Semver (http://semver.org/). + // + // Often set in Makefile with "-X cmd.VersionTag=$(VERSION)" VersionTag string - // BuildTime (if set) will be displayed in the long version output. - BuildTime time.Time - // Authors (if non-empty) are displayed in the long version output. - Authors []cli.Author - // Copyright (if set) is displayed in the long version output. + // BuildTimeTag (if set) will be displayed in the long version + // output and can be accessed thought Context.Info.BuildTime. This + // string must be formatted as the output of UNIX `date`. + // + // Often set in Makefile with "-X cmd.BuildTimeTag=$(shell date)" + BuildTimeTag string + // Authors (if non-empty) are displayed in the long version output and + // can be accessed though Context.Info.Authors. + Authors []Author + // Copyright (if set) is displayed in the long version output and can + // be accessed through Context.Info.Copyright. Copyright string -} - -// Linker flags of the form "-X cmd.Info.VersionTag=1.0" do not work, so we use -// these separate files so variables can be set from the Makefile. -var ( - versionTag string - buildTime string ) // fscrypt specific initialization func init() { - Info.VersionTag = versionTag - Info.BuildTime = buildTime - Info.Authors = []cli.Author{{ + Authors = []Author{{ Name: "Joe Richey", Email: "joerichey@google.com", }} - Info.Copyright = `Copyright 2017 Google, Inc. + 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.` +} + +// Creates the Info structure by parsing the above global variables. Panics if +// the variables to parse are in the incorrect format. +func parseInfo() *Info { + var err error - 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 + var t time.Time + if BuildTimeTag != "" { + if t, err = time.Parse(time.UnixDate, BuildTimeTag); err != nil { + panic(err) + } + } - http://www.apache.org/licenses/LICENSE-2.0 + var v semver.Version + if VersionTag != "" { + if v, err = semver.ParseTolerant(VersionTag); err != nil { + panic(errors.Wrapf(err, "semver: parsing %q", VersionTag)) + } + } - 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.` + return &Info{ + Version: v, + BuildTime: t, + Authors: Authors, + Copyright: Copyright, + } } -- cgit v1.2.3