diff --git a/README.md b/README.md index f9f6dc8..251dee3 100644 --- a/README.md +++ b/README.md @@ -18,15 +18,16 @@ $ go install github.com/kijimaD/upl@main $ docker-compose up -d ``` -実行。カレントディレクトリにある `upload.zip` を、指定パスにある Tiny File Manager にアップロードする。 +実行。カレントディレクトリにある `upload.zip` を、指定パスにある Tiny File Manager の指定ディレクトリにアップロードする。`.`の場合は、Tiny File Manager のトップに設定されているディレクトリに`upload.zip`をアップロードする。 ``` -$ upl localhost:7777 +$ upl localhost:7777 . +upl [ベースパス] [アップロード先ディレクトリ] ``` ( http://localhost:7777 で Tiny File Managerにアクセスできているものとする。) ## docker run ``` -$ docker run -v "$PWD/":/work -w /work --rm -it ghcr.io/kijimad/upl:latest localhost:7777 +$ docker run -v "$PWD/":/work -w /work --rm -it ghcr.io/kijimad/upl:latest localhost:7777 . ``` diff --git a/cmd/animation.go b/cmd/animation.go new file mode 100644 index 0000000..96dae43 --- /dev/null +++ b/cmd/animation.go @@ -0,0 +1,28 @@ +package cmd + +import ( + "fmt" + "io" + "time" +) + +var marks = []string{"|", "/", "-", "\\"} + +const animLoopMilliseconds = 100 + +func mark(i int) string { + return marks[i%len(marks)] +} + +func anim(w io.Writer) { + go func() { + i := 0 + for range time.Tick(animLoopMilliseconds * time.Millisecond) { + if i == len(marks) { + i = 0 + } + fmt.Fprintf(w, "\rwait... %s", mark(i)) + i++ + } + }() +} diff --git a/cmd/cmd.go b/cmd/cmd.go index 5f98a54..6caf552 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -10,7 +10,7 @@ import ( ) var ( - ArgumentCountError = errors.New("引数の数が間違っている。expect: 1") + ArgumentCountError = errors.New("引数の数が間違っている。expect: 2") ) type CLI struct { @@ -38,14 +38,24 @@ uplはカレントディレクトリにある "upload.zip" という名前のフ return nil } - if len(args) != 2 { + if len(args) != 3 { return ArgumentCountError } baseurl := args[1] - task := upl.NewTask(cli.Out, upl.TaskWithBaseurl(baseurl)) + destpath := args[2] + + anim(cli.Out) + + task := upl.NewTask( + cli.Out, + upl.TaskWithBaseurl(baseurl), + upl.TaskWithDestpath(destpath), + ) err := task.Exec() if err != nil { return err } + + fmt.Fprintln(cli.Out, "") return nil } diff --git a/pkg/upl.go b/pkg/upl.go index 8ac59af..03999c1 100644 --- a/pkg/upl.go +++ b/pkg/upl.go @@ -34,6 +34,7 @@ type Task struct { baseurl string adminuser string pwd string + destpath string } func NewTask(w io.Writer, options ...TaskOption) *Task { @@ -43,6 +44,7 @@ func NewTask(w io.Writer, options ...TaskOption) *Task { baseurl: DEFAULT_BASEURL, adminuser: DEFAULT_ADMINUSER, pwd: DEFAULT_PWD, + destpath: ".", } for _, option := range options { option(&task) @@ -65,6 +67,12 @@ func TaskWithLoginUser(adminuser string, pwd string) TaskOption { } } +func TaskWithDestpath(destpath string) TaskOption { + return func(t *Task) { + t.destpath = destpath + } +} + func (t *Task) upload(cookie string) error { file, err := os.Open(UPLOAD_TARGET) if err != nil { @@ -78,7 +86,7 @@ func (t *Task) upload(cookie string) error { body := &bytes.Buffer{} writer := multipart.NewWriter(body) writer.SetBoundary(boundary) - writer.WriteField("p", "") + writer.WriteField("p", t.destpath) writer.WriteField("fullpath", UPLOAD_TARGET) partHeader := make(textproto.MIMEHeader) partHeader.Set("Content-Disposition", fmt.Sprintf(`form-data; name="file"; filename="%s"`, UPLOAD_TARGET))