Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI multi files #515

Merged
merged 14 commits into from
Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
Unreleased changes are available as `avenga/couper:edge` container.

* **Added**
* Couper [reads and merges configuration files](./docs/CLI.md#global-options) from a given directory ([#437](https://github.com/avenga/couper/pull/437))
* provided via `-d` command-line flag or `COUPER_FILE_DIRECTORY` environment variable
* Couper now [reads and merges multiple configuration files](./docs/CLI.md#global-options) ([#437](https://github.com/avenga/couper/pull/437), [#515](https://github.com/avenga/couper/pull/515))
* `beta_health`-block to `backend`-block to enable continuous health-checks for defined backends ([#313](https://github.com/avenga/couper/pull/313))
* `backends.<name>.health` variable to access the current health-check state _(subject to change)_
* Log malformed duration settings ([#487](https://github.com/avenga/couper/pull/487))
Expand Down
2 changes: 2 additions & 0 deletions command/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ func (a AcceptForwardedValue) Set(s string) error {
var limitFn func(entry *logrus.Entry)

func (r *Run) Execute(args Args, config *config.Couper, logEntry *logrus.Entry) error {
logEntry.WithField("files", config.Files.AsList()).Debug("loaded files")

r.settingsMu.Lock()
*r.settings = *config.Settings
r.settingsMu.Unlock()
Expand Down
4 changes: 2 additions & 2 deletions command/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func TestNewRun(t *testing.T) {
return
}

couperFile, err := configload.LoadFiles(filepath.Join(wd, "testdata/settings", tt.file), "")
couperFile, err := configload.LoadFile(filepath.Join(wd, "testdata/settings", tt.file))
if err != nil {
subT.Error(err)
}
Expand Down Expand Up @@ -192,7 +192,7 @@ func TestAcceptForwarded(t *testing.T) {
return
}

couperFile, err := configload.LoadFiles(filepath.Join(wd, "testdata/settings", tt.file), "")
couperFile, err := configload.LoadFile(filepath.Join(wd, "testdata/settings", tt.file))
if err != nil {
subT.Error(err)
}
Expand Down
13 changes: 1 addition & 12 deletions command/verify.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package command

import (
"fmt"

"github.com/avenga/couper/cache"
"github.com/avenga/couper/config"
"github.com/avenga/couper/config/configload"
Expand All @@ -20,16 +18,7 @@ func NewVerify() *Verify {
}

func (v Verify) Execute(args Args, _ *config.Couper, logger *logrus.Entry) error {
if len(args) != 2 {
v.Usage()

err := fmt.Errorf("invalid number of arguments given")
logger.WithError(err).Error()

return err
}

cf, err := configload.LoadFiles(args[0], args[1])
cf, err := configload.LoadFiles(args)
if diags, ok := err.(hcl.Diagnostics); ok {
for _, diag := range diags {
logger.WithError(diag).Error()
Expand Down
108 changes: 108 additions & 0 deletions config/configload/file/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package file

import (
"io/ioutil"
"os"
"path/filepath"
)

type File = struct {
Path string
IsDir bool
Children Files
}

type Files []File

func NewFile(filePath string) File {
return File{
Path: filePath,
IsDir: false,
}
}

func NewDir(filePath string) (File, error) {
children, err := readDir(filePath)
return File{
Path: filePath,
IsDir: true,
Children: children,
}, err
}

func NewFiles(filesList []string) ([]File, error) {
var files []File

for _, f := range filesList {
filePath, err := filepath.Abs(f)
if err != nil {
return nil, err
}

fileInfo, err := os.Stat(filePath)
if err != nil {
return nil, err
}

if fileInfo.IsDir() {
dir, err := NewDir(filePath)
if err != nil {
return nil, err
}

files = append(files, dir)
} else {
files = append(files, NewFile(filePath))
}
}

return files, nil
}

func (f *Files) Refresh() (*Files, error) {
var result Files
for _, file := range *f {
if file.IsDir {
dir, err := NewDir(file.Path)
if err != nil {
return nil, err
}
result = append(result, dir)
} else {
result = append(result, file)
}
}
return &result, nil
}

func (f *Files) AsList() []string {
var list []string
for _, file := range *f {
if file.IsDir {
list = append(list, file.Children.AsList()...)
} else {
list = append(list, file.Path)
}
}
return list
}

func readDir(filePath string) (Files, error) {
// ReadDir ... returns a list ... sorted by filename.
listing, err := ioutil.ReadDir(filePath)
if err != nil {
return nil, err
}

var entries Files
for _, item := range listing {
if item.IsDir() || filepath.Ext(item.Name()) != ".hcl" {
continue
}

filename := filepath.Join(filePath, item.Name())
entries = append(entries, NewFile(filename))
}

return entries, nil
}
3 changes: 1 addition & 2 deletions config/configload/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type helper struct {
}

// newHelper creates a container with some methods to keep things simple here and there.
func newHelper(body hcl.Body, src []byte, filename, dirPath string) (*helper, error) {
func newHelper(body hcl.Body, src []byte, filename string) (*helper, error) {
defaultsBlock := &config.DefaultsBlock{}
if diags := gohcl.DecodeBody(body, nil, defaultsBlock); diags.HasErrors() {
return nil, diags
Expand All @@ -32,7 +32,6 @@ func newHelper(body hcl.Body, src []byte, filename, dirPath string) (*helper, er
Definitions: &config.Definitions{},
Defaults: defaultsBlock.Defaults,
Filename: filename,
Dirpath: dirPath,
Settings: &defSettings,
}

Expand Down
Loading