generated from wesen/wesen-go-template
/
run.go
123 lines (107 loc) · 3.2 KB
/
run.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
package cmds
import (
"context"
"fmt"
"github.com/go-go-golems/cliopatra/pkg"
"github.com/go-go-golems/glazed/pkg/cli/cliopatra"
"github.com/go-go-golems/glazed/pkg/cmds/layers"
"github.com/spf13/cobra"
"os"
"strings"
)
// NewRunCommand returns a command that can be used to run either commands from
// a file or from a repository.
//
// It currently doesn't allow overloading flags in the underlying program run
// by cliopatra.
//
// See https://github.com/go-go-golems/glazed/issues/220
func NewRunCommand() *cobra.Command {
runCommand := &cobra.Command{
Use: "run",
Short: "Run a command from a file or from a repository program",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
repositories, err := cmd.Flags().GetStringSlice("repository")
cobra.CheckErr(err)
repository := pkg.NewRepository(repositories)
err = repository.Load()
cobra.CheckErr(err)
file, err := cmd.Flags().GetString("file")
cobra.CheckErr(err)
program, err := cmd.Flags().GetString("program")
cobra.CheckErr(err)
options := len(args)
if file != "" {
options++
}
if program != "" {
options++
}
if options > 1 {
cobra.CheckErr(fmt.Errorf("cannot specify both file and program"))
}
var p *cliopatra.Program
if file != "" {
f, err := os.Open(file)
cobra.CheckErr(err)
defer func(f *os.File) {
_ = f.Close()
}(f)
p, err = cliopatra.NewProgramFromYAML(f)
cobra.CheckErr(err)
}
programs := repository.GetPrograms()
if program != "" {
p_, ok := programs[program]
if !ok {
cobra.CheckErr(fmt.Errorf("program %s not found", program))
}
p = p_
}
if len(args) > 0 {
// check if args[0] is a yaml file, otherwise treat as program name
f, err := os.Open(args[0])
if err == nil {
defer func(f *os.File) {
_ = f.Close()
}(f)
p, err = cliopatra.NewProgramFromYAML(f)
cobra.CheckErr(err)
} else {
p_, ok := programs[args[0]]
if !ok {
cobra.CheckErr(fmt.Errorf("program %s not found", args[0]))
}
p = p_
}
}
if p == nil {
cobra.CheckErr(fmt.Errorf("either file or program must be specified"))
}
// TODO(manuel, 2023-03-17) To allow the user to override flags of the loaded cliopatra program
// we need to use a similar mechanism to what sqleton does with its run-command hack.
// We basically need to handle flags manually until we know which program we are going to run,
// at that point we can initialize a custom cobra command with the necessary flags,
// and run that instead after doing some os.Args splicing.
//
// See https://github.com/go-go-golems/glazed/issues/220
ctx := context.Background()
buf := &strings.Builder{}
err = p.RunIntoWriter(
ctx,
layers.NewParsedLayers(),
buf,
)
if err != nil {
_, _ = fmt.Fprint(os.Stderr, buf.String())
cobra.CheckErr(err)
}
fmt.Println(buf.String())
},
}
runCommand.Flags().StringSlice("repository", []string{}, "Repository to load commands from")
runCommand.Flags().String("file", "", "File to load commands from")
runCommand.Flags().String("program", "", "Name of the program loaded from the repositories")
return runCommand
}