Skip to content

Commit

Permalink
feat(cli): update export command with new resourcemanager client (#2838)
Browse files Browse the repository at this point in the history
  • Loading branch information
schoren authored Jun 29, 2023
1 parent 4afc361 commit 8ea0ffe
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 37 deletions.
4 changes: 2 additions & 2 deletions cli/cmd/datastore_legacy_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ var dataStoreExportCmd = &cobra.Command{
PreRun: setupCommand(),
Run: func(cmd *cobra.Command, args []string) {
// call new export command
exportResourceID = "current"
exportResourceFile = exportOutputFile
exportParams.ResourceID = "current"
exportParams.OutputFile = exportOutputFile
exportCmd.Run(exportCmd, []string{"datastore"})
},
PostRun: teardownCommand,
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/delete_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func init() {
return "", err
}

resultFormat, err := resourcemanager.Formats.Get(output, "yaml")
resultFormat, err := resourcemanager.Formats.GetWithFallback(output, "yaml")
if err != nil {
return "", err
}
Expand Down
72 changes: 42 additions & 30 deletions cli/cmd/export_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,56 @@ package cmd
import (
"context"
"fmt"
"strings"
"os"

"github.com/kubeshop/tracetest/cli/parameters"
"github.com/kubeshop/tracetest/cli/pkg/resourcemanager"
"github.com/spf13/cobra"
)

var (
exportResourceID string
exportResourceFile string
exportParams = &parameters.ExportParams{}
exportCmd *cobra.Command
)

var exportCmd = &cobra.Command{
GroupID: cmdGroupResources.ID,
Use: fmt.Sprintf("export %s", strings.Join(parameters.ValidResources, "|")),
Long: "Export a resource from your Tracetest server",
Short: "Export resource",
PreRun: setupCommand(),
Run: WithResultHandler(func(_ *cobra.Command, args []string) (string, error) {
resourceType := resourceParams.ResourceName
ctx := context.Background()

resourceActions, err := resourceRegistry.Get(resourceType)
if err != nil {
return "", err
}

err = resourceActions.Export(ctx, exportResourceID, exportResourceFile)
if err != nil {
return "", err
}

return fmt.Sprintf("✔ Definition exported successfully for resource type: %s", resourceType), nil
}),
PostRun: teardownCommand,
}

func init() {
exportCmd.Flags().StringVar(&exportResourceID, "id", "", "id of the resource to export")
exportCmd.Flags().StringVarP(&exportResourceFile, "file", "f", "resource.yaml", "file path with name where to export the resource")
exportCmd = &cobra.Command{
GroupID: cmdGroupResources.ID,
Use: "export " + resourceList(),
Long: "Export a resource from your Tracetest server",
Short: "Export resource",
PreRun: setupCommand(),
Run: WithResourceMiddleware(func(_ *cobra.Command, args []string) (string, error) {
resourceType := resourceParams.ResourceName
ctx := context.Background()

resourceClient, err := resources.Get(resourceType)
if err != nil {
return "", err
}

// export is ALWAYS yaml, so we can hardcode it here
resultFormat, err := resourcemanager.Formats.Get("yaml")
if err != nil {
return "", err
}

result, err := resourceClient.Get(ctx, exportParams.ResourceID, resultFormat)
if err != nil {
return "", err
}

err = os.WriteFile(exportParams.OutputFile, []byte(result), 0644)
if err != nil {
return "", fmt.Errorf("could not write file: %w", err)
}

return fmt.Sprintf("✔ Definition exported successfully for resource type: %s", resourceType), nil
}, exportParams),
PostRun: teardownCommand,
}

exportCmd.Flags().StringVar(&exportParams.ResourceID, "id", "", "id of the resource to export")
exportCmd.Flags().StringVarP(&exportParams.OutputFile, "file", "f", "resource.yaml", "file path with name where to export the resource")
rootCmd.AddCommand(exportCmd)
}
2 changes: 1 addition & 1 deletion cli/cmd/get_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func init() {
return "", err
}

resultFormat, err := resourcemanager.Formats.Get(output, "yaml")
resultFormat, err := resourcemanager.Formats.GetWithFallback(output, "yaml")
if err != nil {
return "", err
}
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/list_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func init() {
return "", err
}

resultFormat, err := resourcemanager.Formats.Get(output, "pretty")
resultFormat, err := resourcemanager.Formats.GetWithFallback(output, "pretty")
if err != nil {
return "", err
}
Expand Down
18 changes: 18 additions & 0 deletions cli/parameters/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@ func (p *ResourceIdParams) Validate(cmd *cobra.Command, args []string) []error {
return errors
}

type ExportParams struct {
ResourceIdParams
OutputFile string
}

func (p *ExportParams) Validate(cmd *cobra.Command, args []string) []error {
errors := p.ResourceIdParams.Validate(cmd, args)

if p.OutputFile == "" {
errors = append(errors, paramError{
Parameter: "file",
Message: "output file must be provided",
})
}

return errors
}

type ApplyParams struct {
DefinitionFile string
}
Expand Down
8 changes: 6 additions & 2 deletions cli/pkg/resourcemanager/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@ type Format interface {

type formatRegistry []Format

func (f formatRegistry) Get(format, fallback string) (Format, error) {
func (f formatRegistry) GetWithFallback(format, fallback string) (Format, error) {
if format == "" && fallback == "" {
return nil, fmt.Errorf("format and fallback cannot be empty at the same time")
}

if format == "" {
return f.Get(fallback, "")
return f.Get(fallback)
}

return f.Get(format)
}

func (f formatRegistry) Get(format string) (Format, error) {
for _, fr := range f {
if fr.String() == format {
return fr, nil
Expand Down

0 comments on commit 8ea0ffe

Please sign in to comment.