Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(refactor): remove unneeded goroutines in cloud login #3075

Merged
merged 2 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 33 additions & 44 deletions cmd/flipt/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ func newCloudCommand() *cobra.Command {

func (c *cloudCommand) login(cmd *cobra.Command, args []string) error {
var (
done = make(chan struct{})
ctx, cancel = context.WithCancel(cmd.Context())
_, cfg, err = buildConfig(ctx)
)
Expand Down Expand Up @@ -125,61 +124,51 @@ func (c *cloudCommand) login(cmd *cobra.Command, args []string) error {
return fmt.Errorf("initializing flow: %w", err)
}

var g errgroup.Group
defer flow.Close()

cloudAuthFile := filepath.Join(userConfigDir, "cloud.json")
var g errgroup.Group

g.Go(func() error {
tok, err := flow.Wait(ctx)
if err != nil {
return fmt.Errorf("waiting for token: %w", err)
if err := flow.StartServer(nil); err != nil && !errors.Is(err, net.ErrClosed) {
return fmt.Errorf("starting server: %w", err)
}
return nil
})

cloudAuth := cloudAuth{
Token: tok,
}
url, err := flow.BrowserURL(fmt.Sprintf("%s/login/device", c.url))
if err != nil {
return fmt.Errorf("creating browser URL: %w", err)
}

cloudAuthBytes, err := json.Marshal(cloudAuth)
if err != nil {
return fmt.Errorf("marshalling cloud auth token: %w", err)
}
if err := util.OpenBrowser(url); err != nil {
return fmt.Errorf("opening browser: %w", err)
}

if err := os.WriteFile(cloudAuthFile, cloudAuthBytes, 0600); err != nil {
return fmt.Errorf("writing cloud auth token: %w", err)
}
cloudAuthFile := filepath.Join(userConfigDir, "cloud.json")

fmt.Println("\n✓ Authenticated with Flipt Cloud!\nYou can now run commands that require cloud authentication.")
tok, err := flow.Wait(ctx)
if err != nil {
return fmt.Errorf("waiting for token: %w", err)
}

return nil
})
if err := flow.Close(); err != nil {
return fmt.Errorf("closing flow: %w", err)
}

g.Go(func() error {
if err := flow.StartServer(nil); err != nil && !errors.Is(err, net.ErrClosed) {
return fmt.Errorf("starting server: %w", err)
}
close(done)
return nil
})
cloudAuth := cloudAuth{
Token: tok,
}

g.Go(func() error {
select {
case <-done:
cancel()
case <-ctx.Done():
if err := flow.Close(); err != nil && !errors.Is(err, context.Canceled) {
return err
}
}
return nil
})
cloudAuthBytes, err := json.Marshal(cloudAuth)
if err != nil {
return fmt.Errorf("marshalling cloud auth token: %w", err)
}

g.Go(func() error {
url, err := flow.BrowserURL(fmt.Sprintf("%s/login/device", c.url))
if err != nil {
return fmt.Errorf("creating browser URL: %w", err)
}
return util.OpenBrowser(url)
})
if err := os.WriteFile(cloudAuthFile, cloudAuthBytes, 0600); err != nil {
return fmt.Errorf("writing cloud auth token: %w", err)
}

fmt.Println("\n✓ Authenticated with Flipt Cloud!\nYou can now run commands that require cloud authentication.")

return g.Wait()
}
Expand Down
Loading
Loading