This repository has been archived by the owner on Jul 5, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
attachments.go
121 lines (107 loc) · 2.47 KB
/
attachments.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package attachments
import (
"context"
"io/ioutil"
"path/filepath"
"strings"
"github.com/kamiazya/tcho/cli/adapter/component"
"github.com/kamiazya/tcho/cli/extarnal"
"github.com/kamiazya/tcho/cli/i18n"
"github.com/kamiazya/tcho/core/application/usecase"
r "github.com/chzyer/readline"
"gopkg.in/urfave/cli.v2"
)
type Option func(*attachments) error
func ContextFunc(contextFunc func() context.Context) Option {
return func(c *attachments) error {
c.contextFunc = contextFunc
return nil
}
}
func NewComponent(uc usecase.AttachmentUseCase, opts ...Option) (component.Component, error) {
c := &attachments{
uc: uc,
}
for _, opt := range opts {
if err := opt(c); err != nil {
return nil, err
}
}
return c, nil
}
type attachments struct {
extarnal.TechoCliColor
uc usecase.AttachmentUseCase
contextFunc func() context.Context
}
func (c *attachments) context() context.Context {
if c.contextFunc != nil {
return c.contextFunc()
}
return context.Background()
}
func (c *attachments) Commands() []*cli.Command {
return []*cli.Command{
&cli.Command{
Name: "attachments",
Aliases: []string{"a"},
Usage: i18n.AttachmentsUsage,
Description: i18n.AttachmentsDescription,
Flags: c.listFlags(),
Action: c.listCommand(),
Subcommands: []*cli.Command{
{
Name: "save",
Usage: i18n.AttachmentsSaveUsage,
Description: i18n.AttachmentsSaveDestination,
Action: c.saveCommand(),
Flags: c.saveFlags(),
},
{
Name: "list",
Usage: i18n.AttachmentsListUsage,
Description: i18n.AttachmentsListDestination,
Action: c.listCommand(),
Flags: c.listFlags(),
},
},
},
}
}
func (c *attachments) paths(dir string) []string {
if dir == "" {
dir = "."
}
files, err := ioutil.ReadDir(dir)
if err != nil {
return []string{}
}
paths := make([]string, 0, len(files))
for _, file := range files {
if file.IsDir() {
paths = append(paths, c.paths(filepath.Join(dir, file.Name()))...)
continue
}
paths = append(paths, filepath.Join(dir, file.Name()))
}
return paths
}
func (c *attachments) Completer() r.PrefixCompleterInterface {
return r.PcItem(
"attachments",
r.PcItem(
"save",
r.PcItemDynamic(func(line string) []string {
words := strings.Split(line, " ")
return c.paths(words[len(words)-1])
}),
),
r.PcItem(
"list",
r.PcItem(
"--mode=simple",
),
),
r.PcItem("help"),
)
}