Skip to content

Commit

Permalink
Merge branch 'main' into deploy-better-integration
Browse files Browse the repository at this point in the history
  • Loading branch information
AskAlice committed May 21, 2021
2 parents 6d25fb8 + 9e011f0 commit 8cb8015
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 29 deletions.
4 changes: 0 additions & 4 deletions commands/apps_test.go
Expand Up @@ -40,7 +40,6 @@ func TestCommandsAppsCreateAttemptsToValidateStackOnError(t *testing.T) {
StackName: "helloworld-1.0.0",
}

// Define Contet
ctx := context.Background()

// Invoke
Expand Down Expand Up @@ -111,9 +110,6 @@ func TestCommandsAppsInitHandlesErrors(t *testing.T) {
fmt.Println("server.conf creation failed")
}


// Define Context

ctx := context.Background()

// Invoke
Expand Down
1 change: 0 additions & 1 deletion commands/deploy_test.go
Expand Up @@ -259,7 +259,6 @@ func TestCommandsDeployUploadsTarball(t *testing.T) {
mockGit := MockGitService{}
globalGitService = &mockGit

// Define Context
ctx := context.Background()

// Invoke
Expand Down
4 changes: 2 additions & 2 deletions commands/helpers.go
Expand Up @@ -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.
Expand Down
3 changes: 0 additions & 3 deletions commands/login_test.go
Expand Up @@ -39,7 +39,6 @@ func TestCommandsLoginValidatesGoodCredentials(t *testing.T) {
out: &out,
}

// Define Context
ctx := context.Background()

// Invoke
Expand Down Expand Up @@ -72,7 +71,6 @@ func TestCommandsLoginValidatesBadCredentials(t *testing.T) {
out: &out,
}

// Define Context
ctx:= context.Background()

// Invoke
Expand Down Expand Up @@ -111,7 +109,6 @@ func TestCommandsLoginUsesAlreadySetAPIToken(t *testing.T) {
out: &bytes.Buffer{},
}

// Define Context
ctx := context.Background()

// Invoke
Expand Down
41 changes: 23 additions & 18 deletions sectionctl.go
Expand Up @@ -46,19 +46,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,
Expand All @@ -67,16 +67,21 @@ func bootstrap(c CLI, ctx *kong.Context) context.Context {
if c.Debug {
filter.MinLevel = logutils.LogLevel("DEBUG")
if(c.DebugOutput || len(c.DebugFile) > 1){
if len(c.DebugFile) == 0 {
c.DebugFile = fmt.Sprintf("sectionctl-debug-%s.log", time.Now().Format("2006-01-02-15-04-05Z0700"))
info, err := os.Stat(c.DebugFile);
if err != nil {
panic(err)
}
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
}
logFile, err := os.OpenFile(logFilePath, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
if err != nil {
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)
Expand All @@ -87,11 +92,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)
Expand All @@ -103,7 +108,7 @@ func bootstrap(c CLI, ctx *kong.Context) context.Context {
}
api.Token = t
}
return contxt
return ctx
}

func main() {
Expand All @@ -114,14 +119,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)
Expand Down
2 changes: 1 addition & 1 deletion sectionctl_test.go
Expand Up @@ -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, DebugFile: d, DebugOutput: true}
var ctx kong.Context

// Invoke
Expand Down

0 comments on commit 8cb8015

Please sign in to comment.