Skip to content

Commit

Permalink
feat(cli): display CDK version
Browse files Browse the repository at this point in the history
Signed-off-by: Salim Afiune Maya <[email protected]>
  • Loading branch information
afiune committed Jul 6, 2022
1 parent 12eee01 commit d21bbef
Showing 1 changed file with 42 additions and 8 deletions.
50 changes: 42 additions & 8 deletions cli/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ import (
"fmt"
"os"
"path"
"strings"
"time"

"github.com/pkg/errors"
"github.com/spf13/cobra"

"github.com/lacework/go-sdk/internal/cache"
"github.com/lacework/go-sdk/internal/file"
"github.com/lacework/go-sdk/lwcomponent"
"github.com/lacework/go-sdk/lwupdater"
)

Expand Down Expand Up @@ -59,18 +61,43 @@ versions available for update.
Set the environment variable 'LW_UPDATES_DISABLE=1' to avoid checking for updates.`,
Args: cobra.NoArgs,
Run: func(_ *cobra.Command, _ []string) {
componentVerionsOutput := &strings.Builder{}
if cli.LwComponents != nil {
for _, component := range cli.LwComponents.Components {
if component.IsInstalled() {
v, err := component.CurrentVersion()
if err == nil {
componentVerionsOutput.WriteString(
fmt.Sprintf(" > %s v%s\n", component.Name, v.String()),
)
continue
}
cli.Log.Errorw("unable to determine component version",
"error", err.Error(), "component", component.Name,
)
}
}
}

if cli.JSONOutput() {
errcheckEXIT(
cli.OutputJSONString(
fmt.Sprintf(
`{"version":"v%s","git_sha":"%s","build_time":"%s"}`,
Version, GitSHA, BuildTime,
),
),
)
vJSON := versionJSON{
Version: fmt.Sprintf("v%s", Version),
GitSHA: GitSHA,
BuildTime: BuildTime,
}

if componentVerionsOutput.String() != "" {
vJSON.CDK = cli.LwComponents
}

errcheckEXIT(cli.OutputJSON(vJSON))
return
}

cli.OutputHuman("lacework v%s (sha:%s) (time:%s)\n", Version, GitSHA, BuildTime)
if componentVerionsOutput.String() != "" {
cli.OutputHuman("\nComponents:\n\n%s", componentVerionsOutput.String())
}

// check the latest version of the cli
if _, err := versionCheck(); err != nil {
Expand Down Expand Up @@ -184,3 +211,10 @@ func dailyVersionCheck() error {

return nil
}

type versionJSON struct {
Version string `json:"version"`
GitSHA string `json:"git_sha"`
BuildTime string `json:"build_time"`
CDK *lwcomponent.State `json:"cdk,omitempty"`
}

0 comments on commit d21bbef

Please sign in to comment.