-
Notifications
You must be signed in to change notification settings - Fork 2
/
execute.go
76 lines (61 loc) · 1.5 KB
/
execute.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
package client
import (
"io/ioutil"
"os"
boshsys "github.com/cloudfoundry/bosh-utils/system"
"github.com/pkg/errors"
)
type ExecuteOptions struct {
SkipAuthRetry bool
Quiet bool
RemoteFile string
ExtraArgs []string
}
func (s Service) Execute(opts ExecuteOptions) error {
wd, err := os.Getwd()
if err != nil {
return errors.Wrap(err, "getting working directory")
}
tmpfile, err := ioutil.TempFile(wd, ".ssoca-exec-")
if err != nil {
return errors.Wrap(err, "creating temp file")
}
defer os.RemoveAll(tmpfile.Name())
err = tmpfile.Close()
if err != nil {
return errors.Wrap(err, "closing tempfile")
}
err = s.Get(GetOptions{
SkipAuthRetry: opts.SkipAuthRetry,
Quiet: opts.Quiet,
RemoteFile: opts.RemoteFile,
LocalFile: tmpfile.Name(),
})
if err != nil {
return errors.Wrap(err, "getting file")
}
err = os.Chmod(tmpfile.Name(), 0700)
if err != nil {
return errors.Wrap(err, "chmoding tempfile")
}
configManager, err := s.runtime.GetConfigManager()
if err != nil {
return errors.Wrap(err, "getting config manager")
}
cmd := boshsys.Command{
Name: tmpfile.Name(),
Args: opts.ExtraArgs,
Env: map[string]string{
"SSOCA_CONFIG": configManager.GetSource(),
"SSOCA_ENVIRONMENT": s.runtime.GetEnvironmentName(),
},
Stdin: s.runtime.GetStdin(),
Stdout: s.runtime.GetStdout(),
Stderr: s.runtime.GetStderr(),
}
_, _, _, err = s.cmdRunner.RunComplexCommand(cmd)
if err != nil {
return errors.Wrap(err, "executing")
}
return nil
}