-
Notifications
You must be signed in to change notification settings - Fork 7
feat(get): add default-org to fetch user default organization #979
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
Merged
+180
−1
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "io" | ||
| "net/http" | ||
| "net/url" | ||
|
|
||
| "github.com/kosli-dev/cli/internal/output" | ||
| "github.com/kosli-dev/cli/internal/requests" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| const getDefaultOrgShortDesc = `Get the default organization for the current user.` | ||
|
|
||
| const getDefaultOrgLongDesc = getDefaultOrgShortDesc + ` | ||
| The default organization is the one selected by default in the Kosli Web UI when you log in.` | ||
|
|
||
| const getDefaultOrgExample = ` | ||
| # get the default organization for the current user: | ||
| kosli get default-org \ | ||
| --api-token yourAPIToken | ||
| ` | ||
|
|
||
| type getDefaultOrgOptions struct { | ||
| output string | ||
| } | ||
|
|
||
| // defaultOrg models the response of GET api/v2/user/default-org. | ||
| type defaultOrg struct { | ||
| DefaultOrgName string `json:"default_org_name"` | ||
| } | ||
|
|
||
| func newGetDefaultOrgCmd(out io.Writer) *cobra.Command { | ||
| o := new(getDefaultOrgOptions) | ||
| cmd := &cobra.Command{ | ||
| Use: "default-org", | ||
| Short: getDefaultOrgShortDesc, | ||
| Long: getDefaultOrgLongDesc, | ||
| Example: getDefaultOrgExample, | ||
| Args: cobra.NoArgs, | ||
| PreRunE: func(cmd *cobra.Command, args []string) error { | ||
| if err := RequireGlobalFlags(global, []string{"ApiToken"}); err != nil { | ||
| return ErrorBeforePrintingUsage(cmd, err.Error()) | ||
| } | ||
| return nil | ||
| }, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| return o.run(out) | ||
| }, | ||
| } | ||
|
|
||
| cmd.Flags().StringVarP(&o.output, "output", "o", "table", outputFlag) | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func (o *getDefaultOrgOptions) run(out io.Writer) error { | ||
| url, err := url.JoinPath(global.Host, "api/v2/user/default-org") | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| reqParams := &requests.RequestParams{ | ||
| Method: http.MethodGet, | ||
| URL: url, | ||
| Token: global.ApiToken, | ||
| } | ||
| response, err := kosliClient.Do(reqParams) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return output.FormattedPrint(response.Body, o.output, out, 0, | ||
| map[string]output.FormatOutputFunc{ | ||
| "table": printDefaultOrgAsTable, | ||
| "json": output.PrintJson, | ||
| }) | ||
| } | ||
|
|
||
| // printDefaultOrgAsTable renders the default organization name. | ||
| func printDefaultOrgAsTable(raw string, out io.Writer, page int) error { | ||
| var org defaultOrg | ||
| if err := json.Unmarshal([]byte(raw), &org); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| name := org.DefaultOrgName | ||
| if name == "" { | ||
| name = "(none set)" | ||
| } | ||
| rows := []string{"Default organization:\t" + name} | ||
| tabFormattedPrint(out, []string{}, rows) | ||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| "github.com/maxcnunes/httpfake" | ||
| "github.com/stretchr/testify/require" | ||
| "github.com/stretchr/testify/suite" | ||
| ) | ||
|
|
||
| func TestPrintDefaultOrgAsTable(t *testing.T) { | ||
| raw := `{"default_org_name":"test-org"}` | ||
|
|
||
| var buf bytes.Buffer | ||
| err := printDefaultOrgAsTable(raw, &buf, 0) | ||
| require.NoError(t, err) | ||
|
|
||
| out := buf.String() | ||
| for _, want := range []string{"Default organization:", "test-org"} { | ||
| require.Contains(t, out, want) | ||
| } | ||
| } | ||
|
|
||
| func TestPrintDefaultOrgAsTableNoneSet(t *testing.T) { | ||
| raw := `{"default_org_name":""}` | ||
|
|
||
| var buf bytes.Buffer | ||
| err := printDefaultOrgAsTable(raw, &buf, 0) | ||
| require.NoError(t, err) | ||
| require.Regexp(t, `Default organization:\s+\(none set\)`, buf.String()) | ||
| } | ||
|
|
||
| type GetDefaultOrgCommandTestSuite struct { | ||
| suite.Suite | ||
| defaultKosliArguments string | ||
| } | ||
|
|
||
| func (suite *GetDefaultOrgCommandTestSuite) SetupTest() { | ||
| global = &GlobalOpts{ | ||
| ApiToken: "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6ImNkNzg4OTg5In0.e8i_lA_QrEhFncb05Xw6E_tkCHU9QfcY4OLTVUCHffY", | ||
| Host: "http://localhost:8001", | ||
| } | ||
| suite.defaultKosliArguments = fmt.Sprintf(" --host %s --api-token %s", global.Host, global.ApiToken) | ||
| } | ||
|
|
||
| func (suite *GetDefaultOrgCommandTestSuite) TestGetDefaultOrgCmd() { | ||
| fake := httpfake.New() | ||
| defer fake.Close() | ||
| fake.NewHandler(). | ||
| Get("/api/v2/user/default-org"). | ||
| Reply(200). | ||
| BodyString(`{"default_org_name":"test-org"}`) | ||
|
|
||
| args := fmt.Sprintf(" --host %s --api-token %s", fake.Server.URL, global.ApiToken) | ||
| tests := []cmdTestCase{ | ||
| { | ||
| wantError: false, | ||
| name: "get default-org prints the org name as a table", | ||
| cmd: "get default-org" + args, | ||
| goldenRegex: `Default organization:\s+test-org`, | ||
| }, | ||
| { | ||
| wantError: false, | ||
| name: "get default-org supports --output json", | ||
| cmd: "get default-org --output json" + args, | ||
| goldenRegex: `(?s)"default_org_name":\s*"test-org"`, | ||
| }, | ||
| { | ||
| wantError: true, | ||
| name: "get default-org fails when an argument is provided", | ||
| cmd: "get default-org extra-arg" + args, | ||
| golden: "Error: unknown command \"extra-arg\" for \"kosli get default-org\"\n", | ||
| }, | ||
| } | ||
|
|
||
| runTestCmd(suite.T(), tests) | ||
| } | ||
|
|
||
| func TestGetDefaultOrgCommandTestSuite(t *testing.T) { | ||
| suite.Run(t, new(GetDefaultOrgCommandTestSuite)) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.