Bladeren bron

Several changes

Add config for gdrivefs service
README changes
implemented <source> file for mounting
luis 7 jaren geleden
bovenliggende
commit
5a87ed14be
8 gewijzigde bestanden met toevoegingen van 97 en 117 verwijderingen
  1. 46 14
      README.md
  2. 3 1
      flags.go
  3. 27 0
      internal/core/util.go
  4. 4 97
      internal/fs/gdrivefs/client.go
  5. 12 0
      internal/fs/gdrivefs/config.go
  6. 2 2
      internal/fs/gdrivefs/gdrivefs.go
  7. 1 1
      main.go
  8. 2 2
      version.go

+ 46 - 14
README.md

@@ -7,8 +7,11 @@ Linux util to Mount cloud drives
 ```bash
 $ cloudmount -h
 
-cloudmount-0.1-5-gea7b804 - built: 2017-07-10 18:24:27 UTC
-Usage: cloudmount [options] MOUNTPOINT
+cloudmount-0.3-3-gadf4880 - built: 2017-07-11 01:34:58 UTC
+
+Usage: cloudmount [options] [<source>] <directory>
+
+Source: can be json/yaml configuration file usually with credentials or cloud specific configuration
 
 Options:
   -d	Run app in background
@@ -20,29 +23,33 @@ Options:
     	which cloud service to use [gdrive] (default "gdrive")
   -v	Verbose log
   -w string
-    	Work dir, path that holds configurations (default "/home/stdio/.cloudmount")
-
+    	Work dir, path that holds configurations (default "<homedir>/.cloudmount")
 ```
 
 
 #### Example:
 ```bash
 $ go get dev.hexasoftware.com/hxs/cloudmount
+# will default source file to $HOME/.cloudmount/gdrive.json
 $ cloudmount MOUNTPOINT
+# or 
+$ cloudmount gdrive.json MOUNTPOINT
 
 ```
+#### Source config:
+Configuration files/source can be written in following formats:
+* json
+* yaml
 
 #### Support for:
 * Google Drive
 
-### Google Drive
 
+### Google Drive
 
 Setup Google client secrets:
 
