Skip to content

Commit 65e3875

Browse files
author
Emmanuel Odeke
committed
touch command added. Centralized cmd keys
1 parent 5557fcc commit 65e3875

File tree

4 files changed

+123
-25
lines changed

4 files changed

+123
-25
lines changed

cmd/drive/main.go

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,20 @@ func main() {
4040
}
4141
runtime.GOMAXPROCS(int(maxProcs))
4242

43-
command.On("diff", drive.DescDiff, &diffCmd{}, []string{})
44-
command.On("init", drive.DescInit, &initCmd{}, []string{})
45-
command.On("list", drive.DescList, &listCmd{}, []string{})
46-
command.On("pull", drive.DescPull, &pullCmd{}, []string{})
47-
command.On("push", drive.DescPush, &pushCmd{}, []string{})
48-
command.On("pub", drive.DescPublish, &publishCmd{}, []string{})
49-
command.On("emptytrash", drive.DescEmptyTrash, &emptyTrashCmd{}, []string{})
50-
command.On("help", drive.DescHelp, &helpCmd{}, []string{})
51-
command.On("quota", drive.DescQuota, &quotaCmd{}, []string{})
52-
command.On("trash", drive.DescTrash, &trashCmd{}, []string{})
53-
command.On("untrash", drive.DescUntrash, &untrashCmd{}, []string{})
54-
command.On("unpub", drive.DescUnpublish, &unpublishCmd{}, []string{})
55-
command.On("version", drive.Version, &versionCmd{}, []string{})
43+
command.On(drive.DiffKey, drive.DescDiff, &diffCmd{}, []string{})
44+
command.On(drive.EmptyTrashKey, drive.DescEmptyTrash, &emptyTrashCmd{}, []string{})
45+
command.On(drive.InitKey, drive.DescInit, &initCmd{}, []string{})
46+
command.On(drive.HelpKey, drive.DescHelp, &helpCmd{}, []string{})
47+
command.On(drive.ListKey, drive.DescList, &listCmd{}, []string{})
48+
command.On(drive.PullKey, drive.DescPull, &pullCmd{}, []string{})
49+
command.On(drive.PushKey, drive.DescPush, &pushCmd{}, []string{})
50+
command.On(drive.PubKey, drive.DescPublish, &publishCmd{}, []string{})
51+
command.On(drive.QuotaKey, drive.DescQuota, &quotaCmd{}, []string{})
52+
command.On(drive.TouchKey, drive.DescTouch, &touchCmd{}, []string{})
53+
command.On(drive.TrashKey, drive.DescTrash, &trashCmd{}, []string{})
54+
command.On(drive.UntrashKey, drive.DescUntrash, &untrashCmd{}, []string{})
55+
command.On(drive.UnpubKey, drive.DescUnpublish, &unpublishCmd{}, []string{})
56+
command.On(drive.VersionKey, drive.Version, &versionCmd{}, []string{})
5657
command.ParseAndRun()
5758
}
5859

@@ -281,6 +282,30 @@ func (cmd *pushCmd) Run(args []string) {
281282
}
282283
}
283284

