mirror of
https://github.com/coder/coder.git
synced 2025-07-18 14:17:22 +00:00
chore: version
sub command remove --version
and -v
flag (#2090)
* test: Add unit test for version cmd
This commit is contained in:
35
cli/root.go
35
cli/root.go
@ -53,7 +53,6 @@ func init() {
|
|||||||
func Root() *cobra.Command {
|
func Root() *cobra.Command {
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "coder",
|
Use: "coder",
|
||||||
Version: buildinfo.Version(),
|
|
||||||
SilenceErrors: true,
|
SilenceErrors: true,
|
||||||
SilenceUsage: true,
|
SilenceUsage: true,
|
||||||
Long: `Coder — A tool for provisioning self-hosted development environments.
|
Long: `Coder — A tool for provisioning self-hosted development environments.
|
||||||
@ -90,10 +89,10 @@ func Root() *cobra.Command {
|
|||||||
users(),
|
users(),
|
||||||
portForward(),
|
portForward(),
|
||||||
workspaceAgent(),
|
workspaceAgent(),
|
||||||
|
versionCmd(),
|
||||||
)
|
)
|
||||||
|
|
||||||
cmd.SetUsageTemplate(usageTemplate())
|
cmd.SetUsageTemplate(usageTemplate())
|
||||||
cmd.SetVersionTemplate(versionTemplate())
|
|
||||||
|
|
||||||
cmd.PersistentFlags().String(varURL, "", "Specify the URL to your deployment.")
|
cmd.PersistentFlags().String(varURL, "", "Specify the URL to your deployment.")
|
||||||
cmd.PersistentFlags().String(varToken, "", "Specify an authentication token.")
|
cmd.PersistentFlags().String(varToken, "", "Specify an authentication token.")
|
||||||
@ -110,6 +109,27 @@ func Root() *cobra.Command {
|
|||||||
return cmd
|
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.
|
// createClient returns a new client from the command context.
|
||||||
// It reads from global configuration files if flags are not set.
|
// It reads from global configuration files if flags are not set.
|
||||||
func createClient(cmd *cobra.Command) (*codersdk.Client, error) {
|
func createClient(cmd *cobra.Command) (*codersdk.Client, error) {
|
||||||
@ -286,17 +306,6 @@ Use "{{.CommandPath}} [command] --help" for more information about a command.
|
|||||||
{{end}}`
|
{{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.
|
// FormatCobraError colorizes and adds "--help" docs to cobra commands.
|
||||||
func FormatCobraError(err error, cmd *cobra.Command) string {
|
func FormatCobraError(err error, cmd *cobra.Command) string {
|
||||||
helpErrMsg := fmt.Sprintf("Run '%s --help' for usage.", cmd.CommandPath())
|
helpErrMsg := fmt.Sprintf("Run '%s --help' for usage.", cmd.CommandPath())
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
package cli_test
|
package cli_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/coder/coder/buildinfo"
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/coder/coder/cli"
|
"github.com/coder/coder/cli"
|
||||||
@ -19,4 +22,18 @@ func TestRoot(t *testing.T) {
|
|||||||
errStr := cli.FormatCobraError(err, cmd)
|
errStr := cli.FormatCobraError(err, cmd)
|
||||||
require.Contains(t, errStr, "Run 'coder delete --help' for usage.")
|
require.Contains(t, errStr, "Run 'coder delete --help' for usage.")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("Version", func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
cmd, _ := clitest.New(t, "version")
|
||||||
|
cmd.SetOut(buf)
|
||||||
|
err := cmd.Execute()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
output := buf.String()
|
||||||
|
require.Contains(t, output, buildinfo.Version(), "has version")
|
||||||
|
require.Contains(t, output, buildinfo.ExternalURL(), "has url")
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user