Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
79 changes: 79 additions & 0 deletions internal/cmd/api/api.go
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>",

@lukasmetzner lukasmetzner Jul 17, 2026

Copy link
Copy Markdown
Contributor

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-Type and defaulting to json?

edit: we already set this in hcloud-go, so we would not need to default here.

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")
Comment thread
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 {
Comment thread
apricote marked this conversation as resolved.
Outdated
return err
}
cmd.Print(formatted.String())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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 }. zsh shows a % after the closing } by default.

It looks like jq prints a final newline after formatted output. I would suggest we do the same, but maybe I am overlooking some drawbacks.

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