-
Notifications
You must be signed in to change notification settings - Fork 230
/
exec.go
72 lines (60 loc) · 1.58 KB
/
exec.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
package main
import (
"context"
"os"
"os/signal"
"path/filepath"
"github.com/spf13/cobra"
"encr.dev/cli/cmd/encore/root"
daemonpb "encr.dev/proto/encore/daemon"
)
var execCmd = &cobra.Command{
Use: "exec path/to/script [args...]",
Short: "Runs executable scripts against the local Encore app",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
args = []string{"."} // current directory
}
appRoot, wd := determineAppRoot()
execScript(appRoot, wd, args)
},
}
func execScript(appRoot, relWD string, args []string) {
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
ctx, cancel := context.WithCancel(context.Background())
go func() {
<-interrupt
cancel()
}()
commandRelPath := filepath.ToSlash(filepath.Join(relWD, args[0]))
scriptArgs := args[1:]
daemon := setupDaemon(ctx)
stream, err := daemon.ExecScript(ctx, &daemonpb.ExecScriptRequest{
AppRoot: appRoot,
WorkingDir: relWD,
CommandRelPath: commandRelPath,
ScriptArgs: scriptArgs,
Environ: os.Environ(),
TraceFile: root.TraceFile,
Namespace: nonZeroPtr(nsName),
})
if err != nil {
fatal(err)
}
clearTerminalExceptFirstLine()
code := streamCommandOutput(stream, convertJSONLogs())
os.Exit(code)
}
var alphaCmd = &cobra.Command{
Use: "alpha",
Short: "Pre-release functionality in alpha stage",
Hidden: true,
}
func init() {
rootCmd.AddCommand(alphaCmd)
}
func init() {
execCmd.Flags().StringVarP(&nsName, "namespace", "n", "", "Namespace to use (defaults to active namespace)")
alphaCmd.AddCommand(execCmd)
}