Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: [MER-2339] add av auth status (#145)
- Loading branch information
Showing
7 changed files
with
107 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var authCmd = &cobra.Command{ | ||
Use: "auth", | ||
Short: "manage authentication", | ||
} | ||
|
||
func init() { | ||
authCmd.AddCommand( | ||
authStatusCmd, | ||
) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/aviator-co/av/internal/actions" | ||
"github.com/aviator-co/av/internal/avgql" | ||
"github.com/aviator-co/av/internal/utils/colors" | ||
"github.com/shurcooL/graphql" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var authStatusCmd = &cobra.Command{ | ||
Use: "status", | ||
Short: "check auth status", | ||
SilenceUsage: true, | ||
Args: cobra.NoArgs, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
client := avgql.NewClient() | ||
|
||
var query struct { | ||
Viewer struct { | ||
Email graphql.String | ||
} | ||
} | ||
|
||
err := client.Query(context.Background(), &query, nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if query.Viewer.Email == "" { | ||
_, _ = fmt.Fprint( | ||
os.Stderr, | ||
colors.Failure( | ||
"You are not logged in. Please verify that your API token is correct.\n", | ||
), | ||
) | ||
return actions.ErrExitSilently{ExitCode: 1} | ||
} | ||
|
||
_, _ = fmt.Fprint(os.Stderr, "Logged in as ", colors.UserInput(query.Viewer.Email), ".\n") | ||
return nil | ||
}, | ||
} |
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 |
---|---|---|
|
@@ -92,6 +92,7 @@ func init() { | |
prCmd, | ||
stackCmd, | ||
versionCmd, | ||
authCmd, | ||
) | ||
} | ||
|
||
|
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package avgql | ||
|
||
import ( | ||
"context" | ||
"os" | ||
|
||
"github.com/aviator-co/av/internal/config" | ||
"github.com/shurcooL/graphql" | ||
"golang.org/x/oauth2" | ||
) | ||
|
||
func NewClient() *graphql.Client { | ||
src := oauth2.StaticTokenSource( | ||
&oauth2.Token{AccessToken: config.Av.Aviator.APIToken}, | ||
) | ||
httpClient := oauth2.NewClient(context.Background(), src) | ||
apiURL := os.Getenv("AV_GRAPHQL_URL") | ||
if apiURL == "" { | ||
apiURL = "https://api.aviator.co/graphql" | ||
} | ||
return graphql.NewClient(apiURL, httpClient) | ||
} |
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