| 76 | } |
| 77 | |
| 78 | func 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 { |