Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/reference/manual/hcloud.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions docs/reference/manual/hcloud_api.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/spf13/cobra"

"github.com/hetznercloud/cli/internal/cmd/all"
"github.com/hetznercloud/cli/internal/cmd/api"
"github.com/hetznercloud/cli/internal/cmd/certificate"
"github.com/hetznercloud/cli/internal/cmd/completion"
configCmd "github.com/hetznercloud/cli/internal/cmd/config"
Expand Down Expand Up @@ -74,6 +75,7 @@ func NewRootCommand(s state.State) *cobra.Command {
completion.NewCommand(s),
context.NewCommand(s),
configCmd.NewCommand(s),
api.APICmd.CobraCommand(s),
)

cmd.PersistentFlags().AddFlagSet(s.Config().FlagSet())
Expand Down
99 changes: 99 additions & 0 deletions internal/cmd/api/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package api

import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"

"github.com/spf13/cobra"

"github.com/hetznercloud/cli/internal/cmd/base"
"github.com/hetznercloud/cli/internal/cmd/cmpl"
"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>",
Comment thread
apricote marked this conversation as resolved.
Short: "Make API call",
}

cmd.Flags().StringP("method", "X", "GET", "HTTP method to use for API call")
_ = cmd.RegisterFlagCompletionFunc("method", cmpl.SuggestCandidates("GET", "POST", "PUT", "DELETE"))

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")
Comment thread
apricote marked this conversation as resolved.
data, _ := cmd.Flags().GetString("data")
values, _ := cmd.Flags().GetStringToString("value")

method = strings.ToUpper(method)
switch method {
case http.MethodGet, http.MethodHead, http.MethodPost,
http.MethodPut, http.MethodPatch, http.MethodDelete,
http.MethodConnect, http.MethodOptions, http.MethodTrace:
break
default:
return fmt.Errorf("unknown HTTP method: %s", method)
}

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 respBuf bytes.Buffer
_, err = s.Client().Do(req, &respBuf)
if err != nil {
return err
}
respBody := respBuf.Bytes()

if json.Valid(respBody) {
var formatted bytes.Buffer
if err := json.Indent(&formatted, respBody, "", " "); err != nil {
return err
}
cmd.Println(formatted.String())
} else if len(respBody) > 0 {
cmd.Println(string(respBody))
}
return nil
},
}
13 changes: 13 additions & 0 deletions internal/hcapi2/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package hcapi2

import (
"context"
"io"
"net/http"
"sync"

"github.com/hetznercloud/hcloud-go/v2/hcloud"
Expand Down Expand Up @@ -31,6 +34,8 @@ type Client interface {
StorageBoxType() StorageBoxTypeClient
Zone() ZoneClient
WithOpts(...hcloud.ClientOption)
NewRequest(ctx context.Context, method, path string, body io.Reader) (*http.Request, error)
Do(req *http.Request, v any) (*hcloud.Response, error)
}

type clientCache struct {
Expand Down Expand Up @@ -82,6 +87,14 @@ func (c *client) WithOpts(opts ...hcloud.ClientOption) {
c.update()
}

func (c *client) NewRequest(ctx context.Context, method, path string, body io.Reader) (*http.Request, error) {
return c.client.NewRequest(ctx, method, path, body)
}

func (c *client) Do(req *http.Request, v any) (*hcloud.Response, error) {
return c.client.Do(req, v)
}

func (c *client) update() {
c.client = hcloud.NewClient(c.opts...)
c.cache = clientCache{}
Expand Down
13 changes: 11 additions & 2 deletions internal/hcapi2/mock/client.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package mock

import (
"context"
"io"
"net/http"

"go.uber.org/mock/gomock"

"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/cli/internal/state/config"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
)

Expand Down Expand Up @@ -157,6 +160,12 @@ func (*Client) WithOpts(_ ...hcloud.ClientOption) {
// no-op
}

func (*Client) FromConfig(_ config.Config) {
func (*Client) NewRequest(_ context.Context, _, _ string, _ io.Reader) (*http.Request, error) {
// no-op
return nil, nil
}

func (*Client) Do(_ *http.Request, _ any) (*hcloud.Response, error) {
// no-op
return nil, nil
}
Loading