diff --git a/README.md b/README.md index e599475..a9eb7de 100644 --- a/README.md +++ b/README.md @@ -128,6 +128,16 @@ In order to use this, please retrieve API key as the following flow. 12. Click "Create credentials" and select API key. 13. Copy the API key. You can use this API key. +##### Registering API key to environment variable +- When API key is registered to the environment variable. When ``GOODLS_APIKEY`` as the environment variable is set API key, goodls uses API key from the environment variable. +- If API key is used with the option at the command line, the priority of option is higher than the environment variable. + +For example, in the case of bash, please add a following code to .bashrc. + +~~~bash +export GOODLS_APIKEY=### your API key ### +~~~ + ### Download When the URL of shared folder is ``https://drive.google.com/drive/folders/#####?usp=sharing``, you can download all files in the folder by the following command. @@ -249,5 +259,11 @@ If you have any questions and commissions for me, feel free to tell me. 1. By using API key, the shared large files can be run [**the resumable download**](#resumabledownloadoffile). - This demonstration can be seen at [Demo](#demo4). + + +* v1.2.1 (November 25, 2018) + + 1. API key got to be able to be used by an environment variable. When ``GOODLS_APIKEY`` as the environment variable is set API key, goodls uses API key from the environment variable. + [TOP](#TOP) diff --git a/goodls.go b/goodls.go index 61ff1a2..724bd94 100644 --- a/goodls.go +++ b/goodls.go @@ -24,6 +24,7 @@ import ( const ( appname = "goodls" + envval = "GOODLS_APIKEY" anyurl = "https://drive.google.com/uc?export=download" docutl = "https://docs.google.com/" ) @@ -138,6 +139,13 @@ func (p *para) getFilename(s *http.Response) error { // downloadLargeFile : When a large size of file is downloaded, this method is used. func (p *para) downloadLargeFile() error { fmt.Println("Now downloading.") + if p.APIKey != "" { + dlfile, err := p.getFileInfFromP() + if err != nil { + return err + } + p.Size = dlfile.Size + } res, err := p.fetch(p.URL + "&confirm=" + p.Code) if err != nil { return err @@ -298,6 +306,9 @@ func handler(c *cli.Context) { WorkDir: workdir, DlFolder: false, } + if envv := os.Getenv(envval); c.String("apikey") == "" && envv != "" { + p.APIKey = strings.TrimSpace(envv) + } if terminal.IsTerminal(int(syscall.Stdin)) { if c.String("url") == "" { createHelp().Run(os.Args) @@ -344,7 +355,7 @@ func createHelp() *cli.App { a.Author = "tanaike [ https://github.com/tanaikech/" + appname + " ] " a.Email = "tanaike@hotmail.com" a.Usage = "Download shared files on Google Drive." - a.Version = "1.2.0" + a.Version = "1.2.1" a.Flags = []cli.Flag{ cli.StringFlag{ Name: "url, u", diff --git a/resumabledownload.go b/resumabledownload.go index 95cb9df..ee62c9b 100644 --- a/resumabledownload.go +++ b/resumabledownload.go @@ -40,8 +40,8 @@ type dlParams struct { End int64 } -// showFileInf : Show file information. -func (p *para) showFileInf() error { +// getFileInfFromP : Retrieve file information from *para. +func (p *para) getFileInfFromP() (*drive.File, error) { v := &valResumableDownload{ para: *p, } @@ -49,9 +49,18 @@ func (p *para) showFileInf() error { Transport: &transport.APIKey{Key: p.APIKey}, } if err := v.getFileInf(); err != nil { + return nil, err + } + return v.DownloadFile, nil +} + +// showFileInf : Show file information. +func (p *para) showFileInf() error { + dlfile, err := p.getFileInfFromP() + if err != nil { return err } - r, err := json.Marshal(v.DownloadFile) + r, err := json.Marshal(dlfile) if err != nil { return err }