-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
200 lines (157 loc) · 3.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/apex/log"
"github.com/apex/log/handlers/text"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/lambda"
"github.com/chzyer/readline"
// plugins
_ "github.com/apex/apex/plugins/env"
_ "github.com/apex/apex/plugins/golang"
_ "github.com/apex/apex/plugins/hooks"
_ "github.com/apex/apex/plugins/inference"
_ "github.com/apex/apex/plugins/nodejs"
_ "github.com/apex/apex/plugins/python"
_ "github.com/apex/apex/plugins/shim"
"github.com/apex/apex/colors"
"github.com/apex/apex/function"
"github.com/apex/apex/project"
)
func init() {
log.SetHandler(text.Default)
}
// version of apex-shell.
var version = "1.0.0"
// prompt for shell.
var prompt = fmt.Sprintf("\033[%dmapex>\033[0m ", colors.Blue)
// source for the lambda function.
var functionSource = `
package main
import (
"encoding/json"
"os/exec"
"github.com/apex/go-apex"
"github.com/apex/log"
"github.com/apex/log/handlers/logfmt"
)
type message struct {
Command string
}
func init() {
log.SetHandler(logfmt.Default)
}
func main() {
log.Info("starting")
apex.HandleFunc(func(event json.RawMessage, ctx *apex.Context) (interface{}, error) {
var msg message
if err := json.Unmarshal(event, &msg); err != nil {
return nil, err
}
log.WithField("command", msg.Command).Info("exec")
cmd := exec.Command("sh", "-c", msg.Command)
out, err := cmd.CombinedOutput()
return string(out), err
})
}
`
// config for shell function.
var functionConfig = `{
"description": "Apex generated REPL function",
"runtime": "golang",
"timeout": %d
}`
// event.
type event struct {
Command string
}
// flags
var (
chdir = flag.String("chdir", "", "Working directory")
logLevel = flag.String("log-level", "info", "Log level")
showVersion = flag.Bool("version", false, "Output version")
timeout = flag.Int("timeout", 60, "Timeout in seconds")
)
func main() {
flag.Parse()
if l, err := log.ParseLevel(*logLevel); err == nil {
log.SetLevel(l)
}
if *showVersion {
fmt.Println(version)
return
}
if *chdir != "" {
if err := os.Chdir(*chdir); err != nil {
log.Fatalf("error: %s", err)
}
}
if err := shell(); err != nil {
log.Fatalf("error: %s", err)
}
}
// shell sets up AWS credentials, deploys the function and starts the REPL.
func shell() error {
config := aws.NewConfig()
project := &project.Project{
Service: lambda.New(session.New(config)),
Log: log.Log,
Path: ".",
}
if err := project.Open(); err != nil {
return err
}
fn, err := deploy(project)
if err != nil {
return err
}
rl, err := readline.New(prompt)
if err != nil {
return err
}
defer rl.Close()
for {
line, err := rl.Readline()
if err != nil {
break
}
reply, _, err := fn.Invoke(event{line}, nil)
if err != nil {
return err
}
var s string
if err := json.NewDecoder(reply).Decode(&s); err != nil {
return err
}
os.Stdout.WriteString(s)
}
return fn.Delete()
}
// deploy shell function.
func deploy(project *project.Project) (*function.Function, error) {
path := filepath.Join(os.TempDir(), "__apex_repl__")
if err := os.MkdirAll(path, 0755); err != nil {
return nil, err
}
if err := ioutil.WriteFile(filepath.Join(path, "main.go"), []byte(functionSource), 0755); err != nil {
return nil, err
}
config := fmt.Sprintf(functionConfig, *timeout)
if err := ioutil.WriteFile(filepath.Join(path, "function.json"), []byte(config), 0755); err != nil {
return nil, err
}
fn, err := project.LoadFunctionByPath("repl", path)
if err != nil {
return nil, err
}
if err := fn.Deploy(); err != nil {
return nil, err
}
return fn, nil
}