Skip to content

Commit

Permalink
feature(cli): version display improvements (#2505)
Browse files Browse the repository at this point in the history
  • Loading branch information
xoscar authored and schoren committed Jun 5, 2023
1 parent 6ac60de commit 7d0a4c0
Show file tree
Hide file tree
Showing 27 changed files with 554 additions and 123 deletions.
17 changes: 17 additions & 0 deletions api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1212,3 +1212,20 @@ paths:
description: "environment not found"
500:
description: "problem deleting an environment"

/version:
get:
tags:
- api
summary: "Get the version of the API"
description: "Get the version of the API"
operationId: getVersion
responses:
200:
description: successful operation
content:
application/json:
schema:
$ref: "./version.yaml#/components/schemas/Version"
500:
description: "problem getting the version of the API"
9 changes: 9 additions & 0 deletions api/version.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
openapi: 3.0.0
components:
schemas:
Version:
type: object
properties:
version:
type: string
example: 1.0.0
40 changes: 40 additions & 0 deletions cli/actions/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,48 @@ package actions

import (
"context"

"github.com/kubeshop/tracetest/cli/config"
"github.com/kubeshop/tracetest/cli/openapi"
"go.uber.org/zap"
)

type Action[T any] interface {
Run(ctx context.Context, args T) error
}

type actionAgs struct {
config config.Config
logger *zap.Logger
client *openapi.APIClient
}

type ActionArgsOption = func(args *actionAgs)

func ActionWithClient(client *openapi.APIClient) ActionArgsOption {
return func(args *actionAgs) {
args.client = client
}
}

func ActionWithLogger(logger *zap.Logger) ActionArgsOption {
return func(args *actionAgs) {
args.logger = logger
}
}

func ActionWithConfig(config config.Config) ActionArgsOption {
return func(args *actionAgs) {
args.config = config
}
}

func NewActionArgs(options ...ActionArgsOption) actionAgs {
args := actionAgs{}

for _, option := range options {
option(&args)
}

return args
}
3 changes: 2 additions & 1 deletion cli/actions/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package actions

import (
"errors"
"fmt"

"github.com/kubeshop/tracetest/cli/config"
"github.com/kubeshop/tracetest/cli/formatters"
Expand Down Expand Up @@ -48,7 +49,7 @@ func (r ResourceRegistry) Get(name string) (resourceActions, error) {
actions, found := r[name]

if !found {
return resourceActions{}, ErrResourceNotRegistered
return resourceActions{}, fmt.Errorf("resource not found: %s", name)
}

return actions, nil
Expand Down
69 changes: 69 additions & 0 deletions cli/actions/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package actions

import (
"context"
"fmt"

"github.com/kubeshop/tracetest/cli/config"
)

type VersionConfig struct{}

type versionAction struct {
actionAgs
}

var _ Action[VersionConfig] = &versionAction{}

func NewGetServerVersionAction(options ...ActionArgsOption) versionAction {
args := NewActionArgs(options...)
return versionAction{actionAgs: args}
}

func (a versionAction) Run(ctx context.Context, args VersionConfig) error {
versionText := a.GetVersionText(ctx)

fmt.Println(versionText)
return nil
}

func (a versionAction) GetVersionText(ctx context.Context) string {
result := fmt.Sprintf(`CLI: %s`, config.Version)

if a.config.IsEmpty() {
return result + `
Server: Not Configured`
}

version, err := a.GetServerVersion(ctx)
if err != nil {
return result + fmt.Sprintf(`
Server: Failed to get the server version - %s`, err.Error())
}

isVersionMatch := version == config.Version
if isVersionMatch {
version += `
✔️ Version match`
} else {
version += `
✖️ Version mismatch`
}

return result + fmt.Sprintf(`
Server: %s`, version)
}

func (a versionAction) GetServerVersion(ctx context.Context) (string, error) {
if a.config.IsEmpty() {
return "", fmt.Errorf("not Configured")
}

req := a.client.ApiApi.GetVersion(ctx)
version, _, err := req.Execute()
if err != nil {
return "", err
}

return *version.Version, nil
}
24 changes: 7 additions & 17 deletions cli/cmd/apply_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ package cmd
import (
"context"
"fmt"
"os"

"github.com/kubeshop/tracetest/cli/actions"
"github.com/kubeshop/tracetest/cli/analytics"
"github.com/kubeshop/tracetest/cli/formatters"
"github.com/spf13/cobra"
"go.uber.org/zap"
)

var definitionFile string
Expand All @@ -21,11 +19,9 @@ var applyCmd = &cobra.Command{
Long: "Apply (create/update) resources to your Tracetest server",
PreRun: setupCommand(),
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
Run: WithResultHandler(func(cmd *cobra.Command, args []string) (string, error) {
if definitionFile == "" {
cliLogger.Error("file with definition must be specified")
os.Exit(1)
return
return "", fmt.Errorf("file with definition must be specified")
}

resourceType := args[0]
Expand All @@ -38,9 +34,7 @@ var applyCmd = &cobra.Command{
resourceActions, err := resourceRegistry.Get(resourceType)

if err != nil {
cliLogger.Error(fmt.Sprintf("failed to get resource instance for type: %s", resourceType), zap.Error(err))
os.Exit(1)
return
return "", err
}

applyArgs := actions.ApplyArgs{
Expand All @@ -49,23 +43,19 @@ var applyCmd = &cobra.Command{

resource, _, err := resourceActions.Apply(ctx, applyArgs)
if err != nil {
cliLogger.Error(fmt.Sprintf("failed to apply definition for type: %s", resourceType), zap.Error(err))
os.Exit(1)
return
return "", err
}

resourceFormatter := resourceActions.Formatter()
formatter := formatters.BuildFormatter(output, formatters.YAML, resourceFormatter)

result, err := formatter.Format(resource)
if err != nil {
cliLogger.Error("failed to format resource", zap.Error(err))
os.Exit(1)
return
return "", err
}

fmt.Println(result)
},
return result, nil
}),
PostRun: teardownCommand,
}

Expand Down
17 changes: 17 additions & 0 deletions cli/cmd/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"context"
"fmt"
"os"

Expand All @@ -17,6 +18,7 @@ import (
var cliConfig config.Config
var cliLogger *zap.Logger
var resourceRegistry = actions.NewResourceRegistry()
var versionText string

type setupConfig struct {
shouldValidateConfig bool
Expand All @@ -43,6 +45,7 @@ func setupCommand(options ...setupOption) func(cmd *cobra.Command, args []string
setupLogger(cmd, args)
loadConfig(cmd, args)
overrideConfig()
setupVersion()

baseOptions := []actions.ResourceArgsOption{actions.WithLogger(cliLogger), actions.WithConfig(cliConfig)}

Expand Down Expand Up @@ -168,3 +171,17 @@ func teardownCommand(cmd *cobra.Command, args []string) {
cliLogger.Sync()
analytics.Close()
}

func setupVersion() {
ctx := context.Background()
options := []actions.ActionArgsOption{
actions.ActionWithClient(utils.GetAPIClient(cliConfig)),
actions.ActionWithConfig(cliConfig),
actions.ActionWithLogger(cliLogger),
}

action := actions.NewGetServerVersionAction(options...)
version := action.GetVersionText(ctx)

versionText = version
}
10 changes: 3 additions & 7 deletions cli/cmd/configure_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/kubeshop/tracetest/cli/analytics"
"github.com/kubeshop/tracetest/cli/utils"
"github.com/spf13/cobra"
"go.uber.org/zap"
)

var analyticsEnabled bool
Expand All @@ -20,7 +19,7 @@ var configureCmd = &cobra.Command{
Short: "Configure your tracetest CLI",
Long: "Configure your tracetest CLI",
PreRun: setupLogger,
Run: func(cmd *cobra.Command, args []string) {
Run: WithResultHandler(func(cmd *cobra.Command, _ []string) (string, error) {
analytics.Track("Configure", "cmd", map[string]string{})

ctx := context.Background()
Expand All @@ -41,11 +40,8 @@ var configureCmd = &cobra.Command{
}

err := action.Run(ctx, actionConfig)
if err != nil {
cliLogger.Error("could not get tests", zap.Error(err))
return
}
},
return "", err
}),
PostRun: teardownCommand,
}

Expand Down
16 changes: 5 additions & 11 deletions cli/cmd/dashboard_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package cmd

import (
"fmt"
"os"

"github.com/kubeshop/tracetest/cli/analytics"
"github.com/kubeshop/tracetest/cli/utils"
"github.com/spf13/cobra"
"go.uber.org/zap"
)

var dashboardCmd = &cobra.Command{
Expand All @@ -16,24 +14,20 @@ var dashboardCmd = &cobra.Command{
Short: "Opens the Tracetest Dashboard URL",
Long: "Opens the Tracetest Dashboard URL",
PreRun: setupCommand(),
Run: func(_ *cobra.Command, _ []string) {
Run: WithResultHandler(func(_ *cobra.Command, _ []string) (string, error) {
analytics.Track("Dashboard", "cmd", map[string]string{})

if cliConfig.IsEmpty() {
cliLogger.Error("Missing Tracetest endpoint configuration")
os.Exit(1)
return
return "", fmt.Errorf("missing Tracetest endpoint configuration")
}

err := utils.OpenBrowser(cliConfig.URL())
if err != nil {
cliLogger.Error(fmt.Sprintf("failed to open the dashboard url: %s", cliConfig.URL()), zap.Error(err))
os.Exit(1)
return
return "", fmt.Errorf("failed to open the dashboard url: %s", cliConfig.URL())
}

fmt.Println(fmt.Sprintf("Opening \"%s\" in the default browser", cliConfig.URL()))
},
return fmt.Sprintf("Opening \"%s\" in the default browser", cliConfig.URL()), nil
}),
PostRun: teardownCommand,
}

Expand Down
Loading

0 comments on commit 7d0a4c0

Please sign in to comment.