-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhandle_pipe.go
84 lines (71 loc) · 1.82 KB
/
handle_pipe.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
package main
import (
"context"
"errors"
"fmt"
"os"
"time"
"github.com/apex/log"
"github.com/fatih/color"
"github.com/jckuester/awsrm/pkg/resource"
"github.com/jckuester/awstools-lib/aws"
"github.com/jckuester/awstools-lib/terraform"
)
func isInputFromPipe() bool {
fileInfo, _ := os.Stdin.Stat()
return fileInfo.Mode()&os.ModeNamedPipe != 0
}
func handleInputFromPipe(ctx context.Context, force bool, dryRun bool) int {
log.Debug("input via pipe")
resources, err := resource.Read(os.Stdin)
if err != nil {
fmt.Fprint(os.Stderr, color.RedString("\nError: %s\n", err))
return 1
}
var clientKeys []aws.ClientKey
for _, r := range resources {
clientKeys = append(clientKeys, aws.ClientKey{Profile: r.Profile, Region: r.Region})
}
err = os.Stdin.Close()
if err != nil {
fmt.Fprint(os.Stderr, color.RedString("\nError: %s\n", err))
return 1
}
providers, err := terraform.NewProviderPool(ctx, clientKeys, terraformAwsProviderVersion, "~/.awsrm", 1*time.Minute)
if err != nil {
if !errors.Is(err, context.Canceled) {
fmt.Fprint(os.Stderr, color.RedString("\nError: %s\n", err))
}
return 1
}
defer func() {
for _, p := range providers {
_ = p.Close()
}
}()
resourcesCh := make(chan resource.UpdatedResources, 1)
go func() { resourcesCh <- resource.Update(resources, providers) }()
select {
case <-ctx.Done():
return 1
case result := <-resourcesCh:
resources = result.Resources
for _, err := range result.Errors {
fmt.Fprint(os.Stderr, color.RedString("Error %s: %s\n", err))
}
}
confirmDevice, err := os.Open("/dev/tty")
if err != nil {
log.Fatalf("can't open /dev/tty: %s", err)
}
doneDelete := make(chan bool, 1)
go func() {
resource.Delete(resources, confirmDevice, force, dryRun, doneDelete)
}()
select {
case <-ctx.Done():
return 0
case <-doneDelete:
}
return 0
}