Skip to content

Commit

Permalink
unify the flag --output of the kusion version command with other …
Browse files Browse the repository at this point in the history
…commands
  • Loading branch information
SparkYuan committed Jul 13, 2023
1 parent 5f6e0f9 commit 1afdbeb
Show file tree
Hide file tree
Showing 15 changed files with 1,270 additions and 1,327 deletions.
6 changes: 3 additions & 3 deletions docs/cmd/en_US/kusion.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ kusion [flags]
* [kusion env](kusion_env.md) - Print Kusion environment information
* [kusion init](kusion_init.md) - Initialize the scaffolding for a project
* [kusion ls](kusion_ls.md) - List all project and stack information
* [kusion preview](kusion_preview.md) - Preview a series of resource changes within the stack
* [kusion version](kusion_version.md) - Print the kusion version info
* [kusion preview](kusion_preview.md) - Preview a series of resource changes within the stack
* [kusion version](kusion_version.md) - Print the kusion version information for the current context.

###### Auto generated by spf13/cobra on 11-Jul-2023
###### Auto generated by spf13/cobra on 13-Jul-2023
10 changes: 4 additions & 6 deletions docs/cmd/en_US/kusion_version.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## kusion version

Print the kusion version info
Print the kusion version information for the current context.

### Synopsis

Expand All @@ -20,14 +20,12 @@ kusion version [flags]
### Options

```
-h, --help help for version
-j, --json Print version info as JSON
-s, --short Print version info as versionShort string
-y, --yaml Print version info as YAML
-h, --help help for version
-o, --output string Output format. Only json format is supported for now
```

### SEE ALSO

* [kusion](kusion.md) - Kusion manages the Kubernetes cluster by code

###### Auto generated by spf13/cobra on 11-Jul-2023
###### Auto generated by spf13/cobra on 13-Jul-2023
6 changes: 3 additions & 3 deletions docs/cmd/zh_CN/kusion.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ kusion [flags]
* [kusion env](kusion_env.md) - Print Kusion environment information
* [kusion init](kusion_init.md) - 初始化一个 Project 的脚手架
* [kusion ls](kusion_ls.md) - List all project and stack information
* [kusion preview](kusion_preview.md) - Preview a series of resource changes within the stack
* [kusion version](kusion_version.md) - Print the kusion version info
* [kusion preview](kusion_preview.md) - Preview a series of resource changes within the stack
* [kusion version](kusion_version.md) - 打印当前 Kusion 的版本信息

###### Auto generated by spf13/cobra on 11-Jul-2023
###### Auto generated by spf13/cobra on 13-Jul-2023
14 changes: 6 additions & 8 deletions docs/cmd/zh_CN/kusion_version.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
## kusion version

Print the kusion version info
打印当前 Kusion 的版本信息

### Synopsis

Print the kusion version information for the current context.
打印当前 Kusion 的版本信息

```
kusion version [flags]
Expand All @@ -13,21 +13,19 @@ kusion version [flags]
### Examples

```
# Print the kusion version
# 打印 Kusion 版本
kusion version
```

### Options

```
-h, --help help for version
-j, --json 将版本信息打印成 JSON 格式
-s, --short 将版本信息打印成短版本字符串
-y, --yaml 将版本信息打印成 YAML 格式
-h, --help help for version
-o, --output string 输出格式。当前只支持 json 格式的输出
```

### SEE ALSO

* [kusion](kusion.md) - Kusion manages the Kubernetes cluster by code

###### Auto generated by spf13/cobra on 11-Jul-2023
###### Auto generated by spf13/cobra on 13-Jul-2023
28 changes: 9 additions & 19 deletions pkg/cmd/version/options.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,34 @@
package version

import (
"errors"
"fmt"
"strings"

"kusionstack.io/kusion/pkg/version"
)

const jsonOutput = "json"

type VersionOptions struct {
ExportJSON bool
ExportYAML bool
Short bool
Output string
}

func NewVersionOptions() *VersionOptions {
return &VersionOptions{}
}

func (o *VersionOptions) Complete() {
if !(o.ExportYAML || o.ExportJSON || o.Short) {
o.ExportYAML = true
}
}

func (o *VersionOptions) Validate() error {
if (o.ExportJSON && o.ExportYAML) || (o.ExportJSON && o.Short) || (o.ExportYAML && o.Short) {
return fmt.Errorf("invalid options")
if o.Output != "" && o.Output != jsonOutput {
return errors.New("invalid output type, output must be 'json'")
}

return nil
}

func (o *VersionOptions) Run() {
switch {
case o.ExportJSON:
if strings.ToLower(o.Output) == jsonOutput {
fmt.Println(version.JSON())
case o.ExportYAML:
fmt.Println(version.YAML())
case o.Short:
fmt.Println(version.ShortString())
default:
} else {
fmt.Println(version.String())
}
}
27 changes: 9 additions & 18 deletions pkg/cmd/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,31 @@ import (
"kusionstack.io/kusion/pkg/util/i18n"
)

var (
versionShort = i18n.T(`Print the kusion version info`)

versionLong = i18n.T(`
Print the kusion version information for the current context.`)

versionExample = i18n.T(`
# Print the kusion version
func NewCmdVersion() *cobra.Command {
var (
versionShort = i18n.T(`Print the Kusion version information for the current context`)
versionExample = i18n.T(`
# Print the Kusion version
kusion version`)
)
)

func NewCmdVersion() *cobra.Command {
o := NewVersionOptions()

cmd := &cobra.Command{
Use: "version",
Short: versionShort,
Long: templates.LongDesc(versionLong),
Long: templates.LongDesc(versionShort),
Example: templates.Examples(versionExample),
RunE: func(cmd *cobra.Command, args []string) (err error) {
defer util.RecoverErr(&err)
o.Complete()
util.CheckErr(o.Validate())
o.Run()
return
},
}

cmd.Flags().BoolVarP(&o.ExportJSON, "json", "j", false,
i18n.T("Print version info as JSON"))
cmd.Flags().BoolVarP(&o.ExportYAML, "yaml", "y", false,
i18n.T("Print version info as YAML"))
cmd.Flags().BoolVarP(&o.Short, "short", "s", false,
i18n.T("Print version info as versionShort string"))
cmd.Flags().StringVarP(&o.Output, "output", "o", "",
i18n.T("Output format. Only json format is supported for now"))

return cmd
}
20 changes: 2 additions & 18 deletions pkg/cmd/version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,9 @@ func TestNewCmdVersion(t *testing.T) {
_, err := version.NewInfo()
assert.Nil(t, err)

t.Run("ExportJSON", func(t *testing.T) {
t.Run("json", func(t *testing.T) {
cmd := NewCmdVersion()
err := cmd.Flags().Set("json", "true")
assert.Nil(t, err)
err = cmd.Execute()
assert.Nil(t, err)
})

t.Run("ExportYaml", func(t *testing.T) {
cmd := NewCmdVersion()
err := cmd.Flags().Set("yaml", "true")
assert.Nil(t, err)
err = cmd.Execute()
assert.Nil(t, err)
})

t.Run("ShortString", func(t *testing.T) {
cmd := NewCmdVersion()
err := cmd.Flags().Set("short", "true")
err := cmd.Flags().Set("output", "json")
assert.Nil(t, err)
err = cmd.Execute()
assert.Nil(t, err)
Expand Down
Binary file modified pkg/util/i18n/translations/kusion/en_US/LC_MESSAGES/kusion.mo
Binary file not shown.
Loading

0 comments on commit 1afdbeb

Please sign in to comment.