Skip to content

Commit

Permalink
Added a way to specify a custom configuration path (fixes #6)
Browse files Browse the repository at this point in the history
  • Loading branch information
eko committed Jul 26, 2019
1 parent 19d5fe9 commit 4189386
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 151 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ $ monday edit

Configuration of Monday lives in one or multiple YAML files, depending on how you want to organize your files.

By default, `monday init` will initiates a `~/monday.yaml` file.
By default, `monday init` will initiates a `~/monday.yaml` file. You can customize the configuration directory by setting the `MONDAY_CONFIG_PATH` environment variable.

Please note that you can also split this configuration in multiple files by respecting the following pattern: `~/monday.<something>.yaml`, for instance:
* `~/monday.localapps.yaml`
Expand Down
28 changes: 24 additions & 4 deletions internal/config/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,21 @@ const (
)

var (
homeDirectory = os.Getenv("HOME")
Filepath = fmt.Sprintf("%s/%s", homeDirectory, Filename)
defaultConfigPath = os.Getenv("HOME")

// Filepath is the path of the YAML configuration files when you
// just have a single config file
Filepath string

// MultipleFilepath is the path of the YAML configuration files when
// you define multiple config files
MultipleFilepath = fmt.Sprintf("%s/%s", homeDirectory, MultipleFilenamePattern)
MultipleFilepath string
)

func init() {
setConfigFilePaths()
}

// Load method loads the configuration from the YAML configuration file
func Load() (*Config, error) {
files := FindMultipleConfigFiles()
Expand Down Expand Up @@ -112,7 +119,7 @@ func createConfigFromMultiple(matches []string) error {
// CheckConfigFileExists ensures that config file is present before going further
func CheckConfigFileExists() error {
if _, err := os.Stat(Filepath); os.IsNotExist(err) {
return errors.New("Configuration file not found in your home directory. If you run for the first time, please use 'init' command")
return errors.New("Configuration file not found. If you run for the first time, please use 'init' command")
}

return nil
Expand All @@ -139,3 +146,16 @@ func (c *Config) GetProjectByName(name string) (*Project, error) {

return nil, fmt.Errorf("Unable to find project name '%s' in the configuration", name)
}

func getConfigPath() string {
if value := os.Getenv("MONDAY_CONFIG_PATH"); value != "" {
return value
}

return defaultConfigPath
}

func setConfigFilePaths() {
Filepath = fmt.Sprintf("%s/%s", getConfigPath(), Filename)
MultipleFilepath = fmt.Sprintf("%s/%s", getConfigPath(), MultipleFilenamePattern)
}
46 changes: 34 additions & 12 deletions internal/config/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
func TestLoadSingleFile(t *testing.T) {
// Given
dir, _ := os.Getwd()
Filepath = dir + "/../tests/config/config.yaml"
MultipleFilepath = dir + "/../tests/config/config.unknown.*.yaml"
Filepath = dir + "/../tests/config/monday.yaml"
MultipleFilepath = dir + "/../tests/config/monday.unknown.*.yaml"

// When
conf, err := Load()
Expand All @@ -20,19 +20,18 @@ func TestLoadSingleFile(t *testing.T) {
assert.IsType(t, new(Config), conf)
assert.Nil(t, err)

assert.Len(t, conf.Projects, 4)
assert.Len(t, conf.Projects, 2)
assert.Equal(t, conf.Watcher.Exclude, []string{
".git",
"node_modules",
})
assert.Equal(t, conf.KubeConfig, "/home/test/.customkube/config")

}

func TestLoadMultipleFiles(t *testing.T) {
// Given
dir, _ := os.Getwd()
Filepath = dir + "/../tests/config/unknown.yaml"
MultipleFilepath = dir + "/../tests/config/config.multiple.*.yaml"
MultipleFilepath = dir + "/../tests/config/monday.multiple.*.yaml"

// Remove single config created file after test
defer os.Remove(Filepath)
Expand All @@ -52,11 +51,34 @@ func TestLoadMultipleFiles(t *testing.T) {
})
}

func TestLoadWhenCustomDirectory(t *testing.T) {
// Given
dir, _ := os.Getwd()
os.Setenv("MONDAY_CONFIG_PATH", dir+"/../tests/config")

setConfigFilePaths() // Normally run during package init()

Filepath = dir + "/../tests/config/unknown.yaml" // don't read the single file

// When
conf, err := Load()

// Then
assert.IsType(t, new(Config), conf)
assert.Nil(t, err)

assert.Len(t, conf.Projects, 2)
assert.Equal(t, conf.Watcher.Exclude, []string{
".git",
"node_modules",
})
}

func TestGetProjectNames(t *testing.T) {
// Given
dir, _ := os.Getwd()
Filepath = dir + "/../tests/config/config.yaml"
MultipleFilepath = dir + "/../tests/config/config.unknown.*.yaml"
Filepath = dir + "/../tests/config/unknown.yaml"
MultipleFilepath = dir + "/../tests/config/monday.*.yaml"

conf, err := Load()

Expand All @@ -76,8 +98,8 @@ func TestGetProjectNames(t *testing.T) {
func TestGetProjectByName(t *testing.T) {
// Given
dir, _ := os.Getwd()
Filepath = dir + "/../tests/config/config.yaml"
MultipleFilepath = dir + "/../tests/config/config.unknown.*.yaml"
Filepath = dir + "/../tests/config/unknown.yaml"
MultipleFilepath = dir + "/../tests/config/monday.*.yaml"

conf, err := Load()

Expand Down Expand Up @@ -126,8 +148,8 @@ func TestGetProjectByName(t *testing.T) {
func TestGetProjectByNameWhenProjectNotFound(t *testing.T) {
// Given
dir, _ := os.Getwd()
Filepath = dir + "/../tests/config/config.yaml"
MultipleFilepath = dir + "/../tests/config/config.unknown.*.yaml"
Filepath = dir + "/../tests/config/monday.yaml"
MultipleFilepath = dir + "/../tests/config/monday.unknown.*.yaml"

conf, err := Load()

Expand Down
134 changes: 0 additions & 134 deletions internal/tests/config/config.yaml

This file was deleted.

54 changes: 54 additions & 0 deletions internal/tests/config/monday.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Settings

gopath: /dev/golang

watcher: # Optional
exclude:
- .git
- node_modules

# Kubernetes forwards

<: &kubernetes-context context-test

<: &grpc-api-forward
name: grpc-api
type: kubernetes
values:
context: *kubernetes-context
namespace: backend
labels:
app: grpc-api
hostname: grpc-api.svc.local
ports:
- 8080:8080

# Local applications

<: &graphql-local
name: graphql
path: github.com/eko/graphql
watch: true
hostname: graphql.svc.local
executable: go
args:
- run
- cmd/main.go
env:
HTTP_PORT: 8005
setup:
- go get github.com/eko/graphql
- echo You can use ~/path syntax and environment variables like $GOPATH in your commands

# Projects

projects:
- name: graphql-only
local:
- *graphql-local

- name: graphql
local:
- *graphql-local
forward:
- *grpc-api-forward

0 comments on commit 4189386

Please sign in to comment.