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
5 changes: 2 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,8 @@ linters:
- github.com/meshcloud/meshstack-cli/client
# the API docs choose the version of a request
- github.com/meshcloud/meshstack-cli/internal/apidocs
# a full URL picks the stored profile whose endpoint holds it, and has to fit an endpoint
# or profile named by flag or environment, so api reads the stored profiles and the
# source of both settings, which pkg/ leaves out
# api-docs takes the endpoint of the profile without a login, and a full URL gives its
# origin as the endpoint setting, both of which pkg/ leaves out
- github.com/meshcloud/meshstack-cli/internal/profile
- github.com/meshcloud/meshstack-cli/internal/meshstack
- github.com/meshcloud/meshstack-cli/internal/setting
Expand Down
19 changes: 4 additions & 15 deletions cmd/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"github.com/meshcloud/meshstack-cli/client/http"
"github.com/meshcloud/meshstack-cli/client/openapi"
"github.com/meshcloud/meshstack-cli/cmd/internal"
"github.com/meshcloud/meshstack-cli/internal/setting"
)

func New() *cobra.Command {
Expand All @@ -28,7 +27,7 @@ func New() *cobra.Command {
cmd := &cobra.Command{
Use: "api <path or URL>",
Short: "Send an authorized request to the meshStack API",
Long: fmt.Sprintf(`Send an authorized request to a path of the meshStack API, and print the response, which is typically JSON. Give the path relative to the endpoint, or give a full URL whose endpoint matches a stored profile with credentials.
Long: fmt.Sprintf(`Send an authorized request to a path of the meshStack API, and print the response, which is typically JSON. Give the path relative to the endpoint, or give a full URL, whose scheme and host the command then uses as the endpoint.

Use this command for everything that the other commands do not cover. For a path of meshStack's public API, the command sets the Accept and Content-Type headers to the latest API version that the API docs list, unless you set them yourself.

Expand All @@ -52,24 +51,14 @@ To see what the API docs say about the request, run the same command line with '
return err
}
ctx := cmd.Context()
var profileSources setting.Sources
if r.target.IsAbs() {
if _, profileSources, err = profileFor(ctx, r.target); err != nil {
return err
}
}
// Resolved before the API docs, whose first download takes a while, so that a missing
// login fails at once.
meshStack, err := internal.ResolveClient(ctx, internal.WithSettingSources(profileSources))
meshStack, err := internal.ResolveClient(ctx, internal.WithSettingSources(r.originAsEndpoint()))
if err != nil {
return err
}
if r.target.IsAbs() {
if err = r.cutTo(meshStack.Endpoint); err != nil {
return err
}
selector.Path = r.target.Path
args = []string{r.target.String()}
if r.target.IsAbs() && !strings.EqualFold(r.origin(), meshStack.Endpoint.String()) {
return fmt.Errorf("%s is not at the endpoint %s of this command; leave out --%s", r.target.Redacted(), meshStack.Endpoint, internal.EndpointFlag.Name)
}
spec, wait, err := flags.loadApiDocs(cmd, meshStack.Endpoint)
defer wait()
Expand Down
8 changes: 4 additions & 4 deletions cmd/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestApi(t *testing.T) {

_, err := meshStack.run(t, api.New(), "", "/api/meshobjects/meshbuildingblocks")

require.ErrorContains(t, err, "selects no credential")
require.ErrorContains(t, err, "no credential for the endpoint")
assert.Empty(t, docs.TakeRequests())
assert.Empty(t, meshStack.requests)
})
Expand Down Expand Up @@ -128,13 +128,13 @@ func TestApi(t *testing.T) {

t.Run("writes the body of a failed answer, and fails with a hint to api-docs on a client error", func(t *testing.T) {
for status, wantError := range map[int]string{
gohttp.StatusConflict: "meshStack answered HTTP 409: still in use. Run meshstack api-docs /api/x -H 'Accept: application/json' -X DELETE to see what the API expects",
gohttp.StatusConflict: "meshStack answered HTTP 409: still in use. Run meshstack api-docs /api/x -H 'Accept: application/json' -X POST to see what the API expects",
gohttp.StatusBadGateway: "meshStack answered HTTP 502: still in use",
gohttp.StatusForbidden: "auth scope profile default: meshStack answered HTTP 403: still in use",
gohttp.StatusForbidden: "auth scope endpoint " + meshStack.URL + ": meshStack answered HTTP 403: still in use",
} {
meshStack.answer(status, `{"message":"still in use"}`)

stdout, err := meshStack.run(t, api.New(), "", "-X", "DELETE", "/api/x", "-H", "Accept: application/json")
stdout, err := meshStack.run(t, api.New(), "", "-X", "POST", "/api/x", "-H", "Accept: application/json")

require.EqualError(t, err, wantError)
assert.JSONEq(t, "{\n \"message\": \"still in use\"\n}\n", stdout)
Expand Down
18 changes: 7 additions & 11 deletions cmd/api/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"github.com/meshcloud/meshstack-cli/client/types/xurl"
"github.com/meshcloud/meshstack-cli/cmd/internal"
"github.com/meshcloud/meshstack-cli/cmd/internal/markdown"
"github.com/meshcloud/meshstack-cli/internal/profile"
"github.com/meshcloud/meshstack-cli/internal/setting"
)

//go:embed docs.md.tmpl
Expand Down Expand Up @@ -67,17 +69,11 @@ The command downloads the API docs from your meshStack, so they list only what y
return fmt.Errorf("--%s cannot be combined with a path; see --help", describeFlagName)
}
var endpoint xurl.URL
if r.target.IsAbs() {
if endpoint, _, err = profileFor(cmd.Context(), r.target); err != nil {
return err
}
if endpoint.URL == nil {
return fmt.Errorf("no stored profile matches %s; give the path relative to the endpoint", r.target.Redacted())
}
if err = r.cutTo(endpoint); err != nil {
return err
}
} else if endpoint, err = docsEndpoint(cmd.Context()); err != nil {
resolved, _, err := profile.ResolveProfile(cmd.Context(), append(internal.SettingSources(), r.originAsEndpoint()...))
switch {
case err == nil:
endpoint = resolved.Endpoint
case !errors.Is(err, setting.ErrNoSourceProvidedValue):
return err
}
selector, err := flags.selector(r)
Expand Down
106 changes: 0 additions & 106 deletions cmd/api/profile.go

This file was deleted.

106 changes: 0 additions & 106 deletions cmd/api/profile_test.go

This file was deleted.

21 changes: 15 additions & 6 deletions cmd/api/request.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"context"
"encoding/json/jsontext"
"encoding/json/v2"
"fmt"
Expand All @@ -17,6 +18,8 @@ import (
"github.com/meshcloud/meshstack-cli/client/types/xurl"
"github.com/meshcloud/meshstack-cli/cmd/internal"
"github.com/meshcloud/meshstack-cli/internal/apidocs"
"github.com/meshcloud/meshstack-cli/internal/meshstack"
"github.com/meshcloud/meshstack-cli/internal/setting"
)

const (
Expand Down Expand Up @@ -81,13 +84,19 @@ func (f *requestFlags) parse(cmd *cobra.Command, args []string) (request, error)
return r, nil
}

func (r *request) cutTo(endpoint xurl.URL) error {
path, held := endpoint.PathTo(r.target)
if !held {
return fmt.Errorf("%s is not under endpoint %s", r.target.Redacted(), endpoint)
func (r *request) origin() string {
return r.target.Scheme + "://" + r.target.Host
}

func (r *request) originAsEndpoint() setting.Sources {
if !r.target.IsAbs() {
return nil
}
r.target = &url.URL{Path: path, RawQuery: r.target.RawQuery}
return nil
return setting.Sources{setting.LookupSource{
MatchingKey: meshstack.EndpointSetting.EnvKey(),
Description: "the URL " + r.target.Redacted(),
Func: func(context.Context) (string, error) { return r.origin(), nil },
}}
}

func (f *requestFlags) selector(r request) (openapi.Selector, error) {
Expand Down
4 changes: 1 addition & 3 deletions cmd/auth/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,7 @@ func TestProfileSelection(t *testing.T) {
var asked bytes.Buffer

source := newProfileSelectionSource(prompt.New(strings.NewReader(tt.answers), &asked), tt.stdin)
ctx := profile.SetSelectionInContext(t.Context(), func() (profile.Selection, error) {
return tt.selection, nil
})
ctx := profile.SetSelectionInContext(t.Context(), tt.selection)

selected, err := source.Lookup(ctx, setting.Profile.EnvKey())

Expand Down
Loading
Loading