1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
package list
import (
"encoding/json"
"fmt"
"github.com/MakeNowJust/heredoc/v2"
"github.com/spf13/cobra"
gitlab "gitlab.com/gitlab-org/api/client-go"
"gitlab.com/gitlab-org/cli/api"
"gitlab.com/gitlab-org/cli/commands/cmdutils"
"gitlab.com/gitlab-org/cli/commands/flag"
"gitlab.com/gitlab-org/cli/internal/glrepo"
"gitlab.com/gitlab-org/cli/pkg/iostreams"
"gitlab.com/gitlab-org/cli/pkg/tableprinter"
)
type ListOpts struct {
HTTPClient func() (*gitlab.Client, error)
IO *iostreams.IOStreams
BaseRepo func() (glrepo.Interface, error)
ValueSet bool
Group string
OutputFormat string
}
func NewCmdList(f *cmdutils.Factory, runE func(opts *ListOpts) error) *cobra.Command {
opts := &ListOpts{
IO: f.IO,
}
cmd := &cobra.Command{
Use: "list",
Short: "List variables for a project or group.",
Aliases: []string{"ls"},
Args: cobra.ExactArgs(0),
Example: heredoc.Doc(
`
glab variable list
`,
),
RunE: func(cmd *cobra.Command, args []string) (err error) {
// Supports repo override
opts.HTTPClient = f.HttpClient
opts.BaseRepo = f.BaseRepo
group, err := flag.GroupOverride(cmd)
if err != nil {
return err
}
opts.Group = group
if runE != nil {
err = runE(opts)
return
}
err = listRun(opts)
return
},
}
cmdutils.EnableRepoOverride(cmd, f)
cmd.PersistentFlags().StringP(
"group",
"g",
"",
"Select a group or subgroup. Ignored if a repository argument is set.",
)
cmd.Flags().StringVarP(&opts.OutputFormat, "output", "F", "text", "Format output as: text, json.")
return cmd
}
func listRun(opts *ListOpts) error {
color := opts.IO.Color()
httpClient, err := opts.HTTPClient()
if err != nil {
return err
}
table := tableprinter.NewTablePrinter()
table.AddRow("KEY", "PROTECTED", "MASKED", "EXPANDED", "SCOPE", "DESCRIPTION")
if opts.Group != "" {
opts.IO.Logf("Listing variables for the %s group:\n\n", color.Bold(opts.Group))
createVarOpts := &gitlab.ListGroupVariablesOptions{}
variables, err := api.ListGroupVariables(httpClient, opts.Group, createVarOpts)
if err != nil {
return err
}
if opts.OutputFormat == "json" {
varListJSON, _ := json.Marshal(variables)
fmt.Fprintln(opts.IO.StdOut, string(varListJSON))
} else {
for _, variable := range variables {
table.AddRow(variable.Key, variable.Protected, variable.Masked, !variable.Raw, variable.EnvironmentScope, variable.Description)
}
}
} else {
repo, err := opts.BaseRepo()
if err != nil {
return err
}
opts.IO.Logf("Listing variables for the %s project:\n\n", color.Bold(repo.FullName()))
createVarOpts := &gitlab.ListProjectVariablesOptions{}
variables, err := api.ListProjectVariables(httpClient, repo.FullName(), createVarOpts)
if err != nil {
return err
}
if opts.OutputFormat == "json" {
varListJSON, _ := json.Marshal(variables)
fmt.Fprintln(opts.IO.StdOut, string(varListJSON))
} else {
for _, variable := range variables {
table.AddRow(variable.Key, variable.Protected, variable.Masked, !variable.Raw, variable.EnvironmentScope, variable.Description)
}
}
}
if opts.OutputFormat != "json" {
fmt.Fprint(opts.IO.StdOut, table.String())
}
return nil
}
|