This repository has been archived by the owner on Mar 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.go
238 lines (196 loc) · 4.95 KB
/
commands.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package main
import (
"fmt"
"log"
)
type ListCommand struct {
All bool `short:"a" long:"available" description:"also prints all available version for installation"`
}
type InitCommand struct{}
type InstallCommand struct {
Use bool `short:"u" long:"use" description:"force use of this new version after installation"`
}
type UseCommand struct{}
type Interactor struct {
archive WebotsArchive
manager WebotsInstanceManager
templates TemplateManager
}
func NewInteractor() (*Interactor, error) {
res := &Interactor{}
var err error
res.archive, err = NewWebotsHttpArchive("http://www.cyberbotics.com/archive/")
if err != nil {
return nil, err
}
manager, err := NewSymlinkManager(res.archive)
if err != nil {
return nil, err
}
res.manager = manager
res.templates = manager.templates
return res, nil
}
func (x *ListCommand) Execute(args []string) error {
xx, err := NewInteractor()
if err != nil {
return err
}
installed := xx.manager.Installed()
if len(installed) == 0 {
fmt.Printf("No webots version installed.\n")
} else {
for _, v := range installed {
if xx.manager.IsUsed(v) == true {
fmt.Printf(" -* %s\n", v)
} else {
fmt.Printf(" - %s\n", v)
}
}
}
if x.All {
fmt.Println("List of all available versions:")
for _, v := range xx.archive.AvailableVersions() {
fmt.Printf(" - %s\n", v)
}
} else {
vers := xx.archive.AvailableVersions()
if len(vers) == 0 {
return fmt.Errorf("No version are available")
}
fmt.Printf("Last available version is %s\n",
vers[len(vers)-1])
}
return nil
}
func (x *InitCommand) Execute(args []string) error {
return SymlinkManagerSystemInit()
}
func (x *InstallCommand) Execute(args []string) error {
if len(args) != 1 {
return fmt.Errorf("Missing version to install")
}
v, err := ParseWebotsVersion(args[0])
if err != nil {
return err
}
xx, err := NewInteractor()
if err != nil {
return err
}
err = xx.manager.Install(v)
if err != nil {
return err
}
notUsed := true
for _, vv := range xx.manager.Installed() {
if xx.manager.IsUsed(vv) {
notUsed = false
break
}
}
if notUsed || x.Use {
err = xx.manager.Use(v)
if err != nil {
return err
}
log.Printf("Using now version %s", v)
}
return nil
}
func (x *UseCommand) Execute(args []string) error {
if len(args) != 1 {
return fmt.Errorf("Missing version to use")
}
v, err := ParseWebotsVersion(args[0])
if err != nil {
return err
}
xx, err := NewInteractor()
if err != nil {
return err
}
return xx.manager.Use(v)
}
type AddTemplateCommand struct {
Only []string `short:"o" long:"only" description:"apply template only for these versions"`
Except []string `short:"e" long:"except" description:"do not apply template on these versions"`
}
func (x *AddTemplateCommand) Execute(args []string) error {
if len(args) != 2 {
return fmt.Errorf("Need file to read and where to install")
}
var white, black []WebotsVersion
for _, w := range x.Only {
v, err := ParseWebotsVersion(w)
if err != nil {
return err
}
white = append(white, v)
}
for _, w := range x.Except {
v, err := ParseWebotsVersion(w)
if err != nil {
return err
}
black = append(black, v)
}
xx, err := NewInteractor()
if err != nil {
return err
}
err = xx.templates.RegisterTemplate(args[0], args[1])
if err != nil {
return err
}
err = xx.templates.WhiteList(args[1], white)
if err != nil {
return err
}
err = xx.templates.BlackList(args[1], black)
if err != nil {
return err
}
return xx.manager.ApplyAllTemplates()
}
type RemoveTemplateCommand struct{}
func (x *RemoveTemplateCommand) Execute(args []string) error {
if len(args) != 1 {
return fmt.Errorf("Need install path to remove template from")
}
xx, err := NewInteractor()
if err != nil {
return err
}
err = xx.templates.RemoveTemplate(args[0])
if err != nil {
return err
}
return xx.manager.ApplyAllTemplates()
}
func init() {
parser.AddCommand("list",
"Prints all the available version of webots",
"Prints all installed version, and current version in use. Can also prinst all available version for installation",
&ListCommand{})
parser.AddCommand("init",
"Initialiaze the system for webots_manager",
"Initialiaze the system with all requirement for webots_manager",
&InitCommand{})
parser.AddCommand("install",
"Install a new webots version on the system",
"Installs a new webots version on the system",
&InstallCommand{})
parser.AddCommand("use",
"Use a webots version on the system",
"Use a webots version on the system. If it is not installed, it will first install it",
&UseCommand{})
parser.AddCommand("add-template",
"Adds a template file to all version",
"Install a file to all version of webots. -o and -e can be used to explicitely whitelist or blacklist a version",
&AddTemplateCommand{})
parser.AddCommand("remove-template",
"Removes a template file from all version",
"Removes a previously installed template from all version of webots.",
&RemoveTemplateCommand{})
}