Shorter CLI name for faster typing. Renames the binary, build targets, goreleaser config, command examples, and documentation. Signed-off-by: Bryan Frimin <bryan@getprobo.com>
188 lines
4.1 KiB
Go
188 lines
4.1 KiB
Go
// Copyright (c) 2026 Probo Inc <hello@getprobo.com>.
|
|
//
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
// copyright notice and this permission notice appear in all copies.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
// PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
package list
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"github.com/spf13/cobra"
|
|
"go.probo.inc/probo/pkg/cli/api"
|
|
"go.probo.inc/probo/pkg/cmd/cmdutil"
|
|
)
|
|
|
|
const listQuery = `
|
|
query($first: Int, $after: CursorKey, $orderBy: ProfileOrder, $filter: ProfileFilter) {
|
|
viewer {
|
|
profiles(first: $first, after: $after, orderBy: $orderBy, filter: $filter) {
|
|
totalCount
|
|
edges {
|
|
node {
|
|
id
|
|
state
|
|
organization {
|
|
id
|
|
name
|
|
}
|
|
membership {
|
|
role
|
|
}
|
|
}
|
|
}
|
|
pageInfo {
|
|
hasNextPage
|
|
endCursor
|
|
}
|
|
}
|
|
}
|
|
}
|
|
`
|
|
|
|
type (
|
|
organization struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
membership struct {
|
|
Role string `json:"role"`
|
|
}
|
|
|
|
profile struct {
|
|
ID string `json:"id"`
|
|
State string `json:"state"`
|
|
Organization *organization `json:"organization"`
|
|
Membership *membership `json:"membership"`
|
|
}
|
|
)
|
|
|
|
func NewCmdList(f *cmdutil.Factory) *cobra.Command {
|
|
var (
|
|
flagLimit int
|
|
flagOutput *string
|
|
)
|
|
|
|
cmd := &cobra.Command{
|
|
Use: "list",
|
|
Short: "List organizations you have access to",
|
|
Aliases: []string{"ls"},
|
|
Example: ` # List all organizations
|
|
prb org list
|
|
|
|
# Output as JSON
|
|
prb org ls --json`,
|
|
Args: cobra.NoArgs,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
if err := cmdutil.ValidateOutputFlag(flagOutput); err != nil {
|
|
return err
|
|
}
|
|
|
|
cfg, err := f.Config()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
host, hc, err := cfg.DefaultHost()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
client := api.NewClient(
|
|
host,
|
|
hc.Token,
|
|
"/api/connect/v1/graphql",
|
|
cfg.HTTPTimeoutDuration(),
|
|
)
|
|
|
|
variables := map[string]any{
|
|
"filter": map[string]any{
|
|
"state": "ACTIVE",
|
|
},
|
|
}
|
|
|
|
profiles, totalCount, err := api.Paginate(
|
|
client,
|
|
listQuery,
|
|
variables,
|
|
flagLimit,
|
|
func(data json.RawMessage) (*api.Connection[profile], error) {
|
|
var resp struct {
|
|
Viewer struct {
|
|
Profiles api.Connection[profile] `json:"profiles"`
|
|
} `json:"viewer"`
|
|
}
|
|
if err := json.Unmarshal(data, &resp); err != nil {
|
|
return nil, err
|
|
}
|
|
return &resp.Viewer.Profiles, nil
|
|
},
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if *flagOutput == cmdutil.OutputJSON {
|
|
return cmdutil.PrintJSON(f.IOStreams.Out, profiles)
|
|
}
|
|
|
|
if len(profiles) == 0 {
|
|
_, _ = fmt.Fprintln(f.IOStreams.Out, "No organizations found.")
|
|
return nil
|
|
}
|
|
|
|
rows := make([][]string, 0, len(profiles))
|
|
for _, p := range profiles {
|
|
orgID := ""
|
|
orgName := ""
|
|
if p.Organization != nil {
|
|
orgID = p.Organization.ID
|
|
orgName = p.Organization.Name
|
|
}
|
|
|
|
role := ""
|
|
if p.Membership != nil {
|
|
role = p.Membership.Role
|
|
}
|
|
|
|
rows = append(rows, []string{
|
|
orgID,
|
|
orgName,
|
|
role,
|
|
})
|
|
}
|
|
|
|
t := cmdutil.NewTable("ID", "NAME", "ROLE").Rows(rows...)
|
|
|
|
_, _ = fmt.Fprintln(f.IOStreams.Out, t)
|
|
|
|
if totalCount > len(profiles) {
|
|
_, _ = fmt.Fprintf(
|
|
f.IOStreams.ErrOut,
|
|
"\nShowing %d of %d organizations\n",
|
|
len(profiles),
|
|
totalCount,
|
|
)
|
|
}
|
|
|
|
return nil
|
|
},
|
|
}
|
|
|
|
cmd.Flags().IntVarP(&flagLimit, "limit", "L", 30, "Maximum number of organizations to list")
|
|
flagOutput = cmdutil.AddOutputFlag(cmd)
|
|
|
|
return cmd
|
|
}
|