285+
type touchCmd struct {
286+
hidden *bool
287+
noPrompt *bool
288+
recursive *bool
289+
}
290+
291+
func (cmd *touchCmd) Flags(fs *flag.FlagSet) *flag.FlagSet {
292+
cmd.hidden = fs.Bool("hidden", false, "allows pushing of hidden paths")
293+
cmd.recursive = fs.Bool("r", true, "performs the push action recursively")
294+
cmd.noPrompt = fs.Bool("no-prompt", false, "shows no prompt before applying the push action")
295+
return fs
296+
}
297+
298+
func (cmd *touchCmd) Run(args []string) {
299+
sources, context, path := preprocessArgs(args)
300+
exitWithError(drive.New(context, &drive.Options{
301+
Hidden: *cmd.hidden,
302+
NoPrompt: *cmd.noPrompt,
303+
Path: path,
304+
Recursive: *cmd.recursive,
305+
Sources: sources,
306+
}).Touch())
307+
}
308+
284309
func pushMounted(cmd *pushCmd, args []string) {
285310
argc := len(args)
286311

help.go

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,23 @@ import (
2020

2121
var Version = "0.0.4c"
2222

23+
const (
24+
DiffKey = "diff"
25+
EmptyTrashKey = "emptytrash"
26+
InitKey = "init"
27+
ListKey = "list"
28+
PullKey = "pull"
29+
PushKey = "push"
30+
PubKey = "pub"
31+
HelpKey = "help"
32+
QuotaKey = "quota"
33+
TouchKey = "touch"
34+
TrashKey = "trash"
35+
UntrashKey = "untrash"
36+
UnpubKey = "unpub"
37+
VersionKey = "version"
38+
)
39+
2340
const (
2441
DescInit = "initializes a directory and authenticates user"
2542
DescPull = "pulls remote changes from Google Drive"
@@ -33,44 +50,48 @@ const (
3350
DescTrash = "moves files to trash"
3451
DescUntrash = "restores files from trash to their original locations"
3552
DescUnpublish = "revokes public access to a file"
53+
DescTouch = "updates a remote file's modification time to that currently on the server"
3654
DescVersion = "prints the version"
3755
DescAll = "print out the entire help section"
3856
All = "all"
3957
)
4058

4159
var docMap = map[string][]string{
42-
"diff": []string{
60+
DiffKey: []string{
4361
DescDiff, "Accepts multiple remote paths for line by line comparison",
4462
},
45-
"emptytrash": []string{
63+
EmptyTrashKey: []string{
4664
DescEmptyTrash,
4765
},
48-
"init": []string{
66+
InitKey: []string{
4967
DescInit, "Requests for access to your Google Drive",
5068
"Creating a folder that contains your credentials",
5169
"Note: init in an already initialized drive will erase the old credentials",
5270
},
53-
"pull": []string{
71+
PullKey: []string{
5472
DescPull, "Downloads content from the remote drive or modifies",
5573
" local content to match that on your Google Drive",
5674
},
57-
"push": []string{
75+
PushKey: []string{
5876
DescPush, "Uploads content to your Google Drive from your local path",
5977
"Push comes in a couple of flavors",
6078
"\t* Ordinary push: `drive push path1 path2 path3`",
6179
"\t* Mounted push: `drive push -m path1 [path2 path3] drive_context_path`",
6280
},
63-
"list": []string{
81+
ListKey: []string{
6482
DescList,
6583
"List the information related a remote path not necessarily present locally",
6684
"Allows printing of long options and by default does minimal printing",
6785
},
68-
"quota": []string{DescQuota},
69-
"trash": []string{DescTrash, "Accepts multiple paths"},
70-
"untrash": []string{DescUntrash, "Accepts multiple paths"},
71-
"pub": []string{DescPublish, "Accepts multiple paths"},
72-
"unpub": []string{DescUnpublish, "Accepts multiple paths"},
73-
"version": []string{DescVersion, fmt.Sprintf("current version is: %s", Version)},
86+
QuotaKey: []string{DescQuota},
87+
TrashKey: []string{DescTrash, "Accepts multiple paths"},
88+
UntrashKey: []string{DescUntrash, "Accepts multiple paths"},
89+
PubKey: []string{DescPublish, "Accepts multiple paths"},
90+
UnpubKey: []string{DescUnpublish, "Accepts multiple paths"},
91+
TouchKey: []string{DescTouch},
92+
VersionKey: []string{
93+
DescVersion, fmt.Sprintf("current version is: %s", Version),
94+
},
7495
}
7596

7697
func ShowAllDescriptions() {

push.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
gopath "path"
2323
"sort"
2424
"strings"
25+
"sync"
2526

2627
"github.com/odeke-em/drive/config"
2728
)
@@ -87,6 +88,52 @@ func (g *Commands) Push() (err error) {
8788
return
8889
}
8990

91+
func (g *Commands) Touch() (err error) {
92+
srcLen := len(g.opts.Sources)
93+
chunkSize := 4
94+
q, r := srcLen/chunkSize, srcLen%chunkSize
95+
i, chunkCount := 0, q
96+
if r != 0 {
97+
chunkCount += 1
98+
}
99+
100+
var wg sync.WaitGroup
101+
wg.Add(chunkCount)
102+
103+
for j := 0; j < chunkCount; j += 1 {
104+
end := i + chunkSize
105+
if end >= srcLen {
106+
end = srcLen
107+
}
108+
chunk := g.opts.Sources[i:end]
109+
go func(wg *sync.WaitGroup, chunk []string) {
110+
for _, relToRootPath := range chunk {
111+
if tErr := g.touch(relToRootPath); tErr != nil {
112+
fmt.Printf("touch: %s %v\n", relToRootPath, tErr)
113+
}
114+
}
115+
wg.Done()
116+
}(&wg, chunk)
117+
118+
i += chunkSize
119+
}
120+
121+
wg.Wait()
122+
return
123+
}
124+
125+
func (g *Commands) touch(relToRootPath string) (err error) {
126+
var file *File
127+
file, err = g.rem.FindByPath(relToRootPath)
128+
if err != nil {
129+
return err
130+
}
131+
if file == nil {
132+
return ErrPathNotExists
133+
}
134+
return g.rem.Touch(file.Id)
135+
}
136+
90137
func (g *Commands) playPushChangeList(cl []*Change) (err error) {
91138
g.taskStart(len(cl))
92139

remote.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,11 @@ func (r *Remote) Download(id string, exportURL string) (io.ReadCloser, error) {
244244
return resp.Body, nil
245245
}
246246

247+
func (r *Remote) Touch(id string) error {
248+
_, err := r.service.Files.Touch(id).Do()
249+
return err
250+
}
251+
247252
func (r *Remote) UpsertByComparison(parentId, fsAbsPath string, src, dest *File) (f *File, err error) {
248253
var body io.Reader
249254
body, err = os.Open(fsAbsPath)

0 commit comments

Comments
 (0)