From 17a5e2c6252ca5a6cb1c17b1ba901152dc92f5d5 Mon Sep 17 00:00:00 2001 From: "Alex Ellis (OpenFaaS Ltd)" Date: Tue, 31 Dec 2019 12:18:27 +0000 Subject: [PATCH] Extract file for version command Signed-off-by: Alex Ellis (OpenFaaS Ltd) --- cmd/root.go | 60 +++++++------------------------------------------- cmd/version.go | 30 +++++++++++++++++++++++++ main.go | 4 +--- pkg/version.go | 7 +++--- 4 files changed, 43 insertions(+), 58 deletions(-) create mode 100644 cmd/version.go diff --git a/cmd/root.go b/cmd/root.go index c649319..6de72ae 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,21 +1,9 @@ package cmd import ( - "fmt" - - "github.com/alexellis/faasd/pkg" - "github.com/morikuni/aec" "github.com/spf13/cobra" ) -var ( - // Version as per git repo - Version string - - // GitCommit as per git repo - GitCommit string -) - // WelcomeMessage to introduce ofc-bootstrap const WelcomeMessage = "Welcome to faasd" @@ -25,6 +13,14 @@ func init() { rootCommand.AddCommand(installCmd) } +func Execute() error { + + if err := rootCommand.Execute(); err != nil { + return err + } + return nil +} + var rootCommand = &cobra.Command{ Use: "faasd", Short: "Start faasd", @@ -35,41 +31,6 @@ faasd - serverless without Kubernetes SilenceUsage: true, } -var versionCmd = &cobra.Command{ - Use: "version", - Short: "Display version information.", - Run: parseBaseCommand, -} - -func getVersion() string { - if len(Version) != 0 { - return Version - } - return "dev" -} - -func parseBaseCommand(_ *cobra.Command, _ []string) { - printLogo() - - fmt.Printf( - `faasd -Commit: %s -Version: %s -`, pkg.GitCommit, pkg.GetVersion()) -} - -func Execute(version, gitCommit string) error { - - // Get Version and GitCommit values from main.go. - Version = version - GitCommit = gitCommit - - if err := rootCommand.Execute(); err != nil { - return err - } - return nil -} - func runRootCommand(cmd *cobra.Command, args []string) error { printLogo() @@ -77,8 +38,3 @@ func runRootCommand(cmd *cobra.Command, args []string) error { return nil } - -func printLogo() { - logoText := aec.WhiteF.Apply(pkg.Logo) - fmt.Println(logoText) -} diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 0000000..ea8eeae --- /dev/null +++ b/cmd/version.go @@ -0,0 +1,30 @@ +package cmd + +import ( + "fmt" + + "github.com/alexellis/faasd/pkg" + "github.com/morikuni/aec" + "github.com/spf13/cobra" +) + +var versionCmd = &cobra.Command{ + Use: "version", + Short: "Display version information.", + Run: parseBaseCommand, +} + +func parseBaseCommand(_ *cobra.Command, _ []string) { + printLogo() + + fmt.Printf( + `faasd +Commit: %s +Version: %s +`, pkg.GitCommit, pkg.GetVersion()) +} + +func printLogo() { + logoText := aec.WhiteF.Apply(pkg.Logo) + fmt.Println(logoText) +} diff --git a/main.go b/main.go index 3bea0a7..4b12013 100644 --- a/main.go +++ b/main.go @@ -4,13 +4,11 @@ import ( "os" "github.com/alexellis/faasd/cmd" - "github.com/alexellis/faasd/pkg" ) func main() { - if err := cmd.Execute(pkg.Version, pkg.GitCommit); err != nil { + if err := cmd.Execute(); err != nil { os.Exit(1) } return - } diff --git a/pkg/version.go b/pkg/version.go index 18d46cc..04bcae4 100644 --- a/pkg/version.go +++ b/pkg/version.go @@ -1,13 +1,13 @@ package pkg var ( - //GitCommit Git Commit SHA + // GitCommit Git Commit SHA GitCommit string - //Version version of the CLI + // Version version of the CLI Version string ) -//GetVersion get latest version +// GetVersion get latest version func GetVersion() string { if len(Version) == 0 { return "dev" @@ -15,6 +15,7 @@ func GetVersion() string { return Version } +// Logo for version and root command const Logo = ` __ _ / _| __ _ __ _ ___ __| | | |_ / _` + "`" + ` |/ _` + "`" + ` / __|/ _` + "`" + ` |