-[https://console.developers.google.com/apis/credentials] (https://console.developers.google.com/apis/credentials)
-
-As of Google drive guidance:
+https://console.developers.google.com/apis/credentials
 
 >	Turn on the Drive API
 
@@ -51,21 +58,46 @@ As of Google drive guidance:
 >	3. At the top of the page, select the OAuth consent screen tab. Select an Email address, enter a Product name if not already set, and click the Save button.
 >	4. Select the Credentials tab, click the Create credentials button and select OAuth client ID.
 >	5. Select the application type Other, enter the name "Drive API Quickstart", and click the Create button.
->	6. Click OK to dismiss the resulting dialog.
->	7. Click the file_download (Download JSON) button to the right of the client ID.
+>	6. With the result dialog, copy clientID and client secret and create json file as shown in example (this can be retrieved any time by clicking on the api key)
+
+sample *gdrive.json* config:    
+```json
+{
+  "client_secret": {
+   "client_id": "CLIENTID",
+   "client_secret": "CLIENTSECRET"
+  }
+}
+```
+or yaml format:
+```yaml
+client_secret:
+  client_id: CLIENTID
+  client_secret: CLIENTSECRET
+```
+
+```bash
+$ cloudmount gdrive.json $HOME/mntpoint
+```
+
+Also it's possible to create the json/yaml file in home directory as 
+__$HOME/.cloudmount/gdrive.json__
+if &lt;source&gt; parameter is ommited it will default to this file
+
+
+cloudmount gdrivefs will retrieve an oauth2 token and save in same file
+
 
-Copy the downloaded JSON file to home directory as:    
-__$HOME/.gdrivemount/client_secret.json__   
 
 #### Signals
 Signal | Action                                                                                               | ex
 -------|------------------------------------------------------------------------------------------------------|-----------------
-USR1   | Refreshes directory tree from google drive                                                           | killall -USR1 gdrivemount
+USR1   | Refreshes directory tree from file system                                                            | killall -USR1 gdrivemount
 HUP    | Perform a GC and shows memory usage <small>Works when its not running in daemon mode</small>         | killall -HUP gdrivemount
 
 
 
 #### TODO & IDEAS:
 * Consider using github.com/codegangsta/cli
-* Use a single .json/ .yaml file for mount source: cloudmount file.json destfolder where file.json contains fs type, credentials, configurations etc
+* Create test suit to implement new packages
 

+ 3 - 1
flags.go

@@ -24,7 +24,9 @@ func parseFlags(config *core.Config) (err error) {
 	flag.StringVar(&mountoptsFlag, "o", "", "uid,gid ex: -o uid=1000,gid=0 ")
 
 	flag.Usage = func() {
-		fmt.Fprintf(os.Stderr, "Usage: %s [options] <SRC/CONFIG?> MOUNTPOINT\n\n", os.Args[0])
+		fmt.Fprintf(os.Stderr, "\n")
+		fmt.Fprintf(os.Stderr, "Usage: %s [options] [<source>] <directory>\n\n", os.Args[0])
+		fmt.Fprintf(os.Stderr, "Source: can be json/yaml configuration file usually with credentials or cloud specific configuration\n\n")
 		fmt.Fprintf(os.Stderr, "Options:\n")
 		flag.PrintDefaults()
 		fmt.Fprintf(os.Stderr, "\n")

+ 27 - 0
internal/core/util.go

@@ -35,3 +35,30 @@ func ParseConfig(srcfile string, out interface{}) (err error) {
 	}
 	return err
 }
+
+func SaveConfig(name string, obj interface{}) (err error) {
+	var data []byte
+	if strings.HasSuffix(name, ".json") {
+		data, err = json.MarshalIndent(obj, "  ", "  ")
+		if err != nil {
+			return err
+		}
+	}
+	if strings.HasSuffix(name, ".yaml") {
+		data, err = yaml.Marshal(obj)
+		if err != nil {
+			return err
+		}
+	}
+
+	f, err := os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
+	if err != nil {
+		log.Fatalf("Unable to save config: %v\n", err)
+	}
+	defer f.Close()
+
+	f.Write(data)
+	f.Sync()
+
+	return err
+}

+ 4 - 97
internal/fs/gdrivefs/client.go

@@ -4,9 +4,7 @@ package gdrivefs
 
 import (
 	"context"
-	"encoding/json"
 	"fmt"
-	"os"
 
 	drive "google.golang.org/api/drive/v3"
 
@@ -15,53 +13,6 @@ import (
 	"golang.org/x/oauth2"
 )
 
-type ServiceConfig struct {
-	ClientSecret struct {
-		ClientID     string   `json:"client_id"`
-		ClientSecret string   `json:"client_secret"`
-		RedirectURIs []string `json:"redirect_uris"`
-		AuthURI      string   `json:"auth_uri"`
-		TokenURI     string   `json:"token_uri"`
-	} `json:"client_secret"`
-
-	Auth *oauth2.Token `json:"auth"`
-	/*struct {
-		AccessToken  string `json:"access_token"`
-		TokenType    string `json:"token_type"`
-		RefreshToken string `json:"refresh_token"`
-		Expiry       string `json:"Expiry"`
-	} `json:"auth"`*/
-}
-
-//func (d *GDriveFS) getClient(ctx context.Context, config *oauth2.Config) *http.Client {
-//	cacheFile, err := d.tokenCacheFile()
-//	if err != nil {
-///		log.Fatalf("Unable to get path to cached credential file. %v", err)
-//	}
-
-/*if d.serviceConfig.Auth.AccessToken == "" {
-		tok := getTokenFromWeb(config)
-		d.serviceConfig.Auth = tok
-		d.saveToken() // Save config actually
-	}
-	//tok, err := d.tokenFromFile(cacheFile)
-	//if err != nil {
-	//	tok = d.getTokenFromWeb(config)
-	//	d.saveToken(cacheFile, tok)
-	//	}
-	return config.Client(ctx, tok)
-
-//}
-
-/*func (d *GDriveFS) tokenCacheFile() (string, error) {
-	tokenCacheDir := d.config.HomeDir
-
-	err := os.MkdirAll(tokenCacheDir, 0700)
-
-	return filepath.Join(tokenCacheDir, url.QueryEscape("auth.json")), err
-
-}*/
-
 func getTokenFromWeb(config *oauth2.Config) *oauth2.Token {
 	authURL := config.AuthCodeURL("state-token", oauth2.AccessTypeOffline)
 
@@ -86,50 +37,6 @@ type the authorization code: `, authURL)
 	return tok
 }
 
-/*func (d *GDriveFS) tokenFromFile(file string) (*oauth2.Token, error) {
-	f, err := os.Open(file)
-	if err != nil {
-		return nil, err
-	}
-	defer f.Close()
-
-	t := &oauth2.Token{}
-	err = json.NewDecoder(f).Decode(t)
-	return t, err
-}*/
-
-//func (d *GDriveFS) saveToken(file string, token *oauth2.Token) {
-/*func (d *GDriveFS) saveToken() { // Save credentials
-
-	// Save token in SOURCE FILE
-	log.Printf("Saving credential file to: %s\n", d.config.Source)
-	f, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
-	if err != nil {
-		log.Fatalf("Unable to cache oauth token: %v\n", err)
-	}
-	defer f.Close()
-
-	json.NewEncoder(f).Encode(token)
-}*/
-
-func (d *GDriveFS) saveConfig() {
-
-	data, err := json.MarshalIndent(d.serviceConfig, "  ", "  ")
-	if err != nil {
-		panic(err)
-	}
-
-	f, err := os.OpenFile(d.config.Source, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
-	if err != nil {
-		log.Fatalf("Unable to cache oauth token: %v\n", err)
-	}
-	defer f.Close()
-
-	f.Write(data)
-	f.Sync()
-
-}
-
 // Init driveService
 func (d *GDriveFS) initClient() {
 
@@ -151,11 +58,11 @@ func (d *GDriveFS) initClient() {
 	config := &oauth2.Config{
 		ClientID:     d.serviceConfig.ClientSecret.ClientID,
 		ClientSecret: d.serviceConfig.ClientSecret.ClientSecret,
-		RedirectURL:  d.serviceConfig.ClientSecret.RedirectURIs[0],
+		RedirectURL:  "urn:ietf:wg:oauth:2.0:oob", //d.serviceConfig.ClientSecret.RedirectURIs[0],
 		Scopes:       []string{drive.DriveScope},
 		Endpoint: oauth2.Endpoint{
-			AuthURL:  d.serviceConfig.ClientSecret.AuthURI,
-			TokenURL: d.serviceConfig.ClientSecret.TokenURI,
+			AuthURL:  "https://accounts.google.com/o/oauth2/auth",  //d.serviceConfig.ClientSecret.AuthURI,
+			TokenURL: "https://accounts.google.com/o/oauth2/token", //d.serviceConfig.ClientSecret.TokenURI,
 		},
 	}
 	// We can deal with oauthToken here too
@@ -163,7 +70,7 @@ func (d *GDriveFS) initClient() {
 	if d.serviceConfig.Auth == nil {
 		tok := getTokenFromWeb(config)
 		d.serviceConfig.Auth = tok
-		d.saveConfig()
+		core.SaveConfig(d.config.Source, d.serviceConfig)
 	}
 
 	/*config, err := google.ConfigFromJSON(b, drive.DriveScope)

+ 12 - 0
internal/fs/gdrivefs/config.go

@@ -0,0 +1,12 @@
+package gdrivefs
+
+import "golang.org/x/oauth2"
+
+type Config struct {
+	ClientSecret struct {
+		ClientID     string `json:"client_id" yaml:"client_id"`
+		ClientSecret string `json:"client_secret" yaml:"client_secret"`
+	} `json:"client_secret" yaml:"client_secret"`
+
+	Auth *oauth2.Token `json:"auth" yaml:"auth"`
+}

+ 2 - 2
internal/fs/gdrivefs/gdrivefs.go

@@ -38,7 +38,7 @@ type GDriveFS struct {
 	fuseutil.NotImplementedFileSystem // Defaults
 
 	config        *core.Config //core   *core.Core // Core Config instead?
-	serviceConfig *ServiceConfig
+	serviceConfig *Config
 	client        *drive.Service
 	//root   *FileEntry // hiearchy reference
 	root *FileContainer
@@ -57,7 +57,7 @@ func New(core *core.Core) core.Driver {
 
 	fs := &GDriveFS{
 		config:        &core.Config,
-		serviceConfig: &ServiceConfig{},
+		serviceConfig: &Config{},
 		fileHandles:   map[fuseops.HandleID]*Handle{},
 		handleMU:      &sync.Mutex{},
 	}

+ 1 - 1
main.go

@@ -26,7 +26,7 @@ func main() {
 	prettylog.Global()
 
 	// getClient
-	log.Printf("%s-%s\n", Name, Version)
+	fmt.Fprintf(os.Stderr, "%s-%s\n", Name, Version)
 	core := core.New()
 
 	// More will be added later

+ 2 - 2
version.go

@@ -1,6 +1,6 @@
 package main
 
 const (
-	//Version contains version of the package
-	Version = "0.3 - built: 2017-07-10 20:25:20 UTC"
+  //Version contains version of the package
+  Version = "0.3-3-gadf4880 - built: 2017-07-11 01:34:58 UTC"
 )