Skip to content

Commit

Permalink
Resolves #336 - Allow - values for sort to be passed in (#384)
Browse files Browse the repository at this point in the history
  • Loading branch information
steve-r-west authored Oct 2, 2023
1 parent 10a9286 commit 44a7371
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 20 deletions.
14 changes: 0 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,20 +146,6 @@ To load completions for each session, execute once:

## Tips

### Sorting in Descending Order

The EPCC CLI supports sorting in descending order however you may get an error:
```bash
$ epcc get customers sort -updated_at
Error: unknown short flag: 'u' in -updated_at
```

You will need to use a bare double dash "--" before the argument, this signals that flag processing is complete and is a convention in many shells.

```bash
$ epcc get customers -- sort -updated_at
```

### JQ Output

The `--output-jq` option can post process the output of `epcc create` `epcc get` and `epcc update`, for instance the following can be used to create richer
Expand Down
9 changes: 4 additions & 5 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,10 @@ func getResource(ctx context.Context, overrides *httpclient.HttpParameterOverrid
params.Add(args[i], args[i+1])
}

if (idCount-len(args)+1)%2 != 0 {
log.Warnf("Extra argument at the end of the command %s", args[len(args)-1])
}

for _, v := range overrides.QueryParameters {
keyAndValue := strings.SplitN(v, "=", 2)
if len(keyAndValue) != 2 {
Expand All @@ -374,11 +378,6 @@ func getResource(ctx context.Context, overrides *httpclient.HttpParameterOverrid
params.Add(keyAndValue[0], keyAndValue[1])
}

// Steve doesn't understand this logic check
if (idCount-len(args)+1)%2 != 0 {
resourceURL = resourceURL + url.QueryEscape(args[len(args)-1])
}

// Submit request
resp, err := httpclient.DoRequest(ctx, "GET", resourceURL, params.Encode(), nil)

Expand Down
3 changes: 3 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/elasticpath/epcc-cli/external/clictx"
"github.com/elasticpath/epcc-cli/external/httpclient"
"github.com/elasticpath/epcc-cli/external/logger"
"github.com/elasticpath/epcc-cli/external/misc"
"github.com/elasticpath/epcc-cli/external/profiles"
"github.com/elasticpath/epcc-cli/external/shutdown"
"github.com/elasticpath/epcc-cli/external/version"
Expand Down Expand Up @@ -58,6 +59,8 @@ var jqCompletionFunc = func(cmd *cobra.Command, args []string, toComplete string
var profileNameFromCommandLine = ""

func InitializeCmd() {

os.Args = misc.AddImplicitDoubleDash(os.Args)
if os.Args[1] == "__complete" {
DisableLongOutput = true
DisableExampleOutput = true
Expand Down
5 changes: 4 additions & 1 deletion cmd/runbooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"github.com/buildkite/shellwords"
"github.com/elasticpath/epcc-cli/external/completion"
"github.com/elasticpath/epcc-cli/external/misc"
"github.com/elasticpath/epcc-cli/external/resources"
"github.com/elasticpath/epcc-cli/external/runbooks"
_ "github.com/elasticpath/epcc-cli/external/runbooks"
Expand Down Expand Up @@ -208,7 +209,9 @@ func initRunbookRunCommands() *cobra.Command {
commandAndResetFunc.reset()
stepCmd := commandAndResetFunc.cmd

stepCmd.SetArgs(rawCmdArguments[1:])
tweakedArguments := misc.AddImplicitDoubleDash(rawCmdArguments)
stepCmd.SetArgs(tweakedArguments[1:])

log.Tracef("(Step %d/%d Command %d/%d) Starting Command", stepIdx+1, numSteps, commandIdx+1, len(funcs))

stepCmd.ResetFlags()
Expand Down
34 changes: 34 additions & 0 deletions external/misc/arg_tweaker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package misc

func AddImplicitDoubleDash(args []string) []string {

newArgs := make([]string, 0, len(args))
dashesAdded := false

// This adds a -- before a sort where the next argument starts with a dash.
// -- is a standard shell idiom to turn of flag parsing.
for i := 0; i < len(args); i++ {
if args[i] == "sort" {
if i < len(args)-1 {
nextArg := args[i+1]
if len(nextArg) > 0 {
if nextArg[0] == '-' {
if !dashesAdded {
newArgs = append(newArgs, "sort", "--", nextArg)
i++
dashesAdded = true
continue
}
}
}
}
} else if args[i] == "--" {
dashesAdded = true
}
newArgs = append(newArgs, args[i])

}

return newArgs

}

0 comments on commit 44a7371

Please sign in to comment.