Skip to content

Commit edce09b

Browse files
committed
Add filemanager action support for rename,move,copy
1 parent 74aa6f5 commit edce09b

3 files changed

Lines changed: 122 additions & 0 deletions

File tree

cmd/mypan/main.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,23 @@ func (myApp MyApp) syncAction(cCtx *cli.Context, src, dst string, up bool) error
184184
return nil
185185
}
186186

187+
func (myApp MyApp) copyMoveAction(
188+
cCtx *cli.Context,
189+
action func(context.Context, string, string) (client.FileManagerResponse, error),
190+
) error {
191+
remotepath0 := cCtx.Args().Get(0)
192+
remotepath1 := cCtx.Args().Get(1)
193+
if remotepath0 == "" || remotepath1 == "" {
194+
return cli.Exit("remotepath0 and remotepath1 arguments are required", 1)
195+
}
196+
resp, err := action(myApp.ctx, remotepath0, remotepath1)
197+
if err != nil {
198+
return cli.Exit(err, 1)
199+
}
200+
myApp.render.Render(resp)
201+
return nil
202+
}
203+
187204
func (myApp MyApp) Run(args []string) {
188205
cfg := config.Global
189206

@@ -417,6 +434,39 @@ func (myApp MyApp) Run(args []string) {
417434
return myApp.syncAction(cCtx, src, dst, false)
418435
},
419436
},
437+
{
438+
Name: "rename",
439+
ArgsUsage: "remotepath newname",
440+
Action: func(cCtx *cli.Context) error {
441+
path := cCtx.Args().Get(0)
442+
newname := cCtx.Args().Get(1)
443+
if path == "" || newname == "" {
444+
return cli.Exit("path and newname arguments are required", 1)
445+
}
446+
resp, err := myApp.dstClient.Rename(myApp.ctx, path, newname)
447+
if err != nil {
448+
return cli.Exit(err, 1)
449+
}
450+
myApp.render.Render(resp)
451+
return nil
452+
},
453+
},
454+
{
455+
Name: "mv",
456+
Aliases: []string{"move"},
457+
ArgsUsage: "remotepath0 remotepath1",
458+
Action: func(cCtx *cli.Context) error {
459+
return myApp.copyMoveAction(cCtx, myApp.dstClient.Move)
460+
},
461+
},
462+
{
463+
Name: "cp",
464+
Aliases: []string{"copy"},
465+
ArgsUsage: "remotepath0 remotepath1",
466+
Action: func(cCtx *cli.Context) error {
467+
return myApp.copyMoveAction(cCtx, myApp.dstClient.Copy)
468+
},
469+
},
420470
},
421471
Authors: []*cli.Author{
422472
&cli.Author{Name: "Yousong Zhou", Email: "yszhou4tech@gmail.com"},

pkg/client/client.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,4 +228,16 @@ type ClientI interface {
228228
ctx context.Context,
229229
fileList []string,
230230
) (FileManagerResponse, error)
231+
Rename(
232+
ctx context.Context,
233+
path, newname string,
234+
) (FileManagerResponse, error)
235+
Copy(
236+
ctx context.Context,
237+
path, dest string,
238+
) (FileManagerResponse, error)
239+
Move(
240+
ctx context.Context,
241+
path, dest string,
242+
) (FileManagerResponse, error)
231243
}

pkg/client/filemanager.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"context"
88
"encoding/json"
99
"net/url"
10+
"path/filepath"
1011
"strconv"
1112

1213
"github.com/pkg/errors"
@@ -45,6 +46,65 @@ func (client *Client) DeleteMulti(
4546
return resp, err
4647
}
4748

49+
func (client *Client) Rename(
50+
ctx context.Context,
51+
path, newname string,
52+
) (FileManagerResponse, error) {
53+
resp, err := client.renameMulti(ctx, [2]string{path, newname})
54+
return resp, err
55+
}
56+
57+
func (client *Client) renameMulti(
58+
ctx context.Context,
59+
pairs ...[2]string,
60+
) (FileManagerResponse, error) {
61+
var filelist []map[string]string
62+
for _, p := range pairs {
63+
filelist = append(filelist, map[string]string{
64+
"path": client.AbsPath(p[0]),
65+
"newname": p[1],
66+
})
67+
}
68+
resp, err := client.doFileManager(ctx, opRename, filelist)
69+
return resp, err
70+
}
71+
72+
func (client *Client) Copy(
73+
ctx context.Context,
74+
path, dest string,
75+
) (FileManagerResponse, error) {
76+
resp, err := client.copyMoveMulti(ctx, opCopy, [2]string{path, dest})
77+
return resp, err
78+
}
79+
80+
func (client *Client) Move(
81+
ctx context.Context,
82+
path, dest string,
83+
) (FileManagerResponse, error) {
84+
resp, err := client.copyMoveMulti(ctx, opMove, [2]string{path, dest})
85+
return resp, err
86+
}
87+
88+
func (client *Client) copyMoveMulti(
89+
ctx context.Context,
90+
op string,
91+
pairs ...[2]string,
92+
) (FileManagerResponse, error) {
93+
var filelist []map[string]string
94+
for _, p := range pairs {
95+
dest := client.AbsPath(p[1])
96+
newname := filepath.Base(dest)
97+
dest = filepath.Dir(dest)
98+
filelist = append(filelist, map[string]string{
99+
"path": client.AbsPath(p[0]),
100+
"dest": dest,
101+
"newname": newname,
102+
})
103+
}
104+
resp, err := client.doFileManager(ctx, op, filelist)
105+
return resp, err
106+
}
107+
48108
func (client *Client) doFileManager(
49109
ctx context.Context,
50110
op string,

0 commit comments

Comments
 (0)