Skip to content
Permalink
Browse files

Added ability to split configuration files into multiple ones

  • Loading branch information...
eko committed Jul 11, 2019
1 parent fb23946 commit efbc4796edcf1ed33df90574f81dca54701edb95
Showing with 80 additions and 7 deletions.
  1. +8 −1 README.md
  2. +9 −1 cmd/edit.go
  3. +1 −1 cmd/init.go
  4. +62 −4 internal/config/reader.go
@@ -99,7 +99,14 @@ $ monday edit

## Configuration example

Here is a configuration example that allows you to see all the things you could do with Monday:
Here is a configuration example on a single file that allows you to see all the things you could do with Monday.

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`
* `~/monday.forwards.yaml`
* `~/monday.projects.yaml`

This will help you in having smaller and more readable configuration files.

```yaml
# Settings
@@ -14,13 +14,21 @@ var editCmd = &cobra.Command{
Long: `For more information about the configuration, see the "example.yaml" file available
in the source code repository.`,
Run: func(cmd *cobra.Command, args []string) {
// Check for multiple configuration file
files := config.FindMultipleConfigFiles()

// Check for single configuration file
err := config.CheckConfigFileExists()
if err != nil {
fmt.Printf("%v\n", err)
return
}

command := exec.Command("open", config.Filepath)
if len(files) == 0 {
files = []string{config.Filepath}
}

command := exec.Command("open", files...)

if err := command.Start(); err != nil {
fmt.Printf("❌ Cannot run the 'open' command to edit config file: %v\n", err)
@@ -5,8 +5,8 @@ import (
"os"
"os/exec"

"github.com/spf13/cobra"
"github.com/eko/monday/internal/config"
"github.com/spf13/cobra"
)

var initCmd = &cobra.Command{
@@ -6,31 +6,50 @@ import (
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"

"gopkg.in/yaml.v2"
)

const (
// Filename is the name of the YAML configuration file
// Filename is the name single YAML configuration file name
Filename = "monday.yaml"
// MultipleFilenamePattern is the name pattern for multiple YAML configuration files
MultipleFilenamePattern = "monday*.yaml"
)

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

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

// Load method loads the configuration from the YAML configuration file
func Load() (*Config, error) {
if err := CheckConfigFileExists(); err != nil {
return nil, err
files := FindMultipleConfigFiles()
if len(files) == 0 {
return nil, err
}

err := createConfigFromMultiple(files)
if err != nil {
return nil, err
}
}

// Check for multiple config files
var conf Config

file, err := ioutil.ReadFile(Filepath)
if err != nil {
log.Printf("Error while reading config file: #%v", err)
}

var conf Config
err = yaml.Unmarshal(file, &conf)
if err != nil {
panic(fmt.Sprintf("An error has occured while reading configuration file:\n%v", err))
@@ -44,6 +63,45 @@ func Load() (*Config, error) {
return &conf, nil
}

// FindMultipleConfigFiles finds if multiple configuration files has been created
func FindMultipleConfigFiles() []string {
matches, _ := filepath.Glob(MultipleFilepath)

for i, match := range matches {
if strings.Contains(match, Filepath) {
matches = append(matches[:i], matches[i+1:]...)
}
}

return matches
}

// Merge multiple configuration files into a single one
func createConfigFromMultiple(matches []string) error {
configFile, err := os.Create(Filepath)
if err != nil {
return err
}
defer configFile.Close()

added := 0
for _, match := range matches {
file, err := ioutil.ReadFile(match)
if err != nil {
continue
}

configFile.Write(file)
added++
}

if added == 0 {
return errors.New("Unable to process any configuration file")
}

return nil
}

// CheckConfigFileExists ensures that config file is present before going further
func CheckConfigFileExists() error {
if _, err := os.Stat(Filepath); os.IsNotExist(err) {

0 comments on commit efbc479

Please sign in to comment.
You can’t perform that action at this time.