MCPcopy
hub / github.com/cli/cli / listRun

Function listRun

pkg/cmd/variable/list/list.go:78–177  ·  view source on GitHub ↗
(opts *ListOptions)

Source from the content-addressed store, hash-verified

76}
77
78func listRun(opts *ListOptions) error {
79 client, err := opts.HttpClient()
80 if err != nil {
81 return fmt.Errorf("could not create http client: %w", err)
82 }
83
84 orgName := opts.OrgName
85 envName := opts.EnvName
86
87 var baseRepo ghrepo.Interface
88 if orgName == "" {
89 baseRepo, err = opts.BaseRepo()
90 if err != nil {
91 return err
92 }
93 }
94
95 variableEntity, err := shared.GetVariableEntity(orgName, envName)
96 if err != nil {
97 return err
98 }
99
100 // Since populating the `NumSelectedRepos` field costs further API requests
101 // (one per secret), it's important to avoid extra calls when the output will
102 // not present the field's value. So, we should only populate this field in
103 // these cases:
104 // 1. The command is run in the TTY mode without the `--json <fields>` option.
105 // 2. The command is run with `--json <fields>` option, and `numSelectedRepos`
106 // is among the selected fields. In this case, TTY mode is irrelevant.
107 showSelectedRepoInfo := opts.IO.IsStdoutTTY()
108 if opts.Exporter != nil {
109 // Note that if there's an exporter set, then we don't mind the TTY mode
110 // because we just have to populate the requested fields.
111 showSelectedRepoInfo = slices.Contains(opts.Exporter.Fields(), fieldNumSelectedRepos)
112 }
113
114 var variables []shared.Variable
115 switch variableEntity {
116 case shared.Repository:
117 variables, err = getRepoVariables(client, baseRepo)
118 case shared.Environment:
119 variables, err = getEnvVariables(client, baseRepo, envName)
120 case shared.Organization:
121 var cfg gh.Config
122 var host string
123 cfg, err = opts.Config()
124 if err != nil {
125 return err
126 }
127 host, _ = cfg.Authentication().DefaultHost()
128 variables, err = getOrgVariables(client, host, orgName, showSelectedRepoInfo)
129 }
130
131 if err != nil {
132 return fmt.Errorf("failed to get variables: %w", err)
133 }
134
135 if len(variables) == 0 && opts.Exporter == nil {

Callers 3

Test_listRunFunction · 0.70
NewCmdListFunction · 0.70

Calls 15

AuthenticationMethod · 0.95
GetVariableEntityFunction · 0.92
NewNoResultsErrorFunction · 0.92
NewFunction · 0.92
WithHeaderFunction · 0.92
getRepoVariablesFunction · 0.85
getEnvVariablesFunction · 0.85
getOrgVariablesFunction · 0.85
IsStdoutTTYMethod · 0.80
ContainsMethod · 0.80
StartPagerMethod · 0.80
StopPagerMethod · 0.80

Tested by 2

Test_listRunFunction · 0.56