-
Notifications
You must be signed in to change notification settings - Fork 231
Expand file tree
/
Copy pathup.go
More file actions
156 lines (123 loc) · 3.84 KB
/
up.go
File metadata and controls
156 lines (123 loc) · 3.84 KB
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
// Copyright (c) OpenFaaS Author(s) 2018. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
package commands
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/go-git/go-git/v5/plumbing/format/gitignore"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
var (
skipPush bool
skipDeploy bool
usePublish bool
watch bool
)
func init() {
upFlagset := pflag.NewFlagSet("up", pflag.ExitOnError)
upFlagset.BoolVar(&usePublish, "publish", false, "Use faas-cli publish instead of faas-cli build followed by faas-cli push")
upFlagset.BoolVar(&skipPush, "skip-push", false, "Skip pushing function to remote registry")
upFlagset.BoolVar(&skipDeploy, "skip-deploy", false, "Skip function deployment")
upFlagset.StringVar(&remoteBuilder, "remote-builder", "", "URL to the builder")
upFlagset.StringVar(&payloadSecretPath, "payload-secret", "", "Path to payload secret file")
upFlagset.BoolVar(&watch, "watch", false, "Watch for changes in files and re-deploy")
upCmd.Flags().AddFlagSet(upFlagset)
build, _, _ := faasCmd.Find([]string{"build"})
upCmd.Flags().AddFlagSet(build.Flags())
push, _, _ := faasCmd.Find([]string{"push"})
upCmd.Flags().AddFlagSet(push.Flags())
deploy, _, _ := faasCmd.Find([]string{"deploy"})
upCmd.Flags().AddFlagSet(deploy.Flags())
faasCmd.AddCommand(upCmd)
}
// upCmd is a wrapper to the build, push and deploy commands
var upCmd = &cobra.Command{
Use: `up -f [YAML_FILE] [--skip-push] [--skip-deploy] [flags from build, push, deploy]`,
Short: "Builds, pushes and deploys OpenFaaS function containers",
Long: `Build, Push, and Deploy OpenFaaS function containers either via the
supplied YAML config using the "--yaml" flag (which may contain multiple function
definitions), or directly via flags.
The push step may be skipped by setting the --skip-push flag
and the deploy step with --skip-deploy.
Note: All flags from the build, push and deploy flags are valid and can be combined,
see the --help text for those commands for details.`,
Example: ` # Deploy everything
faas-cli up
# Deploy a named function
faas-cli up --filter echo
# Deploy but skip the push step
faas-cli up --skip-push
# Build but skip pushing and use a build-arg
faas-cli up --skip-push \
--build-arg GO111MODULE=on
`,
PreRunE: preRunUp,
RunE: upHandler,
}
func preRunUp(cmd *cobra.Command, args []string) error {
if err := preRunBuild(cmd, args); err != nil {
return err
}
if err := preRunDeploy(cmd, args); err != nil {
return err
}
return nil
}
func upHandler(cmd *cobra.Command, args []string) error {
if watch {
return watchLoop(cmd, args, func(cmd *cobra.Command, args []string) error {
if err := upRunner(cmd, args); err != nil {
return err
}
fmt.Println("[Watch] Change a file to trigger a rebuild...")
return nil
})
}
return upRunner(cmd, args)
}
func upRunner(cmd *cobra.Command, args []string) error {
if usePublish {
if err := runPublish(cmd, args); err != nil {
return err
}
} else {
if err := runBuild(cmd, args); err != nil {
return err
}
if !skipPush && remoteBuilder == "" {
if err := runPush(cmd, args); err != nil {
return err
}
}
}
if !skipDeploy {
if err := runDeploy(cmd, args); err != nil {
return err
}
}
return nil
}
func ignorePatterns() ([]gitignore.Pattern, error) {
gitignorePath := ".gitignore"
file, err := os.Open(gitignorePath)
if err != nil {
return nil, err
}
defer file.Close()
patterns := []gitignore.Pattern{gitignore.ParsePattern(".git", nil)}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" || strings.HasPrefix(line, "#") {
continue
}
patterns = append(patterns, gitignore.ParsePattern(line, nil))
}
if err := scanner.Err(); err != nil {
return nil, err
}
return patterns, nil
}