"Fossies" - the Fresh Open Source Software Archive

Member "gdrive-2.1.1/drive/changes.go" (28 May 2021, 2240 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.

A hint: This file contains one or more very long lines, so maybe it is better readable using the pure text view mode that shows the contents as wrapped lines within the browser window.


    1 package drive
    2 
    3 import (
    4     "fmt"
    5     "google.golang.org/api/drive/v3"
    6     "io"
    7     "text/tabwriter"
    8 )
    9 
   10 type ListChangesArgs struct {
   11     Out        io.Writer
   12     PageToken  string
   13     MaxChanges int64
   14     Now        bool
   15     NameWidth  int64
   16     SkipHeader bool
   17 }
   18 
   19 func (self *Drive) ListChanges(args ListChangesArgs) error {
   20     if args.Now {
   21         pageToken, err := self.GetChangesStartPageToken()
   22         if err != nil {
   23             return err
   24         }
   25 
   26         fmt.Fprintf(args.Out, "Page token: %s\n", pageToken)
   27         return nil
   28     }
   29 
   30     changeList, err := self.service.Changes.List(args.PageToken).PageSize(args.MaxChanges).RestrictToMyDrive(true).Fields("newStartPageToken", "nextPageToken", "changes(fileId,removed,time,file(id,name,md5Checksum,mimeType,createdTime,modifiedTime))").Do()
   31     if err != nil {
   32         return fmt.Errorf("Failed listing changes: %s", err)
   33     }
   34 
   35     PrintChanges(PrintChangesArgs{
   36         Out:        args.Out,
   37         ChangeList: changeList,
   38         NameWidth:  int(args.NameWidth),
   39         SkipHeader: args.SkipHeader,
   40     })
   41 
   42     return nil
   43 }
   44 
   45 func (self *Drive) GetChangesStartPageToken() (string, error) {
   46     res, err := self.service.Changes.GetStartPageToken().Do()
   47     if err != nil {
   48         return "", fmt.Errorf("Failed getting start page token: %s", err)
   49     }
   50 
   51     return res.StartPageToken, nil
   52 }
   53 
   54 type PrintChangesArgs struct {
   55     Out        io.Writer
   56     ChangeList *drive.ChangeList
   57     NameWidth  int
   58     SkipHeader bool
   59 }
   60 
   61 func PrintChanges(args PrintChangesArgs) {
   62     w := new(tabwriter.Writer)
   63     w.Init(args.Out, 0, 0, 3, ' ', 0)
   64 
   65     if !args.SkipHeader {
   66         fmt.Fprintln(w, "Id\tName\tAction\tTime")
   67     }
   68 
   69     for _, c := range args.ChangeList.Changes {
   70         var name string
   71         var action string
   72 
   73         if c.Removed {
   74             action = "remove"
   75         } else {
   76             name = c.File.Name
   77             action = "update"
   78         }
   79 
   80         fmt.Fprintf(w, "%s\t%s\t%s\t%s\n",
   81             c.FileId,
   82             truncateString(name, args.NameWidth),
   83             action,
   84             formatDatetime(c.Time),
   85         )
   86     }
   87 
   88     if len(args.ChangeList.Changes) > 0 {
   89         w.Flush()
   90         pageToken, hasMore := nextChangesPageToken(args.ChangeList)
   91         fmt.Fprintf(args.Out, "\nToken: %s, more: %t\n", pageToken, hasMore)
   92     } else {
   93         fmt.Fprintln(args.Out, "No changes")
   94     }
   95 }
   96 
   97 func nextChangesPageToken(cl *drive.ChangeList) (string, bool) {
   98     if cl.NextPageToken != "" {
   99         return cl.NextPageToken, true
  100     }
  101 
  102     return cl.NewStartPageToken, false
  103 }