-
Notifications
You must be signed in to change notification settings - Fork 574
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
cmd/snap: add debug api
command
#14116
Conversation
debug api
commanddebug api
command
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.
LGTM with a tweak
cmd/snap/cmd_debug_api.go
Outdated
hdrs := x.Positional.Headers | ||
reqHdrs := make(map[string]string, len(hdrs)) | ||
for _, arg := range x.Positional.Headers { | ||
split := strings.SplitN(arg, "=", 2) |
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.
Have a look at strings.Cut
for a nicer alternative.
Codecov ReportAttention: Patch coverage is
❗ Your organization needs to install the Codecov GitHub app to enable full functionality. Additional details and impacted files@@ Coverage Diff @@
## master #14116 +/- ##
==========================================
- Coverage 78.72% 78.70% -0.03%
==========================================
Files 1053 1054 +1
Lines 137711 137892 +181
==========================================
+ Hits 108419 108524 +105
- Misses 22494 22562 +68
- Partials 6798 6806 +8
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
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.
lgtm, a couple of small comments
cmd/snap/cmd_debug_api.go
Outdated
// -*- Mode: Go; indent-tabs-mode: t -*- | ||
|
||
/* | ||
* Copyright (C) 2021 Canonical Ltd |
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.
* Copyright (C) 2021 Canonical Ltd | |
* Copyright (C) 2024 Canonical Ltd |
@@ -0,0 +1,108 @@ | |||
// -*- Mode: Go; indent-tabs-mode: t -*- |
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.
I think somewhere here it warrants a simple example just to show how it's used
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.
If you add an example (like you have in the PR description), you could add it to the command's long description so it's easily accessible
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.
I've added some examples in long description of the command.
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.
lgtm, thank you
client/client.go
Outdated
@@ -801,3 +801,9 @@ func (c *Client) MigrateSnapHome(snaps []string) (changeID string, err error) { | |||
|
|||
return c.doAsync("POST", "/v2/debug", nil, nil, bytes.NewReader(body)) | |||
} | |||
|
|||
// DebugRaw allows to make raw queries to the API, but the intention of using it |
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.
// DebugRaw allows to make raw queries to the API, but the intention of using it | |
// DebugRaw allows to make raw queries to the API with the intention of using it |
cmd/snap/cmd_debug_api.go
Outdated
if rsp != nil { | ||
defer rsp.Body.Close() | ||
} | ||
if err != nil { | ||
return err | ||
} |
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.
The error and the response are mutually exclusive, so this can be simplified slightly
if rsp != nil { | |
defer rsp.Body.Close() | |
} | |
if err != nil { | |
return err | |
} | |
if err != nil { | |
return err | |
} | |
defer rsp.Body.Close() |
@@ -0,0 +1,108 @@ | |||
// -*- Mode: Go; indent-tabs-mode: t -*- |
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.
If you add an example (like you have in the PR description), you could add it to the command's long description so it's easily accessible
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.
couple comments
cmd/snap/cmd_debug_api.go
Outdated
|
||
Positional struct { | ||
Method string `positional-arg-name:"<method>"` | ||
Query string `positional-arg-name:"<query>"` |
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.
this is a bit of a strange as it contains also the path?
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.
Renamed the argument. I didn't want to split it into path and query params, such that snap debug api GET /v2/snaps
works and you need to pass additional query args it's simply snap debug api GET '/v2/find?name=foo'
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.
I've tweaked the arguments now so that we're closer to what curl expects, i.e.:
Usage:
test-snap debug api [api-OPTIONS] <path-and-query>
...
[api command options]
--pretty Pretty print if response is JSON
-H, --header= Set header (can be repeated multiple times)
-X, --request= HTTP method to use (defaults to GET)
--fail Fail on request errors
cmd/snap/cmd_debug_api.go
Outdated
return err | ||
} | ||
|
||
if x.Pretty { |
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.
are we consistent enough with the content type of returns in the API? that we can simply do this any time (or at least when the output is a terminal) it's JSON without needing to have the option?
cmd/snap/cmd_debug_api.go
Outdated
Positional struct { | ||
Method string `positional-arg-name:"<method>"` | ||
Query string `positional-arg-name:"<query>"` | ||
Headers []string `positional-arg-name:"<headers>"` |
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.
the order query/path then headers is a bit confusing tbh
debug api
commanddebug api
command
Signed-off-by: Maciej Borzecki <[email protected]>
3fe3e04
to
e96bb1d
Compare
cmd/snap/cmd_debug_api.go
Outdated
} | ||
defer rsp.Body.Close() | ||
|
||
if x.Pretty && rsp.Header.Get("Content-Type") == "application/json" { |
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.
my idea was not to have --pretty/Pretty at all and just check for the type?
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.
aah I see, ok let me update the code then
a26f967
to
d92e8d6
Compare
Add a debug command to execute raw queries to the snapd API. This is useful when trying to query snapd endpoints directly, but curl is other similar tool is not available. Signed-off-by: Maciej Borzecki <[email protected]>
d92e8d6
to
a033a40
Compare
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.
lgtm
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.
thank you
Add a new
snap debug api
command which can be used to execute raw queries against snapd API. This is supposed to be a replacement for curl which is not part of the base snaps and thus, especially on core, needs to be installed separately using --devmode.Trigger snap refresh:
Grab some static output:
Or stacktraces from pprof:
Related: SNAPDENG-23792