diff --git a/cmd/akamai/command.go b/cmd/akamai/command.go index 09fa0a208..2871b45bc 100644 --- a/cmd/akamai/command.go +++ b/cmd/akamai/command.go @@ -66,8 +66,8 @@ func Create() *cobra.Command { return wrerr } - isValid, catalogApps, err := catalog.ValidateCatalogApps(ctx, cliFlags.InstallCatalogApps) - if !isValid { + catalogApps, err := catalog.ValidateCatalogApps(ctx, cliFlags.InstallCatalogApps) + if err != nil { wrerr := fmt.Errorf("catalog validation failed: %w", err) stepper.FailCurrentStep(wrerr) return wrerr diff --git a/cmd/aws/command.go b/cmd/aws/command.go index 92da66a6a..c7033f88a 100644 --- a/cmd/aws/command.go +++ b/cmd/aws/command.go @@ -98,8 +98,8 @@ func Create() *cobra.Command { return wrerr } - isValid, catalogApps, err := catalog.ValidateCatalogApps(ctx, cliFlags.InstallCatalogApps) - if !isValid { + catalogApps, err := catalog.ValidateCatalogApps(ctx, cliFlags.InstallCatalogApps) + if err != nil { wrerr := fmt.Errorf("invalid catalog apps: %w", err) stepper.FailCurrentStep(wrerr) return wrerr diff --git a/cmd/azure/command.go b/cmd/azure/command.go index 9f5a375ae..5270cee17 100644 --- a/cmd/azure/command.go +++ b/cmd/azure/command.go @@ -69,8 +69,8 @@ func Create() *cobra.Command { return wrerr } - isValid, catalogApps, err := catalog.ValidateCatalogApps(ctx, cliFlags.InstallCatalogApps) - if !isValid { + catalogApps, err := catalog.ValidateCatalogApps(ctx, cliFlags.InstallCatalogApps) + if err != nil { wrerr := fmt.Errorf("invalid catalog apps: %w", err) stepper.FailCurrentStep(wrerr) return wrerr diff --git a/cmd/civo/command.go b/cmd/civo/command.go index ec9204d55..2fe5a7024 100644 --- a/cmd/civo/command.go +++ b/cmd/civo/command.go @@ -74,8 +74,8 @@ func Create() *cobra.Command { stepper.NewProgressStep("Validate Configuration") - isValid, catalogApps, err := catalog.ValidateCatalogApps(ctx, cliFlags.InstallCatalogApps) - if !isValid { + catalogApps, err := catalog.ValidateCatalogApps(ctx, cliFlags.InstallCatalogApps) + if err != nil { wrerr := fmt.Errorf("catalog validation failed: %w", err) stepper.FailCurrentStep(wrerr) return wrerr diff --git a/cmd/digitalocean/command.go b/cmd/digitalocean/command.go index 7d08e33ea..78962b714 100644 --- a/cmd/digitalocean/command.go +++ b/cmd/digitalocean/command.go @@ -69,7 +69,7 @@ func Create() *cobra.Command { return wrerr } - _, catalogApps, err := catalog.ValidateCatalogApps(ctx, cliFlags.InstallCatalogApps) + catalogApps, err := catalog.ValidateCatalogApps(ctx, cliFlags.InstallCatalogApps) if err != nil { wrerr := fmt.Errorf("failed to validate catalog apps: %w", err) stepper.FailCurrentStep(wrerr) diff --git a/cmd/google/command.go b/cmd/google/command.go index 0aeddb8a5..dd4115a15 100644 --- a/cmd/google/command.go +++ b/cmd/google/command.go @@ -70,7 +70,7 @@ func Create() *cobra.Command { return wrerr } - _, catalogApps, err := catalog.ValidateCatalogApps(ctx, cliFlags.InstallCatalogApps) + catalogApps, err := catalog.ValidateCatalogApps(ctx, cliFlags.InstallCatalogApps) if err != nil { wrerr := fmt.Errorf("failed to validate catalog apps: %w", err) stepper.FailCurrentStep(wrerr) diff --git a/cmd/k3d/create.go b/cmd/k3d/create.go index 48d351a85..a73c2fc95 100644 --- a/cmd/k3d/create.go +++ b/cmd/k3d/create.go @@ -73,15 +73,11 @@ func runK3d(cmd *cobra.Command, _ []string) error { utilities.CreateK1ClusterDirectory(cliFlags.ClusterName) utils.DisplayLogHints() - isValid, catalogApps, err := catalog.ValidateCatalogApps(cmd.Context(), cliFlags.InstallCatalogApps) + catalogApps, err := catalog.ValidateCatalogApps(cmd.Context(), cliFlags.InstallCatalogApps) if err != nil { return fmt.Errorf("failed to validate catalog apps: %w", err) } - if !isValid { - return errors.New("catalog apps validation failed") - } - switch cliFlags.GitProvider { case "github": key, err := internalssh.GetHostKey("github.com") diff --git a/cmd/k3s/command.go b/cmd/k3s/command.go index b1964cc58..30161710c 100644 --- a/cmd/k3s/command.go +++ b/cmd/k3s/command.go @@ -65,7 +65,7 @@ func Create() *cobra.Command { return wrerr } - _, catalogApps, err := catalog.ValidateCatalogApps(ctx, cliFlags.InstallCatalogApps) + catalogApps, err := catalog.ValidateCatalogApps(ctx, cliFlags.InstallCatalogApps) if err != nil { wrerr := fmt.Errorf("validation of catalog apps failed: %w", err) stepper.FailCurrentStep(wrerr) diff --git a/cmd/vultr/command.go b/cmd/vultr/command.go index c291b4e58..d4a67dd65 100644 --- a/cmd/vultr/command.go +++ b/cmd/vultr/command.go @@ -69,7 +69,7 @@ func Create() *cobra.Command { return wrerr } - _, catalogApps, err := catalog.ValidateCatalogApps(ctx, cliFlags.InstallCatalogApps) + catalogApps, err := catalog.ValidateCatalogApps(ctx, cliFlags.InstallCatalogApps) if err != nil { wrerr := fmt.Errorf("catalog validation failed: %w", err) stepper.FailCurrentStep(wrerr) diff --git a/internal/catalog/catalog.go b/internal/catalog/catalog.go index f011730da..91627f11f 100644 --- a/internal/catalog/catalog.go +++ b/internal/catalog/catalog.go @@ -17,7 +17,6 @@ import ( apiTypes "github.com/konstructio/kubefirst-api/pkg/types" - "github.com/rs/zerolog/log" "gopkg.in/yaml.v3" ) @@ -36,71 +35,69 @@ func NewGitHub() *git.Client { return git.NewClient(nil) } -func ReadActiveApplications(ctx context.Context) (apiTypes.GitopsCatalogApps, error) { +func ReadActiveApplications(ctx context.Context) (*apiTypes.GitopsCatalogApps, error) { gh := GitHubClient{ Client: NewGitHub(), } activeContent, err := gh.ReadGitopsCatalogRepoContents(ctx) if err != nil { - return apiTypes.GitopsCatalogApps{}, fmt.Errorf("error retrieving gitops catalog repository content: %w", err) + return nil, fmt.Errorf("error retrieving gitops catalog repository content: %w", err) } index, err := gh.ReadGitopsCatalogIndex(ctx, activeContent) if err != nil { - return apiTypes.GitopsCatalogApps{}, fmt.Errorf("error retrieving gitops catalog index content: %w", err) + return nil, fmt.Errorf("error retrieving gitops catalog index content: %w", err) } var out apiTypes.GitopsCatalogApps err = yaml.Unmarshal(index, &out) if err != nil { - return apiTypes.GitopsCatalogApps{}, fmt.Errorf("error retrieving gitops catalog applications: %w", err) + return nil, fmt.Errorf("error retrieving gitops catalog applications: %w", err) } - return out, nil + return &out, nil } -func ValidateCatalogApps(ctx context.Context, catalogApps string) (bool, []apiTypes.GitopsCatalogApp, error) { - items := strings.Split(catalogApps, ",") - - gitopsCatalogapps := []apiTypes.GitopsCatalogApp{} +func ValidateCatalogApps(ctx context.Context, catalogApps string) ([]apiTypes.GitopsCatalogApp, error) { if catalogApps == "" { - return true, gitopsCatalogapps, nil + // No catalog apps to install + return nil, nil } apps, err := ReadActiveApplications(ctx) if err != nil { - log.Error().Msgf("error getting gitops catalog applications: %s", err) - return false, gitopsCatalogapps, err + return nil, err } + items := strings.Split(catalogApps, ",") + gitopsCatalogapps := make([]apiTypes.GitopsCatalogApp, 0, len(items)) for _, app := range items { found := false + for _, catalogApp := range apps.Apps { if app == catalogApp.Name { found = true - if catalogApp.SecretKeys != nil { - for _, secret := range catalogApp.SecretKeys { - secretValue := os.Getenv(secret.Env) - - if secretValue == "" { - return false, gitopsCatalogapps, fmt.Errorf("your %q environment variable is not set for %q catalog application. Please set and try again", secret.Env, app) - } - - secret.Value = secretValue + for pos, secret := range catalogApp.SecretKeys { + secretValue := os.Getenv(secret.Env) + if secretValue == "" { + return nil, fmt.Errorf("your %q environment variable is not set for %q catalog application. Please set and try again", secret.Env, app) } + + secret.Value = secretValue + catalogApp.SecretKeys[pos] = secret } - if catalogApp.ConfigKeys != nil { - for _, config := range catalogApp.ConfigKeys { - configValue := os.Getenv(config.Env) - if configValue == "" { - return false, gitopsCatalogapps, fmt.Errorf("your %q environment variable is not set for %q catalog application. Please set and try again", config.Env, app) - } - config.Value = configValue + for pos, config := range catalogApp.ConfigKeys { + configValue := os.Getenv(config.Env) + if configValue == "" { + return nil, fmt.Errorf("your %q environment variable is not set for %q catalog application. Please set and try again", config.Env, app) } + + config.Value = configValue + catalogApp.ConfigKeys[pos] = config } gitopsCatalogapps = append(gitopsCatalogapps, catalogApp) @@ -108,12 +105,13 @@ func ValidateCatalogApps(ctx context.Context, catalogApps string) (bool, []apiTy break } } + if !found { - return false, gitopsCatalogapps, fmt.Errorf("catalog app is not supported: %q", app) + return nil, fmt.Errorf("catalog app is not supported: %q", app) } } - return true, gitopsCatalogapps, nil + return gitopsCatalogapps, nil } func (gh *GitHubClient) ReadGitopsCatalogRepoContents(ctx context.Context) ([]*git.RepositoryContent, error) {