-
Notifications
You must be signed in to change notification settings - Fork 103
feat: add api command #1457
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
base: main
Are you sure you want to change the base?
feat: add api command #1457
Changes from 4 commits
e986a9b
95eaa36
93e9fcf
2cea792
87cb8ff
ebf62ae
d2e8e8b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/json" | ||
| "io" | ||
| "net/url" | ||
| "os" | ||
| "strings" | ||
|
|
||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/hetznercloud/cli/internal/cmd/base" | ||
| "github.com/hetznercloud/cli/internal/hcapi2" | ||
| "github.com/hetznercloud/cli/internal/state" | ||
| ) | ||
|
|
||
| var APICmd = base.Cmd{ | ||
| BaseCobraCommand: func(_ hcapi2.Client) *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "api [options] <path>", | ||
| Short: "Make API call", | ||
| } | ||
|
|
||
| cmd.Flags().StringP("method", "X", "GET", "HTTP method to use for API call") | ||
| cmd.Flags().StringP("data", "d", "", "HTTP request body content (use - to read from stdin)") | ||
| cmd.Flags().StringToStringP("value", "v", nil, "key=value pairs to pass as query parameters") | ||
| return cmd | ||
| }, | ||
| Run: func(s state.State, cmd *cobra.Command, args []string) error { | ||
| path := args[0] | ||
| method, _ := cmd.Flags().GetString("method") | ||
|
apricote marked this conversation as resolved.
|
||
| data, _ := cmd.Flags().GetString("data") | ||
| values, _ := cmd.Flags().GetStringToString("value") | ||
|
|
||
| var body io.Reader | ||
| switch data { | ||
| case "": | ||
| body = nil | ||
| case "-": | ||
| body = os.Stdin | ||
| default: | ||
| body = strings.NewReader(data) | ||
| } | ||
|
|
||
| if !strings.HasPrefix(path, "/") { | ||
| path = "/" + path | ||
| } | ||
|
|
||
| u, err := url.Parse(path) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| params := u.Query() | ||
| for k, v := range values { | ||
| params.Set(k, v) | ||
| } | ||
| u.RawQuery = params.Encode() | ||
|
|
||
| req, err := s.Client().NewRequest(s, method, u.String(), body) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| var respBody json.RawMessage | ||
| _, err = s.Client().Do(req, &respBody) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| var formatted bytes.Buffer | ||
| if err := json.Indent(&formatted, respBody, "", " "); err != nil { | ||
|
apricote marked this conversation as resolved.
Outdated
|
||
| return err | ||
| } | ||
| cmd.Print(formatted.String()) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This prints the formatted string without a final newline. In some shells this causes the prompt to be printed on the same line as the closing It looks like |
||
| return nil | ||
| }, | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if this is relevant for the first iteration, but what about setting
Content-Typeand defaulting to json?edit: we already set this in hcloud-go, so we would not need to default here.