-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #164 from gphotosuploader/add-config-auth
Add a new command to refresh tokens
- Loading branch information
Showing
3 changed files
with
78 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
"golang.org/x/oauth2" | ||
|
||
"github.com/gphotosuploader/gphotos-uploader-cli/app" | ||
"github.com/gphotosuploader/gphotos-uploader-cli/cmd/flags" | ||
"github.com/gphotosuploader/gphotos-uploader-cli/config" | ||
"github.com/gphotosuploader/gphotos-uploader-cli/photos" | ||
) | ||
|
||
// InitCmd holds the required data for the init cmd | ||
type AuthCmd struct { | ||
*flags.GlobalFlags | ||
} | ||
|
||
func NewAuthCmd(globalFlags *flags.GlobalFlags) *cobra.Command { | ||
cmd := &AuthCmd{GlobalFlags: globalFlags} | ||
|
||
authCmd := &cobra.Command{ | ||
Use: "auth", | ||
Short: "Authenticate with Google Photos to refresh tokens", | ||
Long: `Force authentication against Google Photos to refresh tokens.`, | ||
Args: cobra.NoArgs, | ||
RunE: cmd.Run, | ||
} | ||
|
||
return authCmd | ||
} | ||
|
||
func (cmd *AuthCmd) Run(cobraCmd *cobra.Command, args []string) error { | ||
cfg, err := config.LoadConfigAndValidate(cmd.CfgDir) | ||
if err != nil { | ||
return fmt.Errorf("please review your configuration or run 'gphotos-uploader-cli init': file=%s, err=%s", cmd.CfgDir, err) | ||
} | ||
|
||
cli, err := app.Start(cfg) | ||
if err != nil { | ||
return err | ||
} | ||
defer func() { | ||
_ = cli.Stop() | ||
}() | ||
|
||
// get OAuth2 Configuration with our App credentials | ||
oauth2Config := oauth2.Config{ | ||
ClientID: cfg.APIAppCredentials.ClientID, | ||
ClientSecret: cfg.APIAppCredentials.ClientSecret, | ||
Endpoint: photos.Endpoint, | ||
Scopes: photos.Scopes, | ||
} | ||
|
||
ctx := context.Background() | ||
for _, job := range cfg.Jobs { | ||
if _, err := cli.NewOAuth2Client(ctx, oauth2Config, job.Account); err != nil { | ||
cli.Logger.Failf("Failed authentication for account: %s", job.Account) | ||
cli.Logger.Debugf("Authentication error: err=%s", err) | ||
return err | ||
} | ||
cli.Logger.Donef("Successful authentication for account: %s", job.Account) | ||
} | ||
|
||
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