-
Notifications
You must be signed in to change notification settings - Fork 30
/
run.go
132 lines (108 loc) · 2.78 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
124
125
126
127
128
129
130
131
132
package cmd
import (
"context"
"os"
"os/signal"
"strings"
"syscall"
"github.com/stateful/runme/internal/document"
"github.com/pkg/errors"
"github.com/rwtodd/Go.Sed/sed"
"github.com/spf13/cobra"
"github.com/stateful/runme/internal/runner"
)
func runCmd() *cobra.Command {
var (
dryRun bool
replaceScripts []string
)
cmd := cobra.Command{
Use: "run",
Aliases: []string{"exec"},
Short: "Run a selected command.",
Long: "Run a selected command identified based on its unique parsed name.",
Args: cobra.ExactArgs(1),
ValidArgsFunction: validCmdNames,
RunE: func(cmd *cobra.Command, args []string) error {
blocks, err := getCodeBlocks()
if err != nil {
return err
}
block, err := lookupCodeBlock(blocks, args[0])
if err != nil {
return err
}
if err := replace(block, replaceScripts); err != nil {
return err
}
executable, err := newExecutable(cmd, block)
if err != nil {
return err
}
ctx, cancel := sigCtxCancel(cmd.Context())
defer cancel()
if dryRun {
executable.DryRun(ctx, cmd.ErrOrStderr())
return nil
}
return errors.WithStack(executable.Run(ctx))
},
}
cmd.Flags().BoolVar(&dryRun, "dry-run", false, "Print the final command without executing.")
cmd.Flags().StringArrayVarP(&replaceScripts, "replace", "r", nil, "Replace instructions using sed.")
return &cmd
}
func newExecutable(cmd *cobra.Command, block *document.CodeBlock) (runner.Executable, error) {
switch block.Executable() {
case "sh":
return &runner.Shell{
Cmds: block.Lines(),
Base: runner.Base{
Dir: chdir,
Stdin: cmd.InOrStdin(),
Stdout: cmd.OutOrStdout(),
Stderr: cmd.ErrOrStderr(),
},
}, nil
case "go":
return &runner.Go{
Source: block.Content(),
Base: runner.Base{
Dir: chdir,
Stdin: cmd.InOrStdin(),
Stdout: cmd.OutOrStdout(),
Stderr: cmd.ErrOrStderr(),
},
}, nil
default:
return nil, errors.Errorf("unknown executable: %q", block.Executable())
}
}
func sigCtxCancel(ctx context.Context) (context.Context, context.CancelFunc) {
ctx, cancel := context.WithCancel(ctx)
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigs
cancel()
}()
return ctx, cancel
}
func replace(block *document.CodeBlock, scripts []string) error {
if len(scripts) == 0 {
return nil
}
content := block.Content()
for _, script := range scripts {
engine, err := sed.New(strings.NewReader(script))
if err != nil {
return errors.Wrapf(err, "failed to compile sed script %q", script)
}
content, err = engine.RunString(content)
if err != nil {
return errors.Wrapf(err, "failed to run sed script %q", script)
}
}
block.SetContent(content)
return nil
}