Extract file for version command

Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
This commit is contained in:
Alex Ellis (OpenFaaS Ltd) 2019-12-31 12:18:27 +00:00
parent f0172e618a
commit 17a5e2c625
4 changed files with 43 additions and 58 deletions

View File

@ -1,21 +1,9 @@
package cmd package cmd
import ( import (
"fmt"
"github.com/alexellis/faasd/pkg"
"github.com/morikuni/aec"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var (
// Version as per git repo
Version string
// GitCommit as per git repo
GitCommit string
)
// WelcomeMessage to introduce ofc-bootstrap // WelcomeMessage to introduce ofc-bootstrap
const WelcomeMessage = "Welcome to faasd" const WelcomeMessage = "Welcome to faasd"
@ -25,6 +13,14 @@ func init() {
rootCommand.AddCommand(installCmd) rootCommand.AddCommand(installCmd)
} }
func Execute() error {
if err := rootCommand.Execute(); err != nil {
return err
}
return nil
}
var rootCommand = &cobra.Command{ var rootCommand = &cobra.Command{
Use: "faasd", Use: "faasd",
Short: "Start faasd", Short: "Start faasd",
@ -35,41 +31,6 @@ faasd - serverless without Kubernetes
SilenceUsage: true, 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 { func runRootCommand(cmd *cobra.Command, args []string) error {
printLogo() printLogo()
@ -77,8 +38,3 @@ func runRootCommand(cmd *cobra.Command, args []string) error {
return nil return nil
} }
func printLogo() {
logoText := aec.WhiteF.Apply(pkg.Logo)
fmt.Println(logoText)
}

30
cmd/version.go Normal file
View File

@ -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)
}

View File

@ -4,13 +4,11 @@ import (
"os" "os"
"github.com/alexellis/faasd/cmd" "github.com/alexellis/faasd/cmd"
"github.com/alexellis/faasd/pkg"
) )
func main() { func main() {
if err := cmd.Execute(pkg.Version, pkg.GitCommit); err != nil { if err := cmd.Execute(); err != nil {
os.Exit(1) os.Exit(1)
} }
return return
} }

View File

@ -15,6 +15,7 @@ func GetVersion() string {
return Version return Version
} }
// Logo for version and root command
const Logo = ` __ _ const Logo = ` __ _
/ _| __ _ __ _ ___ __| | / _| __ _ __ _ ___ __| |
| |_ / _` + "`" + ` |/ _` + "`" + ` / __|/ _` + "`" + ` | | |_ / _` + "`" + ` |/ _` + "`" + ` / __|/ _` + "`" + ` |