-
Notifications
You must be signed in to change notification settings - Fork 74
/
plan.go
336 lines (297 loc) · 7.99 KB
/
plan.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package cmd
import (
"fmt"
"os"
"path/filepath"
"strings"
"text/tabwriter"
"text/template"
"github.com/testground/testground/pkg/api"
"github.com/testground/testground/pkg/config"
"github.com/testground/testground/pkg/logging"
ttmpl "github.com/testground/plan-templates/templates"
"github.com/BurntSushi/toml"
"github.com/go-git/go-git/v5"
gitcfg "github.com/go-git/go-git/v5/config"
"github.com/mattn/go-zglob"
"github.com/urfave/cli/v2"
giturls "github.com/whilp/git-urls"
)
var PlanCommand = cli.Command{
Name: "plan",
Usage: "manage the plans known to the client",
Subcommands: cli.Commands{
&cli.Command{
Name: "create",
Usage: "creates a new test plan, using a template from github.com/testground/plan-templates",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "remote",
Usage: "`URL` of the repo where this plan will be hosted i.e. git@github.com:your/repo",
Required: false,
},
&cli.StringFlag{
Name: "target",
Usage: "use template for target `LANGUAGE`; values: go",
Required: false,
Value: "go",
},
&cli.StringFlag{
Name: "module",
Usage: "set `MODULE_NAME`, used for initial templating",
Value: "github.com/your/module/name",
},
&cli.StringFlag{
Name: "plan",
Aliases: []string{"p"},
Usage: "set `NAME` of the plan to create",
Required: true,
},
},
Action: createCommand,
},
&cli.Command{
Name: "import",
Usage: "import a plan from the local filesystem or a git repository into $TESTGROUND_HOME",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "from",
Usage: "the source `URL` of the plan to be imported; either a path, or a Git remote",
Required: true,
},
&cli.BoolFlag{
Name: "git",
Usage: "import from a git repository",
Required: false,
Value: false,
},
&cli.StringFlag{
Name: "name",
Usage: "override the `NAME` of the plan directory",
Required: false,
DefaultText: "automatic",
},
},
Action: importCommand,
},
&cli.Command{
Name: "rm",
Usage: "remove a plan directory from $TESTGROUND_HOME",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "yes",
Usage: "confirm removal (without this, the command does nothing)",
Required: false,
Value: false,
},
&cli.StringFlag{
Name: "plan",
Aliases: []string{"p"},
Usage: "specifies the name of the plan to create",
Required: true,
},
},
Action: rmCommand,
},
&cli.Command{
Name: "list",
Usage: "enumerate all test plans or test cases known to the client",
Action: listCommand,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "testcases",
Usage: "display testcases",
},
},
},
},
}
// These are the variables used for executing templates during `testground plan create`
// TODO (cory) Make these specific to the target lang?
type templateVars struct {
Name string
Module string
}
func createCommand(c *cli.Context) error {
cfg := &config.EnvConfig{}
if err := cfg.Load(); err != nil {
return err
}
var (
planName = c.String("plan")
targetLang = c.String("target")
remote = c.String("remote")
module = c.String("module")
)
pdir := filepath.Join(cfg.Dirs().Plans(), planName)
repo, err := git.PlainInit(pdir, false)
if err != nil {
return err
}
if remote != "" {
_, err := repo.CreateRemote(&gitcfg.RemoteConfig{
Name: "origin",
URLs: []string{remote},
})
if err != nil {
return err
}
}
// Get file templates for the supplied target lang
assetPath := fmt.Sprintf("/%s-templates", targetLang)
var tset ttmpl.TemplateSet
err = ttmpl.Fill(assetPath, &tset)
if err != nil {
return err
}
if tset == nil {
return fmt.Errorf("unknown language target %s", targetLang)
}
for _, ts := range tset {
tmpl, err := template.New(ts.Filename).Parse(ts.Template)
if err != nil {
return err
}
f, err := os.Create(filepath.Join(pdir, tmpl.Name()))
if err != nil {
return err
}
err = tmpl.Execute(f, templateVars{Name: planName, Module: module})
if err != nil {
return err
}
f.Close()
}
fmt.Println("new test plan created under:", pdir)
return nil
}
func importCommand(c *cli.Context) error {
cfg := &config.EnvConfig{}
if err := cfg.Load(); err != nil {
return err
}
from := c.String("from")
parsed, err := giturls.Parse(from)
if err != nil {
return err
}
var importer func(string, string) error
// determine the destation, either from flag or intuited from the soruce.
baseDest := c.String("name")
if baseDest == "" {
baseDest = filepath.Base(parsed.Path)
}
dstPath := filepath.Join(cfg.Dirs().Home(), "plans", baseDest)
// check if path exists
if _, err := os.Stat(dstPath); !os.IsNotExist(err) {
logging.S().Warnw("destination dir already exists", "path", dstPath)
return nil
}
// Use git to clone. Any scheme supported by git is acceptable.
if c.Bool("git") {
// Remove the '.git' from the end, if there is one, then git clone.
dstPath = strings.TrimSuffix(dstPath, ".git")
importer = clonePlan
} else {
// not using git, simply symlink the directory. Remove the file:// scheme if it is included.
switch parsed.Scheme {
case "file":
from = parsed.Path
case "":
// this is what we expect without file://; do nothing
default:
return fmt.Errorf("unknown scheme %s for local files. did you forget to pass --git?", parsed.Scheme)
}
importer = symlinkPlan
}
err = importer(dstPath, from)
if err == nil {
fmt.Println("imported plans:")
_ = printPlans(cfg, dstPath, true)
}
return err
}
func symlinkPlan(dst, src string) error {
abs, err := filepath.Abs(src)
if err != nil {
return err
}
ev, err := filepath.EvalSymlinks(abs)
if err != nil {
return err
}
fmt.Printf("created symlink %s -> %s\n", dst, src)
return os.Symlink(ev, dst)
}
func clonePlan(dst, src string) error {
cloneOpts := git.CloneOptions{
URL: src,
Progress: os.Stderr,
}
_, err := git.PlainClone(dst, false, &cloneOpts)
if err != nil {
msg := `could not clone %s.
please double-check the git source is correct.
1. the remote repository may not exist.
2. the local directory may not be empty.
3. the permissions over the given transport (ssh, git, https, etc..) may be restricted.
4. if using the SSH transport, double-check your ssh-agent is running with private keys added.
this is the error message I received:
%v
`
return fmt.Errorf(msg, cloneOpts.URL, err)
}
fmt.Printf("cloned plan %s -> %s\n", dst, src)
return nil
}
func rmCommand(c *cli.Context) error {
cfg := &config.EnvConfig{}
if err := cfg.Load(); err != nil {
return err
}
if c.Bool("yes") {
pdir := filepath.Join(cfg.Dirs().Plans(), c.String("plan"))
err := os.RemoveAll(pdir)
if err != nil {
return err
}
fmt.Printf("plan at %s removed.\n", pdir)
return nil
}
fmt.Println("really delete? pass --yes flag to confirm.")
return nil
}
func listCommand(c *cli.Context) error {
cfg := &config.EnvConfig{}
if err := cfg.Load(); err != nil {
return err
}
return printPlans(cfg, cfg.Dirs().Plans(), c.Bool("testcases"))
}
func printPlans(cfg *config.EnvConfig, rootDir string, testcases bool) error {
manifests, err := zglob.GlobFollowSymlinks(filepath.Join(rootDir, "**", "manifest.toml"))
if err != nil {
return fmt.Errorf("failed to discover test plans under %s: %w", cfg.Dirs().Plans(), err)
}
tw := tabwriter.NewWriter(os.Stdout, 1, 1, 1, ' ', 0)
defer tw.Flush()
for _, file := range manifests {
dir := filepath.Dir(file)
plan, err := filepath.Rel(cfg.Dirs().Plans(), dir)
if err != nil {
return fmt.Errorf("failed to relativize plan directory %s: %w", dir, err)
}
var manifest api.TestPlanManifest
if _, err = toml.DecodeFile(file, &manifest); err != nil {
return fmt.Errorf("failed to process manifest file at %s: %w", file, err)
}
if testcases {
for _, tc := range manifest.TestCases {
_, _ = fmt.Fprintf(tw, "%s\t%s\n", plan, tc.Name)
}
} else {
_, _ = fmt.Fprintln(tw, plan)
}
}
return nil
}