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

fix: make enclave identifier arg passable to service identifier completion provider #1107

Merged
merged 2 commits into from Aug 15, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -13,45 +13,46 @@ import (
)

const (
enclaveIdentifierArgKey = "enclave"
validShortenedUuidOrNameMatches = 1
uuidDelimiter = ", "
)

func NewServiceIdentifierArg(
argKey string,
serviceIdentifierArgKey string,
enclaveIdentifierArgKey string,
isOptional bool,
isGreedy bool,
) *args.ArgConfig {

validate := getValidationFunc(argKey, isGreedy)
validate := getValidationFunc(serviceIdentifierArgKey, isGreedy, enclaveIdentifierArgKey)

return &args.ArgConfig{
Key: argKey,
Key: serviceIdentifierArgKey,
IsOptional: isOptional,
DefaultValue: "",
IsGreedy: isGreedy,
ArgCompletionProvider: args.NewManualCompletionsProvider(getCompletionsOfActiveServices),
ArgCompletionProvider: args.NewManualCompletionsProvider(getCompletionsOfActiveServices(enclaveIdentifierArgKey)),
ValidationFunc: validate,
}
}

// TODO we added this constructor for allowing 'service logs' command to disable the validation for consuming logs from removed or stopped enclaves
// TODO after https://github.com/kurtosis-tech/kurtosis/issues/879 is done
func NewHistoricalServiceIdentifierArgWithValidationDisabled(
argKey string,
serviceIdentifierArgKey string,
enclaveIdentifierArgKey string,
isOptional bool,
isGreedy bool,
) *args.ArgConfig {

var noValidationFunc func(ctx context.Context, flags *flags.ParsedFlags, args *args.ParsedArgs) error

return &args.ArgConfig{
Key: argKey,
Key: serviceIdentifierArgKey,
IsOptional: isOptional,
DefaultValue: "",
IsGreedy: isGreedy,
ArgCompletionProvider: args.NewManualCompletionsProvider(getCompletionsForExistingAndHistoricalServices),
ArgCompletionProvider: args.NewManualCompletionsProvider(getCompletionsForExistingAndHistoricalServices(enclaveIdentifierArgKey)),
ValidationFunc: noValidationFunc,
}
}
Expand Down Expand Up @@ -88,75 +89,82 @@ func getServiceUuidsAndNamesForEnclave(ctx context.Context, enclaveIdentifier st
return serviceUuids, serviceNamesToUuid, nil
}

