-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.go
79 lines (63 loc) · 1.16 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"os/exec"
)
func run(prog string, args ...string) ([]byte, error) {
cmd := exec.Command(prog, args...)
outPipe, err := cmd.StdoutPipe()
if err != nil {
return []byte{}, err
}
errPipe, err := cmd.StderrPipe()
if err != nil {
return []byte{}, err
}
err = cmd.Start()
if err != nil {
return []byte{}, err
}
var outBuf bytes.Buffer
var errBuf bytes.Buffer
ch := make(chan bool)
go func() {
io.Copy(&outBuf, outPipe)
ch <- true
}()
io.Copy(&errBuf, errPipe)
_ = <-ch
err = cmd.Wait()
if err != nil {
return outBuf.Bytes(), err
}
if len(errBuf.Bytes()) != 0 {
return outBuf.Bytes(), fmt.Errorf("%s", errBuf.Bytes())
}
return outBuf.Bytes(), nil
}
type RoleList struct {
Roles []Role
}
type Role struct {
RoleName string
Arn string
}
func RoleMap() (map[string]string, error) {
res := make(map[string]string)
data, err := run("aws", "iam", "list-roles")
if err != nil {
return res, err
}
var rlist RoleList
err = json.Unmarshal(data, &rlist)
if err != nil {
return res, err
}
for _, v := range rlist.Roles {
res[v.RoleName] = v.Arn
}
return res, nil
}