Skip to content

Commit

Permalink
feat(cli): make resourcename optional for run command (#3242)
Browse files Browse the repository at this point in the history
  • Loading branch information
schoren authored Oct 11, 2023
1 parent 50fe208 commit 6c6cec5
Show file tree
Hide file tree
Showing 6 changed files with 309 additions and 111 deletions.
12 changes: 12 additions & 0 deletions cli/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var (
type setupConfig struct {
shouldValidateConfig bool
shouldValidateVersionMismatch bool
optionalResourceName bool
}

type setupOption func(*setupConfig)
Expand All @@ -41,10 +42,17 @@ func SkipVersionMismatchCheck() setupOption {
}
}

func WithOptionalResourceName() setupOption {
return func(sc *setupConfig) {
sc.optionalResourceName = true
}
}

func setupCommand(options ...setupOption) func(cmd *cobra.Command, args []string) {
config := setupConfig{
shouldValidateConfig: true,
shouldValidateVersionMismatch: true,
optionalResourceName: false,
}
for _, option := range options {
option(&config)
Expand All @@ -67,6 +75,10 @@ func setupCommand(options ...setupOption) func(cmd *cobra.Command, args []string
validateVersionMismatch()
}

if config.optionalResourceName {
resourceParams.optional = true
}

analytics.Init()
}
}
Expand Down
11 changes: 11 additions & 0 deletions cli/cmd/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,20 @@ func WithResourceMiddleware(runFn RunFn, params ...Validator) CobraRunFn {

type resourceParameters struct {
ResourceName string
optional bool
}

func (p *resourceParameters) Validate(cmd *cobra.Command, args []string) []error {
// if the resourceName is optional, skip validation.
if p.optional {
// we still need to bind it to the struct in case the user provided a value.
// we need to check the args has at least one element to avoid a panic.
if len(args) > 0 {
p.ResourceName = args[0]
}
return nil
}

if len(args) == 0 || args[0] == "" {
return []error{
paramError{
Expand Down
27 changes: 25 additions & 2 deletions cli/cmd/resource_run_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/kubeshop/tracetest/cli/config"
"github.com/kubeshop/tracetest/cli/openapi"
"github.com/kubeshop/tracetest/cli/pkg/fileutil"
"github.com/kubeshop/tracetest/cli/runner"
"github.com/spf13/cobra"
)
Expand All @@ -22,10 +23,13 @@ func init() {
Use: "run " + runnableResourceList(),
Short: "run resources",
Long: "run resources",
PreRun: setupCommand(),
PreRun: setupCommand(WithOptionalResourceName()),
Run: WithResourceMiddleware(func(_ *cobra.Command, args []string) (string, error) {
resourceType := resourceParams.ResourceName
ctx := context.Background()
resourceType, err := getResourceType(runParams, resourceParams)
if err != nil {
return "", err
}

r, err := runnerRegistry.Get(resourceType)
if err != nil {
Expand Down Expand Up @@ -80,6 +84,25 @@ func init() {
rootCmd.AddCommand(runCmd)
}

func getResourceType(runParams *runParameters, resourceParams *resourceParameters) (string, error) {
if resourceParams.ResourceName != "" {
return resourceParams.ResourceName, nil
}

if runParams.DefinitionFile != "" {
filePath := runParams.DefinitionFile
f, err := fileutil.Read(filePath)
if err != nil {
return "", fmt.Errorf("cannot read file %s: %w", filePath, err)
}

return strings.ToLower(f.Type()), nil
}

return "", fmt.Errorf("resourceName is empty and no definition file provided")

}

func validRequiredGatesMsg() string {
opts := make([]string, 0, len(openapi.AllowedSupportedGatesEnumValues))
for _, v := range openapi.AllowedSupportedGatesEnumValues {
Expand Down
87 changes: 58 additions & 29 deletions cli/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ require (
github.com/golang-jwt/jwt/v4 v4.5.0
github.com/goware/urlx v0.3.2
github.com/kubeshop/tracetest/agent v0.0.0-20230907210810-84198fc9f4ef
github.com/kubeshop/tracetest/server v0.0.0-20230809150857-6314696222d3
github.com/kubeshop/tracetest/server v0.0.0-20231010015728-ab7381aa0030
github.com/pterm/pterm v0.12.55
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.15.0
github.com/stretchr/testify v1.8.4
go.uber.org/zap v1.23.0
go.uber.org/zap v1.26.0
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1
golang.org/x/text v0.9.0
golang.org/x/text v0.13.0
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1
)
Expand All @@ -38,16 +38,21 @@ require (
github.com/Azure/azure-sdk-for-go/sdk/monitor/azquery v1.1.0 // indirect
github.com/AzureAD/microsoft-authentication-library-for-go v1.0.0 // indirect
github.com/Code-Hex/go-generics-cache v1.3.1 // indirect
github.com/IBM/sarama v1.40.1 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/alecthomas/participle/v2 v2.0.0-alpha8 // indirect
github.com/alitto/pond v1.8.3 // indirect
github.com/aws/aws-sdk-go v1.44.196 // indirect
github.com/brianvoe/gofakeit/v6 v6.17.0 // indirect
github.com/containerd/console v1.0.3 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/distribution/distribution/v3 v3.0.0-20220907155224-78b9c98c5c31 // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/eapache/go-resiliency v1.3.0 // indirect
github.com/eapache/go-xerial-snappy v0.0.0-20230111030713-bf00bc1b83b6 // indirect
github.com/eapache/queue v1.1.0 // indirect
github.com/elastic/elastic-transport-go/v8 v8.0.0-20211216131617-bbee439d559c // indirect
github.com/elastic/go-elasticsearch/v8 v8.4.0-alpha.1.0.20221227164349-c40d762a40ad // indirect
github.com/fatih/color v1.13.0 // indirect
Expand All @@ -59,27 +64,34 @@ require (
github.com/go-logr/stdr v1.2.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gookit/color v1.5.2 // indirect
github.com/gorilla/handlers v1.5.1 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-uuid v1.0.3 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.4.2 // indirect
github.com/jackc/puddle/v2 v2.2.0 // indirect
github.com/jcmturner/aescts/v2 v2.0.0 // indirect
github.com/jcmturner/dnsutils/v2 v2.0.0 // indirect
github.com/jcmturner/gofork v1.7.6 // indirect
github.com/jcmturner/gokrb5/v8 v8.4.3 // indirect
github.com/jcmturner/rpc/v2 v2.0.3 // indirect
github.com/jdvr/go-again v1.0.0 // indirect
github.com/jhump/protoreflect v1.12.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.15.10 // indirect
github.com/knadh/koanf v1.4.3 // indirect
github.com/klauspost/compress v1.17.0 // indirect
github.com/knadh/koanf v1.5.0 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/lithammer/fuzzysearch v1.1.5 // indirect
github.com/magiconair/properties v1.8.7 // indirect
Expand All @@ -88,19 +100,22 @@ require (
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/mattn/go-shellwords v1.0.12 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/mapstructure v1.5.1-0.20220423185008-bf980b35cac4 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/mostynb/go-grpc-compression v1.1.17 // indirect
github.com/mostynb/go-grpc-compression v1.2.1 // indirect
github.com/ohler55/ojg v1.14.4 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opensearch-project/opensearch-go v1.1.0 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/pierrec/lz4/v4 v4.1.17 // indirect
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/prometheus v1.8.2-0.20211217191541-41f1a8125e66 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/segmentio/analytics-go/v3 v3.2.1 // indirect
Expand All @@ -116,29 +131,43 @@ require (
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/collector v0.60.0 // indirect
go.opentelemetry.io/collector/pdata v0.60.0 // indirect
go.opentelemetry.io/collector/semconv v0.71.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.35.0 // indirect
go.opentelemetry.io/collector v0.80.0 // indirect
go.opentelemetry.io/collector/component v0.80.0 // indirect
go.opentelemetry.io/collector/config/configauth v0.80.0 // indirect
go.opentelemetry.io/collector/config/configcompression v0.80.0 // indirect
go.opentelemetry.io/collector/config/configgrpc v0.80.0 // indirect
go.opentelemetry.io/collector/config/confignet v0.80.0 // indirect
go.opentelemetry.io/collector/config/configopaque v0.80.0 // indirect
go.opentelemetry.io/collector/config/configtelemetry v0.80.0 // indirect
go.opentelemetry.io/collector/config/configtls v0.80.0 // indirect
go.opentelemetry.io/collector/config/internal v0.80.0 // indirect
go.opentelemetry.io/collector/confmap v0.80.0 // indirect
go.opentelemetry.io/collector/extension v0.80.0 // indirect
go.opentelemetry.io/collector/extension/auth v0.80.0 // indirect
go.opentelemetry.io/collector/featuregate v1.0.0-rcv0015 // indirect
go.opentelemetry.io/collector/pdata v1.0.0-rcv0015 // indirect
go.opentelemetry.io/collector/semconv v0.80.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.44.0 // indirect
go.opentelemetry.io/contrib/propagators/aws v1.5.0 // indirect
go.opentelemetry.io/contrib/propagators/b3 v1.9.0 // indirect
go.opentelemetry.io/contrib/propagators/b3 v1.17.0 // indirect
go.opentelemetry.io/contrib/propagators/jaeger v1.5.0 // indirect
go.opentelemetry.io/contrib/propagators/ot v1.5.0 // indirect
go.opentelemetry.io/otel v1.10.0 // indirect
go.opentelemetry.io/otel/metric v0.32.0 // indirect
go.opentelemetry.io/otel/sdk v1.10.0 // indirect
go.opentelemetry.io/otel/trace v1.10.0 // indirect
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/term v0.8.0 // indirect
go.opentelemetry.io/otel v1.19.0 // indirect
go.opentelemetry.io/otel/metric v1.19.0 // indirect
go.opentelemetry.io/otel/sdk v1.19.0 // indirect
go.opentelemetry.io/otel/trace v1.19.0 // indirect
go.opentelemetry.io/proto/otlp v1.0.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.13.0 // indirect
golang.org/x/net v0.15.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/term v0.12.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect
google.golang.org/grpc v1.53.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
google.golang.org/genproto v0.0.0-20230711160842-782d3b101e98 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230711160842-782d3b101e98 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 // indirect
google.golang.org/grpc v1.58.2 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
)
Loading

0 comments on commit 6c6cec5

Please sign in to comment.