Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(cli): move version command out of root #1556

Merged
merged 1 commit into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cmd/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"github.com/akuity/kargo/internal/cli/cmd/promote"
"github.com/akuity/kargo/internal/cli/cmd/refresh"
"github.com/akuity/kargo/internal/cli/cmd/update"
"github.com/akuity/kargo/internal/cli/cmd/version"
clicfg "github.com/akuity/kargo/internal/cli/config"
"github.com/akuity/kargo/internal/cli/option"
)
Expand Down Expand Up @@ -110,7 +111,7 @@
cmd.AddCommand(update.NewCommand(cfg, opt))
cmd.AddCommand(dashboard.NewCommand(cfg))
cmd.AddCommand(promote.NewCommand(cfg, opt))
cmd.AddCommand(newVersionCommand(cfg, opt))
cmd.AddCommand(version.NewCommand(cfg, opt))

Check warning on line 114 in cmd/cli/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/cli/root.go#L114

Added line #L114 was not covered by tests
cmd.AddCommand(
cobracompletefig.CreateCompletionSpecCommand(
cobracompletefig.Opts{
Expand Down
112 changes: 0 additions & 112 deletions cmd/cli/version.go

This file was deleted.

144 changes: 144 additions & 0 deletions internal/cli/cmd/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package version

import (
"context"
"encoding/json"
"fmt"

"connectrpc.com/connect"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"google.golang.org/protobuf/encoding/protojson"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/utils/ptr"

kargoapi "github.com/akuity/kargo/api/v1alpha1"
typesv1alpha1 "github.com/akuity/kargo/internal/api/types/v1alpha1"
"github.com/akuity/kargo/internal/cli/client"
"github.com/akuity/kargo/internal/cli/config"
"github.com/akuity/kargo/internal/cli/option"
versionpkg "github.com/akuity/kargo/internal/version"
svcv1alpha1 "github.com/akuity/kargo/pkg/api/service/v1alpha1"
)

type versionOptions struct {
*option.Option
Config config.CLIConfig

ClientOnly bool
}

func NewCommand(cfg config.CLIConfig, opt *option.Option) *cobra.Command {
cmdOpts := &versionOptions{
Option: opt,
Config: cfg,

Check warning on line 35 in internal/cli/cmd/version/version.go

View check run for this annotation

Codecov / codecov/patch

internal/cli/cmd/version/version.go#L32-L35

Added lines #L32 - L35 were not covered by tests
}

cmd := &cobra.Command{
Use: "version",
Short: "Show the client and server version information",
Args: option.NoArgs,
Example: `
# Print the client and server version information
kargo version

Check warning on line 44 in internal/cli/cmd/version/version.go

View check run for this annotation

Codecov / codecov/patch

internal/cli/cmd/version/version.go#L38-L44

Added lines #L38 - L44 were not covered by tests

# Print the client version information only
kargo version --client
`,
RunE: func(cmd *cobra.Command, args []string) error {
return cmdOpts.run(cmd.Context())
},

Check warning on line 51 in internal/cli/cmd/version/version.go

View check run for this annotation

Codecov / codecov/patch

internal/cli/cmd/version/version.go#L46-L51

Added lines #L46 - L51 were not covered by tests
}

// Register the option flags on the command.
cmdOpts.addFlags(cmd)

Check warning on line 55 in internal/cli/cmd/version/version.go

View check run for this annotation

Codecov / codecov/patch

internal/cli/cmd/version/version.go#L55

Added line #L55 was not covered by tests

return cmd

Check warning on line 57 in internal/cli/cmd/version/version.go

View check run for this annotation

Codecov / codecov/patch

internal/cli/cmd/version/version.go#L57

Added line #L57 was not covered by tests
}

// addFlags adds the flags for the version options to the provided command.
func (o *versionOptions) addFlags(cmd *cobra.Command) {
o.PrintFlags.AddFlags(cmd)

Check warning on line 62 in internal/cli/cmd/version/version.go

View check run for this annotation

Codecov / codecov/patch

internal/cli/cmd/version/version.go#L61-L62

Added lines #L61 - L62 were not covered by tests

option.InsecureTLS(cmd.PersistentFlags(), o.Option)
option.LocalServer(cmd.PersistentFlags(), o.Option)

Check warning on line 65 in internal/cli/cmd/version/version.go

View check run for this annotation

Codecov / codecov/patch

internal/cli/cmd/version/version.go#L64-L65

Added lines #L64 - L65 were not covered by tests

cmd.Flags().BoolVar(&o.ClientOnly, "client", o.ClientOnly, "If true, shows client version only (no server required)")

Check warning on line 67 in internal/cli/cmd/version/version.go

View check run for this annotation

Codecov / codecov/patch

internal/cli/cmd/version/version.go#L67

Added line #L67 was not covered by tests
}

// run prints the client and server version information.
func (o *versionOptions) run(ctx context.Context) error {
printToStdout := ptr.Deref(o.PrintFlags.OutputFormat, "") == ""

Check warning on line 72 in internal/cli/cmd/version/version.go

View check run for this annotation

Codecov / codecov/patch

internal/cli/cmd/version/version.go#L71-L72

Added lines #L71 - L72 were not covered by tests

cliVersion := typesv1alpha1.ToVersionProto(versionpkg.GetVersion())
if printToStdout {
fmt.Println("Client Version:", cliVersion.GetVersion())

Check warning on line 76 in internal/cli/cmd/version/version.go

View check run for this annotation

Codecov / codecov/patch

internal/cli/cmd/version/version.go#L74-L76

Added lines #L74 - L76 were not covered by tests
}

var serverVersion *svcv1alpha1.VersionInfo
var serverErr error
if !o.UseLocalServer && !o.ClientOnly {
serverVersion, serverErr = getServerVersion(ctx, o.Config, o.Option)

Check warning on line 82 in internal/cli/cmd/version/version.go

View check run for this annotation

Codecov / codecov/patch

internal/cli/cmd/version/version.go#L79-L82

Added lines #L79 - L82 were not covered by tests
}

if printToStdout {
if serverVersion != nil {
fmt.Println("Server Version:", serverVersion.GetVersion())

Check warning on line 87 in internal/cli/cmd/version/version.go

View check run for this annotation

Codecov / codecov/patch

internal/cli/cmd/version/version.go#L85-L87

Added lines #L85 - L87 were not covered by tests
}
return serverErr

Check warning on line 89 in internal/cli/cmd/version/version.go

View check run for this annotation

Codecov / codecov/patch

internal/cli/cmd/version/version.go#L89

Added line #L89 was not covered by tests
}

printer, err := o.PrintFlags.ToPrinter()
if err != nil {
return errors.Wrap(err, "new printer")

Check warning on line 94 in internal/cli/cmd/version/version.go

View check run for this annotation

Codecov / codecov/patch

internal/cli/cmd/version/version.go#L92-L94

Added lines #L92 - L94 were not covered by tests
}
obj, err := componentVersionsToRuntimeObject(&svcv1alpha1.ComponentVersions{
Server: serverVersion,
Cli: cliVersion,
})
if err != nil {
return errors.Wrap(err, "map component versions to runtime object")

Check warning on line 101 in internal/cli/cmd/version/version.go

View check run for this annotation

Codecov / codecov/patch

internal/cli/cmd/version/version.go#L96-L101

Added lines #L96 - L101 were not covered by tests
}

if err := printer.PrintObj(obj, o.IOStreams.Out); err != nil {
return errors.Wrap(err, "printing object")

Check warning on line 105 in internal/cli/cmd/version/version.go

View check run for this annotation

Codecov / codecov/patch

internal/cli/cmd/version/version.go#L104-L105

Added lines #L104 - L105 were not covered by tests
}
return serverErr

Check warning on line 107 in internal/cli/cmd/version/version.go

View check run for this annotation

Codecov / codecov/patch

internal/cli/cmd/version/version.go#L107

Added line #L107 was not covered by tests
}

func getServerVersion(ctx context.Context, cfg config.CLIConfig, opt *option.Option) (*svcv1alpha1.VersionInfo, error) {
if cfg.APIAddress == "" || cfg.BearerToken == "" {
return nil, nil

Check warning on line 112 in internal/cli/cmd/version/version.go

View check run for this annotation

Codecov / codecov/patch

internal/cli/cmd/version/version.go#L110-L112

Added lines #L110 - L112 were not covered by tests
}

kargoSvcCli, err := client.GetClientFromConfig(ctx, cfg, opt)
if err != nil {
return nil, errors.Wrap(err, "get client from config")

Check warning on line 117 in internal/cli/cmd/version/version.go

View check run for this annotation

Codecov / codecov/patch

internal/cli/cmd/version/version.go#L115-L117

Added lines #L115 - L117 were not covered by tests
}
resp, err := kargoSvcCli.GetVersionInfo(
ctx,
connect.NewRequest(&svcv1alpha1.GetVersionInfoRequest{}),
)
if err != nil {
return nil, errors.Wrap(err, "get version info from server")

Check warning on line 124 in internal/cli/cmd/version/version.go

View check run for this annotation

Codecov / codecov/patch

internal/cli/cmd/version/version.go#L119-L124

Added lines #L119 - L124 were not covered by tests
}

return resp.Msg.GetVersionInfo(), nil

Check warning on line 127 in internal/cli/cmd/version/version.go

View check run for this annotation

Codecov / codecov/patch

internal/cli/cmd/version/version.go#L127

Added line #L127 was not covered by tests
}

func componentVersionsToRuntimeObject(v *svcv1alpha1.ComponentVersions) (runtime.Object, error) {
data, err := protojson.Marshal(v)
if err != nil {
return nil, errors.Wrap(err, "marshal component versions")

Check warning on line 133 in internal/cli/cmd/version/version.go

View check run for this annotation

Codecov / codecov/patch

internal/cli/cmd/version/version.go#L130-L133

Added lines #L130 - L133 were not covered by tests
}
var content map[string]any
if err := json.Unmarshal(data, &content); err != nil {
return nil, errors.Wrap(err, "unmarshal component versions")

Check warning on line 137 in internal/cli/cmd/version/version.go

View check run for this annotation

Codecov / codecov/patch

internal/cli/cmd/version/version.go#L135-L137

Added lines #L135 - L137 were not covered by tests
}
u := &unstructured.Unstructured{}
u.SetUnstructuredContent(content)
u.SetAPIVersion(kargoapi.GroupVersion.String())
u.SetKind("ComponentVersions")
return u, nil

Check warning on line 143 in internal/cli/cmd/version/version.go

View check run for this annotation

Codecov / codecov/patch

internal/cli/cmd/version/version.go#L139-L143

Added lines #L139 - L143 were not covered by tests
}
4 changes: 0 additions & 4 deletions internal/cli/option/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ func LocalServer(fs *pflag.FlagSet, opt *Option) {
fs.BoolVar(&opt.UseLocalServer, "local-server", false, "Use local server")
}

func ClientVersion(fs *pflag.FlagSet, opt *Option) {
fs.BoolVar(&opt.ClientVersionOnly, "client", false, "If true, shows client version only (no server required)")
}

// Project adds the ProjectFlag and ProjectShortFlag to the provided flag set.
func Project(fs *pflag.FlagSet, project *string, defaultProject, usage string) {
fs.StringVarP(project, ProjectFlag, ProjectShortFlag, defaultProject, usage)
Expand Down
2 changes: 0 additions & 2 deletions internal/cli/option/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ type Option struct {
LocalServerAddress string
UseLocalServer bool

ClientVersionOnly bool

Project string

IOStreams *genericiooptions.IOStreams
Expand Down
Loading