Skip to content
Open
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
16 changes: 7 additions & 9 deletions cmd/src/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,13 @@ Examples:
}

commanders := map[string]*commander{
"": &commands,
"batch": &batchCommands,
"config": &configCommands,
"extsvc": &extsvcCommands,
"code-intel": &codeintelCommands,
"orgs": &orgsCommands,
"orgs members": &orgsMembersCommands,
"repos": &reposCommands,
"users": &usersCommands,
"": &commands,
"batch": &batchCommands,
"config": &configCommands,
"extsvc": &extsvcCommands,
"code-intel": &codeintelCommands,
"repos": &reposCommands,
"users": &usersCommands,
}

pending := out.Pending(output.Line("", output.StylePending, "Rendering Markdown..."))
Expand Down
42 changes: 19 additions & 23 deletions cmd/src/orgs.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
package main

import (
"flag"
"fmt"
"github.com/sourcegraph/src-cli/internal/clicompat"
"github.com/urfave/cli/v3"
)

var orgsCommands commander

func init() {
usage := `'src orgs' is a tool that manages organizations on a Sourcegraph instance.
var orgsCommand = clicompat.Wrap(&cli.Command{
Name: "orgs",
Aliases: []string{"org"},
Usage: "manages organizations",
UsageText: "src orgs [command options]",
Description: orgsExamples,
HideVersion: true,
Commands: []*cli.Command{
orgsListCommand,
orgsGetCommand,
orgsCreateCommand,
orgsDeleteCommand,
orgsMembersCommand,
},
})

const orgsExamples = `'src orgs' is a tool that manages organizations on a Sourcegraph instance.

Usage:

Expand All @@ -25,23 +38,6 @@ The commands are:
Use "src orgs [command] -h" for more information about a command.
`

flagSet := flag.NewFlagSet("orgs", flag.ExitOnError)
handler := func(args []string) error {
orgsCommands.run(flagSet, "src orgs", usage, args)
return nil
}

// Register the command.
commands = append(commands, &command{
flagSet: flagSet,
aliases: []string{"org"},
handler: handler,
usageFunc: func() {
fmt.Println(usage)
},
})
}

const orgFragment = `
fragment OrgFields on Org {
id
Expand Down
65 changes: 30 additions & 35 deletions cmd/src/orgs_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package main

import (
"context"
"flag"
"fmt"

"github.com/sourcegraph/src-cli/internal/api"
"github.com/sourcegraph/src-cli/internal/clicompat"
"github.com/urfave/cli/v3"
)

func init() {
usage := `
const orgsCreateExamples = `
Examples:

Create an organization:
Expand All @@ -18,24 +17,27 @@ Examples:

`

flagSet := flag.NewFlagSet("create", flag.ExitOnError)
usageFunc := func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'src orgs %s':\n", flagSet.Name())
flagSet.PrintDefaults()
fmt.Println(usage)
}
var (
nameFlag = flagSet.String("name", "", `The new organization's name. (required)`)
displayNameFlag = flagSet.String("display-name", "", `The new organization's display name. Defaults to organization name if unspecified.`)
apiFlags = api.NewFlags(flagSet)
)
var orgsCreateCommand = clicompat.Wrap(&cli.Command{
Name: "create",
Usage: "creates an organization",
UsageText: "src orgs create [options]",
Description: orgsCreateExamples,
HideVersion: true,
Flags: clicompat.WithAPIFlags(
&cli.StringFlag{
Name: "name",
Usage: "The new organization's name. (required)",
},
&cli.StringFlag{
Name: "display-name",
Usage: "The new organization's display name. Defaults to organization name if unspecified.",
},
),
Action: func(ctx context.Context, cmd *cli.Command) error {
name := cmd.String("name")
displayName := cmd.String("display-name")

handler := func(args []string) error {
if err := flagSet.Parse(args); err != nil {
return err
}

client := cfg.apiClient(apiFlags, flagSet.Output())
client := cfg.apiClient(clicompat.APIFlagsFromCmd(cmd), cmd.Writer)

query := `mutation CreateOrg(
$name: String!,
Expand All @@ -53,20 +55,13 @@ Examples:
CreateOrg Org
}
if ok, err := client.NewRequest(query, map[string]any{
"name": *nameFlag,
"displayName": *displayNameFlag,
}).Do(context.Background(), &result); err != nil || !ok {
"name": name,
"displayName": displayName,
}).Do(ctx, &result); err != nil || !ok {
return err
}

fmt.Printf("Organization %q created.\n", *nameFlag)
return nil
}

// Register the command.
orgsCommands = append(orgsCommands, &command{
flagSet: flagSet,
handler: handler,
usageFunc: usageFunc,
})
}
_, err := fmt.Fprintf(cmd.Writer, "Organization %q created.\n", name)
return err
},
})
58 changes: 24 additions & 34 deletions cmd/src/orgs_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package main

import (
"context"
"flag"
"fmt"

"github.com/sourcegraph/src-cli/internal/api"
"github.com/sourcegraph/src-cli/internal/clicompat"
"github.com/urfave/cli/v3"
)

func init() {
usage := `
const orgsDeleteExamples = `
Examples:

Delete an organization by ID:
Expand All @@ -26,23 +25,21 @@ Examples:

`

flagSet := flag.NewFlagSet("delete", flag.ExitOnError)
usageFunc := func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'src orgs %s':\n", flagSet.Name())
flagSet.PrintDefaults()
fmt.Println(usage)
}
var (
orgIDFlag = flagSet.String("id", "", `The ID of the organization to delete.`)
apiFlags = api.NewFlags(flagSet)
)

handler := func(args []string) error {
if err := flagSet.Parse(args); err != nil {
return err
}

client := cfg.apiClient(apiFlags, flagSet.Output())
var orgsDeleteCommand = clicompat.Wrap(&cli.Command{
Name: "delete",
Usage: "deletes an organization",
UsageText: "src orgs delete [options]",
Description: orgsDeleteExamples,
HideVersion: true,
Flags: clicompat.WithAPIFlags(
&cli.StringFlag{
Name: "id",
Usage: "The ID of the organization to delete.",
},
),
Action: func(ctx context.Context, cmd *cli.Command) error {
orgID := cmd.String("id")
client := cfg.apiClient(clicompat.APIFlagsFromCmd(cmd), cmd.Writer)

query := `mutation DeleteOrganization(
$organization: ID!
Expand All @@ -58,19 +55,12 @@ Examples:
DeleteOrganization struct{}
}
if ok, err := client.NewRequest(query, map[string]any{
"organization": *orgIDFlag,
}).Do(context.Background(), &result); err != nil || !ok {
"organization": orgID,
}).Do(ctx, &result); err != nil || !ok {
return err
}

fmt.Printf("Organization with ID %q deleted.\n", *orgIDFlag)
return nil
}

// Register the command.
orgsCommands = append(orgsCommands, &command{
flagSet: flagSet,
handler: handler,
usageFunc: usageFunc,
})
}
_, err := fmt.Fprintf(cmd.Writer, "Organization with ID %q deleted.\n", orgID)
return err
},
})
67 changes: 31 additions & 36 deletions cmd/src/orgs_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ package main

import (
"context"
"flag"
"fmt"

"github.com/sourcegraph/src-cli/internal/api"
"github.com/sourcegraph/src-cli/internal/clicompat"
"github.com/urfave/cli/v3"
)

func init() {
usage := `
const orgsGetExamples = `
Examples:

Get organization named abc-org:
Expand All @@ -22,26 +20,30 @@ Examples:

`

flagSet := flag.NewFlagSet("get", flag.ExitOnError)
usageFunc := func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'src orgs %s':\n", flagSet.Name())
flagSet.PrintDefaults()
fmt.Println(usage)
}
var (
nameFlag = flagSet.String("name", "", `Look up organization by name. (e.g. "abc-org")`)
formatFlag = flagSet.String("f", "{{.|json}}", `Format for the output, using the syntax of Go package text/template. (e.g. "{{.ID}}: {{.Name}} ({{.DisplayName}})")`)
apiFlags = api.NewFlags(flagSet)
)

handler := func(args []string) error {
if err := flagSet.Parse(args); err != nil {
return err
}

client := cfg.apiClient(apiFlags, flagSet.Output())

tmpl, err := parseTemplate(*formatFlag)
var orgsGetCommand = clicompat.Wrap(&cli.Command{
Name: "get",
Usage: "gets an organization",
UsageText: "src orgs get [options]",
Description: orgsGetExamples,
HideVersion: true,
Flags: clicompat.WithAPIFlags(
&cli.StringFlag{
Name: "name",
Usage: `Look up organization by name. (e.g. "abc-org")`,
},
&cli.StringFlag{
Name: "f",
Value: "{{.|json}}",
Usage: `Format for the output, using the syntax of Go package text/template. (e.g. "{{.ID}}: {{.Name}} ({{.DisplayName}})")`,
},
),
Action: func(ctx context.Context, cmd *cli.Command) error {
name := cmd.String("name")
format := cmd.String("f")

client := cfg.apiClient(clicompat.APIFlagsFromCmd(cmd), cmd.Writer)

tmpl, err := parseTemplate(format)
if err != nil {
return err
}
Expand All @@ -60,18 +62,11 @@ Examples:
Organization *Org
}
if ok, err := client.NewRequest(query, map[string]any{
"name": *nameFlag,
}).Do(context.Background(), &result); err != nil || !ok {
"name": name,
}).Do(ctx, &result); err != nil || !ok {
return err
}

return execTemplate(tmpl, result.Organization)
}

// Register the command.
orgsCommands = append(orgsCommands, &command{
flagSet: flagSet,
handler: handler,
usageFunc: usageFunc,
})
}
},
})
Loading
Loading