Skip to content

Commit 977115e

Browse files
sjmiller609stainless-app[bot]
authored andcommitted
Use SDK for logs
1 parent 0454d6e commit 977115e

File tree

1 file changed

+16
-46
lines changed

1 file changed

+16
-46
lines changed

pkg/cmd/logs.go

Lines changed: 16 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@ package cmd
33
import (
44
"context"
55
"fmt"
6-
"io"
7-
"net/http"
8-
"net/url"
9-
"os"
106

117
"github.com/onkernel/hypeman-go"
8+
"github.com/onkernel/hypeman-go/option"
129
"github.com/urfave/cli/v3"
1310
)
1411

@@ -46,54 +43,27 @@ func handleLogs(ctx context.Context, cmd *cli.Command) error {
4643
return err
4744
}
4845

49-
// Build URL for logs endpoint
50-
baseURL := cmd.Root().String("base-url")
51-
if baseURL == "" {
52-
baseURL = os.Getenv("HYPEMAN_BASE_URL")
46+
params := hypeman.InstanceLogsParams{}
47+
if cmd.IsSet("follow") {
48+
params.Follow = hypeman.Opt(cmd.Bool("follow"))
5349
}
54-
if baseURL == "" {
55-
baseURL = "http://localhost:8080"
50+
if cmd.IsSet("tail") {
51+
params.Tail = hypeman.Opt(int64(cmd.Int("tail")))
5652
}
5753

58-
u, err := url.Parse(baseURL)
59-
if err != nil {
60-
return fmt.Errorf("invalid base URL: %w", err)
61-
}
62-
u.Path = fmt.Sprintf("/instances/%s/logs", instanceID)
63-
64-
// Add query parameters
65-
q := u.Query()
66-
q.Set("tail", fmt.Sprintf("%d", cmd.Int("tail")))
67-
if cmd.Bool("follow") {
68-
q.Set("follow", "true")
69-
}
70-
u.RawQuery = q.Encode()
71-
72-
// Make HTTP request
73-
req, err := http.NewRequestWithContext(ctx, "GET", u.String(), nil)
74-
if err != nil {
75-
return fmt.Errorf("failed to create request: %w", err)
76-
}
77-
78-
apiKey := os.Getenv("HYPEMAN_API_KEY")
79-
if apiKey != "" {
80-
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", apiKey))
81-
}
82-
83-
resp, err := http.DefaultClient.Do(req)
84-
if err != nil {
85-
return fmt.Errorf("failed to fetch logs: %w", err)
86-
}
87-
defer resp.Body.Close()
54+
stream := client.Instances.LogsStreaming(
55+
ctx,
56+
instanceID,
57+
params,
58+
option.WithMiddleware(debugMiddleware(cmd.Root().Bool("debug"))),
59+
)
60+
defer stream.Close()
8861

89-
if resp.StatusCode != http.StatusOK {
90-
body, _ := io.ReadAll(resp.Body)
91-
return fmt.Errorf("failed to fetch logs (HTTP %d): %s", resp.StatusCode, string(body))
62+
for stream.Next() {
63+
fmt.Println(stream.Current())
9264
}
9365

94-
// Stream the response to stdout
95-
_, err = io.Copy(os.Stdout, resp.Body)
96-
return err
66+
return stream.Err()
9767
}
9868

9969

0 commit comments

Comments
 (0)