Skip to content

Commit 2a39f5c

Browse files
authored
add global debug flag (#357)
* add global debug flag * add debug for command execution
1 parent 331ce26 commit 2a39f5c

11 files changed

Lines changed: 74 additions & 8 deletions

File tree

cmd/plural/app.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ func handleInfo(c *cli.Context) error {
5252

5353
_, err := exec.LookPath("k9s")
5454
if err != nil {
55+
utils.LogError().Println(err)
5556
if strings.Contains(err.Error(), exec.ErrNotFound.Error()) {
5657
utils.Error("Application k9s not installed.\n")
5758
fmt.Println("Please install it first from here: https://k9scli.io/topics/install/ and try again")

cmd/plural/init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (p *Plural) handleInit(c *cli.Context) error {
3939

4040
me, err := p.Me()
4141
if err != nil {
42-
return err
42+
return api.GetErrorResponse(err, "Me")
4343
}
4444
if me.Demoing {
4545
return fmt.Errorf(DemoingErrorMsg)

cmd/plural/plural.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ func (p *Plural) InitPluralClient() {
4545
if project, err := manifest.FetchProject(); err == nil && config.Exists() {
4646
conf := config.Read()
4747
if owner := project.Owner; owner != nil && conf.Email != owner.Email {
48+
utils.LogInfo().Printf("Trying to impersonate service account: %s \n", owner.Email)
4849
jwt, email, err := api.FromConfig(&conf).ImpersonateServiceAccount(owner.Email)
4950
if err != nil {
5051
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 {
473474
EnvVar: "PLURAL_ENCRYPTION_KEY_FILE",
474475
Destination: &crypto.EncryptionKeyFile,
475476
},
477+
cli.BoolFlag{
478+
Name: "debug",
479+
Usage: "enable debug mode",
480+
EnvVar: "PLURAL_DEBUG_ENABLE",
481+
Destination: &utils.EnableDebug,
482+
},
476483
}
477484
}
478485

cmd/plural/validation.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ func upstreamSynced(fn func(*cli.Context) error) func(*cli.Context) error {
173173
return func(c *cli.Context) error {
174174
changed, sha, err := git.HasUpstreamChanges()
175175
if err != nil {
176+
utils.LogError().Println(err)
176177
return errors.ErrorWrap(errNoGit, "Failed to get git information")
177178
}
178179

pkg/api/client.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/pkg/errors"
1010
"github.com/pluralsh/gqlclient"
1111
"github.com/pluralsh/plural/pkg/config"
12+
"github.com/pluralsh/plural/pkg/utils"
1213
)
1314

1415
type authedTransport struct {
@@ -111,6 +112,7 @@ func GetErrorResponse(err error, methodName string) error {
111112
if err == nil {
112113
return nil
113114
}
115+
utils.LogError().Println(err)
114116
errResponse := &rawclient.ErrorResponse{}
115117
newErr := json.Unmarshal([]byte(err.Error()), errResponse)
116118
if newErr != nil {

pkg/executor/output.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package executor
33
import (
44
"io"
55
"strings"
6+
7+
"github.com/pluralsh/plural/pkg/utils"
68
)
79

810
type OutputWriter struct {
@@ -17,10 +19,14 @@ func (out *OutputWriter) Write(line []byte) (int, error) {
1719
}
1820

1921
out.lines = append(out.lines, string(line))
20-
_, err := out.delegate.Write([]byte("."))
21-
if err != nil {
22-
return 0, err
22+
utils.LogInfo().Println(string(line))
23+
if !utils.EnableDebug {
24+
_, err := out.delegate.Write([]byte("."))
25+
if err != nil {
26+
return 0, err
27+
}
2328
}
29+
2430
return len(line), nil
2531
}
2632

pkg/executor/step.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ func SuppressedCommand(command string, args ...string) (cmd *exec.Cmd, output *O
3838
output = &OutputWriter{delegate: os.Stdout}
3939
cmd.Stdout = output
4040
cmd.Stderr = output
41+
if utils.EnableDebug {
42+
cmd.Env = os.Environ()
43+
cmd.Env = append(cmd.Env, fmt.Sprintf("PLURAL_DEBUG_ENABLE=%t", true))
44+
}
4145
return
4246
}
4347

pkg/helm/helm.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@ import (
66
"log"
77
"strings"
88

9+
"github.com/pluralsh/plural/pkg/utils"
910
"helm.sh/helm/v3/pkg/action"
1011
"helm.sh/helm/v3/pkg/chart/loader"
1112
"helm.sh/helm/v3/pkg/cli"
1213
)
1314

14-
const enableDebug = false
15-
1615
func debug(format string, v ...interface{}) {
17-
if enableDebug {
18-
format = fmt.Sprintf("[debug] %s\n", format)
16+
if utils.EnableDebug {
17+
format = fmt.Sprintf("INFO: %s\n", format)
1918
err := log.Output(2, fmt.Sprintf(format, v...))
2019
if err != nil {
2120
log.Panic(err)

pkg/provider/azure.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ func (az *AzureProvider) CreateResourceGroup(resourceGroup string) error {
251251
}
252252

253253
if isNotFoundResourceGroup(err) {
254+
utils.LogInfo().Printf("The resource group %s is not found, creating ...", resourceGroup)
254255
param := armresources.ResourceGroup{
255256
Location: to.StringPtr(az.region),
256257
}
@@ -259,6 +260,7 @@ func (az *AzureProvider) CreateResourceGroup(resourceGroup string) error {
259260
if err != nil {
260261
return err
261262
}
263+
utils.LogInfo().Printf("The resource group %s created successfully", resourceGroup)
262264
}
263265

264266
return nil
@@ -399,6 +401,7 @@ func (az *AzureProvider) upsertStorageAccount(account string) (storage.Account,
399401
}
400402

401403
if inNotFoundStorageAccount(err) {
404+
utils.LogInfo().Printf("The storage account %s is not found, creating ...", account)
402405
ctx := context.Background()
403406
future, err := az.clients.Accounts.Create(
404407
ctx,

pkg/provider/gcp.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ func (gcp *GCPProvider) validateEnabled() error {
360360
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.")
361361
proj, err := gcp.getProject()
362362
if err != nil {
363+
utils.LogError().Println(err)
363364
return errEnabled
364365
}
365366

@@ -376,11 +377,13 @@ func (gcp *GCPProvider) validateEnabled() error {
376377
}
377378
resp, err := c.BatchGetServices(ctx, req)
378379
if err != nil {
380+
utils.LogError().Println(err)
379381
return errEnabled
380382
}
381383

382384
for _, svc := range resp.Services {
383385
if svc.State != serviceusagepb.State_ENABLED {
386+
utils.LogError().Printf("the service state %v != %v", svc.State, serviceusagepb.State_ENABLED)
384387
return errEnabled
385388
}
386389
}
@@ -397,6 +400,7 @@ func (gcp *GCPProvider) Permissions() (permissions.Checker, error) {
397400
}
398401

399402
func (gcp *GCPProvider) validatePermissions() error {
403+
utils.LogInfo().Println("Validate GCP permissions")
400404
ctx := context.Background()
401405
proj, err := gcp.getProject()
402406
if err != nil {
@@ -414,6 +418,7 @@ func (gcp *GCPProvider) validatePermissions() error {
414418
}
415419

416420
for _, perm := range missing {
421+
utils.LogError().Printf("Required GCP permission %s \n", perm)
417422
provUtils.FailedPermission(perm)
418423
}
419424

0 commit comments

Comments
 (0)