Skip to content

Commit

Permalink
return an error instead of attempting to auth without the right info
Browse files Browse the repository at this point in the history
  • Loading branch information
dan9186 committed Apr 30, 2024
1 parent 7c34432 commit 0a277e4
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions cmd/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,24 @@ var authCmd = &cobra.Command{
Use: "auth",
Short: "auth with github",
Long: `authorize align against github`,
Run: authFunc,
RunE: authFunc,
}

const (
state = "9292a768-34bf-4002-8a69-8ace5302709d"
)

func authFunc(cmd *cobra.Command, args []string) {
func authFunc(cmd *cobra.Command, args []string) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

if clientID == "" || clientSecret == "" {
return fmt.Errorf("client id and secret must be baked into the binary, and are not present")
}

listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
fmt.Printf("Error: %v", err.Error())
os.Exit(1)
return err
}

port := listener.Addr().(*net.TCPAddr).Port
Expand All @@ -54,8 +57,7 @@ func authFunc(cmd *cobra.Command, args []string) {

certs, err := pool.CACerts()
if err != nil {
fmt.Printf("failed to create cert pool: %v\n", err.Error())
os.Exit(1)
return fmt.Errorf("failed to create cert pool: %w", err)
}

httpClient := &http.Client{
Expand Down Expand Up @@ -95,26 +97,25 @@ func authFunc(cmd *cobra.Command, args []string) {

err = openBrowser(url)
if err != nil {
fmt.Printf("Error: %v", err.Error())
os.Exit(1)
return err
}

tkn := <-token
close(token)

c, err := config.ParseFromFile()
if err != nil {
fmt.Printf("Error: %v", err.Error())
os.Exit(1)
return err
}

c.Github.Token = tkn

err = c.WriteFile()
if err != nil {
fmt.Printf("Error: %v", err.Error())
os.Exit(1)
return err
}

return nil
}

func startServer(ctx context.Context, listener net.Listener, conf *oauth2.Config, token chan string) {
Expand Down

0 comments on commit 0a277e4

Please sign in to comment.