From 2727b459e5597bf62220fdbb46ce3351e13d7e93 Mon Sep 17 00:00:00 2001 From: mrrishi Date: Tue, 12 Nov 2024 00:12:36 +0530 Subject: [PATCH] feat: add ci flag --- cmd/akamai/create.go | 8 ++++-- cmd/aws/create.go | 9 ++++--- cmd/azure/create.go | 5 ++-- cmd/civo/create.go | 8 ++++-- cmd/digitalocean/create.go | 10 ++++--- cmd/google/create.go | 9 ++++--- cmd/launch.go | 4 ++- cmd/vultr/create.go | 8 ++++-- internal/progress/ci.go | 41 ++++++++++++++++++++++++++++ internal/progress/message.go | 51 ++++++++++++++++++++++++++++++++--- internal/progress/progress.go | 9 ++++++- main.go | 5 ++-- 12 files changed, 141 insertions(+), 26 deletions(-) create mode 100644 internal/progress/ci.go diff --git a/cmd/akamai/create.go b/cmd/akamai/create.go index 682308132..18ca16e2e 100644 --- a/cmd/akamai/create.go +++ b/cmd/akamai/create.go @@ -99,7 +99,9 @@ func createAkamai(cmd *cobra.Command, _ []string) error { } func ValidateProvidedFlags(gitProvider string) error { - progress.AddStep("Validate provided flags") + if progress.CanRunBubbleTea { + progress.AddStep("Validate provided flags") + } if os.Getenv("LINODE_TOKEN") == "" { return fmt.Errorf("your LINODE_TOKEN is not set - please set and re-run your last command") @@ -126,7 +128,9 @@ func ValidateProvidedFlags(gitProvider string) error { log.Info().Msgf("%q %s", "gitlab.com", key.Type()) } - progress.CompleteStep("Validate provided flags") + if progress.CanRunBubbleTea { + progress.CompleteStep("Validate provided flags") + } return nil } diff --git a/cmd/aws/create.go b/cmd/aws/create.go index 80c194817..d454d9fc5 100644 --- a/cmd/aws/create.go +++ b/cmd/aws/create.go @@ -137,8 +137,9 @@ func createAws(cmd *cobra.Command, _ []string) error { } func ValidateProvidedFlags(gitProvider string) error { - progress.AddStep("Validate provided flags") - + if progress.CanRunBubbleTea { + progress.AddStep("Validate provided flags") + } // Validate required environment variables for dns provider if dnsProviderFlag == "cloudflare" { if os.Getenv("CF_API_TOKEN") == "" { @@ -161,7 +162,9 @@ func ValidateProvidedFlags(gitProvider string) error { log.Info().Msgf("%q %s", "gitlab.com", key.Type()) } - progress.CompleteStep("Validate provided flags") + if progress.CanRunBubbleTea { + progress.CompleteStep("Validate provided flags") + } return nil } diff --git a/cmd/azure/create.go b/cmd/azure/create.go index b42352f34..809fa7777 100644 --- a/cmd/azure/create.go +++ b/cmd/azure/create.go @@ -113,8 +113,9 @@ func createAzure(cmd *cobra.Command, _ []string) error { } func ValidateProvidedFlags(gitProvider, dnsProvider, dnsAzureResourceGroup string) error { - progress.AddStep("Validate provided flags") - + if progress.CanRunBubbleTea { + progress.AddStep("Validate provided flags") + } for _, env := range envvarSecrets { if os.Getenv(env) == "" { return fmt.Errorf("your %s is not set - please set and re-run your last command", env) diff --git a/cmd/civo/create.go b/cmd/civo/create.go index 14592cea4..7a01d717e 100644 --- a/cmd/civo/create.go +++ b/cmd/civo/create.go @@ -102,7 +102,9 @@ func createCivo(cmd *cobra.Command, _ []string) error { } func ValidateProvidedFlags(gitProvider string) error { - progress.AddStep("Validate provided flags") + if progress.CanRunBubbleTea { + progress.AddStep("Validate provided flags") + } if os.Getenv("CIVO_TOKEN") == "" { return fmt.Errorf("your CIVO_TOKEN is not set - please set and re-run your last command") @@ -130,7 +132,9 @@ func ValidateProvidedFlags(gitProvider string) error { log.Info().Msgf("gitlab.com %q", key.Type()) } - progress.CompleteStep("Validate provided flags") + if progress.CanRunBubbleTea { + progress.CompleteStep("Validate provided flags") + } return nil } diff --git a/cmd/digitalocean/create.go b/cmd/digitalocean/create.go index c12134962..9bbac465e 100644 --- a/cmd/digitalocean/create.go +++ b/cmd/digitalocean/create.go @@ -111,8 +111,9 @@ func createDigitalocean(cmd *cobra.Command, _ []string) error { } func ValidateProvidedFlags(gitProvider string) error { - progress.AddStep("Validate provided flags") - + if progress.CanRunBubbleTea { + progress.AddStep("Validate provided flags") + } // Validate required environment variables for dns provider if dnsProviderFlag == "cloudflare" { if os.Getenv("CF_API_TOKEN") == "" { @@ -141,7 +142,8 @@ func ValidateProvidedFlags(gitProvider string) error { log.Info().Msgf("%q %s", "gitlab.com", key.Type()) } - progress.CompleteStep("Validate provided flags") - + if progress.CanRunBubbleTea { + progress.CompleteStep("Validate provided flags") + } return nil } diff --git a/cmd/google/create.go b/cmd/google/create.go index bb074d9bb..5aaabc71b 100644 --- a/cmd/google/create.go +++ b/cmd/google/create.go @@ -104,7 +104,9 @@ func createGoogle(cmd *cobra.Command, _ []string) error { } func ValidateProvidedFlags(gitProvider string) error { - progress.AddStep("Validate provided flags") + if progress.CanRunBubbleTea { + progress.AddStep("Validate provided flags") + } if os.Getenv("GOOGLE_APPLICATION_CREDENTIALS") == "" { return fmt.Errorf("your GOOGLE_APPLICATION_CREDENTIALS is not set - please set and re-run your last command") @@ -131,7 +133,8 @@ func ValidateProvidedFlags(gitProvider string) error { log.Info().Msgf("%q %s", "gitlab.com", key.Type()) } - progress.CompleteStep("Validate provided flags") - + if progress.CanRunBubbleTea { + progress.CompleteStep("Validate provided flags") + } return nil } diff --git a/cmd/launch.go b/cmd/launch.go index 88b23c5ac..4bc09ea3f 100644 --- a/cmd/launch.go +++ b/cmd/launch.go @@ -13,6 +13,8 @@ import ( "github.com/spf13/cobra" ) +var ciFlag bool + // additionalHelmFlags can optionally pass user-supplied flags to helm var additionalHelmFlags []string @@ -102,6 +104,6 @@ func launchDeleteCluster() *cobra.Command { launch.DeleteCluster(args[0]) }, } - + launchDeleteClusterCmd.Flags().BoolVar(&ciFlag, "ci", false, "if running kubefirst in ci, set this flag to disable interactive features") return launchDeleteClusterCmd } diff --git a/cmd/vultr/create.go b/cmd/vultr/create.go index 6086ae2b4..020e96801 100644 --- a/cmd/vultr/create.go +++ b/cmd/vultr/create.go @@ -111,7 +111,9 @@ func createVultr(cmd *cobra.Command, _ []string) error { } func ValidateProvidedFlags(gitProvider string) error { - progress.AddStep("Validate provided flags") + if progress.CanRunBubbleTea { + progress.AddStep("Validate provided flags") + } if os.Getenv("VULTR_API_KEY") == "" { return fmt.Errorf("your VULTR_API_KEY variable is unset - please set it before continuing") @@ -138,6 +140,8 @@ func ValidateProvidedFlags(gitProvider string) error { log.Info().Msgf("%q %s", "gitlab.com", key.Type()) } - progress.CompleteStep("Validate provided flags") + if progress.CanRunBubbleTea { + progress.CompleteStep("Validate provided flags") + } return nil } diff --git a/internal/progress/ci.go b/internal/progress/ci.go new file mode 100644 index 000000000..bd9f75366 --- /dev/null +++ b/internal/progress/ci.go @@ -0,0 +1,41 @@ +package progress + +import ( + "fmt" + "log" + "time" + + "github.com/konstructio/kubefirst/internal/cluster" +) + +func WatchClusterForCi(clusterName string) { + + ticker := time.NewTicker(10 * time.Second) + defer ticker.Stop() + + done := make(chan bool) + + go func() { + for { + select { + case <-done: + return + case <-ticker.C: + provisioningCluster, _ := cluster.GetCluster(clusterName) + + if provisioningCluster.Status == "error" { + log.Fatalf("unable to provision cluster: %s", provisioningCluster.LastCondition) + done <- true + } + + if provisioningCluster.Status == "provisioned" { + fmt.Println("\n cluster has been provisioned via ci") + fmt.Printf("\n kubefirst URL: https://kubefirst.%s \n", provisioningCluster.DomainName) + done <- true + } + } + } + }() + + <-done +} diff --git a/internal/progress/message.go b/internal/progress/message.go index c6313abb5..812eef09d 100644 --- a/internal/progress/message.go +++ b/internal/progress/message.go @@ -71,6 +71,11 @@ func DisplayLogHints(estimatedTime int) { headerMessage := renderMessage(header) + if !CanRunBubbleTea { + fmt.Println(headerMessage) + return + } + Progress.Send(headerMsg{ message: headerMessage, }) @@ -141,6 +146,11 @@ func DisplaySuccessMessage(cluster types.Cluster) successMsg { successMessage := renderMessage(success) + if !CanRunBubbleTea { + fmt.Println(successMessage) + return successMsg{} + } + return successMsg{ message: successMessage, } @@ -165,6 +175,11 @@ func DisplayCredentials(cluster types.Cluster) { headerMessage := renderMessage(header) + if !CanRunBubbleTea { + fmt.Println(headerMessage) + return + } + Progress.Send(headerMsg{ message: headerMessage, }) @@ -174,10 +189,22 @@ func DisplayCredentials(cluster types.Cluster) { func AddStep(message string) { renderedMessage := createStep(fmt.Sprintf("%s %s", ":dizzy:", message)) + + if !CanRunBubbleTea { + fmt.Println(renderedMessage) + return + } + Progress.Send(renderedMessage) } func CompleteStep(message string) { + + if !CanRunBubbleTea { + fmt.Println(message) + return + } + Progress.Send(completeStep{ message: message, }) @@ -186,6 +213,11 @@ func CompleteStep(message string) { func Success(success string) { successMessage := renderMessage(success) + if !CanRunBubbleTea { + fmt.Println(successMessage) + return + } + Progress.Send( successMsg{ message: successMessage, @@ -194,13 +226,24 @@ func Success(success string) { func Error(message string) { renderedMessage := createErrorLog(message) + + if !CanRunBubbleTea { + fmt.Println(renderedMessage) + return + } + Progress.Send(renderedMessage) } func StartProvisioning(clusterName string) { - provisioningMessage := startProvision{ - clusterName: clusterName, - } - Progress.Send(provisioningMessage) + if !CanRunBubbleTea { + WatchClusterForCi(clusterName) + } else { + provisioningMessage := startProvision{ + clusterName: clusterName, + } + + Progress.Send(provisioningMessage) + } } diff --git a/internal/progress/progress.go b/internal/progress/progress.go index 6d0b42c7f..5ed89a0a4 100644 --- a/internal/progress/progress.go +++ b/internal/progress/progress.go @@ -14,7 +14,10 @@ import ( "github.com/spf13/viper" ) -var Progress *tea.Program +var ( + Progress *tea.Program + CanRunBubbleTea bool +) //nolint:revive // will be removed after refactoring func NewModel() progressModel { @@ -23,6 +26,10 @@ func NewModel() progressModel { } } +func DiableBubbleTeaExecution() { + CanRunBubbleTea = false +} + // Bubbletea functions func InitializeProgressTerminal() { Progress = tea.NewProgram(NewModel()) diff --git a/main.go b/main.go index e030707ed..cd48a2002 100644 --- a/main.go +++ b/main.go @@ -24,8 +24,8 @@ import ( func main() { argsWithProg := os.Args - - bubbleTeaBlacklist := []string{"completion", "help", "--help", "-h", "quota", "logs"} + fmt.Println(argsWithProg) + bubbleTeaBlacklist := []string{"completion", "help", "--help", "-h", "quota", "logs", "--ci"} canRunBubbleTea := true for _, arg := range argsWithProg { @@ -143,5 +143,6 @@ func main() { progress.Progress.Run() } else { cmd.Execute() + progress.DiableBubbleTeaExecution() } }