func getCompletionsForExistingAndHistoricalServices(ctx context.Context, _ *flags.ParsedFlags, previousArgs *args.ParsedArgs) ([]string, error) {
enclaveIdentifier, err := previousArgs.GetNonGreedyArg(enclaveIdentifierArgKey)
if err != nil {
return nil, stacktrace.Propagate(err, "An error occurred getting the enclave identifier using key '%v'", enclaveIdentifierArgKey)
}
func getCompletionsForExistingAndHistoricalServices(enclaveIdentifierArgKey string) func(ctx context.Context, _ *flags.ParsedFlags, previousArgs *args.ParsedArgs) ([]string, error) {
return func(ctx context.Context, _ *flags.ParsedFlags, previousArgs *args.ParsedArgs) ([]string, error) {
enclaveIdentifier, err := previousArgs.GetNonGreedyArg(enclaveIdentifierArgKey)
if err != nil {
return nil, stacktrace.Propagate(err, "An error occurred getting the enclave identifier using key '%v'", enclaveIdentifierArgKey)
}

kurtosisCtx, err := kurtosis_context.NewKurtosisContextFromLocalEngine()
if err != nil {
return nil, stacktrace.Propagate(
err,
"An error occurred connecting to the Kurtosis engine for retrieving the service UUIDs & names for tab completion",
)
}
kurtosisCtx, err := kurtosis_context.NewKurtosisContextFromLocalEngine()
if err != nil {
return nil, stacktrace.Propagate(
err,
"An error occurred connecting to the Kurtosis engine for retrieving the service UUIDs & names for tab completion",
)
}

enclaveContext, err := kurtosisCtx.GetEnclaveContext(ctx, enclaveIdentifier)
if err != nil {
return nil, stacktrace.Propagate(err, "An error occurred while fetching enclave for identifier '%v'", enclaveIdentifier)
}
enclaveContext, err := kurtosisCtx.GetEnclaveContext(ctx, enclaveIdentifier)
if err != nil {
return nil, stacktrace.Propagate(err, "An error occurred while fetching enclave for identifier '%v'", enclaveIdentifier)
}

serviceIdentifiers, err := enclaveContext.GetExistingAndHistoricalServiceIdentifiers(ctx)
if err != nil {
return nil, stacktrace.Propagate(err, "An error occurred while fetching services for enclave '%v'", enclaveContext.GetEnclaveName())
}
serviceIdentifiers, err := enclaveContext.GetExistingAndHistoricalServiceIdentifiers(ctx)
if err != nil {
return nil, stacktrace.Propagate(err, "An error occurred while fetching services for enclave '%v'", enclaveContext.GetEnclaveName())
}

return serviceIdentifiers.GetOrderedListOfNames(), nil
return serviceIdentifiers.GetOrderedListOfNames(), nil
}
}

func getCompletionsOfActiveServices(ctx context.Context, flags *flags.ParsedFlags, previousArgs *args.ParsedArgs) ([]string, error) {
enclaveIdentifier, err := previousArgs.GetNonGreedyArg(enclaveIdentifierArgKey)
if err != nil {
return nil, stacktrace.Propagate(err, "An error occurred getting the enclave identifier using key '%v'", enclaveIdentifierArgKey)
}
func getCompletionsOfActiveServices(enclaveIdentifierArgKey string) func(ctx context.Context, flags *flags.ParsedFlags, previousArgs *args.ParsedArgs) ([]string, error) {
return func(ctx context.Context, _ *flags.ParsedFlags, previousArgs *args.ParsedArgs) ([]string, error) {
enclaveIdentifier, err := previousArgs.GetNonGreedyArg(enclaveIdentifierArgKey)
if err != nil {
return nil, stacktrace.Propagate(err, "An error occurred getting the enclave identifier using key '%v'", enclaveIdentifierArgKey)
}

_, serviceNames, err := getServiceUuidsAndNamesForEnclave(ctx, enclaveIdentifier)
if err != nil {
return nil, stacktrace.Propagate(err, "An error occurred getting the services retrieving for enclave identifier tab completion")
}
_, serviceNames, err := getServiceUuidsAndNamesForEnclave(ctx, enclaveIdentifier)
if err != nil {
return nil, stacktrace.Propagate(err, "An error occurred getting the services retrieving for enclave identifier tab completion")
}

serviceNamesList := []string{}
for serviceName := range serviceNames {
serviceNamesList = append(serviceNamesList, string(serviceName))
}
sort.Strings(serviceNamesList)
serviceNamesList := []string{}
for serviceName := range serviceNames {
serviceNamesList = append(serviceNamesList, string(serviceName))
}
sort.Strings(serviceNamesList)

return serviceNamesList, nil
return serviceNamesList, nil
}
}

func getServiceIdentifiersForValidation(ctx context.Context, _ *flags.ParsedFlags, previousArgs *args.ParsedArgs) (map[services.ServiceUUID]bool, map[services.ServiceName]services.ServiceUUID, map[string][]services.ServiceUUID, error) {
enclaveIdentifier, err := previousArgs.GetNonGreedyArg(enclaveIdentifierArgKey)
if err != nil {
return nil, nil, nil, stacktrace.Propagate(err, "An error occurred getting the enclave identifier using key '%v'", enclaveIdentifierArgKey)
}
func getServiceIdentifiersForValidation(enclaveIdentifierArgKey string) func(ctx context.Context, _ *flags.ParsedFlags, previousArgs *args.ParsedArgs) (map[services.ServiceUUID]bool, map[services.ServiceName]services.ServiceUUID, map[string][]services.ServiceUUID, error) {
return func(ctx context.Context, _ *flags.ParsedFlags, previousArgs *args.ParsedArgs) (map[services.ServiceUUID]bool, map[services.ServiceName]services.ServiceUUID, map[string][]services.ServiceUUID, error) {
enclaveIdentifier, err := previousArgs.GetNonGreedyArg(enclaveIdentifierArgKey)
if err != nil {
return nil, nil, nil, stacktrace.Propagate(err, "An error occurred getting the enclave identifier using key '%v'", enclaveIdentifierArgKey)
}

serviceUuids, serviceNames, err := getServiceUuidsAndNamesForEnclave(ctx, enclaveIdentifier)
if err != nil {
return nil, nil, nil, stacktrace.Propagate(err, "An error occurred getting the services retrieving for enclave identifier tab completion")
}
serviceUuids, serviceNames, err := getServiceUuidsAndNamesForEnclave(ctx, enclaveIdentifier)
if err != nil {
return nil, nil, nil, stacktrace.Propagate(err, "An error occurred getting the services retrieving for enclave identifier tab completion")
}

shortenedUuidsToUuids := make(map[string][]services.ServiceUUID)
for serviceUuid := range serviceUuids {
shortenedUuid := uuid_generator.ShortenedUUIDString(string(serviceUuid))
shortenedUuidsToUuids[shortenedUuid] = append(shortenedUuidsToUuids[shortenedUuid], serviceUuid)
shortenedUuidsToUuids := make(map[string][]services.ServiceUUID)
for serviceUuid := range serviceUuids {
shortenedUuid := uuid_generator.ShortenedUUIDString(string(serviceUuid))
shortenedUuidsToUuids[shortenedUuid] = append(shortenedUuidsToUuids[shortenedUuid], serviceUuid)
}
return serviceUuids, serviceNames, shortenedUuidsToUuids, nil
}
return serviceUuids, serviceNames, shortenedUuidsToUuids, nil
}

func getValidationFunc(
argKey string,
isGreedy bool,
enclaveIdentifierArgKey string,
) func(context.Context, *flags.ParsedFlags, *args.ParsedArgs) error {
return func(ctx context.Context, flags *flags.ParsedFlags, args *args.ParsedArgs) error {
var serviceIdentifiersToValidate []string
Expand All @@ -174,7 +182,7 @@ func getValidationFunc(
serviceIdentifiersToValidate = []string{serviceUuid}
}

serviceUuids, serviceNames, shortenedUuidsToUuids, err := getServiceIdentifiersForValidation(ctx, flags, args)
serviceUuids, serviceNames, shortenedUuidsToUuids, err := getServiceIdentifiersForValidation(enclaveIdentifierArgKey)(ctx, flags, args)
if err != nil {
return stacktrace.Propagate(err, "An error occurred while getting the services for the enclave")
}
Expand Down
1 change: 1 addition & 0 deletions cli/cli/commands/files/storeservice/storeservice.go
Expand Up @@ -89,6 +89,7 @@ var FilesStoreServiceCmd = &engine_consuming_kurtosis_command.EngineConsumingKur
),
service_identifier_arg.NewServiceIdentifierArg(
serviceIdentifierArgKey,
enclaveIdentifierArgKey,
isServiceIdentifierArgOptional,
isServiceIdentifierArgGreedy,
),
Expand Down
1 change: 1 addition & 0 deletions cli/cli/commands/port/print/print.go
Expand Up @@ -53,6 +53,7 @@ var PortPrintCmd = &engine_consuming_kurtosis_command.EngineConsumingKurtosisCom
// TODO we should fix this after https://github.com/kurtosis-tech/kurtosis/issues/879
service_identifier_arg.NewHistoricalServiceIdentifierArgWithValidationDisabled(
serviceIdentifierArgKey,
enclaveIdentifierArgKey,
isServiceIdentifierArgOptional,
isServiceIdentifierArgGreedy,
),
Expand Down
1 change: 1 addition & 0 deletions cli/cli/commands/service/exec/exec.go
Expand Up @@ -55,6 +55,7 @@ var ServiceShellCmd = &engine_consuming_kurtosis_command.EngineConsumingKurtosis
),
service_identifier_arg.NewServiceIdentifierArg(
serviceIdentifierArgKey,
enclaveIdentifierArgKey,
isServiceGuidArgOptional,
isServiceGuidArgGreedy,
),
Expand Down
1 change: 1 addition & 0 deletions cli/cli/commands/service/logs/logs.go
Expand Up @@ -113,6 +113,7 @@ var ServiceLogsCmd = &engine_consuming_kurtosis_command.EngineConsumingKurtosisC
),
service_identifier_arg.NewServiceIdentifierArg(
serviceIdentifierArgKey,
enclaveIdentifierArgKey,
isServiceIdentifierArgGreedy,
isServiceIdentifierArgOptional,
),
Expand Down
1 change: 1 addition & 0 deletions cli/cli/commands/service/rm/rm.go
Expand Up @@ -60,6 +60,7 @@ var ServiceRmCmd = &engine_consuming_kurtosis_command.EngineConsumingKurtosisCom
),
service_identifier_arg.NewServiceIdentifierArg(
serviceIdentifierArgKey,
enclaveIdentifierArgKey,
isServiceIdentifierArgGreedy,
isServiceIdentifierArgOptional,
),
Expand Down
1 change: 1 addition & 0 deletions cli/cli/commands/service/shell/shell.go
Expand Up @@ -51,6 +51,7 @@ var ServiceShellCmd = &engine_consuming_kurtosis_command.EngineConsumingKurtosis
),
service_identifier_arg.NewServiceIdentifierArg(
serviceIdentifierArgKey,
enclaveIdentifierArgKey,
isServiceGuidArgOptional,
isServiceGuidArgGreedy,
),
Expand Down
1 change: 1 addition & 0 deletions cli/cli/commands/service/start/start.go
Expand Up @@ -60,6 +60,7 @@ var ServiceStartCmd = &engine_consuming_kurtosis_command.EngineConsumingKurtosis
),
service_identifier_arg.NewServiceIdentifierArg(
serviceIdentifierArgKey,
enclaveIdentifierArgKey,
isServiceIdentifierArgGreedy,
isServiceIdentifierArgOptional,
),
Expand Down
1 change: 1 addition & 0 deletions cli/cli/commands/service/stop/stop.go
Expand Up @@ -60,6 +60,7 @@ var ServiceStopCmd = &engine_consuming_kurtosis_command.EngineConsumingKurtosisC
),
service_identifier_arg.NewServiceIdentifierArg(
serviceIdentifierArgKey,
enclaveIdentifierArgKey,
isServiceIdentifierArgGreedy,
isServiceIdentifierArgOptional,
),
Expand Down