| 17 | } |
| 18 | |
| 19 | func ExternalAuth(ctx context.Context, writer io.Writer, opts ExternalAuthOptions) error { |
| 20 | if opts.FetchInterval == 0 { |
| 21 | opts.FetchInterval = 500 * time.Millisecond |
| 22 | } |
| 23 | gitAuth, err := opts.Fetch(ctx) |
| 24 | if err != nil { |
| 25 | return err |
| 26 | } |
| 27 | |
| 28 | spin := spinner.New(spinner.CharSets[78], 100*time.Millisecond, spinner.WithColor("fgHiGreen")) |
| 29 | spin.Writer = writer |
| 30 | spin.ForceOutput = true |
| 31 | spin.Suffix = " Waiting for Git authentication..." |
| 32 | defer spin.Stop() |
| 33 | |
| 34 | ticker := time.NewTicker(opts.FetchInterval) |
| 35 | defer ticker.Stop() |
| 36 | for _, auth := range gitAuth { |
| 37 | if auth.Authenticated { |
| 38 | return nil |
| 39 | } |
| 40 | if auth.Optional { |
| 41 | continue |
| 42 | } |
| 43 | |
| 44 | _, _ = fmt.Fprintf(writer, "You must authenticate with %s to create a workspace with this template. Visit:\n\n\t%s\n\n", auth.DisplayName, auth.AuthenticateURL) |
| 45 | |
| 46 | ticker.Reset(opts.FetchInterval) |
| 47 | spin.Start() |
| 48 | for { |
| 49 | select { |
| 50 | case <-ctx.Done(): |
| 51 | return ctx.Err() |
| 52 | case <-ticker.C: |
| 53 | } |
| 54 | gitAuth, err := opts.Fetch(ctx) |
| 55 | if err != nil { |
| 56 | return err |
| 57 | } |
| 58 | var authed bool |
| 59 | for _, a := range gitAuth { |
| 60 | if !a.Authenticated || a.ID != auth.ID { |
| 61 | continue |
| 62 | } |
| 63 | authed = true |
| 64 | break |
| 65 | } |
| 66 | // The user authenticated with the provider! |
| 67 | if authed { |
| 68 | break |
| 69 | } |
| 70 | } |
| 71 | spin.Stop() |
| 72 | _, _ = fmt.Fprintf(writer, "Successfully authenticated with %s!\n\n", auth.DisplayName) |
| 73 | } |
| 74 | return nil |
| 75 | } |