| 70 | } |
| 71 | |
| 72 | func getRun(opts *GetOptions) error { |
| 73 | c, err := opts.HttpClient() |
| 74 | if err != nil { |
| 75 | return fmt.Errorf("could not create http client: %w", err) |
| 76 | } |
| 77 | client := api.NewClientFromHTTP(c) |
| 78 | |
| 79 | orgName := opts.OrgName |
| 80 | envName := opts.EnvName |
| 81 | |
| 82 | variableEntity, err := shared.GetVariableEntity(orgName, envName) |
| 83 | if err != nil { |
| 84 | return err |
| 85 | } |
| 86 | |
| 87 | var baseRepo ghrepo.Interface |
| 88 | if variableEntity == shared.Repository || variableEntity == shared.Environment { |
| 89 | baseRepo, err = opts.BaseRepo() |
| 90 | if err != nil { |
| 91 | return err |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | cfg, err := opts.Config() |
| 96 | if err != nil { |
| 97 | return err |
| 98 | } |
| 99 | |
| 100 | var path string |
| 101 | var host string |
| 102 | switch variableEntity { |
| 103 | case shared.Organization: |
| 104 | path = fmt.Sprintf("orgs/%s/actions/variables/%s", orgName, opts.VariableName) |
| 105 | host, _ = cfg.Authentication().DefaultHost() |
| 106 | case shared.Environment: |
| 107 | path = fmt.Sprintf("repos/%s/environments/%s/variables/%s", ghrepo.FullName(baseRepo), envName, opts.VariableName) |
| 108 | host = baseRepo.RepoHost() |
| 109 | case shared.Repository: |
| 110 | path = fmt.Sprintf("repos/%s/actions/variables/%s", ghrepo.FullName(baseRepo), opts.VariableName) |
| 111 | host = baseRepo.RepoHost() |
| 112 | } |
| 113 | |
| 114 | var variable shared.Variable |
| 115 | if err = client.REST(host, "GET", path, nil, &variable); err != nil { |
| 116 | var httpErr api.HTTPError |
| 117 | if errors.As(err, &httpErr) && httpErr.StatusCode == http.StatusNotFound { |
| 118 | return fmt.Errorf("variable %s was not found", opts.VariableName) |
| 119 | } |
| 120 | |
| 121 | return fmt.Errorf("failed to get variable %s: %w", opts.VariableName, err) |
| 122 | } |
| 123 | |
| 124 | if opts.Exporter != nil { |
| 125 | if err := shared.PopulateSelectedRepositoryInformation(client, host, &variable); err != nil { |
| 126 | return err |
| 127 | } |
| 128 | return opts.Exporter.Write(opts.IO, &variable) |
| 129 | } |