-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
53 lines (46 loc) · 1.26 KB
/
util.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
package ssos
import (
"context"
"fmt"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ssm"
)
func WaitCommandInvocations(ctx context.Context, ssmCli *ssm.Client, commandID *string) error {
ticker := time.NewTicker(500 * time.Millisecond)
defer ticker.Stop()
for {
select {
case <-ticker.C:
req := ssmCli.ListCommandsRequest(&ssm.ListCommandsInput{
CommandId: commandID,
})
res, err := req.Send(ctx)
if err != nil {
return err
}
if res.ListCommandsOutput.Commands[0].Status != "Pending" && res.ListCommandsOutput.Commands[0].Status != "InProgress" {
return nil
}
}
}
}
func PrintCommandOutput(ctx context.Context, ssmCli *ssm.Client, commandID *string) error {
listCommandInvocationReq := ssmCli.ListCommandInvocationsRequest(&ssm.ListCommandInvocationsInput{
CommandId: commandID,
Details: aws.Bool(true),
})
listCommandInvocationRes, err := listCommandInvocationReq.Send(ctx)
if err != nil {
return err
}
for _, invocation := range listCommandInvocationRes.CommandInvocations {
p := invocation.CommandPlugins[0]
lines := strings.Split(strings.TrimSpace(*p.Output), "\n")
for _, l := range lines {
fmt.Printf("%s %s\n", *invocation.InstanceId, l)
}
}
return nil
}