Skip to content

Commit

Permalink
Merge pull request #3 from kijimaD/progressloop
Browse files Browse the repository at this point in the history
アニメーション表示
  • Loading branch information
kijimaD committed Oct 21, 2023
2 parents 9ddd84f + 4f2e42a commit b0cb78f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 .
```
28 changes: 28 additions & 0 deletions cmd/animation.go
Original file line number Diff line number Diff line change
@@ -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++
}
}()
}
16 changes: 13 additions & 3 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

var (
ArgumentCountError = errors.New("引数の数が間違っている。expect: 1")
ArgumentCountError = errors.New("引数の数が間違っている。expect: 2")
)

type CLI struct {
Expand Down Expand Up @@ -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
}
10 changes: 9 additions & 1 deletion pkg/upl.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Task struct {
baseurl string
adminuser string
pwd string
destpath string
}

func NewTask(w io.Writer, options ...TaskOption) *Task {
Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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))
Expand Down

0 comments on commit b0cb78f

Please sign in to comment.