Skip to content

Commit fae27da

Browse files
authored
fix: correct value for config path and robustify (#130)
* fix: correct value for config path and robustify The hardcoded, initial value for the configuration path was set to `.faas/config`. But `configPath()` immediately sets this to the correct value of ~/.config. Both the create and init commands use `configPath()` to search for additional templates, if they exist, and were each doing `filepath.Join(configPath(), "faas", "templates")`. This commit also changes `configPath()` so that it is `~/.config/faas` and does so in a cross platform friendly way. If the `$HOME` directory cannot be determined, the config is assumed to be at `./.config/faas`. * squash: remove config variable entirely
1 parent 341d3d2 commit fae27da

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

cmd/create.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func init() {
2323
createCmd.Flags().StringP("path", "p", cwd(), "Path to the new project directory - $FAAS_PATH")
2424
createCmd.Flags().StringP("repository", "r", "", "Repository for built images, ex 'docker.io/myuser' or just 'myuser'. Optional if --image provided. - $FAAS_REPOSITORY")
2525
createCmd.Flags().StringP("runtime", "l", faas.DefaultRuntime, "Function runtime language/framework. - $FAAS_RUNTIME")
26-
createCmd.Flags().StringP("templates", "", filepath.Join(configPath(), "faas", "templates"), "Extensible templates path. - $FAAS_TEMPLATES")
26+
createCmd.Flags().StringP("templates", "", filepath.Join(configPath(), "templates"), "Extensible templates path. - $FAAS_TEMPLATES")
2727
createCmd.Flags().StringP("trigger", "t", faas.DefaultTrigger, "Function trigger (ex: 'http','events') - $FAAS_TRIGGER")
2828

2929
var err error

cmd/init.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func init() {
1616
initCmd.Flags().BoolP("confirm", "c", false, "Prompt to confirm all configuration options - $FAAS_CONFIRM")
1717
initCmd.Flags().StringP("path", "p", cwd(), "Path to the new project directory - $FAAS_PATH")
1818
initCmd.Flags().StringP("runtime", "l", faas.DefaultRuntime, "Function runtime language/framework. - $FAAS_RUNTIME")
19-
initCmd.Flags().StringP("templates", "", filepath.Join(configPath(), "faas", "templates"), "Extensible templates path. - $FAAS_TEMPLATES")
19+
initCmd.Flags().StringP("templates", "", filepath.Join(configPath(), "templates"), "Extensible templates path. - $FAAS_TEMPLATES")
2020
initCmd.Flags().StringP("trigger", "t", faas.DefaultTrigger, "Function trigger (ex: 'http','events') - $FAAS_TRIGGER")
2121

2222
if err := initCmd.RegisterFlagCompletionFunc("runtime", CompleteRuntimeList); err != nil {

cmd/root.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"fmt"
55
"os"
6+
"path/filepath"
67

78
"github.com/mitchellh/go-homedir"
89
"github.com/ory/viper"
@@ -11,8 +12,6 @@ import (
1112
"github.com/boson-project/faas"
1213
)
1314

14-
var config = "~/.faas/config" // Location of the optional system-wide config file.
15-
1615
// The root of the command tree defines the command name, descriotion, globally
1716
// available flags, etc. It has no action of its own, such that running the
1817
// resultant binary with no arguments prints the help/usage text.
@@ -30,9 +29,6 @@ Create and run Functions as a Service.`,
3029
// are invoked to gather system context. This includes reading the configuration
3130
// file, environment variables, and parsing the command flags.
3231
func init() {
33-
// Populate `config` var with the value of --config flag, if provided.
34-
root.PersistentFlags().StringVar(&config, "config", config, "config file path")
35-
3632
// read in environment variables that match
3733
viper.AutomaticEnv()
3834

@@ -95,11 +91,15 @@ func cwd() (cwd string) {
9591
// function defaults and extensible templates.
9692
func configPath() (path string) {
9793
if path = os.Getenv("XDG_CONFIG_HOME"); path != "" {
94+
path = filepath.Join(path, "faas")
9895
return
9996
}
100-
path, err := homedir.Expand("~/.config")
97+
home, err := homedir.Expand("~")
10198
if err != nil {
10299
fmt.Fprintf(os.Stderr, "could not derive home directory for use as default templates path: %v", err)
100+
path = filepath.Join(".config", "faas")
101+
} else {
102+
path = filepath.Join(home, ".config", "faas")
103103
}
104104
return
105105
}

0 commit comments

Comments
 (0)