Skip to content

Commit

Permalink
Merge pull request #164 from gphotosuploader/add-config-auth
Browse files Browse the repository at this point in the history
Add a new command to refresh tokens
  • Loading branch information
pacoorozco authored Dec 6, 2019
2 parents 8134953 + 4c02f4f commit 4f761fe
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/) and this project adheres to [Semantic Versioning](https://semver.org/).

## 1.0.1
### Added
- New command `auth` to authenticate against Google Photos. It's useful to refresh authentication tokens. ([#125][i125])

[i125]: https://github.com/gphotosuploader/gphotos-uploader-cli/issues/125

## 1.0.0
> This is a **major upgrade** and it has several **non-backwards compatible changes**. See more details below.
### Added
Expand Down
71 changes: 71 additions & 0 deletions cmd/auth.go
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
}



1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func init() {
rootCmd.AddCommand(NewVersionCmd())
rootCmd.AddCommand(NewInitCmd(globalFlags))
rootCmd.AddCommand(NewPushCmd(globalFlags))
rootCmd.AddCommand(NewAuthCmd(globalFlags))
}

// GetRoot returns the root command
Expand Down

0 comments on commit 4f761fe

Please sign in to comment.