Skip to content

Commit

Permalink
feat: Add format flag to kurtosis port print (#1319)
Browse files Browse the repository at this point in the history
## Description:
<!-- Describe this change, how it works, and the motivation behind it.
-->
Allows for customization via `--format` flag of the pieces to be shown.
Options are:
1. Protocol
2. Port
3. Ip

## Is this change user facing?
YES
<!-- If yes, please add the "user facing" label to the PR -->
<!-- If yes, don't forget to include docs changes where relevant -->

## References (if applicable):
<!-- Add relevant Github Issues, Discord threads, or other helpful
information. -->
Closes #1316
  • Loading branch information
victorcolombo authored Sep 19, 2023
1 parent 3905782 commit cbbf260
Showing 1 changed file with 59 additions and 7 deletions.
66 changes: 59 additions & 7 deletions cli/cli/commands/port/print/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package print
import (
"context"
"fmt"
"github.com/kurtosis-tech/kurtosis/api/golang/core/lib/services"
"github.com/kurtosis-tech/kurtosis/api/golang/engine/kurtosis_engine_rpc_api_bindings"
"github.com/kurtosis-tech/kurtosis/api/golang/engine/lib/kurtosis_context"
"github.com/kurtosis-tech/kurtosis/cli/cli/command_framework/highlevel/enclave_id_arg"
Expand All @@ -15,6 +16,8 @@ import (
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface"
metrics_client "github.com/kurtosis-tech/metrics-library/golang/lib/client"
"github.com/kurtosis-tech/stacktrace"
"github.com/sirupsen/logrus"
"strings"
)

const (
Expand All @@ -33,16 +36,39 @@ const (
isPortIdentifierArgOptional = false
isPortIdentifierArgGreedy = false

formatFlagKey = "format"
protocolStr = "protocol"
ipStr = "ip"
numberStr = "number"

ipAddress = "127.0.0.1"
)

var (
formatFlagKeyDefault = fmt.Sprintf("'%s,%s,%s'", protocolStr, ipStr, numberStr)
formatFlagKeyExamples = []string{
fmt.Sprintf("'%s'", ipStr),
fmt.Sprintf("'%s'", numberStr),
fmt.Sprintf("'%s,%s'", protocolStr, ipStr),
}
)

var PortPrintCmd = &engine_consuming_kurtosis_command.EngineConsumingKurtosisCommand{
CommandStr: command_str_consts.PortPrintCmdStr,
ShortDescription: "Get information about port",
LongDescription: "Get information for port using port id",
KurtosisBackendContextKey: kurtosisBackendCtxKey,
EngineClientContextKey: engineClientCtxKey,
Flags: []*flags.FlagConfig{},
Flags: []*flags.FlagConfig{
{
Key: formatFlagKey,
Usage: fmt.Sprintf(
"Allows selecting what pieces of port are printed, using comma separated values (examples: %s). Default %s.",
strings.Join(formatFlagKeyExamples, ", "), formatFlagKeyDefault),
Type: flags.FlagType_String,
Default: formatFlagKeyDefault,
},
},
Args: []*args.ArgConfig{
enclave_id_arg.NewHistoricalEnclaveIdentifiersArgWithValidationDisabled(
enclaveIdentifierArgKey,
Expand Down Expand Up @@ -89,6 +115,11 @@ func run(
return stacktrace.Propagate(err, "An error occurred getting the port identifier using arg key '%v'", portIdentifier)
}

format, err := flags.GetString(formatFlagKey)
if err != nil {
return stacktrace.Propagate(err, "An error occurred getting the output flag key '%v'", formatFlagKey)
}

kurtosisCtx, err := kurtosis_context.NewKurtosisContextFromLocalEngine()
if err != nil {
return stacktrace.Propagate(err, "An error occurred connecting to the local Kurtosis engine")
Expand All @@ -113,13 +144,34 @@ func run(
)
}

fullUrl := fmt.Sprintf("%v:%v", ipAddress, publicPort.GetNumber())
maybeApplicationProtocol := publicPort.GetMaybeApplicationProtocol()

if maybeApplicationProtocol != "" {
fullUrl = fmt.Sprintf("%v://%v", maybeApplicationProtocol, fullUrl)
fullUrl, err := formatOutput(format, ipAddress, publicPort)
if err != nil {
return stacktrace.Propagate(err, "Couldn't format the output according to formatting string '%v'", format)
}

out.PrintOutLn(fullUrl)
return nil
}

func formatOutput(format string, ipAddress string, spec *services.PortSpec) (string, error) {
parts := strings.Split(format, ",")
var resultParts []string
for _, part := range parts {
switch part {
case protocolStr:
if spec.GetMaybeApplicationProtocol() != "" {
resultParts = append(resultParts, spec.GetMaybeApplicationProtocol()+"://")
} else {
// TODO(victor.colombo): What should we do here? Panic? Warn?
// Left it as a debug for now so it doesn't pollute the output
logrus.Debugf("Expected protocol but was empty, skipping")
}
case ipStr:
resultParts = append(resultParts, ipAddress)
case numberStr:
resultParts = append(resultParts, fmt.Sprintf(":%d", spec.GetNumber()))
default:
return "", stacktrace.NewError("Invalid format piece '%v'", part)
}
}
return strings.Join(resultParts, ""), nil
}

0 comments on commit cbbf260

Please sign in to comment.