|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | +"fmt" |
| 5 | +"net/url" |
| 6 | +"os/user" |
| 7 | +"strings" |
| 8 | + |
| 9 | +"github.com/fatih/color" |
| 10 | +"github.com/go-playground/validator/v10" |
| 11 | +"github.com/manifoldco/promptui" |
| 12 | +"github.com/spf13/cobra" |
| 13 | +"golang.org/x/xerrors" |
| 14 | + |
| 15 | +"github.com/coder/coder/coderd" |
| 16 | +"github.com/coder/coder/codersdk" |
| 17 | +) |
| 18 | + |
| 19 | +funclogin()*cobra.Command { |
| 20 | +return&cobra.Command{ |
| 21 | +Use:"login <url>", |
| 22 | +Args:cobra.ExactArgs(1), |
| 23 | +RunE:func(cmd*cobra.Command,args []string)error { |
| 24 | +rawURL:=args[0] |
| 25 | +if!strings.HasPrefix(rawURL,"http://")&&!strings.HasPrefix(rawURL,"https://") { |
| 26 | +scheme:="https" |
| 27 | +ifstrings.HasPrefix(rawURL,"localhost") { |
| 28 | +scheme="http" |
| 29 | +} |
| 30 | +rawURL=fmt.Sprintf("%s://%s",scheme,rawURL) |
| 31 | +} |
| 32 | +serverURL,err:=url.Parse(rawURL) |
| 33 | +iferr!=nil { |
| 34 | +returnxerrors.Errorf("parse raw url %q: %w",rawURL,err) |
| 35 | +} |
| 36 | +// Default to HTTPs. Enables simple URLs like: master.cdr.dev |
| 37 | +ifserverURL.Scheme=="" { |
| 38 | +serverURL.Scheme="https" |
| 39 | +} |
| 40 | + |
| 41 | +client:=codersdk.New(serverURL) |
| 42 | +hasInitialUser,err:=client.HasInitialUser(cmd.Context()) |
| 43 | +iferr!=nil { |
| 44 | +returnxerrors.Errorf("has initial user: %w",err) |
| 45 | +} |
| 46 | +if!hasInitialUser { |
| 47 | +if!isTTY(cmd.InOrStdin()) { |
| 48 | +returnxerrors.New("the initial user cannot be created in non-interactive mode. use the API") |
| 49 | +} |
| 50 | +_,_=fmt.Fprintf(cmd.OutOrStdout(),"%s Your Coder deployment hasn't been set up!\n",color.HiBlackString(">")) |
| 51 | + |
| 52 | +_,err:=runPrompt(cmd,&promptui.Prompt{ |
| 53 | +Label:"Would you like to create the first user?", |
| 54 | +IsConfirm:true, |
| 55 | +Default:"y", |
| 56 | +}) |
| 57 | +iferr!=nil { |
| 58 | +returnxerrors.Errorf("create user prompt: %w",err) |
| 59 | +} |
| 60 | +currentUser,err:=user.Current() |
| 61 | +iferr!=nil { |
| 62 | +returnxerrors.Errorf("get current user: %w",err) |
| 63 | +} |
| 64 | +username,err:=runPrompt(cmd,&promptui.Prompt{ |
| 65 | +Label:"What username would you like?", |
| 66 | +Default:currentUser.Username, |
| 67 | +}) |
| 68 | +iferr!=nil { |
| 69 | +returnxerrors.Errorf("pick username prompt: %w",err) |
| 70 | +} |
| 71 | + |
| 72 | +organization,err:=runPrompt(cmd,&promptui.Prompt{ |
| 73 | +Label:"What is the name of your organization?", |
| 74 | +Default:"acme-corp", |
| 75 | +}) |
| 76 | +iferr!=nil { |
| 77 | +returnxerrors.Errorf("pick organization prompt: %w",err) |
| 78 | +} |
| 79 | + |
| 80 | +email,err:=runPrompt(cmd,&promptui.Prompt{ |
| 81 | +Label:"What's your email?", |
| 82 | +Validate:func(sstring)error { |
| 83 | +err:=validator.New().Var(s,"email") |
| 84 | +iferr!=nil { |
| 85 | +returnxerrors.New("That's not a valid email address!") |
| 86 | +} |
| 87 | +returnerr |
| 88 | +}, |
| 89 | +}) |
| 90 | +iferr!=nil { |
| 91 | +returnxerrors.Errorf("specify email prompt: %w",err) |
| 92 | +} |
| 93 | + |
| 94 | +password,err:=runPrompt(cmd,&promptui.Prompt{ |
| 95 | +Label:"Enter a password:", |
| 96 | +Mask:'*', |
| 97 | +}) |
| 98 | +iferr!=nil { |
| 99 | +returnxerrors.Errorf("specify password prompt: %w",err) |
| 100 | +} |
| 101 | + |
| 102 | +_,err=client.CreateInitialUser(cmd.Context(), coderd.CreateInitialUserRequest{ |
| 103 | +Email:email, |
| 104 | +Username:username, |
| 105 | +Password:password, |
| 106 | +Organization:organization, |
| 107 | +}) |
| 108 | +iferr!=nil { |
| 109 | +returnxerrors.Errorf("create initial user: %w",err) |
| 110 | +} |
| 111 | +resp,err:=client.LoginWithPassword(cmd.Context(), coderd.LoginWithPasswordRequest{ |
| 112 | +Email:email, |
| 113 | +Password:password, |
| 114 | +}) |
| 115 | +iferr!=nil { |
| 116 | +returnxerrors.Errorf("login with password: %w",err) |
| 117 | +} |
| 118 | +config:=createConfig(cmd) |
| 119 | +err=config.Session().Write(resp.SessionToken) |
| 120 | +iferr!=nil { |
| 121 | +returnxerrors.Errorf("write session token: %w",err) |
| 122 | +} |
| 123 | +err=config.URL().Write(serverURL.String()) |
| 124 | +iferr!=nil { |
| 125 | +returnxerrors.Errorf("write server url: %w",err) |
| 126 | +} |
| 127 | + |
| 128 | +_,_=fmt.Fprintf(cmd.OutOrStdout(),"%s Welcome to Coder, %s! You're authenticated.\n",color.HiBlackString(">"),color.HiCyanString(username)) |
| 129 | +returnnil |
| 130 | +} |
| 131 | + |
| 132 | +returnnil |
| 133 | +}, |
| 134 | +} |
| 135 | +} |