MCPcopy Create free account
hub / github.com/cli/cli / listRun

Function listRun

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

Source from the content-addressed store, hash-verified

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