-
Notifications
You must be signed in to change notification settings - Fork 231
Expand file tree
/
Copy pathfaas.go
More file actions
161 lines (133 loc) · 3.69 KB
/
faas.go
File metadata and controls
161 lines (133 loc) · 3.69 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
157
158
159
160
161
// Copyright (c) Alex Ellis 2017. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
package commands
import (
"context"
"fmt"
"log"
"os"
"path"
"strings"
"syscall"
"github.com/moby/term"
"github.com/openfaas/faas-cli/contexts"
"github.com/openfaas/faas-cli/version"
"github.com/spf13/cobra"
)
const (
defaultGateway = "http://127.0.0.1:8080"
defaultNetwork = ""
defaultYAML = "stack.yml"
defaultSchemaVersion = "1.0"
)
// Flags that are to be added to all commands.
var (
yamlFile string
regex string
filter string
)
// Flags that are to be added to subset of commands.
var (
fprocess string
functionName string
handlerDir string
network string
gateway string
handler string
image string
imagePrefix string
language string
tlsInsecure bool
)
var stat = func(filename string) (os.FileInfo, error) {
return os.Stat(filename)
}
// TODO: remove this workaround once these vars are no longer global
func resetForTest() {
yamlFile = ""
regex = ""
filter = ""
version.Version = ""
shortVersion = false
appendFile = ""
}
func init() {
// Setup terminal std
term.StdStreams()
faasCmd.PersistentFlags().StringVarP(&yamlFile, "yaml", "f", "", "Path to YAML file describing function(s)")
faasCmd.PersistentFlags().StringVarP(®ex, "regex", "", "", "Regex to match with function names in YAML file")
faasCmd.PersistentFlags().StringVarP(&filter, "filter", "", "", "Wildcard to match with function names in YAML file")
// Set Bash completion options
validYAMLFilenames := []string{"yaml", "yml"}
_ = faasCmd.PersistentFlags().SetAnnotation("yaml", cobra.BashCompFilenameExt, validYAMLFilenames)
}
func Execute(customArgs []string) {
checkAndSetDefaultYaml()
ctx, cancel := contexts.WithSignals(context.Background(), os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
defer cancel()
faasCmd.SilenceUsage = true
faasCmd.SilenceErrors = true
faasCmd.SetArgs(customArgs[1:])
args1 := os.Args[1:]
cmd1, _, _ := faasCmd.Find(args1)
plugins, err := getPlugins()
if err != nil {
log.Fatal(err)
}
if cmd1 != nil && len(args1) > 0 {
found := ""
for _, plugin := range plugins {
if path.Base(plugin) == args1[0] {
found = plugin
}
}
if len(found) > 0 {
// if we have found the plugin then sysexec it by replacing current process.
if err := syscall.Exec(found, append([]string{found}, os.Args[2:]...), os.Environ()); err != nil {
fmt.Fprintf(os.Stderr, "Error from plugin: %v", err)
os.Exit(127)
}
return
}
}
if err := faasCmd.ExecuteContext(ctx); err != nil {
e := err.Error()
fmt.Println(strings.ToUpper(e[:1]) + e[1:])
os.Exit(1)
}
}
func checkAndSetDefaultYaml() {
// Check if there is a default yaml file and set it
if _, err := stat(defaultYAML); err == nil {
yamlFile = defaultYAML
}
}
// faasCmd is the faas-cli root command and mimics the legacy client behaviour
// Every other command attached to FaasCmd is a child command to it.
var faasCmd = &cobra.Command{
Use: "faas-cli",
Short: "Manage your OpenFaaS functions from the command line",
Long: `
Manage your OpenFaaS functions from the command line`,
Run: runFaas,
}
// runFaas TODO
func runFaas(cmd *cobra.Command, args []string) {
printLogo()
cmd.Help()
}
func getPlugins() ([]string, error) {
plugins := []string{}
pluginHome := os.ExpandEnv("$HOME/.openfaas/plugins")
if _, err := os.Stat(pluginHome); err != nil && os.IsNotExist(err) {
return plugins, nil
}
res, err := os.ReadDir(pluginHome)
if err != nil {
return nil, err
}
for _, file := range res {
plugins = append(plugins, path.Join(pluginHome, file.Name()))
}
return plugins, nil
}