From 2a39f5ce87316b1a456a4d9e5f78a6abf9184c24 Mon Sep 17 00:00:00 2001 From: Lukasz Zajaczkowski Date: Fri, 3 Mar 2023 09:05:37 +0100 Subject: [PATCH] add global debug flag (#357) * add global debug flag * add debug for command execution --- cmd/plural/app.go | 1 + cmd/plural/init.go | 2 +- cmd/plural/plural.go | 7 +++++++ cmd/plural/validation.go | 1 + pkg/api/client.go | 2 ++ pkg/executor/output.go | 12 +++++++++--- pkg/executor/step.go | 4 ++++ pkg/helm/helm.go | 7 +++---- pkg/provider/azure.go | 3 +++ pkg/provider/gcp.go | 5 +++++ pkg/utils/logs.go | 38 ++++++++++++++++++++++++++++++++++++++ 11 files changed, 74 insertions(+), 8 deletions(-) create mode 100644 pkg/utils/logs.go diff --git a/cmd/plural/app.go b/cmd/plural/app.go index cbf68d08..b626a939 100644 --- a/cmd/plural/app.go +++ b/cmd/plural/app.go @@ -52,6 +52,7 @@ func handleInfo(c *cli.Context) error { _, err := exec.LookPath("k9s") if err != nil { + utils.LogError().Println(err) if strings.Contains(err.Error(), exec.ErrNotFound.Error()) { utils.Error("Application k9s not installed.\n") fmt.Println("Please install it first from here: https://k9scli.io/topics/install/ and try again") diff --git a/cmd/plural/init.go b/cmd/plural/init.go index 90278a81..96a929fb 100644 --- a/cmd/plural/init.go +++ b/cmd/plural/init.go @@ -39,7 +39,7 @@ func (p *Plural) handleInit(c *cli.Context) error { me, err := p.Me() if err != nil { - return err + return api.GetErrorResponse(err, "Me") } if me.Demoing { return fmt.Errorf(DemoingErrorMsg) diff --git a/cmd/plural/plural.go b/cmd/plural/plural.go index 9baba4be..a70dfd49 100644 --- a/cmd/plural/plural.go +++ b/cmd/plural/plural.go @@ -45,6 +45,7 @@ func (p *Plural) InitPluralClient() { if project, err := manifest.FetchProject(); err == nil && config.Exists() { conf := config.Read() if owner := project.Owner; owner != nil && conf.Email != owner.Email { + utils.LogInfo().Printf("Trying to impersonate service account: %s \n", owner.Email) jwt, email, err := api.FromConfig(&conf).ImpersonateServiceAccount(owner.Email) if err != nil { utils.Error("You (%s) are not the owner of this repo %s, %v \n", conf.Email, owner.Email, api.GetErrorResponse(err, "ImpersonateServiceAccount")) @@ -473,6 +474,12 @@ func globalFlags() []cli.Flag { EnvVar: "PLURAL_ENCRYPTION_KEY_FILE", Destination: &crypto.EncryptionKeyFile, }, + cli.BoolFlag{ + Name: "debug", + Usage: "enable debug mode", + EnvVar: "PLURAL_DEBUG_ENABLE", + Destination: &utils.EnableDebug, + }, } } diff --git a/cmd/plural/validation.go b/cmd/plural/validation.go index ae02e1c2..4501e0c0 100644 --- a/cmd/plural/validation.go +++ b/cmd/plural/validation.go @@ -173,6 +173,7 @@ func upstreamSynced(fn func(*cli.Context) error) func(*cli.Context) error { return func(c *cli.Context) error { changed, sha, err := git.HasUpstreamChanges() if err != nil { + utils.LogError().Println(err) return errors.ErrorWrap(errNoGit, "Failed to get git information") } diff --git a/pkg/api/client.go b/pkg/api/client.go index 2ec013ee..ba75c725 100644 --- a/pkg/api/client.go +++ b/pkg/api/client.go @@ -9,6 +9,7 @@ import ( "github.com/pkg/errors" "github.com/pluralsh/gqlclient" "github.com/pluralsh/plural/pkg/config" + "github.com/pluralsh/plural/pkg/utils" ) type authedTransport struct { @@ -111,6 +112,7 @@ func GetErrorResponse(err error, methodName string) error { if err == nil { return nil } + utils.LogError().Println(err) errResponse := &rawclient.ErrorResponse{} newErr := json.Unmarshal([]byte(err.Error()), errResponse) if newErr != nil { diff --git a/pkg/executor/output.go b/pkg/executor/output.go index 151e707d..50881bcc 100644 --- a/pkg/executor/output.go +++ b/pkg/executor/output.go @@ -3,6 +3,8 @@ package executor import ( "io" "strings" + + "github.com/pluralsh/plural/pkg/utils" ) type OutputWriter struct { @@ -17,10 +19,14 @@ func (out *OutputWriter) Write(line []byte) (int, error) { } out.lines = append(out.lines, string(line)) - _, err := out.delegate.Write([]byte(".")) - if err != nil { - return 0, err + utils.LogInfo().Println(string(line)) + if !utils.EnableDebug { + _, err := out.delegate.Write([]byte(".")) + if err != nil { + return 0, err + } } + return len(line), nil } diff --git a/pkg/executor/step.go b/pkg/executor/step.go index 881e1267..5f5fdc58 100644 --- a/pkg/executor/step.go +++ b/pkg/executor/step.go @@ -38,6 +38,10 @@ func SuppressedCommand(command string, args ...string) (cmd *exec.Cmd, output *O output = &OutputWriter{delegate: os.Stdout} cmd.Stdout = output cmd.Stderr = output + if utils.EnableDebug { + cmd.Env = os.Environ() + cmd.Env = append(cmd.Env, fmt.Sprintf("PLURAL_DEBUG_ENABLE=%t", true)) + } return } diff --git a/pkg/helm/helm.go b/pkg/helm/helm.go index 550a9c45..12932b14 100644 --- a/pkg/helm/helm.go +++ b/pkg/helm/helm.go @@ -6,16 +6,15 @@ import ( "log" "strings" + "github.com/pluralsh/plural/pkg/utils" "helm.sh/helm/v3/pkg/action" "helm.sh/helm/v3/pkg/chart/loader" "helm.sh/helm/v3/pkg/cli" ) -const enableDebug = false - func debug(format string, v ...interface{}) { - if enableDebug { - format = fmt.Sprintf("[debug] %s\n", format) + if utils.EnableDebug { + format = fmt.Sprintf("INFO: %s\n", format) err := log.Output(2, fmt.Sprintf(format, v...)) if err != nil { log.Panic(err) diff --git a/pkg/provider/azure.go b/pkg/provider/azure.go index a4d2f7eb..c4e4a70b 100644 --- a/pkg/provider/azure.go +++ b/pkg/provider/azure.go @@ -251,6 +251,7 @@ func (az *AzureProvider) CreateResourceGroup(resourceGroup string) error { } if isNotFoundResourceGroup(err) { + utils.LogInfo().Printf("The resource group %s is not found, creating ...", resourceGroup) param := armresources.ResourceGroup{ Location: to.StringPtr(az.region), } @@ -259,6 +260,7 @@ func (az *AzureProvider) CreateResourceGroup(resourceGroup string) error { if err != nil { return err } + utils.LogInfo().Printf("The resource group %s created successfully", resourceGroup) } return nil @@ -399,6 +401,7 @@ func (az *AzureProvider) upsertStorageAccount(account string) (storage.Account, } if inNotFoundStorageAccount(err) { + utils.LogInfo().Printf("The storage account %s is not found, creating ...", account) ctx := context.Background() future, err := az.clients.Accounts.Create( ctx, diff --git a/pkg/provider/gcp.go b/pkg/provider/gcp.go index 1427983e..80541b69 100644 --- a/pkg/provider/gcp.go +++ b/pkg/provider/gcp.go @@ -360,6 +360,7 @@ func (gcp *GCPProvider) validateEnabled() error { errEnabled := fmt.Errorf("You don't have necessary services enabled. Please run: `gcloud services enable serviceusage.googleapis.com cloudresourcemanager.googleapis.com container.googleapis.com` with an owner of the project to enable or enable them in the GCP console.") proj, err := gcp.getProject() if err != nil { + utils.LogError().Println(err) return errEnabled } @@ -376,11 +377,13 @@ func (gcp *GCPProvider) validateEnabled() error { } resp, err := c.BatchGetServices(ctx, req) if err != nil { + utils.LogError().Println(err) return errEnabled } for _, svc := range resp.Services { if svc.State != serviceusagepb.State_ENABLED { + utils.LogError().Printf("the service state %v != %v", svc.State, serviceusagepb.State_ENABLED) return errEnabled } } @@ -397,6 +400,7 @@ func (gcp *GCPProvider) Permissions() (permissions.Checker, error) { } func (gcp *GCPProvider) validatePermissions() error { + utils.LogInfo().Println("Validate GCP permissions") ctx := context.Background() proj, err := gcp.getProject() if err != nil { @@ -414,6 +418,7 @@ func (gcp *GCPProvider) validatePermissions() error { } for _, perm := range missing { + utils.LogError().Printf("Required GCP permission %s \n", perm) provUtils.FailedPermission(perm) } diff --git a/pkg/utils/logs.go b/pkg/utils/logs.go new file mode 100644 index 00000000..e1f150a6 --- /dev/null +++ b/pkg/utils/logs.go @@ -0,0 +1,38 @@ +package utils + +import ( + "io" + "log" + "os" +) + +func init() { + EnableDebug = false +} + +var infoLogger *log.Logger +var errorLogger *log.Logger + +var EnableDebug bool + +func LogInfo() *log.Logger { + if infoLogger == nil { + infoLogger = log.New(getOutputWriter(), "INFO: ", log.Ldate|log.Ltime|log.Lshortfile) + } + return infoLogger +} + +func LogError() *log.Logger { + if errorLogger == nil { + errorLogger = log.New(getOutputWriter(), "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile) + } + return errorLogger +} + +func getOutputWriter() (out io.Writer) { + out = os.Stdout + if !EnableDebug { + out = io.Discard + } + return +}