Skip to content

Commit

Permalink
List orgs
Browse files Browse the repository at this point in the history
  • Loading branch information
jhamon committed Apr 26, 2024
1 parent db047d2 commit 1bcdfcf
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 0 deletions.
16 changes: 16 additions & 0 deletions internal/pkg/cli/command/org/cmd.go
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
}
58 changes: 58 additions & 0 deletions internal/pkg/cli/command/org/list.go
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()
}
2 changes: 2 additions & 0 deletions internal/pkg/cli/command/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
collection "github.com/pinecone-io/cli/internal/pkg/cli/command/collection"
configCmd "github.com/pinecone-io/cli/internal/pkg/cli/command/config"
index "github.com/pinecone-io/cli/internal/pkg/cli/command/index"
org "github.com/pinecone-io/cli/internal/pkg/cli/command/org"
version "github.com/pinecone-io/cli/internal/pkg/cli/command/version"
"github.com/pinecone-io/cli/internal/pkg/utils/configuration/config"
"github.com/pinecone-io/cli/internal/pkg/utils/configuration/secrets"
Expand Down Expand Up @@ -40,4 +41,5 @@ func init() {
rootCmd.AddCommand(collection.NewCollectionCmd())
rootCmd.AddCommand(configCmd.NewConfigCmd())
rootCmd.AddCommand(version.NewVersionCmd())
rootCmd.AddCommand(org.NewOrgCmd())
}
62 changes: 62 additions & 0 deletions internal/pkg/dashboard/organizations.go
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
}

0 comments on commit 1bcdfcf

Please sign in to comment.