-
Notifications
You must be signed in to change notification settings - Fork 4
/
cli.go
204 lines (179 loc) · 4.9 KB
/
cli.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package viewer
import (
"flag"
"fmt"
"io"
"log"
"os"
"path/filepath"
"github.com/diginatu/nagome/nicolive"
)
const (
eventBufferSize = 50
accountFileName = "account.yml"
logFileName = "info.log"
logFlags = log.Lshortfile | log.Ltime
pluginDirName = "plugin"
pluginConfigName = "plugin.yml"
userDBDirName = "userdb"
settingsFileName = "setting.yml"
)
// CLI has valuables and settings for a CLI environment.
type CLI struct {
InStream io.ReadCloser
OutStream, ErrStream io.WriteCloser
SavePath string
SettingsSlots SettingsSlots
log *log.Logger
AppName, Version string
}
// NewCLI creates new default values CLI struct.
func NewCLI(name, appname string) *CLI {
return &CLI{
InStream: os.Stdin,
OutStream: os.Stdout,
ErrStream: os.Stderr,
AppName: appname,
SavePath: findUserConfigPath(appname),
log: log.New(os.Stderr, name, logFlags),
}
}
// RunCli runs CLI functions as one command line program.
// This returns the CLI return value.
func (c *CLI) RunCli(args []string) int {
// set command line options
var (
printHelp bool
mainyml string
)
flagst := flag.NewFlagSet("nagome-cli", flag.ContinueOnError)
flagst.SetOutput(c.ErrStream)
flagst.StringVar(&c.SavePath, "savepath", findUserConfigPath(c.AppName), "Set <string> to save directory.")
tcpPort := flagst.String("p", "8025", `Port to wait TCP server for plugins. Set 0 to try to find free port. (see docs/plugin.md)`)
debugToStderr := flagst.Bool("dbgtostd", false, `Output debug information to stderr.
(in default, output to the log file in the save directory)`)
flagst.BoolVar(&printHelp, "help", false, "Print this help.")
flagst.BoolVar(&printHelp, "h", false, "Print this help. (shorthand)")
printVersion := flagst.Bool("v", false, "Print version information.")
mkplug := flagst.String("makeplug", "", "Make new plugin template with given name.")
flagst.StringVar(&mainyml, "ymlmain", "", `specfy the config file of main plugin.
Its format is same as yml file of normal plugins.`)
flagst.StringVar(&mainyml, "y", "", `specfy the config file of main plugin. (shorthand)`)
err := flagst.Parse(args[1:])
if err != nil {
return 1
}
err = c.SettingsSlots.Load(filepath.Join(c.SavePath, settingsFileName))
if err != nil {
c.log.Println(err)
}
pluginPath := filepath.Join(c.SavePath, pluginDirName)
if printHelp {
flagst.Usage()
return 0
}
if *printVersion {
fmt.Fprintln(c.OutStream, c.AppName, " ", c.Version)
return 0
}
if *mkplug != "" {
err = c.generatePluginTemplate(*mkplug, pluginPath)
if err != nil {
c.log.Println(err)
return 1
}
return 0
}
if err := os.MkdirAll(pluginPath, 0777); err != nil {
c.log.Println("could not make save directory\n", err)
return 1
}
// set log
var logw io.Writer
if *debugToStderr {
logw = c.ErrStream
} else {
file, err := os.Create(filepath.Join(c.SavePath, logFileName))
if err != nil {
c.log.Println("could not open log file\n", err)
return 1
}
defer func() {
err := file.Close()
if err != nil {
c.log.Println(err)
}
}()
logw = file
}
c.log.SetOutput(logw)
cv := NewCommentViewer(*tcpPort, c)
ac, err := nicolive.AccountLoad(filepath.Join(c.SavePath, accountFileName))
if err != nil {
c.log.Println(err)
cv.Ac = new(nicolive.Account)
cv.Evch <- NewMessageMust(DomainUI, CommUIConfigAccount, nil)
} else {
cv.Ac = ac
}
// add main plugin
plug := newPlugin(cv)
plug.Name = "main"
plug.Description = "main plugin"
plug.Version = "0.0"
plug.Method = pluginMethodStd
plug.Subscribe = []string{DomainNagome, DomainComment, DomainUI}
if mainyml != "" {
err = plug.Load(mainyml)
if err != nil {
c.log.Println(err)
return 1
}
}
cv.AddPlugin(plug)
if plug.Method == pluginMethodStd {
err := plug.Open(&stdReadWriteCloser{c.InStream, c.OutStream}, true)
if err != nil {
c.log.Println(err)
return 1
}
}
cv.Start()
if cv.Ac != nil {
cv.AntennaConnect()
}
cv.Wait()
if cv.Settings.AutoSaveTo0Slot {
c.SettingsSlots.Config[0] = &cv.Settings
}
err = c.SettingsSlots.Save(filepath.Join(cv.cli.SavePath, settingsFileName))
if err != nil {
c.log.Println(err)
return 1
}
return 0
}
func (c *CLI) generatePluginTemplate(name, pluginPath string) error {
p := filepath.Join(pluginPath, name)
// check if the directory already exists
_, err := os.Stat(p)
if err == nil {
return fmt.Errorf("same name directory is already exists")
}
if err := os.MkdirAll(p, 0777); err != nil {
return fmt.Errorf("Could not make save directory : %s", err)
}
pl := Plugin{
Name: name,
Version: "1.0",
Subscribe: []string{DomainNagome},
Method: "tcp",
Exec: []string{"{{path}}/" + name, "{{port}}", "{{no}}"},
}
err = pl.Save(filepath.Join(p, pluginConfigName))
if err != nil {
return fmt.Errorf("failed to save file : %s", err)
}
fmt.Fprintf(c.OutStream, "Create your plugin in : %s\n", p)
return nil
}