func CloudAccounts()

in cmd/hub/api/cloudaccount.go [36:120]


func CloudAccounts(selector string, showSecrets, showLogs,
	getCloudCredentials, shFormat, nativeConfigFormat, jsonFormat bool) {

	cloudAccounts, err := cloudAccountsBy(selector, showSecrets)
	if err != nil {
		log.Fatalf("Unable to query for Cloud Account(s): %v", err)
	}
	if getCloudCredentials && (shFormat || nativeConfigFormat) {
		if len(cloudAccounts) == 0 {
			fmt.Print("# No Cloud Accounts\n")
		} else {
			errors := make([]error, 0)
			for i, cloudAccount := range cloudAccounts {
				keys, err := cloudAccountCredentials(cloudAccount.Id, cloudAccount.Kind)
				if err != nil {
					errors = append(errors, err)
				} else {
					sh := ""
					if shFormat {
						sh, err = formatCloudAccountCredentialsSh(keys)
						if err != nil {
							errors = append(errors, err)
						}
					}
					nativeConfig := ""
					if nativeConfigFormat {
						nativeConfig, err = formatCloudAccountCredentialsNativeConfig(&cloudAccount, keys)
						if err != nil {
							errors = append(errors, err)
						}
					}
					if i > 0 {
						fmt.Print("\n")
					}
					header := ""
					if len(cloudAccounts) > 1 {
						header = fmt.Sprintf("# %s\n", cloudAccount.BaseDomain)
					}
					if sh != "" || nativeConfig != "" {
						fmt.Printf("%s%s%s", header, sh, nativeConfig)
					}
				}
			}
			if len(errors) > 0 {
				fmt.Print("# Errors encountered:\n")
				for _, err := range errors {
					fmt.Printf("#\t%s\n", strings.ReplaceAll(err.Error(), "\n", "\n#\t"))
				}
			}
		}
	} else if len(cloudAccounts) == 0 {
		if jsonFormat {
			log.Print("No Cloud Accounts")
		} else {
			fmt.Print("No Cloud Accounts\n")
		}
	} else {
		if jsonFormat {
			var toMarshal interface{}
			if len(cloudAccounts) == 1 {
				toMarshal = &cloudAccounts[0]
			} else {
				toMarshal = cloudAccounts
			}
			out, err := json.MarshalIndent(toMarshal, "", "  ")
			if err != nil {
				log.Fatalf("Error marshalling JSON response for output: %v", err)
			}
			os.Stdout.Write(out)
			os.Stdout.Write([]byte("\n"))
		} else {
			fmt.Print("Cloud Accounts:\n")
			errors := make([]error, 0)
			for _, cloudAccount := range cloudAccounts {
				errors = formatCloudAccountEntity(&cloudAccount, getCloudCredentials, showSecrets, showLogs, errors)
			}
			if len(errors) > 0 {
				fmt.Print("Errors encountered:\n")
				for _, err := range errors {
					fmt.Printf("\t%v\n", err)
				}
			}
		}
	}
}