()
| 171 | } |
| 172 | |
| 173 | func (h *Helper) Client() (*client.Client, error) { |
| 174 | // The admin token and URL may have changed (e.g. if the user did a separate login or env switch). |
| 175 | // Reload the admin config from disk to get the latest values. |
| 176 | err := h.ReloadAdminConfig() |
| 177 | if err != nil { |
| 178 | return nil, err |
| 179 | } |
| 180 | |
| 181 | // Compute and cache a hash of the admin config values to detect changes. |
| 182 | // If the hash has changed, we should close the existing client. |
| 183 | hash := hashStr(h.AdminToken(), h.AdminURL()) |
| 184 | if h.adminClient != nil && h.adminClientHash != hash { |
| 185 | _ = h.adminClient.Close() |
| 186 | h.adminClient = nil |
| 187 | h.adminClientHash = hash |
| 188 | } |
| 189 | h.adminClientHash = hash |
| 190 | |
| 191 | // Make a new client if we don't have one. |
| 192 | if h.adminClient == nil { |
| 193 | cliVersion := h.Version.Number |
| 194 | if cliVersion == "" { |
| 195 | cliVersion = "unknown" |
| 196 | } |
| 197 | |
| 198 | userAgent := fmt.Sprintf("rill-cli/%v", cliVersion) |
| 199 | c, err := client.New(h.AdminURL(), h.AdminToken(), userAgent) |
| 200 | if err != nil { |
| 201 | return nil, err |
| 202 | } |
| 203 | |
| 204 | h.adminClient = c |
| 205 | } |
| 206 | |
| 207 | return h.adminClient, nil |
| 208 | } |
| 209 | |
| 210 | // Telemetry returns a client for recording events. |
| 211 | // Note: It should only be used for parts of the CLI that run on users' local computer because: |
no test coverage detected