forked from imjerrybao/apex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.go
64 lines (50 loc) · 1.03 KB
/
build.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
// Package build outputs a function's zip to stdout.
package build
import (
"errors"
"io"
"os"
"github.com/spf13/cobra"
"github.com/apex/apex/cmd/apex/root"
)
// name of function.
var name string
// example output.
const example = ` Build zip output for a function
$ apex build foo > /tmp/out.zip`
// Command config.
var Command = &cobra.Command{
Use: "build <name>",
Short: "Build a function",
Example: example,
PreRunE: preRun,
RunE: run,
}
// Initialize.
func init() {
root.Register(Command)
}
// PreRun errors if argument is missing.
func preRun(c *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("Missing name argument")
}
name = args[0]
return nil
}
// Run command.
func run(c *cobra.Command, args []string) error {
if err := root.Project.LoadFunctions(name); err != nil {
return err
}
fn := root.Project.Functions[0]
zip, err := fn.Build()
if err != nil {
return err
}
if err := fn.Clean(); err != nil {
return err
}
_, err = io.Copy(os.Stdout, zip)
return err
}