-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
160 lines (140 loc) · 3.2 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"archive/zip"
"bytes"
"context"
"fmt"
"io"
"log"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/kelseyhightower/envconfig"
pb "github.com/nhite/pb-nhite"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
const envPrefix = "NHITE_CLIENT"
type configuration struct {
CertFile string `envconfig:"CERT_FILE" required:"true"`
NhiteAddress string `envconfig:"NHITE_ADDRESS" required:"true"`
}
func main() {
var config configuration
err := envconfig.Process(envPrefix, &config)
if err != nil {
envconfig.Usage(envPrefix, &config)
fmt.Println(err)
os.Exit(1)
}
creds, err := credentials.NewClientTLSFromFile(config.CertFile, "")
if err != nil {
log.Fatal("Cannot load certificate ", err)
}
conn, err := grpc.Dial(config.NhiteAddress, grpc.WithTransportCredentials(creds))
if err != nil {
log.Fatal("Cannot reach grpc server", err)
}
defer conn.Close()
if len(os.Args) < 2 {
log.Fatal("Wrong numbers of arguments")
}
client := pb.NewTerraformClient(conn)
output := &pb.Output{}
switch os.Args[1] {
case "push":
// Create a buffer to write our archive to.
buf := new(bytes.Buffer)
// Create a new zip archive.
archive := zip.NewWriter(buf)
source := "."
info, err := os.Stat(source)
if err != nil {
log.Fatal(err)
}
var baseDir string
if info.IsDir() {
baseDir = filepath.Base(source)
}
filepath.Walk(source, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
header, err := zip.FileInfoHeader(info)
if err != nil {
return err
}
if baseDir != "" {
header.Name = filepath.Join(baseDir, strings.TrimPrefix(path, source))
}
if info.IsDir() {
header.Name += "/"
} else {
ok, err := regexp.MatchString(".tf$", info.Name())
if err != nil {
return err
}
if !ok {
return nil
}
header.Method = zip.Deflate
}
writer, err := archive.CreateHeader(header)
if err != nil {
return err
}
if info.IsDir() {
return nil
}
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(writer, file)
return err
})
// Make sure to check the error on Close.
err = archive.Close()
if err != nil {
log.Fatal("Cannot close zip writer", err)
}
pushClient, err := client.Push(context.Background(), grpc.MaxCallRecvMsgSize(65536))
if err != nil {
log.Fatal("Cannot create grpc push client", err)
}
err = pushClient.Send(&pb.Body{
Zipfile: buf.Bytes(),
})
if err != nil {
log.Fatal("Send error", err)
}
id, err := pushClient.CloseAndRecv()
if err != nil {
log.Fatal("Received returned an error", err)
}
fmt.Println(id.Sha256)
case "init":
output, err = client.Init(context.Background(), &pb.Arg{
os.Args[2],
os.Args[3:],
})
case "plan":
output, err = client.Plan(context.Background(), &pb.Arg{
os.Args[2],
os.Args[3:],
})
case "apply":
output, err = client.Apply(context.Background(), &pb.Arg{
os.Args[2],
os.Args[3:],
})
default:
log.Fatal("Unknown command")
}
stdout := bytes.NewBuffer(output.Stdout)
stderr := bytes.NewBuffer(output.Stderr)
io.Copy(os.Stdout, stdout)
io.Copy(os.Stderr, stderr)
}