Commit 17584dfd authored by Timo Furrer's avatar Timo Furrer
Browse files

feat(runner-controller): add runner controller scope list command

parent 3e599c1e
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -38,5 +38,6 @@ rc
- [`create`](create.md)
- [`delete`](delete.md)
- [`list`](list.md)
- [`scope`](scope/_index.md)
- [`token`](token/_index.md)
- [`update`](update.md)
+32 −0
Original line number Diff line number Diff line
---
title: glab runner-controller scope
stage: Create
group: Code Review
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://handbook.gitlab.com/handbook/product/ux/technical-writing/#assignments
---

<!--
This documentation is auto generated by a script.
Please do not edit this file directly. Run `make gen-docs` instead.
-->

Manage runner controller scopes. (EXPERIMENTAL)

## Synopsis

Manages runner controller scopes. This is an admin-only feature.

This feature is an experiment and is not ready for production use.
It might be unstable or removed at any time.
For more information, see
[https://docs.gitlab.com/policy/development_stages_support/](https://docs.gitlab.com/policy/development_stages_support/).

## Options inherited from parent commands

```plaintext
  -h, --help   Show help for this command.
```

## Subcommands

- [`list`](list.md)
+40 −0
Original line number Diff line number Diff line
---
title: glab runner-controller scope list
stage: Create
group: Code Review
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://handbook.gitlab.com/handbook/product/ux/technical-writing/#assignments
---

<!--
This documentation is auto generated by a script.
Please do not edit this file directly. Run `make gen-docs` instead.
-->

List scopes for a runner controller. (EXPERIMENTAL)

```plaintext
glab runner-controller scope list <controller-id> [flags]
```

## Examples

```console
# List all scopes for runner controller 42
$ glab runner-controller scope list 42

# List scopes as JSON
$ glab runner-controller scope list 42 --output json

```

## Options

```plaintext
  -F, --output string   Format output as: text, json. (default "text")
```

## Options inherited from parent commands

```plaintext
  -h, --help   Show help for this command.
```
+2 −0
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ import (
	createCmd "gitlab.com/gitlab-org/cli/internal/commands/runnercontroller/create"
	deleteCmd "gitlab.com/gitlab-org/cli/internal/commands/runnercontroller/delete"
	listCmd "gitlab.com/gitlab-org/cli/internal/commands/runnercontroller/list"
	scopeCmd "gitlab.com/gitlab-org/cli/internal/commands/runnercontroller/scope"
	tokenCmd "gitlab.com/gitlab-org/cli/internal/commands/runnercontroller/token"
	updateCmd "gitlab.com/gitlab-org/cli/internal/commands/runnercontroller/update"
	"gitlab.com/gitlab-org/cli/internal/text"
@@ -23,6 +24,7 @@ func NewCmd(f cmdutils.Factory) *cobra.Command {
	cmd.AddCommand(createCmd.NewCmd(f))
	cmd.AddCommand(deleteCmd.NewCmd(f))
	cmd.AddCommand(listCmd.NewCmd(f))
	cmd.AddCommand(scopeCmd.NewCmd(f))
	cmd.AddCommand(tokenCmd.NewCmd(f))
	cmd.AddCommand(updateCmd.NewCmd(f))
	return cmd
+105 −0
Original line number Diff line number Diff line
package list

import (
	"context"
	"fmt"
	"strconv"

	"github.com/MakeNowJust/heredoc/v2"
	"github.com/spf13/cobra"

	gitlab "gitlab.com/gitlab-org/api/client-go"

	"gitlab.com/gitlab-org/cli/internal/api"
	"gitlab.com/gitlab-org/cli/internal/cmdutils"
	"gitlab.com/gitlab-org/cli/internal/iostreams"
	"gitlab.com/gitlab-org/cli/internal/mcpannotations"
	"gitlab.com/gitlab-org/cli/internal/tableprinter"
)

type options struct {
	io        *iostreams.IOStreams
	apiClient func(repoHost string) (*api.Client, error)

	controllerID int64
	outputFormat string
}

func NewCmd(f cmdutils.Factory) *cobra.Command {
	opts := &options{
		io:        f.IO(),
		apiClient: f.ApiClient,
	}

	cmd := &cobra.Command{
		Use:   "list <controller-id> [flags]",
		Short: `List scopes for a runner controller. (EXPERIMENTAL)`,
		Args:  cobra.ExactArgs(1),
		Example: heredoc.Doc(`
			# List all scopes for runner controller 42
			$ glab runner-controller scope list 42

			# List scopes as JSON
			$ glab runner-controller scope list 42 --output json
		`),
		Annotations: map[string]string{
			mcpannotations.Safe: "true",
		},
		RunE: func(cmd *cobra.Command, args []string) error {
			if err := opts.complete(args); err != nil {
				return err
			}
			return opts.run(cmd.Context())
		},
	}

	cmd.Flags().VarP(cmdutils.NewEnumValue([]string{"text", "json"}, "text", &opts.outputFormat), "output", "F", "Format output as: text, json.")

	return cmd
}

func (o *options) complete(args []string) error {
	id, err := strconv.ParseInt(args[0], 10, 64)
	if err != nil {
		return cmdutils.WrapError(err, "invalid runner controller ID")
	}
	o.controllerID = id
	return nil
}

func (o *options) run(ctx context.Context) error {
	apiClient, err := o.apiClient("")
	if err != nil {
		return err
	}
	client := apiClient.Lab()

	scopes, _, err := client.RunnerControllerScopes.ListRunnerControllerScopes(o.controllerID, gitlab.WithContext(ctx))
	if err != nil {
		return cmdutils.WrapError(err, "failed to list runner controller scopes")
	}

	switch o.outputFormat {
	case "json":
		return o.io.PrintJSON(scopes)
	default:
		return o.printTable(scopes)
	}
}

func (o *options) printTable(scopes *gitlab.RunnerControllerScopes) error {
	c := o.io.Color()

	if len(scopes.InstanceLevelScopings) == 0 {
		o.io.LogInfof("No scopes configured for runner controller %d\n", o.controllerID)
		return nil
	}

	table := tableprinter.NewTablePrinter()
	table.AddRow(c.Bold("Scope Type"), c.Bold("Created At"), c.Bold("Updated At"))
	for _, s := range scopes.InstanceLevelScopings {
		table.AddRow("instance", s.CreatedAt, s.UpdatedAt)
	}
	fmt.Fprint(o.io.StdOut, table.Render())
	return nil
}
Loading