-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b806ceb
commit 85f9fe6
Showing
1 changed file
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/overmindtech/aws-source/proc" | ||
"github.com/overmindtech/cli/tfutils" | ||
stdlibsource "github.com/overmindtech/stdlib-source/sources" | ||
"github.com/pterm/pterm" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// exploreCmd represents the explore command | ||
var exploreCmd = &cobra.Command{ | ||
Use: "explore", | ||
Short: "Run local sources for using in the Explore page", | ||
Long: `Run sources locally using terraform's configured authorization to provide data when using https://app.overmind.tech/explore.`, | ||
PreRun: PreRunSetup, | ||
RunE: Explore, | ||
|
||
// SilenceErrors: false, | ||
} | ||
|
||
func Explore(cmd *cobra.Command, args []string) error { | ||
ctx := cmd.Context() | ||
|
||
multi := pterm.DefaultMultiPrinter | ||
|
||
loginSpinner, _ := pterm.DefaultSpinner.WithWriter(multi.NewWriter()).Start("Connecting to Overmind") | ||
|
||
_, _ = multi.Start() | ||
defer func() { | ||
// TODO: remove debugging delay | ||
pterm.DefaultParagraph.WithWriter(multi.NewWriter()).Println("Sleeping for 5 seconds") | ||
time.Sleep(time.Second * 5) | ||
_, _ = multi.Stop() | ||
}() | ||
|
||
ctx, oi, token, err := login(ctx, cmd, []string{"explore:read", "changes:read"}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
loginSpinner.Success("Connected to Overmind") | ||
|
||
stdlibSpinner, _ := pterm.DefaultSpinner.WithWriter(multi.NewWriter()).Start("Starting stdlib source engine") | ||
awsSpinner, _ := pterm.DefaultSpinner.WithWriter(multi.NewWriter()).Start("Starting AWS source engine") | ||
|
||
natsOptions := natsOptions(ctx, oi, token) | ||
|
||
stdlibEngine, err := stdlibsource.InitializeEngine(natsOptions, 2_000, true) | ||
if err != nil { | ||
stdlibSpinner.Fail("Failed to initialize stdlib source engine") | ||
return 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 { | ||
stdlibSpinner.Fail("Failed to start stdlib source engine") | ||
return fmt.Errorf("failed to start stdlib source engine: %w", err) | ||
} | ||
stdlibSpinner.Success("Stdlib source engine started") | ||
|
||
tfEval, err := tfutils.LoadEvalContext(args, os.Environ()) | ||
if err != nil { | ||
awsSpinner.Fail("Failed to load variables from the environment") | ||
return fmt.Errorf("failed to load variables from the environment: %w", err) | ||
} | ||
|
||
providers, err := tfutils.ParseAWSProviders(".", tfEval) | ||
if err != nil { | ||
awsSpinner.Fail("Failed to parse providers") | ||
return 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 | ||
// providers we _could_ detect, while still failing if there is | ||
// a true syntax error and no providers are available at all. | ||
continue | ||
} | ||
c, err := tfutils.ConfigFromProvider(ctx, *p.Provider) | ||
if err != nil { | ||
awsSpinner.Fail("Error when converting provider to config") | ||
return fmt.Errorf("error when converting provider to config: %w", err) | ||
} | ||
configs = append(configs, c) | ||
} | ||
|
||
awsEngine, err := proc.InitializeAwsSourceEngine(ctx, natsOptions, 2_000, configs...) | ||
if err != nil { | ||
awsSpinner.Fail("Failed to initialize AWS source engine") | ||
return fmt.Errorf("failed to initialize AWS source engine: %w", err) | ||
} | ||
|
||
// todo: pass in context with timeout to abort timely and allow Ctrl-C to work | ||
err = awsEngine.Start() | ||
if err != nil { | ||
awsSpinner.Fail("Failed to start AWS source engine") | ||
return fmt.Errorf("failed to start AWS source engine: %w", err) | ||
} | ||
|
||
awsSpinner.Success("AWS source engine started") | ||
|
||
// for i := 0; i < 20; i++ { | ||
// fmt.Println("terraform plan output") | ||
// } | ||
|
||
// multi.Start() | ||
// _, err = multi.Stop() | ||
return nil | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(exploreCmd) | ||
|
||
addAPIFlags(exploreCmd) | ||
|
||
// Here you will define your flags and configuration settings. | ||
|
||
// Cobra supports Persistent Flags which will work for this command | ||
// and all subcommands, e.g.: | ||
// exploreCmd.PersistentFlags().String("foo", "", "A help for foo") | ||
|
||
// Cobra supports local flags which will only run when this command | ||
// is called directly, e.g.: | ||
// exploreCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") | ||
} |