/
main.go
89 lines (73 loc) · 1.69 KB
/
main.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
package main
import (
"bufio"
"context"
"flag"
"fmt"
"log"
"os"
"os/signal"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/sqs"
"github.com/yosadchyi/go-client-server/pkg/client"
"github.com/yosadchyi/go-client-server/pkg/util"
)
func main() {
awsEndpoint := os.Getenv("AWS_ENDPOINT")
awsRegion := os.Getenv("AWS_REGION")
resolver := util.LocalResolver(awsEndpoint, awsRegion)
queueUrl := flag.String(
"queue-url",
os.Getenv("QUEUE_URL"),
"SQS queue",
)
inputFile := flag.String(
"input-file",
"",
"input file to read commands from, otherwise stdin will be used",
)
flag.Parse()
ctx, cancelFn := context.WithCancel(context.Background())
cfg, err := config.LoadDefaultConfig(
ctx,
config.WithEndpointResolverWithOptions(resolver),
)
if err != nil {
log.Fatalf("failed to load default config %e", err)
}
svc := sqs.NewFromConfig(cfg)
file := os.Stdin
if *inputFile != "" {
file, err = os.OpenFile(*inputFile, os.O_RDONLY, 0)
if err != nil {
log.Fatalf("can't open input file %s", *inputFile)
}
}
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt)
go func() {
s := <-sig
fmt.Printf("system signal: %+v\n", s)
cancelFn()
}()
lines := make(chan string, 1)
isInteractive := file == os.Stdin
go func() {
s := bufio.NewScanner(file)
for s.Scan() {
lines <- s.Text()
}
if !isInteractive {
lines <- "EOF"
}
}()
executor := client.NewExecutor(file, svc, *queueUrl)
var responder client.Responder
if isInteractive {
responder = client.NewInteractiveResponder()
} else {
responder = client.NewBatchResponder()
}
processor := client.NewProcessor(lines, executor, responder)
processor.Run(ctx)
}