-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Move version to internal package * Add internal config package * Add internal client package * Refactor subcommands to use internal client constructor * Add VerifyToken method * Add login subcommand * Update demo tape
- Loading branch information
Showing
22 changed files
with
371 additions
and
68 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/replicate/cli/internal" | ||
"github.com/replicate/cli/internal/config" | ||
"github.com/replicate/replicate-go" | ||
) | ||
|
||
func NewClient(opts ...replicate.ClientOption) (*replicate.Client, error) { | ||
baseURL := config.GetAPIBaseURL() | ||
|
||
token, err := config.GetAPIToken() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get API token: %w", err) | ||
} | ||
if token == "" { | ||
return nil, fmt.Errorf("please authenticate with `replicate login`") | ||
} | ||
|
||
userAgent := fmt.Sprintf("replicate/%s", internal.Version()) | ||
|
||
opts = append([]replicate.ClientOption{ | ||
replicate.WithBaseURL(baseURL), | ||
replicate.WithToken(token), | ||
replicate.WithUserAgent(userAgent), | ||
}, opts...) | ||
|
||
r8, err := replicate.NewClient(opts...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return r8, nil | ||
} | ||
|
||
func VerifyToken(ctx context.Context, token string) (bool, error) { | ||
r8, err := NewClient(replicate.WithToken(token)) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
// FIXME: Add better endpoint for verifying token | ||
_, err = r8.ListHardware(ctx) | ||
if err != nil { | ||
return false, nil | ||
} | ||
|
||
return true, 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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package auth | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"strings" | ||
|
||
"github.com/replicate/cli/internal/client" | ||
"github.com/replicate/cli/internal/config" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// loginCmd represents the login command | ||
var loginCmd = &cobra.Command{ | ||
Use: "login --token-stdin", | ||
Short: "Log in to Replicate", | ||
Long: `Log in to Replicate | ||
You can find your Replicate API token at https://replicate.com/account`, | ||
Example: ` | ||
# Log in with environment variable | ||
$ echo $REPLICATE_API_TOKEN | replicate auth login --token-stdin | ||
# Log in with token file | ||
$ replicate auth login --token-stdin < path/to/token`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
ctx := cmd.Context() | ||
|
||
tokenStdin, err := cmd.Flags().GetBool("token-stdin") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var token string | ||
if tokenStdin { | ||
token, err = readTokenFromStdin() | ||
if err != nil { | ||
return fmt.Errorf("failed to read token from stdin: %w", err) | ||
} | ||
} else { | ||
return fmt.Errorf("token must be passed to stdin with --token-stdin flag") | ||
} | ||
token = strings.TrimSpace(token) | ||
|
||
ok, err := client.VerifyToken(ctx, token) | ||
if err != nil { | ||
return fmt.Errorf("error verifying token: %w", err) | ||
} | ||
if !ok { | ||
return fmt.Errorf("invalid token") | ||
} | ||
|
||
if err := config.SetAPIToken(token); err != nil { | ||
return fmt.Errorf("failed to set API token: %w", err) | ||
} | ||
|
||
fmt.Println("Login successful.") | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
func readTokenFromStdin() (string, error) { | ||
tokenBytes, err := io.ReadAll(os.Stdin) | ||
if err != nil { | ||
return "", fmt.Errorf("Failed to read token from stdin: %w", err) | ||
} | ||
return string(tokenBytes), nil | ||
} | ||
|
||
func init() { | ||
loginCmd.Flags().Bool("token-stdin", false, "Take the token from stdin.") | ||
_ = loginCmd.MarkFlagRequired("token-stdin") | ||
} |
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,23 @@ | ||
package auth | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var RootCmd = &cobra.Command{ | ||
Use: "auth [subcommand]", | ||
Short: "Authenticate with Replicate", | ||
} | ||
|
||
func init() { | ||
RootCmd.AddGroup(&cobra.Group{ | ||
ID: "subcommand", | ||
Title: "Subcommands:", | ||
}) | ||
for _, cmd := range []*cobra.Command{ | ||
loginCmd, | ||
} { | ||
RootCmd.AddCommand(cmd) | ||
cmd.GroupID = "subcommand" | ||
} | ||
} |
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.