From 4cf450bbb0725751136549c618c3b2e86fda85b2 Mon Sep 17 00:00:00 2001 From: Nadiv Gold Date: Wed, 19 May 2021 08:34:46 -0600 Subject: [PATCH 1/4] fixed issues that pavel caught --- commands/apps_test.go | 4 ---- commands/helpers.go | 4 ++-- commands/login_test.go | 3 --- sectionctl.go | 32 ++++++++++++++++---------------- 4 files changed, 18 insertions(+), 25 deletions(-) diff --git a/commands/apps_test.go b/commands/apps_test.go index f089bfa..a385265 100644 --- a/commands/apps_test.go +++ b/commands/apps_test.go @@ -40,7 +40,6 @@ func TestCommandsAppsCreateAttemptsToValidateStackOnError(t *testing.T) { StackName: "helloworld-1.0.0", } - // Define Contet ctx := context.Background() // Invoke @@ -111,9 +110,6 @@ func TestCommandsAppsInitHandlesErrors(t *testing.T) { fmt.Println("server.conf creation failed") } - - // Define Context - ctx := context.Background() // Invoke diff --git a/commands/helpers.go b/commands/helpers.go index 254a8cd..83388b4 100644 --- a/commands/helpers.go +++ b/commands/helpers.go @@ -17,10 +17,10 @@ func NewSpinner() *spinner.Spinner { } */ -type CTXKEY string +type CtxKey string func IsInCtxBool(ctx context.Context, arg string) bool { - return ctx.Value(CTXKEY(arg)) != nil && ctx.Value(CTXKEY(arg)).(bool) + return ctx.Value(CtxKey(arg)) != nil && ctx.Value(CtxKey(arg)).(bool) } // NewSpinner returns a nicely formatted spinner for display while users are waiting. diff --git a/commands/login_test.go b/commands/login_test.go index 4014484..5f94e53 100644 --- a/commands/login_test.go +++ b/commands/login_test.go @@ -39,7 +39,6 @@ func TestCommandsLoginValidatesGoodCredentials(t *testing.T) { out: &out, } - // Define Context ctx := context.Background() // Invoke @@ -72,7 +71,6 @@ func TestCommandsLoginValidatesBadCredentials(t *testing.T) { out: &out, } - // Define Context ctx:= context.Background() // Invoke @@ -111,7 +109,6 @@ func TestCommandsLoginUsesAlreadySetAPIToken(t *testing.T) { out: &bytes.Buffer{}, } - // Define Context ctx := context.Background() // Invoke diff --git a/sectionctl.go b/sectionctl.go index 1ae915b..f003187 100644 --- a/sectionctl.go +++ b/sectionctl.go @@ -45,19 +45,19 @@ type CLI struct { -func bootstrap(c CLI, ctx *kong.Context) context.Context { +func bootstrap(c CLI, cmd *kong.Context) context.Context { api.PrefixURI = c.SectionAPIPrefix api.Timeout = c.SectionAPITimeout - contxt := context.Background() - contxt = context.WithValue(contxt, commands.CTXKEY("quiet"), c.Quiet) - - colorableWriter := colorable.NewColorableStderr() - + ctx := context.Background() minLogLevel := logutils.LogLevel("INFO") - if contxt.Value(commands.CTXKEY("quiet")).(bool) { + if c.Quiet { + ctx = context.WithValue(ctx, commands.CtxKey("quiet"), c.Quiet) minLogLevel = logutils.LogLevel("ERROR") } + + colorableWriter := colorable.NewColorableStderr() + filter := &logutils.LevelFilter{ Levels: []logutils.LogLevel{"DEBUG", "INFO", "WARN", "ERROR"}, MinLevel: minLogLevel, @@ -72,7 +72,7 @@ func bootstrap(c CLI, ctx *kong.Context) context.Context { panic(err) } fmt.Fprintf(logFile, "Version: %s\n", commands.VersionCmd{}.String()) - fmt.Fprintf(logFile, "Command: %s\n", ctx.Args) + fmt.Fprintf(logFile, "Command: %s\n", cmd.Args) fmt.Fprintf(logFile, "PrefixURI: %s\n", api.PrefixURI) fmt.Fprintf(logFile, "Timeout: %s\n", api.Timeout) fmt.Printf("Writing debug log to: %s\n", logFilePath) @@ -82,11 +82,11 @@ func bootstrap(c CLI, ctx *kong.Context) context.Context { log.SetOutput(filter) switch { - case ctx.Command() == "version": + case cmd.Command() == "version": // bypass auth check for version command - case ctx.Command() == "login": + case cmd.Command() == "login": api.Token = c.SectionToken - case ctx.Command() != "login" && ctx.Command() != "logout": + case cmd.Command() != "login" && cmd.Command() != "logout": t := c.SectionToken if t == "" { to, err := credentials.Setup(api.PrefixURI.Host) @@ -98,7 +98,7 @@ func bootstrap(c CLI, ctx *kong.Context) context.Context { } api.Token = t } - return contxt + return ctx } func main() { @@ -109,14 +109,14 @@ func main() { kongplete.WithPredictor("file", complete.PredictFiles("*")), ) - ctx := kong.Parse(&cli, + cmd := kong.Parse(&cli, kong.Description("CLI to interact with Section."), kong.UsageOnError(), kong.ConfigureHelp(kong.HelpOptions{Tree: true}), ) - contxt := bootstrap(cli, ctx) - ctx.BindTo(contxt, (*context.Context)(nil)) - err := ctx.Run(contxt) + ctx := bootstrap(cli, cmd) + cmd.BindTo(ctx, (*context.Context)(nil)) + err := cmd.Run(ctx) if err != nil { log.Printf("[ERROR] %s\n", err) os.Exit(2) From 0e5268474fbdcc730fa0eac51c0373764aa372ad Mon Sep 17 00:00:00 2001 From: Nadiv Gold Date: Wed, 19 May 2021 08:58:19 -0600 Subject: [PATCH 2/4] lint --- commands/deploy_test.go | 1 - sectionctl_test.go | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/commands/deploy_test.go b/commands/deploy_test.go index 3af693b..bd48ee3 100644 --- a/commands/deploy_test.go +++ b/commands/deploy_test.go @@ -259,7 +259,6 @@ func TestCommandsDeployUploadsTarball(t *testing.T) { mockGit := MockGitService{} globalGitService = &mockGit - // Define Context ctx := context.Background() // Invoke diff --git a/sectionctl_test.go b/sectionctl_test.go index a247e99..f16a401 100644 --- a/sectionctl_test.go +++ b/sectionctl_test.go @@ -19,7 +19,7 @@ func TestBootstrapSetsUpDebugLogFile(t *testing.T) { d, err := ioutil.TempDir("", "sectionctl") assert.NoError(err) - var cli = CLI{Debug: true, SectionToken: "s3cr3t", SectionAPIPrefix: u, DebugFileDir: d} + var cli = CLI{Debug: true, SectionToken: "s3cr3t", SectionAPIPrefix: u, DebugFileDir: d, DebugOutput: true} var ctx kong.Context // Invoke From eaedf70a54eb0cc98d2d218f49d0eaca83f6b6bc Mon Sep 17 00:00:00 2001 From: Nadiv Gold Date: Wed, 19 May 2021 09:20:33 -0600 Subject: [PATCH 3/4] added additional QoL for debug logging --- sectionctl.go | 17 +++++++++++++---- sectionctl_test.go | 2 +- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/sectionctl.go b/sectionctl.go index 9d17557..2d41f84 100644 --- a/sectionctl.go +++ b/sectionctl.go @@ -35,7 +35,7 @@ type CLI struct { WhoAmI commands.WhoAmICmd `cmd name:"whoami" help:"Show information about the currently authenticated user"` Debug bool `env:"DEBUG" help:"Enable debug output"` DebugOutput bool `short:"out" help:"Enable logging on the debug output."` - DebugFileDir string `default:"." help:"Directory where debug output should be written"` + DebugFile string `default:"." help:"Directory where debug output should be written"` SectionToken string `env:"SECTION_TOKEN" help:"Secret token for API auth"` SectionAPIPrefix *url.URL `default:"https://aperture.section.io" env:"SECTION_API_PREFIX"` SectionAPITimeout time.Duration `default:"30s" env:"SECTION_API_TIMEOUT" help:"Request timeout for the Section API"` @@ -66,9 +66,18 @@ func bootstrap(c CLI, cmd *kong.Context) context.Context { } if c.Debug { filter.MinLevel = logutils.LogLevel("DEBUG") - if(c.DebugOutput){ - logFilename := fmt.Sprintf("sectionctl-debug-%s.log", time.Now().Format("2006-01-02-15-04-05Z0700")) - logFilePath := filepath.Join(c.DebugFileDir, logFilename) + if(c.DebugOutput || len(c.DebugFile) > 1){ + info, err := os.Stat(c.DebugFile); + if err != nil { + panic(err) + } + var logFilePath string + if len(c.DebugFile) == 0 || info.IsDir() { + logFilePath = filepath.Join(c.DebugFile, fmt.Sprintf("sectionctl-debug-%s.log", time.Now().Format("2006-01-02-15-04-05Z0700"))) + c.DebugFile = logFilePath + } else { + logFilePath = filepath.Join(c.DebugFile) + } logFile, err := os.OpenFile(logFilePath, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666) if err != nil { panic(err) diff --git a/sectionctl_test.go b/sectionctl_test.go index f16a401..ad4b835 100644 --- a/sectionctl_test.go +++ b/sectionctl_test.go @@ -19,7 +19,7 @@ func TestBootstrapSetsUpDebugLogFile(t *testing.T) { d, err := ioutil.TempDir("", "sectionctl") assert.NoError(err) - var cli = CLI{Debug: true, SectionToken: "s3cr3t", SectionAPIPrefix: u, DebugFileDir: d, DebugOutput: true} + var cli = CLI{Debug: true, SectionToken: "s3cr3t", SectionAPIPrefix: u, DebugFile: d, DebugOutput: true} var ctx kong.Context // Invoke From 32fa2ab0a2c974c891404dcc7f9495787ec58f7e Mon Sep 17 00:00:00 2001 From: Nadiv Gold Date: Wed, 19 May 2021 09:35:22 -0600 Subject: [PATCH 4/4] cleaner if --- sectionctl.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/sectionctl.go b/sectionctl.go index 2d41f84..6984ff6 100644 --- a/sectionctl.go +++ b/sectionctl.go @@ -71,12 +71,10 @@ func bootstrap(c CLI, cmd *kong.Context) context.Context { if err != nil { panic(err) } - var logFilePath string + logFilePath := filepath.Join(c.DebugFile) if len(c.DebugFile) == 0 || info.IsDir() { logFilePath = filepath.Join(c.DebugFile, fmt.Sprintf("sectionctl-debug-%s.log", time.Now().Format("2006-01-02-15-04-05Z0700"))) c.DebugFile = logFilePath - } else { - logFilePath = filepath.Join(c.DebugFile) } logFile, err := os.OpenFile(logFilePath, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666) if err != nil {