github.com/criteo/command-launcher@v0.0.0-20230407142452-fb616f546e98/cmd/version.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strings"
     7  
     8  	"github.com/criteo/command-launcher/internal/context"
     9  	"github.com/spf13/cobra"
    10  )
    11  
    12  func printVersion() {
    13  	ctx, _ := context.AppContext()
    14  	fmt.Printf("%s version %s\n", ctx.AppName(), getVersion(ctx.AppVersion(), ctx.AppBuildNum()))
    15  }
    16  
    17  func getVersion(version string, buildNum string) string {
    18  	if version == "" {
    19  		return fmt.Sprintf("dev, build %s", os.Getenv("USER"))
    20  	}
    21  
    22  	return fmt.Sprintf("%s, build %s", version, buildNum)
    23  }
    24  
    25  func AddVersionCmd(rootCmd *cobra.Command, appCtx context.LauncherContext) {
    26  	versionCmd := &cobra.Command{
    27  		Use:   "version",
    28  		Short: fmt.Sprintf("Print the version number of %s command", strings.ToTitle(appCtx.AppName())),
    29  		Long:  fmt.Sprintf(`All software has versions. This is %s's`, strings.ToTitle(appCtx.AppName())),
    30  		Run: func(cmd *cobra.Command, args []string) {
    31  			printVersion()
    32  		},
    33  	}
    34  	rootCmd.AddCommand(versionCmd)
    35  }