Skip to content

Commit

Permalink
Merge pull request #18 from SijmenHuizenga/json-file-locations
Browse files Browse the repository at this point in the history
Options to configure credentials file and token file location
  • Loading branch information
dtylman authored Dec 13, 2020
2 parents 60ebb88 + 0d0a9d5 commit a1ffb1c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,15 @@ Usage of ./gitmoo-goog:
- album
download only from this album (use google album id)
-folder string
backup folder
backup folder (default current working directory)
-force
ignore errors, and force working
-logfile string
log to this file
-credentials-file string
filepath to where the credentials file can be found (default 'credentials.json')
-token-file string
filepath to where the token should be stored (default 'token.json')
-loop
loops forever (use as daemon)
-max int
Expand Down
4 changes: 4 additions & 0 deletions downloader/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@ type Options struct {
ConcurrentDownloads int
//Google photos AlbumID
AlbumID string
//CredentialsFile Google API credentials.json file
CredentialsFile string
//TokenFile Google oauth client token.json file
TokenFile string
}
11 changes: 6 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ var options struct {
}

// Retrieve a token, saves the token, then returns the generated client.
func getClient(config *oauth2.Config) *http.Client {
tokFile := "token.json"
func getClient(config *oauth2.Config, tokFile string) *http.Client {
tok, err := tokenFromFile(tokFile)
if err != nil {
tok = getTokenFromWeb(config)
Expand Down Expand Up @@ -79,7 +78,7 @@ func saveToken(path string, token *oauth2.Token) {
}

func process(downloader *downloader.Downloader) error {
b, err := ioutil.ReadFile("credentials.json")
b, err := ioutil.ReadFile(downloader.Options.CredentialsFile)
if err != nil {
log.Println("Enable photos API here: https://developers.google.com/photos/library/guides/get-started#enable-the-api")
return fmt.Errorf("Unable to read client secret file: %v", err)
Expand All @@ -90,11 +89,11 @@ func process(downloader *downloader.Downloader) error {
if err != nil {
return fmt.Errorf("Unable to parse client secret file to config: %v", err)
}
client := getClient(config)
client := getClient(config, downloader.Options.TokenFile)
log.Printf("Connecting ...")
srv, err := photoslibrary.New(client)
if err != nil {
return fmt.Errorf("Unable to retrieve Sheets client: %v", err)
return fmt.Errorf("Unable to retrieve Google Photos API client: %v", err)
}
for true {
err := downloader.DownloadAll(srv)
Expand Down Expand Up @@ -128,6 +127,8 @@ func main() {
flag.BoolVar(&downloader.Options.UseFileName, "use-file-name", false, "use file name when uploaded to Google Photos")
flag.Float64Var(&downloader.Options.DownloadThrottle, "download-throttle", 0, "rate in KB/sec, to limit downloading of items")
flag.IntVar(&downloader.Options.ConcurrentDownloads, "concurrent-downloads", 5, "number of concurrent item downloads")
flag.StringVar(&downloader.Options.CredentialsFile, "credentials-file", "credentials.json", "filepath to where the credentials file can be found")
flag.StringVar(&downloader.Options.TokenFile, "token-file", "token.json", "filepath to where the token should be stored")

flag.Parse()
if options.logfile != "" {
Expand Down

0 comments on commit a1ffb1c

Please sign in to comment.