-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
272 additions
and
243 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,86 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"emperror.dev/errors" | ||
"github.com/aviator-co/av/internal/gh" | ||
|
||
"github.com/aviator-co/av/internal/avgql" | ||
"github.com/aviator-co/av/internal/utils/colors" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var authCmd = &cobra.Command{ | ||
Use: "auth", | ||
Short: "Manage authentication", | ||
Use: "auth", | ||
Short: "Check user authentication status", | ||
SilenceUsage: true, | ||
Args: cobra.NoArgs, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if err := checkAviatorAuthStatus(); err != nil { | ||
fmt.Fprintln(os.Stderr, colors.Warning(err.Error())) | ||
} | ||
if err := checkGitHubAuthStatus(); err != nil { | ||
fmt.Fprintln(os.Stderr, colors.Failure(err.Error())) | ||
} | ||
}, | ||
} | ||
|
||
func checkAviatorAuthStatus() error { | ||
avClient, err := avgql.NewClient() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var query struct{ avgql.ViewerSubquery } | ||
if err := avClient.Query(context.Background(), &query, nil); err != nil { | ||
return err | ||
} | ||
if err := query.CheckViewer(); err != nil { | ||
return err | ||
} | ||
|
||
fmt.Fprint(os.Stderr, | ||
"Logged in to Aviator as ", colors.UserInput(query.Viewer.FullName), | ||
" (", colors.UserInput(query.Viewer.Email), ").\n", | ||
) | ||
return nil | ||
} | ||
|
||
func checkGitHubAuthStatus() error { | ||
ghClient, err := getGitHubClient() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
viewer, err := ghClient.Viewer(context.Background()) | ||
if err != nil { | ||
// GitHub API returns 401 Unauthorized if the token is invalid or | ||
// expired. | ||
if gh.IsHTTPUnauthorized(err) { | ||
return errors.New( | ||
"You are not logged in to GitHub. Please verify that your API token is correct.", | ||
) | ||
} | ||
return errors.Wrap(err, "Failed to query GitHub") | ||
} | ||
|
||
fmt.Fprint(os.Stderr, | ||
"Logged in to GitHub as ", colors.UserInput(viewer.Name), | ||
" (", colors.UserInput(viewer.Login), ").\n", | ||
) | ||
return nil | ||
} | ||
|
||
func init() { | ||
// deprecated 'av auth status', hidden to avoid it showing up in 'av auth --help' | ||
// since that is the new command name | ||
var deprecatedAuthStatus = deprecateCommand(*authCmd, "av auth", "status") | ||
deprecatedAuthStatus.Hidden = true | ||
|
||
authCmd.AddCommand( | ||
authStatusCmd, | ||
deprecatedAuthStatus, | ||
) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,6 +99,7 @@ func init() { | |
stackCmd, | ||
versionCmd, | ||
authCmd, | ||
syncCmd, | ||
) | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.