Skip to content

Commit

Permalink
chore(refactor): remove unneeded goroutines in cloud login (#3075)
Browse files Browse the repository at this point in the history
  • Loading branch information
markphelps authored May 13, 2024
1 parent 51e6b06 commit 057191a
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 46 deletions.
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

0 comments on commit 057191a

Please sign in to comment.