Skip to content

Commit 614313b

Browse files
committed
Refactor external auth config structure for defaults
1 parent a2ccfb0 commit 614313b

File tree

10 files changed

+331
-338
lines changed

10 files changed

+331
-338
lines changed

cli/cliui/gitauth.go renamed to cli/cliui/externalauth.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import (
1111
"github.com/coder/coder/v2/codersdk"
1212
)
1313

14-
type GitAuthOptions struct {
14+
type ExternalAuthOptions struct {
1515
Fetch func(context.Context) ([]codersdk.TemplateVersionExternalAuth, error)
1616
FetchInterval time.Duration
1717
}
1818

19-
func GitAuth(ctx context.Context, writer io.Writer, opts GitAuthOptions) error {
19+
func ExternalAuth(ctx context.Context, writer io.Writer, opts ExternalAuthOptions) error {
2020
if opts.FetchInterval == 0 {
2121
opts.FetchInterval = 500 * time.Millisecond
2222
}
@@ -38,7 +38,7 @@ func GitAuth(ctx context.Context, writer io.Writer, opts GitAuthOptions) error {
3838
return nil
3939
}
4040

41-
_, _ = fmt.Fprintf(writer, "You must authenticate with %s to create a workspace with this template. Visit:\n\n\t%s\n\n", auth.Type.Pretty(), auth.AuthenticateURL)
41+
_, _ = 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)
4242

4343
ticker.Reset(opts.FetchInterval)
4444
spin.Start()
@@ -66,7 +66,7 @@ func GitAuth(ctx context.Context, writer io.Writer, opts GitAuthOptions) error {
6666
}
6767
}
6868
spin.Stop()
69-
_, _ = fmt.Fprintf(writer, "Successfully authenticated with %s!\n\n", auth.Type.Pretty())
69+
_, _ = fmt.Fprintf(writer, "Successfully authenticated with %s!\n\n", auth.DisplayName)
7070
}
7171
return nil
7272
}

cli/cliui/gitauth_test.go renamed to cli/cliui/externalauth_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
"github.com/coder/coder/v2/testutil"
1616
)
1717

18-
func TestGitAuth(t *testing.T) {
18+
func TestExternalAuth(t *testing.T) {
1919
t.Parallel()
2020

2121
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
@@ -25,11 +25,12 @@ func TestGitAuth(t *testing.T) {
2525
cmd := &clibase.Cmd{
2626
Handler: func(inv *clibase.Invocation) error {
2727
var fetched atomic.Bool
28-
return cliui.GitAuth(inv.Context(), inv.Stdout, cliui.GitAuthOptions{
28+
return cliui.ExternalAuth(inv.Context(), inv.Stdout, cliui.ExternalAuthOptions{
2929
Fetch: func(ctx context.Context) ([]codersdk.TemplateVersionExternalAuth, error) {
3030
defer fetched.Store(true)
3131
return []codersdk.TemplateVersionExternalAuth{{
3232
ID: "github",
33+
DisplayName: "GitHub",
3334
Type: codersdk.ExternalAuthProviderGitHub,
3435
Authenticated: fetched.Load(),
3536
AuthenticateURL: "https://example.com/gitauth/github",

cli/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ func prepWorkspaceBuild(inv *clibase.Invocation, client *codersdk.Client, args p
265265
return nil, err
266266
}
267267

268-
err = cliui.GitAuth(ctx, inv.Stdout, cliui.GitAuthOptions{
268+
err = cliui.ExternalAuth(ctx, inv.Stdout, cliui.ExternalAuthOptions{
269269
Fetch: func(ctx context.Context) ([]codersdk.TemplateVersionExternalAuth, error) {
270270
return client.TemplateVersionExternalAuth(ctx, templateVersion.ID)
271271
},

cli/server.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,11 @@ import (
101101
// ReadGitAuthProvidersFromEnv is provided for compatibility purposes with the
102102
// viper CLI.
103103
// DEPRECATED
104-
func ReadGitAuthProvidersFromEnv(environ []string) ([]codersdk.GitAuthConfig, error) {
104+
func ReadGitAuthProvidersFromEnv(environ []string) ([]codersdk.ExternalAuthConfig, error) {
105105
// The index numbers must be in-order.
106106
sort.Strings(environ)
107107

108-
var providers []codersdk.GitAuthConfig
108+
var providers []codersdk.ExternalAuthConfig
109109
for _, v := range clibase.ParseEnviron(environ, "CODER_GITAUTH_") {
110110
tokens := strings.SplitN(v.Name, "_", 2)
111111
if len(tokens) != 2 {
@@ -117,7 +117,7 @@ func ReadGitAuthProvidersFromEnv(environ []string) ([]codersdk.GitAuthConfig, er
117117
return nil, xerrors.Errorf("parse number: %s", v.Name)
118118
}
119119

120-
var provider codersdk.GitAuthConfig
120+
var provider codersdk.ExternalAuthConfig
121121
switch {
122122
case len(providers) < providerNum:
123123
return nil, xerrors.Errorf(
@@ -820,7 +820,7 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
820820
if vals.Telemetry.Enable {
821821
gitAuth := make([]telemetry.GitAuth, 0)
822822
// TODO:
823-
var gitAuthConfigs []codersdk.GitAuthConfig
823+
var gitAuthConfigs []codersdk.ExternalAuthConfig
824824
for _, cfg := range gitAuthConfigs {
825825
gitAuth = append(gitAuth, telemetry.GitAuth{
826826
Type: cfg.Type,

cmd/cliui/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ func main() {
331331
// Complete the auth!
332332
gitlabAuthed.Store(true)
333333
}()
334-
return cliui.GitAuth(inv.Context(), inv.Stdout, cliui.GitAuthOptions{
334+
return cliui.ExternalAuth(inv.Context(), inv.Stdout, cliui.ExternalAuthOptions{
335335
Fetch: func(ctx context.Context) ([]codersdk.TemplateVersionExternalAuth, error) {
336336
count.Add(1)
337337
return []codersdk.TemplateVersionExternalAuth{{

0 commit comments

Comments
 (0)