Skip to content

Commit

Permalink
Add api patch command (#407)
Browse files Browse the repository at this point in the history
  • Loading branch information
divolgin authored Aug 16, 2024
1 parent 1ce8a1e commit c36961c
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 2 deletions.
66 changes: 66 additions & 0 deletions cli/cmd/api_patch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package cmd

import (
"fmt"
"strings"

"github.com/replicatedhq/replicated/pkg/kotsclient"
"github.com/spf13/cobra"
)

func (r *runners) InitAPIPatch(parent *cobra.Command) *cobra.Command {
cmd := &cobra.Command{
Use: "patch",
Short: "Make ad-hoc PATCH API calls to the Replicated API",
Long: `This is essentially like curl for the Replicated API, but
uses your local credentials and prints the response unmodified.
We recommend piping the output to jq for easier reading.
Pass the PATH of the request as the final argument. Do not include the host or version.
Example:
replicated api patch /v3/customer/2VffY549paATVfHSGpJhjh6Ehpy -b '{"name":"Valuable Customer"}'
`,
RunE: r.apiPatch,
SilenceUsage: true,
Args: cobra.ExactArgs(1),
}
parent.AddCommand(cmd)

cmd.Flags().StringVarP(&r.args.apiPatchBody, "body", "b", "", "JSON body to send with the request")

return cmd
}

func (r *runners) apiPatch(cmd *cobra.Command, args []string) error {
path := args[0]

if !strings.HasPrefix(args[0], "/") {
path = fmt.Sprintf("/%s", args[0])
}
pathParts := strings.Split(path, "/")
// remove any empty parts
for i := len(pathParts) - 1; i >= 0; i-- {
if pathParts[i] == "" {
pathParts = append(pathParts[:i], pathParts[i+1:]...)
}
}

// v1 and v2 paths use platform client, v3 uses kots client
// split the path on the first slash to determine which client to use
if pathParts[0] == "v1" {

} else if pathParts[0] == "v3" {
kotsRestClient := kotsclient.VendorV3Client{HTTPClient: *r.platformAPI}
response, err := kotsRestClient.Patch(path, r.args.apiPatchBody)
if err != nil {
return err
}

fmt.Printf("%s", response)
}

return nil
}
1 change: 1 addition & 0 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ func Execute(rootCmd *cobra.Command, stdin io.Reader, stdout io.Writer, stderr i
runCmds.InitAPIGet(apiCmd)
runCmds.InitAPIPost(apiCmd)
runCmds.InitAPIPut(apiCmd)
runCmds.InitAPIPatch(apiCmd)

runCmds.rootCmd.SetUsageTemplate(rootCmdUsageTmpl)

Expand Down
5 changes: 3 additions & 2 deletions cli/cmd/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,9 @@ type runnerArgs struct {

loginEndpoint string

apiPostBody string
apiPutBody string
apiPostBody string
apiPutBody string
apiPatchBody string

customerInspectCustomer string

Expand Down
9 changes: 9 additions & 0 deletions pkg/kotsclient/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,12 @@ func (c *VendorV3Client) Put(path string, body string) ([]byte, error) {

return resp, nil
}

func (c *VendorV3Client) Patch(path string, body string) ([]byte, error) {
resp, err := c.DoJSONWithoutUnmarshal("PATCH", path, body)
if err != nil {
return nil, err
}

return resp, nil
}

0 comments on commit c36961c

Please sign in to comment.