Skip to content

Commit

Permalink
(feat) display more information about aws creds being used.
Browse files Browse the repository at this point in the history
  • Loading branch information
tphoney committed Oct 1, 2024
1 parent 963a1d9 commit 989d410
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 19 deletions.
39 changes: 27 additions & 12 deletions cmd/explore.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import (
"atomicgo.dev/keyboard"
"atomicgo.dev/keyboard/keys"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/google/uuid"
"github.com/overmindtech/aws-source/proc"
"github.com/overmindtech/cli/tfutils"
"github.com/overmindtech/discovery"
"github.com/overmindtech/pterm"
"github.com/overmindtech/sdp-go"
stdlibsource "github.com/overmindtech/stdlib-source/sources"
stdlibSource "github.com/overmindtech/stdlib-source/sources"
"github.com/pkg/browser"
log "github.com/sirupsen/logrus"
"github.com/sourcegraph/conc/pool"
Expand All @@ -37,11 +38,17 @@ var exploreCmd = &cobra.Command{
// any query or request during the runtime of the CLI. for proper cleanup,
// execute the returned function. The method returns once the sources are
// started. Progress is reported into the provided multi printer.
func StartLocalSources(ctx context.Context, oi sdp.OvermindInstance, token *oauth2.Token, tfArgs []string, multi *pterm.MultiPrinter) (func(), error) {
func StartLocalSources(ctx context.Context, oi sdp.OvermindInstance, token *oauth2.Token, tfArgs []string, failOverToAws bool) (func(), error) {
var err error

multi := pterm.DefaultMultiPrinter
_, _ = multi.Start()
defer func() {
_, _ = multi.Stop()
}()
stdlibSpinner, _ := pterm.DefaultSpinner.WithWriter(multi.NewWriter()).Start("Starting stdlib source engine")
awsSpinner, _ := pterm.DefaultSpinner.WithWriter(multi.NewWriter()).Start("Starting AWS source engine")
statusArea := pterm.DefaultBasicText.WithStyle(pterm.NewStyle(pterm.FgLightCyan)).WithWriter(multi.NewWriter())

natsOptions := natsOptions(ctx, oi, token)
heartbeatOptions := heartbeatOptions(oi, token)
Expand All @@ -54,7 +61,7 @@ func StartLocalSources(ctx context.Context, oi sdp.OvermindInstance, token *oaut
p := pool.NewWithResults[*discovery.Engine]().WithErrors()

p.Go(func() (*discovery.Engine, error) {
stdlibEngine, err := stdlibsource.InitializeEngine(
stdlibEngine, err := stdlibSource.InitializeEngine(
natsOptions,
fmt.Sprintf("stdlib-source-%v", hostname),
fmt.Sprintf("cli-%v", cliVersion),
Expand All @@ -67,7 +74,6 @@ func StartLocalSources(ctx context.Context, oi sdp.OvermindInstance, token *oaut
stdlibSpinner.Fail("Failed to initialize stdlib source engine")
return nil, fmt.Errorf("failed to initialize stdlib source engine: %w", err)
}

// todo: pass in context with timeout to abort timely and allow Ctrl-C to work
err = stdlibEngine.Start()
if err != nil {
Expand All @@ -91,7 +97,6 @@ func StartLocalSources(ctx context.Context, oi sdp.OvermindInstance, token *oaut
return nil, fmt.Errorf("failed to parse providers: %w", err)
}
configs := []aws.Config{}

for _, p := range providers {
if p.Error != nil {
// skip providers that had errors. This allows us to use
Expand All @@ -104,9 +109,19 @@ func StartLocalSources(ctx context.Context, oi sdp.OvermindInstance, token *oaut
awsSpinner.Fail("Error when converting provider to config")
return nil, fmt.Errorf("error when converting provider to config: %w", err)
}
credentials, _ := c.Credentials.Retrieve(ctx)
statusArea.Println(fmt.Sprintf("Using AWS provider in %s with %s.", p.FilePath, credentials.Source))
configs = append(configs, c)
}

if len(configs) == 0 && failOverToAws {
userConfig, err := config.LoadDefaultConfig(ctx)
if err != nil {
awsSpinner.Fail("Failed to load default AWS config")
return nil, fmt.Errorf("failed to load default AWS config: %w", err)
}
statusArea.Println("Using default AWS CLI config. No AWS terraform providers found.")
configs = append(configs, userConfig)
}
awsEngine, err := proc.InitializeAwsSourceEngine(
ctx,
fmt.Sprintf("aws-source-%v", hostname),
Expand Down Expand Up @@ -155,22 +170,22 @@ func Explore(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

multi := pterm.DefaultMultiPrinter

_, _ = multi.Start()

_, _ = multi.Start() // multi-printer controls the lifecycle of screen output, it needs to be stopped before printing anything else
defer func() {
_, _ = multi.Stop()
}()
ctx, oi, token, err := login(ctx, cmd, []string{"request:receive"}, multi.NewWriter())
_, _ = multi.Stop()
if err != nil {
return err
}

cleanup, err := StartLocalSources(ctx, oi, token, args, &multi)
cleanup, err := StartLocalSources(ctx, oi, token, args, true)
if err != nil {
return err
}
defer cleanup()

_, _ = multi.Stop()

exploreURL := fmt.Sprintf("%v/explore", oi.FrontendUrl)
err = browser.OpenURL(exploreURL)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/pterm.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func StartSources(ctx context.Context, cmd *cobra.Command, args []string) (conte
return ctx, sdp.OvermindInstance{}, nil, nil, err
}

cleanup, err := StartLocalSources(ctx, oi, token, args, &multi)
cleanup, err := StartLocalSources(ctx, oi, token, args, false)
if err != nil {
return ctx, sdp.OvermindInstance{}, nil, nil, err
}
Expand Down
11 changes: 5 additions & 6 deletions cmd/terraform_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TerraformApply(cmd *cobra.Command, args []string) error {
PTermSetup()

hasPlanSet := false
autoapprove := false
autoApprove := false
planFile := "overmind.plan"
if len(args) >= 1 {
f, err := os.Stat(args[len(args)-1])
Expand All @@ -51,7 +51,7 @@ func TerraformApply(cmd *cobra.Command, args []string) error {
}
if hasPlanSet {
planFile = args[len(args)-1]
autoapprove = true
autoApprove = true
}
}

Expand All @@ -76,18 +76,18 @@ func TerraformApply(cmd *cobra.Command, args []string) error {
// therefore we only check for the flag when no plan file is supplied
for _, a := range args {
if a == "-auto-approve" || a == "-auto-approve=true" || a == "-auto-approve=TRUE" || a == "--auto-approve" || a == "--auto-approve=true" || a == "--auto-approve=TRUE" {
autoapprove = true
autoApprove = true
}
if a == "-auto-approve=false" || a == "-auto-approve=FALSE" || a == "--auto-approve=false" || a == "--auto-approve=FALSE" {
autoapprove = false
autoApprove = false
}
}
}

args = append([]string{"apply"}, args...)

needPlan := !hasPlanSet
needApproval := !autoapprove
needApproval := !autoApprove

ctx, oi, _, cleanup, err := StartSources(ctx, cmd, args)
if err != nil {
Expand All @@ -103,7 +103,6 @@ func TerraformApply(cmd *cobra.Command, args []string) error {
}

if needApproval {

pterm.Println("")
pterm.Println("Do you want to perform these actions?")
pterm.Println("")
Expand Down

0 comments on commit 989d410

Please sign in to comment.