"Fossies" - the Fresh Open Source Software Archive

Member "gdrive-2.1.1/drive/share.go" (28 May 2021, 2427 Bytes) of package /linux/misc/old/gdrive-2.1.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Go source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 package drive
    2 
    3 import (
    4     "fmt"
    5     "google.golang.org/api/drive/v3"
    6     "io"
    7     "text/tabwriter"
    8 )
    9 
   10 type ShareArgs struct {
   11     Out          io.Writer
   12     FileId       string
   13     Role         string
   14     Type         string
   15     Email        string
   16     Domain       string
   17     Discoverable bool
   18 }
   19 
   20 func (self *Drive) Share(args ShareArgs) error {
   21     permission := &drive.Permission{
   22         AllowFileDiscovery: args.Discoverable,
   23         Role:               args.Role,
   24         Type:               args.Type,
   25         EmailAddress:       args.Email,
   26         Domain:             args.Domain,
   27     }
   28 
   29     _, err := self.service.Permissions.Create(args.FileId, permission).Do()
   30     if err != nil {
   31         return fmt.Errorf("Failed to share file: %s", err)
   32     }
   33 
   34     fmt.Fprintf(args.Out, "Granted %s permission to %s\n", args.Role, args.Type)
   35     return nil
   36 }
   37 
   38 type RevokePermissionArgs struct {
   39     Out          io.Writer
   40     FileId       string
   41     PermissionId string
   42 }
   43 
   44 func (self *Drive) RevokePermission(args RevokePermissionArgs) error {
   45     err := self.service.Permissions.Delete(args.FileId, args.PermissionId).Do()
   46     if err != nil {
   47         fmt.Errorf("Failed to revoke permission: %s", err)
   48         return err
   49     }
   50 
   51     fmt.Fprintf(args.Out, "Permission revoked\n")
   52     return nil
   53 }
   54 
   55 type ListPermissionsArgs struct {
   56     Out    io.Writer
   57     FileId string
   58 }
   59 
   60 func (self *Drive) ListPermissions(args ListPermissionsArgs) error {
   61     permList, err := self.service.Permissions.List(args.FileId).Fields("permissions(id,role,type,domain,emailAddress,allowFileDiscovery)").Do()
   62     if err != nil {
   63         fmt.Errorf("Failed to list permissions: %s", err)
   64         return err
   65     }
   66 
   67     printPermissions(printPermissionsArgs{
   68         out:         args.Out,
   69         permissions: permList.Permissions,
   70     })
   71     return nil
   72 }
   73 
   74 func (self *Drive) shareAnyoneReader(fileId string) error {
   75     permission := &drive.Permission{
   76         Role: "reader",
   77         Type: "anyone",
   78     }
   79 
   80     _, err := self.service.Permissions.Create(fileId, permission).Do()
   81     if err != nil {
   82         return fmt.Errorf("Failed to share file: %s", err)
   83     }
   84 
   85     return nil
   86 }
   87 
   88 type printPermissionsArgs struct {
   89     out         io.Writer
   90     permissions []*drive.Permission
   91 }
   92 
   93 func printPermissions(args printPermissionsArgs) {
   94     w := new(tabwriter.Writer)
   95     w.Init(args.out, 0, 0, 3, ' ', 0)
   96 
   97     fmt.Fprintln(w, "Id\tType\tRole\tEmail\tDomain\tDiscoverable")
   98 
   99     for _, p := range args.permissions {
  100         fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\n",
  101             p.Id,
  102             p.Type,
  103             p.Role,
  104             p.EmailAddress,
  105             p.Domain,
  106             formatBool(p.AllowFileDiscovery),
  107         )
  108     }
  109 
  110     w.Flush()
  111 }