Skip to content

Commit

Permalink
refactor: rename variable to improve readability of the code, make th…
Browse files Browse the repository at this point in the history
…e download fail if token not provided and classic or settings api should be downloaded
  • Loading branch information
tomazpu committed Oct 17, 2024
1 parent 5c5a01c commit 8ea9973
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions cmd/monaco/download/download_configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,28 +62,28 @@ type auth struct {

func (a auth) mapToAuth() (*manifest.Auth, []error) {
errs := make([]error, 0)
retVal := manifest.Auth{}
mAuth := manifest.Auth{}

if v, err := readEnvVariable(a.token); err != nil {
if token, err := readEnvVariable(a.token); err != nil {
errs = append(errs, err)
} else {
retVal.Token = &v
mAuth.Token = &token
}

if a.clientID != "" && a.clientSecret != "" {
retVal.OAuth = &manifest.OAuth{}
if v, err := readEnvVariable(a.clientID); err != nil {
mAuth.OAuth = &manifest.OAuth{}
if clientId, err := readEnvVariable(a.clientID); err != nil {
errs = append(errs, err)
} else {
retVal.OAuth.ClientID = v
mAuth.OAuth.ClientID = clientId
}
if v, err := readEnvVariable(a.clientSecret); err != nil {
if clientSecret, err := readEnvVariable(a.clientSecret); err != nil {
errs = append(errs, err)
} else {
retVal.OAuth.ClientSecret = v
mAuth.OAuth.ClientSecret = clientSecret
}
}
return &retVal, errs
return &mAuth, errs
}

func readEnvVariable(envVar string) (manifest.AuthSecret, error) {
Expand Down Expand Up @@ -246,15 +246,21 @@ var defaultDownloadFn = downloadFn{

func downloadConfigs(clientSet *client.ClientSet, apisToDownload api.APIs, opts downloadConfigsOptions, fn downloadFn) (project.ConfigsPerType, error) {
configs := make(project.ConfigsPerType)
if shouldDownloadConfigs(opts) && opts.auth.Token != nil {
if shouldDownloadConfigs(opts) {
if opts.auth.Token == nil {
return nil, errors.New("classic client config requires token")
}
classicCfgs, err := fn.classicDownload(clientSet.Classic(), opts.projectName, prepareAPIs(apisToDownload, opts), classic.ApiContentFilters)
if err != nil {
return nil, err
}
copyConfigs(configs, classicCfgs)
}

if shouldDownloadSettings(opts) && opts.auth.Token != nil {
if shouldDownloadSettings(opts) {
if opts.auth.Token == nil {
return nil, errors.New("settings client config requires token")
}
log.Info("Downloading settings objects")
settingCfgs, err := fn.settingsDownload(clientSet.Settings(), opts.projectName, settings.DefaultSettingsFilters, makeSettingTypes(opts.specificSchemas)...)
if err != nil {
Expand Down

0 comments on commit 8ea9973

Please sign in to comment.