-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec.go
165 lines (141 loc) · 3.55 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
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
package app
import (
"bufio"
"io"
"os"
"os/exec"
"sync"
"github.com/aserto-dev/ds-load/cli/pkg/cc"
"github.com/aserto-dev/ds-load/cli/pkg/clients"
"github.com/aserto-dev/ds-load/cli/pkg/publish"
plug "github.com/aserto-dev/ds-load/sdk/plugin"
"github.com/aserto-dev/ds-load/cli/pkg/plugin"
"github.com/aserto-dev/ds-load/sdk/common"
"github.com/pkg/errors"
"golang.org/x/exp/slices"
)
type ExecCmd struct {
PublishCmd
CommandArgs []string `name:"command" passthrough:"" arg:"" help:"available commands are: ${plugins}"`
Print bool `name:"print" short:"p" help:"print output to stdout"`
PluginFolder string `hidden:""`
execPlugin *plugin.Plugin `kong:"-"`
pluginArgs []string `kong:"-"`
publisher plug.Publisher `kong:"-"`
}
func (e *ExecCmd) Run(c *cc.CommonCtx) error {
defaultPrintCmd := []string{"fetch", "version", "export-transform"}
var err error
var find *plugin.Finder
if e.PluginFolder != "" {
find = plugin.NewFinder(true, e.PluginFolder)
} else {
find, err = plugin.NewHomeDirFinder(true)
if err != nil {
return err
}
}
pl := e.CommandArgs[0]
plugins, err := find.Find()
if err != nil {
return err
}
for _, p := range plugins {
if pl == p.Name {
e.execPlugin = p
break
}
}
if e.execPlugin == nil {
return errors.Errorf("plugin [%s] not found", pl)
}
e.pluginArgs = e.CommandArgs[1:]
var pluginSubCommand string
if len(e.CommandArgs) > 1 {
pluginSubCommand = e.CommandArgs[1]
}
if slices.Contains(e.pluginArgs, "-h") || slices.Contains(e.pluginArgs, "--help") || slices.Contains(defaultPrintCmd, pluginSubCommand) {
e.Print = true
}
if !e.Print {
if e.V2 {
dirClient, err := clients.NewDirectoryV2ImportClient(c.Context, &e.Config)
if err != nil {
return errors.Wrap(err, "Could not connect to the directory")
}
e.publisher = publish.NewDirectoryV2Publisher(c, dirClient)
} else {
dirClient, err := clients.NewDirectoryV3ImportClient(c.Context, &e.Config)
if err != nil {
return errors.Wrap(err, "Could not connect to the directory")
}
e.publisher = publish.NewDirectoryPublisher(c, dirClient)
}
}
return e.LaunchPlugin(c)
}
func (e *ExecCmd) LaunchPlugin(c *cc.CommonCtx) error {
if (!slices.Contains(e.pluginArgs, "-c") || !slices.Contains(e.pluginArgs, "--config")) && c.ConfigPath != "" {
e.pluginArgs = append(e.pluginArgs, "-c", c.ConfigPath)
}
pluginCmd := exec.Command(e.execPlugin.Path, e.pluginArgs...) //nolint:gosec
var pStdout io.ReadCloser
var wg sync.WaitGroup
pStderr, err := pluginCmd.StderrPipe()
if err != nil {
return err
}
defer pStderr.Close()
wg.Add(1)
go listenOnStderr(c, &wg, pStderr)
if e.Print {
pluginCmd.Stdout = os.Stdout
} else {
pStdout, err = pluginCmd.StdoutPipe()
if err != nil {
return err
}
defer pStdout.Close()
}
fi, err := os.Stdin.Stat()
if err != nil {
return err
}
if (fi.Mode() & os.ModeCharDevice) == 0 {
pluginCmd.Stdin = os.Stdin
}
err = pluginCmd.Start()
if err != nil {
return err
}
if !e.Print {
err = e.publisher.Publish(c.Context, pStdout)
}
if err != nil {
wg.Wait()
return err
}
wg.Wait()
return pluginCmd.Wait()
}
func listenOnStderr(c *cc.CommonCtx, wg *sync.WaitGroup, stderr io.ReadCloser) {
scanner := bufio.NewReader(stderr)
gotError := false
for {
line, err := scanner.ReadBytes('\n')
os.Stderr.Write(line)
if len(line) > 0 {
gotError = true
}
// we have reached the end of the stream
if err == io.EOF {
if gotError {
common.SetExitCode(1)
}
break
} else if err != nil {
c.Log.Fatal().Err(err)
}
}
wg.Done()
}