Skip to content

Commit

Permalink
Merge pull request #169 from yohamta/fix/admin-config-issue
Browse files Browse the repository at this point in the history
fix: admin config issue
  • Loading branch information
yottahmd authored Jun 13, 2022
2 parents ae273f8 + 56476ff commit e884bdd
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 72 deletions.
64 changes: 0 additions & 64 deletions internal/admin/confg_test.go

This file was deleted.

14 changes: 8 additions & 6 deletions internal/admin/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,14 @@ func buildFromDefinition(def *configDefinition) (c *Config, err error) {
if err != nil {
return nil, err
}
if !filepath.IsAbs(jd) {
return nil, fmt.Errorf("DAGs directory should be absolute path. was %s", jd)
}
c.DAGs, err = filepath.Abs(jd)
if err != nil {
return nil, err
if len(jd) > 0 {
if !filepath.IsAbs(jd) {
return nil, fmt.Errorf("DAGs directory should be absolute path. was %s", jd)
}
c.DAGs, err = filepath.Abs(jd)
if err != nil {
return nil, err
}
}
c.Command, err = utils.ParseVariable(def.Command)
if err != nil {
Expand Down
93 changes: 93 additions & 0 deletions internal/admin/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package admin

import (
"os"
"testing"

"github.com/stretchr/testify/require"
)

var testLoadConfigYaml = `
dags: "` + "`echo /dags_dir`" + `"
host: localhost
port: 8081
command: /bin/current/dagu
workdir: /dags_dir
basicAuthUsername: user
basicAuthPassword: password
logEncodingCharset: utf-8
`

func TestLoadConfig(t *testing.T) {
wd, _ := os.Getwd()
for _, test := range []struct {
Yaml string
Want *Config
}{
{
Yaml: testLoadConfigYaml,
Want: &Config{
DAGs: "/dags_dir",
Host: "localhost",
Port: "8081",
Command: "/bin/current/dagu",
WorkDir: "/dags_dir",
BasicAuthUsername: "user",
BasicAuthPassword: "password",
LogEncodingCharset: "utf-8",
Env: []string{},
},
},
{
Yaml: ``,
Want: &Config{
DAGs: wd,
Host: "127.0.0.1",
Port: "8080",
Command: "dagu",
WorkDir: wd,
BasicAuthUsername: "",
BasicAuthPassword: "",
LogEncodingCharset: "",
Env: []string{},
},
},
} {
l := &Loader{}
d, err := l.unmarshalData([]byte(test.Yaml))
require.NoError(t, err)

def, err := l.decode(d)
require.NoError(t, err)

c, err := buildFromDefinition(def)
require.NoError(t, err)

c.setup()
c.Env = []string{}

require.Equal(t, test.Want, c)
}
}

func TestLoadInvalidConfigError(t *testing.T) {
for _, c := range []string{
`dags: ./relative`,
`dags: "` + "`ech /dags_dir`" + `"`,
`command: "` + "`ech cmd`" + `"`,
`workDir: "` + "`ech /dags`" + `"`,
`basicAuthUsername: "` + "`ech foo`" + `"`,
`basicAuthPassword: "` + "`ech foo`" + `"`,
`logEncodingCharset: "` + "`ech foo`" + `"`,
} {
l := &Loader{}
d, err := l.unmarshalData([]byte(c))
require.NoError(t, err)

def, err := l.decode(d)
require.NoError(t, err)

_, err = buildFromDefinition(def)
require.Error(t, err)
}
}
12 changes: 10 additions & 2 deletions internal/admin/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ func (cl *Loader) LoadAdminConfig(file string) (*Config, error) {
}
}

return buildFromDefinition(def)
cfg, err := buildFromDefinition(def)
if err != nil {
return nil, err
}
cfg.setup()
return cfg, nil
}

func (cl *Loader) load(file string) (config map[string]interface{}, err error) {
Expand All @@ -61,7 +66,10 @@ func (cl *Loader) readFile(file string) (config map[string]interface{}, err erro

func (cl *Loader) unmarshalData(data []byte) (map[string]interface{}, error) {
var cm map[string]interface{}
err := yaml.NewDecoder(bytes.NewReader(data)).Decode(&cm)
var err error
if len(data) > 0 {
err = yaml.NewDecoder(bytes.NewReader(data)).Decode(&cm)
}
return cm, err
}

Expand Down

0 comments on commit e884bdd

Please sign in to comment.