chore: version sub command remove --version and -v flag (#2090)

* test: Add unit test for version cmd
This commit is contained in:
Steven Masley
2022-06-06 17:38:51 -05:00
committed by GitHub
parent a7a7e7561d
commit e2b2580196
2 changed files with 39 additions and 13 deletions

View File

@ -53,7 +53,6 @@ func init() {
func Root() *cobra.Command {
cmd := &cobra.Command{
Use: "coder",
Version: buildinfo.Version(),
SilenceErrors: true,
SilenceUsage: true,
Long: `Coder — A tool for provisioning self-hosted development environments.
@ -90,10 +89,10 @@ func Root() *cobra.Command {
users(),
portForward(),
workspaceAgent(),
versionCmd(),
)
cmd.SetUsageTemplate(usageTemplate())
cmd.SetVersionTemplate(versionTemplate())
cmd.PersistentFlags().String(varURL, "", "Specify the URL to your deployment.")
cmd.PersistentFlags().String(varToken, "", "Specify an authentication token.")
@ -110,6 +109,27 @@ func Root() *cobra.Command {
return cmd
}
// versionCmd prints the coder version
func versionCmd() *cobra.Command {
return &cobra.Command{
Use: "version",
Short: "Show coder version",
Example: "coder version",
RunE: func(cmd *cobra.Command, args []string) error {
var str strings.Builder
_, _ = str.WriteString(fmt.Sprintf("Coder %s", buildinfo.Version()))
buildTime, valid := buildinfo.Time()
if valid {
_, _ = str.WriteString(" " + buildTime.Format(time.UnixDate))
}
_, _ = str.WriteString("\r\n" + buildinfo.ExternalURL() + "\r\n")
_, _ = fmt.Fprint(cmd.OutOrStdout(), str.String())
return nil
},
}
}
// createClient returns a new client from the command context.
// It reads from global configuration files if flags are not set.
func createClient(cmd *cobra.Command) (*codersdk.Client, error) {
@ -286,17 +306,6 @@ Use "{{.CommandPath}} [command] --help" for more information about a command.
{{end}}`
}
func versionTemplate() string {
template := `Coder {{printf "%s" .Version}}`
buildTime, valid := buildinfo.Time()
if valid {
template += " " + buildTime.Format(time.UnixDate)
}
template += "\r\n" + buildinfo.ExternalURL()
template += "\r\n"
return template
}
// FormatCobraError colorizes and adds "--help" docs to cobra commands.
func FormatCobraError(err error, cmd *cobra.Command) string {
helpErrMsg := fmt.Sprintf("Run '%s --help' for usage.", cmd.CommandPath())