-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,16 @@ | ||
package org | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewOrgCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "org <command>", | ||
Short: "Manage Pinecone orgs", | ||
} | ||
|
||
cmd.AddCommand(NewListOrgsCmd()) | ||
|
||
return cmd | ||
} |
This file contains 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,58 @@ | ||
package org | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
"text/tabwriter" | ||
|
||
"github.com/pinecone-io/cli/internal/pkg/dashboard" | ||
"github.com/pinecone-io/cli/internal/pkg/utils/configuration/secrets" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/pinecone-io/cli/internal/pkg/utils/text" | ||
) | ||
|
||
type ListOrgCmdOptions struct { | ||
json bool | ||
} | ||
|
||
func NewListOrgsCmd() *cobra.Command { | ||
options := ListOrgCmdOptions{} | ||
|
||
cmd := &cobra.Command{ | ||
Use: "list <command>", | ||
Short: "list organizations", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
orgs, err := dashboard.GetOrganizations(secrets.AccessToken.Get()) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
if options.json { | ||
text.PrettyPrintJSON(orgs) | ||
return | ||
} | ||
|
||
printTable(orgs.Organizations) | ||
}, | ||
} | ||
|
||
cmd.Flags().BoolVar(&options.json, "json", false, "output as JSON") | ||
|
||
return cmd | ||
} | ||
|
||
func printTable(orgs []dashboard.Organization) { | ||
writer := tabwriter.NewWriter(os.Stdout, 10, 1, 3, ' ', 0) | ||
|
||
columns := []string{"ID", "NAME", "PROJECTS"} | ||
header := strings.Join(columns, "\t") + "\n" | ||
fmt.Fprint(writer, header) | ||
|
||
for _, org := range orgs { | ||
values := []string{org.Id, org.Name, fmt.Sprintf("%d", len(org.Projects))} | ||
fmt.Fprintf(writer, strings.Join(values, "\t")+"\n") | ||
} | ||
writer.Flush() | ||
} |
This file contains 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 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,62 @@ | ||
package dashboard | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
type OrganizationsResponse struct { | ||
Organizations []Organization `json:"organizations"` | ||
} | ||
|
||
type Organization struct { | ||
Id string `json:"id"` | ||
Name string `json:"name"` | ||
Projects []Project `json:"projects"` | ||
} | ||
|
||
type Project struct { | ||
Id string `json:"id"` | ||
Name string `json:"name"` | ||
GlobalProject GlobalProject `json:"globalProject"` | ||
} | ||
|
||
type GlobalProject struct { | ||
Id string `json:"id"` | ||
Name string `json:"name"` | ||
} | ||
|
||
func GetOrganizations(accessToken string) (*OrganizationsResponse, error) { | ||
url := "https://console-api.pinecone.io/v2/dashboard/organizations" | ||
req, err := http.NewRequest("GET", url, nil) | ||
if err != nil { | ||
fmt.Println("Error creating request:", err) | ||
return nil, err | ||
} | ||
|
||
// Add headers to the request | ||
req.Header.Add("Authorization", "Bearer "+accessToken) | ||
req.Header.Add("User-Agent", "Pinecone CLI") | ||
req.Header.Set("Content-Type", "application/json") | ||
|
||
client := &http.Client{} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
fmt.Println("Error sending request:", err) | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return nil, fmt.Errorf("received non-200 response status: %d %s", resp.StatusCode, resp.Status) | ||
} | ||
|
||
var OrganizationsResponse OrganizationsResponse | ||
err = json.NewDecoder(resp.Body).Decode(&OrganizationsResponse) | ||
if err != nil { | ||
return nil, fmt.Errorf("error decoding JSON: %v", err) | ||
} | ||
|
||
return &OrganizationsResponse, nil | ||
} |