-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathvariant.go
899 lines (698 loc) · 17.2 KB
/
variant.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
package variant
import (
"context"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"sync"
"github.com/hashicorp/hcl/v2/ext/typeexpr"
"github.com/mattn/go-isatty"
"github.com/spf13/cobra"
"github.com/zclconf/go-cty/cty"
"golang.org/x/xerrors"
"github.com/mumoshu/variant2/pkg/app"
)
var Version string
type Main struct {
// Command is the name of the executable used for this process.
// E.g. `go build -o myapp ./` and `./myapp cmd --flag1` results in Command being "myapp".
Command string
Source []byte
// Path can be a path to the directory or the file containing the definition for the Variant command being run
Path string
Stdout, Stderr io.Writer
Args []string
Getenv func(string) string
Getwd func() (string, error)
Setup app.Setup
}
type Setup func() (*Main, error)
type InitParams struct {
Command string
Setup app.Setup
}
type Option func(*Main)
func FromPath(path string, opts ...Option) Setup {
return func() (*Main, error) {
if path == "" {
var err error
path, err = os.Getwd()
if err != nil {
return nil, xerrors.Errorf("getwd: %w", err)
}
}
info, err := os.Stat(path)
if err != nil {
return nil, xerrors.Errorf("stat %s: %w", path, err)
}
var setup app.Setup
if info.IsDir() {
setup = app.FromDir(path)
} else {
setup = app.FromFile(path)
}
m := &Main{
Setup: setup,
}
if m.Command == "" {
m.Command = filepath.Base(path)
}
for _, o := range opts {
o(m)
}
return m, nil
}
}
func FromSource(cmd, source string) Setup {
return func() (*Main, error) {
if cmd == "" {
return nil, errors.New("command name must be set when loadling from Variant source file")
}
return &Main{
Command: cmd,
Setup: app.FromSources(map[string][]byte{cmd: []byte(source)}),
}, nil
}
}
func Load(setup Setup) (*Runner, error) {
initParams, err := setup()
if err != nil {
return nil, err
}
m := Init(*initParams)
return m.createRunner(m.Command, m.Setup)
}
func MustLoad(setup Setup) *Runner {
r, err := Load(setup)
if err != nil {
panic(err)
}
return r
}
func New() Main {
return Init(Main{})
}
type Env struct {
Args []string
Getenv func(name string) string
Getwd func() (string, error)
}
func GetPathAndArgsFromEnv(env Env) (string, string, []string) {
osArgs := env.Args
var cmd string
var path string
if len(osArgs) > 1 {
file := osArgs[1]
info, err := os.Stat(file)
if err == nil && info != nil && !info.IsDir() {
osArgs = osArgs[2:]
path = file
cmd = filepath.Base(file)
} else {
osArgs = osArgs[1:]
}
} else {
osArgs = []string{}
}
if path == "" {
dirFromEnv := env.Getenv("VARIANT_DIR")
if dirFromEnv != "" {
path = dirFromEnv
} else {
var err error
path, err = env.Getwd()
if err != nil {
panic(err)
}
}
}
return cmd, path, osArgs
}
func Init(m Main) Main {
if m.Stdout == nil {
m.Stdout = os.Stdout
}
if m.Stderr == nil {
m.Stderr = os.Stderr
}
if m.Getenv == nil {
m.Getenv = os.Getenv
}
if m.Getwd == nil {
m.Getwd = os.Getwd
}
cmdNameFromEnv := m.Getenv("VARIANT_NAME")
if cmdNameFromEnv != "" {
m.Command = cmdNameFromEnv
}
return m
}
type Config struct {
Parameters func([]string) (map[string]interface{}, error)
Options func() map[string]func() interface{}
}
func valueOnChange(cli *cobra.Command, name string, v interface{}) func() interface{} {
return func() interface{} {
// This avoids setting "" when the flag is actually missing, so that
// we can differentiate between when (1)an empty string is specified vs (2)no flag is provided.
if cli.PersistentFlags().Lookup(name).Changed {
return v
}
return nil
}
}
func createCobraFlagsFromVariantOptions(cli *cobra.Command, opts []app.OptionSpec, interactive bool) (map[string]func() interface{}, error) {
lazyOptionValues := map[string]func() interface{}{}
for i := range opts {
o := opts[i]
var tpe cty.Type
tpe, diags := typeexpr.TypeConstraint(o.Type)
if diags != nil {
return nil, diags
}
var desc string
if o.Description != nil {
desc = *o.Description
}
switch tpe {
case cty.String:
var v string
if o.Short != nil {
cli.PersistentFlags().StringVarP(&v, o.Name, *o.Short, "", desc)
} else {
cli.PersistentFlags().StringVar(&v, o.Name, "", desc)
}
lazyOptionValues[o.Name] = valueOnChange(cli, o.Name, &v)
case cty.Bool:
var v bool
if o.Short != nil {
cli.PersistentFlags().BoolVarP(&v, o.Name, *o.Short, false, desc)
} else {
cli.PersistentFlags().BoolVar(&v, o.Name, false, desc)
}
lazyOptionValues[o.Name] = valueOnChange(cli, o.Name, &v)
case cty.Number:
var v int
if o.Short != nil {
cli.PersistentFlags().IntVarP(&v, o.Name, *o.Short, 0, desc)
} else {
cli.PersistentFlags().IntVar(&v, o.Name, 0, desc)
}
lazyOptionValues[o.Name] = valueOnChange(cli, o.Name, &v)
case cty.List(cty.String):
v := []string{}
if o.Short != nil {
cli.PersistentFlags().StringSliceVarP(&v, o.Name, *o.Short, []string{}, desc)
} else {
cli.PersistentFlags().StringSliceVar(&v, o.Name, []string{}, desc)
}
lazyOptionValues[o.Name] = valueOnChange(cli, o.Name, &v)
case cty.List(cty.Number):
v := []int{}
if o.Short != nil {
cli.PersistentFlags().IntSliceVarP(&v, o.Name, *o.Short, []int{}, desc)
} else {
cli.PersistentFlags().IntSliceVar(&v, o.Name, []int{}, desc)
}
lazyOptionValues[o.Name] = valueOnChange(cli, o.Name, &v)
}
if !app.IsExpressionEmpty(o.Default) || interactive {
} else if err := cli.MarkPersistentFlagRequired(o.Name); err != nil {
panic(err)
}
}
return lazyOptionValues, nil
}
func configureCommand(cli *cobra.Command, root app.JobSpec, interactive bool) (*Config, error) {
lazyOptionValues, err := createCobraFlagsFromVariantOptions(cli, root.Options, interactive)
if err != nil {
return nil, err
}
opts := func() map[string]func() interface{} {
m := map[string]func() interface{}{}
for name, f := range lazyOptionValues {
m[name] = f
}
return m
}
var minArgs int
var maxArgs int
lazyParamValues := map[string]func(args []string) (interface{}, error){}
var hasVarArgs bool
for i := range root.Parameters {
maxArgs++
p := root.Parameters[i]
r := p.Default.Range()
if r.Start == r.End {
minArgs++
}
ii := i
ty, err := typeexpr.TypeConstraint(p.Type)
if err != nil {
return nil, err
}
var f func([]string, int) (interface{}, error)
switch ty {
case cty.Bool:
f = func(args []string, i int) (interface{}, error) {
return strconv.ParseBool(args[i])
}
case cty.String:
f = func(args []string, i int) (interface{}, error) {
return args[i], nil
}
case cty.Number:
f = func(args []string, i int) (interface{}, error) {
return strconv.Atoi(args[i])
}
case cty.List(cty.String):
if i != len(root.Parameters)-1 {
return nil, fmt.Errorf("list(string) parameter %q must be positioned at last", p.Name)
}
f = func(args []string, i int) (interface{}, error) {
return args[i:], nil
}
hasVarArgs = true
default:
return nil, fmt.Errorf("invalid parameter %q: type %s is not supported", p.Name, ty.FriendlyName())
}
lazyParamValues[p.Name] = func(args []string) (interface{}, error) {
if len(args) <= ii {
return nil, nil
}
return f(args, ii)
}
}
if hasVarArgs {
cli.Args = cobra.MinimumNArgs(minArgs)
} else {
cli.Args = cobra.RangeArgs(minArgs, maxArgs)
}
params := func(args []string) (map[string]interface{}, error) {
m := map[string]interface{}{}
for name, f := range lazyParamValues {
v, err := f(args)
if err != nil {
return nil, err
}
m[name] = v
}
return m, nil
}
return &Config{Parameters: params, Options: opts}, nil
}
func getMergedParamsAndOpts(
cfgs map[string]*Config, cmdName string, args []string) (map[string]interface{}, map[string]interface{}, error) {
names := strings.Split(cmdName, " ")
optGetters := map[string]func() interface{}{}
for i := range names {
curName := strings.Join(names[:i+1], " ")
if curCfg, ok := cfgs[curName]; ok {
curOpts := curCfg.Options()
for n := range curOpts {
optGetters[n] = curOpts[n]
}
}
}
cfg := cfgs[cmdName]
params, err := cfg.Parameters(args)
if err != nil {
return nil, nil, err
}
opts := map[string]interface{}{}
for n, get := range optGetters {
opts[n] = get()
}
return params, opts, nil
}
func (m *Main) initApp(setup app.Setup) (*app.App, error) {
ap, err := app.New(setup)
if err != nil {
if ap == nil {
fmt.Fprintf(os.Stderr, "%+v\n", err)
} else {
ap.PrintError(err)
}
//nolint:wrapcheck
return nil, err
}
ap.Stdout = m.Stdout
ap.Stderr = m.Stderr
return ap, nil
}
func (m Main) createRunner(cmd string, setup app.Setup) (*Runner, error) {
ap, err := m.initApp(setup)
if err != nil {
return nil, err
}
return m.newRunner(ap, cmd), nil
}
func (m Main) newRunner(ap *app.App, cmdName string) *Runner {
m2 := &Runner{
mut: &sync.Mutex{},
ap: ap,
runCmdName: cmdName,
}
m.initRunner(m2)
return m2
}
func (m Main) initRunner(r *Runner) {
siTty := isatty.IsTerminal(os.Stdin.Fd())
soTty := isatty.IsTerminal(os.Stdout.Fd())
// Enable prompts for missing inputs when stdin and stdout are connected to a tty
r.Interactive = siTty && soTty
if r.Interactive {
r.SetOpts = app.DefaultSetOpts
}
r.goJobs = map[string]Job{}
r.jobRunProviders = map[string]func(State) JobRun{}
for jobName := range r.ap.JobByName {
n := jobName
r.jobRunProviders[n] = func(st State) JobRun {
return func(ctx context.Context) error {
if st.Stdout != nil {
defer func() {
if err := st.Stdout.Close(); err != nil {
panic(err)
}
}()
}
if st.Stderr != nil {
defer func() {
if err := st.Stderr.Close(); err != nil {
panic(err)
}
}()
}
r, err := r.ap.Run(n, st.Parameters, st.Options)
if err != nil {
return xerrors.Errorf("running job %q: %w", n, err)
}
if st.Stdout != nil {
if _, err := st.Stdout.Write([]byte(r.Stdout)); err != nil {
return xerrors.Errorf("writing stdout of job %q: %w", n, err)
}
}
if st.Stderr != nil {
if _, err := st.Stderr.Write([]byte(r.Stderr)); err != nil {
return xerrors.Errorf("writing stderr of job %q: %w", n, err)
}
}
return nil
}
}
}
}
type Runner struct {
ap *app.App
runCmdName string
runCmd *cobra.Command
variantCmd *cobra.Command
goJobs map[string]Job
jobRunProviders map[string]func(State) JobRun
Interactive bool
SetOpts app.SetOptsFunc
mut *sync.Mutex
}
func (r *Runner) Cobra() (*cobra.Command, error) {
ap, rootCmdName := r.ap, r.runCmdName
if rootCmdName == "" {
rootCmdName = "run"
}
jobs := map[string]app.JobSpec{}
jobNames := []string{}
for jobName, j := range ap.JobByName {
var name string
if jobName == "" {
name = rootCmdName
} else {
name = fmt.Sprintf("%s %s", rootCmdName, jobName)
}
jobs[name] = j
jobNames = append(jobNames, name)
}
sort.Strings(jobNames)
commands := map[string]*cobra.Command{}
cfgs := map[string]*Config{}
for _, n := range jobNames {
name := n
job := jobs[name]
names := strings.Split(name, " ")
var parent *cobra.Command
cmdName := names[len(names)-1]
switch len(names) {
case 1:
default:
names = names[:len(names)-1]
var ok bool
parent, ok = commands[strings.Join(names, " ")]
if !ok {
for i := range names {
intName := strings.Join(names[:i+1], " ")
cur, ok := commands[intName]
if !ok {
cur = &cobra.Command{
Use: names[i],
}
parent.AddCommand(cur)
commands[intName] = cur
}
parent = cur
}
}
}
var desc string
if job.Description != nil {
desc = *job.Description
}
for _, p := range job.Parameters {
cmdName += fmt.Sprintf(" [%s]", strings.ToUpper(p.Name))
}
cli := &cobra.Command{
Use: cmdName,
Short: strings.Split(desc, "\n")[0],
Long: desc,
}
if job.Private != nil {
cli.Hidden = *job.Private
}
cfg, err := configureCommand(cli, job, r.Interactive)
if err != nil {
return nil, err
}
cfgs[name] = cfg
cli.RunE = func(cmd *cobra.Command, args []string) error {
params, opts, err := getMergedParamsAndOpts(cfgs, name, args)
if err != nil {
return err
}
_, err = ap.Run(job.Name, params, opts, r.SetOpts)
if err != nil && err.Error() != app.NoRunMessage {
cmd.SilenceUsage = true
}
//nolint:wrapcheck
return err
}
commands[name] = cli
if parent != nil {
parent.AddCommand(cli)
}
}
rootCmd := commands[rootCmdName]
return rootCmd, nil
}
type RunOptions struct {
Stdout io.Writer
Stderr io.Writer
SetOpts app.SetOptsFunc
DisableLocking bool
}
// Add adds a job to this runner so that it can later by calling `Job`.
func (r Runner) Add(job Job) {
r.goJobs[job.Name] = job
if job.Name == "" {
panic(fmt.Errorf("invalid job name %q", job.Name))
}
r.jobRunProviders[job.Name] = func(st State) JobRun {
return func(ctx context.Context) error {
return job.Run(ctx, st)
}
}
}
// Job prepares a job to be run.
func (r Runner) Job(job string, opts State) (JobRun, error) {
f, ok := r.jobRunProviders[job]
if !ok {
return nil, fmt.Errorf("job %q not added", job)
}
if opts.Options == nil {
opts.Options = map[string]interface{}{}
}
if opts.Parameters == nil {
opts.Parameters = map[string]interface{}{}
}
jr := f(opts)
return jr, nil
}
func (r *Runner) Run(arguments []string, opt ...RunOptions) error {
var opts RunOptions
if len(opt) > 0 {
opts = opt[0]
}
if !opts.DisableLocking {
r.mut.Lock()
defer r.mut.Unlock()
}
if opts.SetOpts != nil {
r.SetOpts = opts.SetOpts
defer func() {
r.SetOpts = nil
}()
}
if r.runCmd == nil {
var err error
r.runCmd, err = r.Cobra()
if err != nil {
r.ap.PrintError(err)
return err
}
}
var cmd *cobra.Command
if r.runCmdName != "" {
cmd = r.runCmd
} else {
if r.variantCmd == nil {
r.variantCmd = r.createVariantRootCommand()
}
cmd = r.variantCmd
}
var err error
{
cmdStdout := cmd.OutOrStdout()
cmdStderr := cmd.OutOrStderr()
appStdout := r.ap.Stdout
appStderr := r.ap.Stderr
cmd.SetArgs(arguments)
if opts.Stdout != nil {
cmd.SetOut(opts.Stdout)
r.ap.Stdout = opts.Stdout
}
if opts.Stderr != nil {
cmd.SetErr(opts.Stderr)
r.ap.Stderr = opts.Stderr
}
err = cmd.Execute()
cmd.SetOut(cmdStdout)
cmd.SetErr(cmdStderr)
r.ap.Stdout = appStdout
r.ap.Stderr = appStderr
}
//nolint:wrapcheck
return err
}
type Error struct {
Message string
ExitCode int
}
func (e Error) Error() string {
return e.Message
}
func (r *Runner) createVariantRootCommand() *cobra.Command {
const VariantBinName = "variant"
rootCmd := &cobra.Command{
Use: VariantBinName,
Version: Version,
}
testCmd := &cobra.Command{
Use: "test [NAME]",
Short: "Run test(s)",
Args: cobra.MaximumNArgs(1),
RunE: func(c *cobra.Command, args []string) error {
var prefix string
if len(args) > 0 {
prefix = args[0]
}
_, err := r.ap.RunTests(prefix)
if err != nil {
c.SilenceUsage = true
}
//nolint:wrapcheck
return err
},
}
exportCmd := &cobra.Command{
Use: "export SUBCOMMAND SRC_DIR OUTPUT_PATH",
Short: "Export the Variant command defined in SRC_DIR to OUTPUT_PATH",
}
{
shimCmd := &cobra.Command{
Use: "shim SRC_DIR DST_DIR",
Short: "Copy and generate shim for the Variant command defined in the SRC",
Args: cobra.ExactArgs(2),
RunE: func(c *cobra.Command, args []string) error {
err := r.ap.ExportShim(args[0], args[1])
if err != nil {
c.SilenceUsage = true
}
//nolint:wrapcheck
return err
},
}
exportCmd.AddCommand(shimCmd)
exportCmd.AddCommand(newExportGo(r))
exportCmd.AddCommand(newExportBinary(r))
}
generateCmd := &cobra.Command{
Use: "generate RESOURCE DIR",
Short: "Generate RESOURCE for the Variant command defined in DIR",
}
{
generateShimCmd := &cobra.Command{
Use: "shim DIR",
Short: "Generate a shim for the Variant command defined in DIR",
Args: cobra.ExactArgs(1),
RunE: func(c *cobra.Command, args []string) error {
err := app.GenerateShim(VariantBinName, args[0])
if err != nil {
c.SilenceUsage = true
}
return err
},
}
generateCmd.AddCommand(generateShimCmd)
}
startCmd := &cobra.Command{
Use: "start NAME",
Short: "Start the named integration to turn the Variant command to whatever",
}
{
var botName string
startSlackbotCmd := &cobra.Command{
Use: "slackbot",
Short: "Start the slackbot that responds to slash commands by running corresopnding Variant commands",
RunE: func(c *cobra.Command, args []string) error {
err := r.StartSlackbot(botName)
if err != nil {
c.SilenceUsage = true
}
return err
},
}
startSlackbotCmd.Flags().StringVarP(&botName, "name", "n", "", "Name of the slash command without /. For example, \"--name foo\" results in the bot responding to \"/foo <CMD> <ARGS>\"")
if err := startSlackbotCmd.MarkFlagRequired("name"); err != nil {
panic(err)
}
startCmd.AddCommand(startSlackbotCmd)
}
rootCmd.AddCommand(r.runCmd)
rootCmd.AddCommand(testCmd)
rootCmd.AddCommand(exportCmd)
rootCmd.AddCommand(generateCmd)
rootCmd.AddCommand(startCmd)
return rootCmd
}