Skip to content

Commit

Permalink
Merge pull request #247 from yohamta/develop
Browse files Browse the repository at this point in the history
cmd: Add `--config=<admin config file>` option to all subcommands
  • Loading branch information
yottahmd authored Aug 10, 2022
2 parents cb22e6f + 7a8e06f commit 72d5de8
Show file tree
Hide file tree
Showing 15 changed files with 114 additions and 58 deletions.
57 changes: 36 additions & 21 deletions cmd/dagu.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@ import (
)

var (
version = "0.0.0"
stdin io.ReadCloser
sigs chan os.Signal
globalConfig *admin.Config
version = "0.0.0"
stdin io.ReadCloser
sigs chan os.Signal
globalFlags = []cli.Flag{
&cli.StringFlag{
Name: "config",
Usage: "Admin config",
Value: "",
Required: false,
},
}
)

func main() {
Expand All @@ -30,10 +37,31 @@ func main() {
}
}

func loadDAG(dagPath, params string) (cfg *config.Config, err error) {
cl := &config.Loader{BaseConfig: globalConfig.BaseConfig}
cfg, err = cl.Load(dagPath, params)
return
func loadGlobalConfig(c *cli.Context) (cfg *admin.Config, err error) {
l := &admin.Loader{}
cfgFile := c.String("config")
if cfgFile == "" {
cfgFile = settings.MustGet(settings.SETTING__ADMIN_CONFIG)
}
cfg, err = l.LoadAdminConfig(cfgFile)
if err == admin.ErrConfigNotFound {
cfg = admin.DefaultConfig()
err = nil
}
if err != nil {
return nil, fmt.Errorf("loading admin config failed: %w", err)
}
return cfg, err
}

func loadDAG(c *cli.Context, dagPath, params string) (dag *config.Config, err error) {
cfg, err := loadGlobalConfig(c)
if err != nil {
return nil, err
}
cl := &config.Loader{BaseConfig: cfg.BaseConfig}
dag, err = cl.Load(dagPath, params)
return dag, err
}

func listenSignals(abortFunc func(sig os.Signal)) {
Expand Down Expand Up @@ -72,18 +100,5 @@ func makeApp() *cli.App {
newSchedulerCommand(),
newVersionCommand(),
},
Before: func(c *cli.Context) error {
l := &admin.Loader{}
cfg, err := l.LoadAdminConfig(settings.MustGet(settings.SETTING__ADMIN_CONFIG))
if err == admin.ErrConfigNotFound {
cfg = admin.DefaultConfig()
err = nil
}
if err != nil {
return fmt.Errorf("loading admin config failed: %w", err)
}
globalConfig = cfg
return nil
},
}
}
7 changes: 7 additions & 0 deletions cmd/dagu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type appTest struct {
args []string
errored bool
output []string
errMessage []string
exactOutput string
stdin io.ReadCloser
}
Expand Down Expand Up @@ -77,6 +78,12 @@ func runAppTestOutput(app *cli.App, test appTest, t *testing.T) {
return
}

if err != nil && len(test.errMessage) > 0 {
for _, v := range test.errMessage {
require.Contains(t, err.Error(), v)
}
}

var buf bytes.Buffer
_, err = io.Copy(&buf, r)
require.NoError(t, err)
Expand Down
7 changes: 4 additions & 3 deletions cmd/dry.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@ func newDryCommand() *cli.Command {
return &cli.Command{
Name: "dry",
Usage: "dagu dry [--params=\"<params>\"] <config>",
Flags: []cli.Flag{
Flags: append(
globalFlags,
&cli.StringFlag{
Name: "params",
Usage: "parameters",
Value: "",
Required: false,
},
},
),
Action: func(c *cli.Context) error {
cfg, err := loadDAG(c.Args().Get(0), c.String("params"))
cfg, err := loadDAG(c, c.Args().Get(0), c.String("params"))
if err != nil {
return err
}
Expand Down
31 changes: 14 additions & 17 deletions cmd/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,41 @@ import (
"path/filepath"

"github.com/yohamta/dagu"
"github.com/yohamta/dagu/internal/config"
"github.com/yohamta/dagu/internal/database"
"github.com/yohamta/dagu/internal/models"

"github.com/urfave/cli/v2"
)

func newRetryCommand() *cli.Command {
return &cli.Command{
Name: "retry",
Usage: "dagu retry --req=<request-id> <config>",
Flags: []cli.Flag{
Usage: "dagu retry --req=<request-id> <DAG file>",
Flags: append(
globalFlags,
&cli.StringFlag{
Name: "req",
Usage: "request-id",
Value: "",
Required: true,
},
},
),
Action: func(c *cli.Context) error {
f, _ := filepath.Abs(c.Args().Get(0))
db := database.Database{Config: database.DefaultConfig()}
requestId := c.String("req")
return retry(f, requestId)
status, err := db.FindByRequestId(f, requestId)
if err != nil {
return err
}
cfg, err := loadDAG(c, c.Args().Get(0), status.Status.Params)
return retry(cfg, status)
},
}
}

func retry(f, requestId string) error {
db := database.Database{
Config: database.DefaultConfig(),
}
status, err := db.FindByRequestId(f, requestId)
if err != nil {
return err
}
cfg, err := loadDAG(f, status.Status.Params)
if err != nil {
return err
}

func retry(cfg *config.Config, status *models.StatusFile) error {
a := &dagu.Agent{
AgentConfig: &dagu.AgentConfig{
DAG: cfg,
Expand Down
8 changes: 6 additions & 2 deletions cmd/retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ func Test_retryCommand(t *testing.T) {
}

func Test_retryFail(t *testing.T) {
configPath := testConfig("cmd_retry.yaml")
require.Error(t, retry(configPath, "invalid-request-id"))
app := makeApp()
runAppTestOutput(app, appTest{
args: []string{"", "retry", fmt.Sprintf("--req=%s",
"invalid-request-id"), testConfig("cmd_retry.yaml")}, errored: true,
errMessage: []string{"request id not found"},
}, t)
}
13 changes: 9 additions & 4 deletions cmd/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,25 @@ func newSchedulerCommand() *cli.Command {
return &cli.Command{
Name: "scheduler",
Usage: "dagu scheduler",
Flags: []cli.Flag{
Flags: append(
globalFlags,
&cli.StringFlag{
Name: "dags",
Usage: "DAGs directory",
Value: "",
Required: false,
},
},
),
Action: func(c *cli.Context) error {
cfg, err := loadGlobalConfig(c)
if err != nil {
return err
}
dagsDir := c.String("dags")
if dagsDir != "" {
globalConfig.DAGs = dagsDir
cfg.DAGs = dagsDir
}
return startScheduler(globalConfig)
return startScheduler(cfg)
},
}
}
Expand Down
7 changes: 6 additions & 1 deletion cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ func newServerCommand() *cli.Command {
return &cli.Command{
Name: "server",
Usage: "dagu server",
Flags: globalFlags,
Action: func(c *cli.Context) error {
return startServer(globalConfig)
cfg, err := loadGlobalConfig(c)
if err != nil {
return err
}
return startServer(cfg)
},
}
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ import (
func newStartCommand() *cli.Command {
return &cli.Command{
Name: "start",
Usage: "dagu start [--params=\"<params>\"] <config>",
Flags: []cli.Flag{
Usage: "dagu start [--params=\"<params>\"] <DAG file>",
Flags: append(
globalFlags,
&cli.StringFlag{
Name: "params",
Usage: "parameters",
Value: "",
Required: false,
},
},
),
Action: func(c *cli.Context) error {
config_file_path := c.Args().Get(0)
cfg, err := loadDAG(config_file_path, c.String("params"))
cfg, err := loadDAG(c, c.Args().Get(0), c.String("params"))
if err != nil {
return err
}
Expand Down
11 changes: 11 additions & 0 deletions cmd/start_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"fmt"
"os"
"testing"
)

Expand All @@ -22,8 +24,17 @@ func Test_startCommand(t *testing.T) {
args: []string{"", "start", testConfig("cmd_start_success")}, errored: false,
output: []string{"1 finished"},
},
{
args: []string{"", "start",
fmt.Sprintf("--config=%s", testConfig("cmd_start_global_config.yaml")),
testConfig("cmd_start_global_config_check.yaml")}, errored: false,
output: []string{"GLOBAL_ENV_VAR"},
},
}

// For testing --config parameter we need to set the environment variable for now.
os.Setenv("TEST_CONFIG_BASE", testsDir)

for _, v := range tests {
app := makeApp()
runAppTestOutput(app, v, t)
Expand Down
5 changes: 3 additions & 2 deletions cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import (
func newStatusCommand() *cli.Command {
return &cli.Command{
Name: "status",
Usage: "dagu status <config>",
Usage: "dagu status <DAG file>",
Flags: globalFlags,
Action: func(c *cli.Context) error {
cfg, err := loadDAG(c.Args().Get(0), "")
cfg, err := loadDAG(c, c.Args().Get(0), "")
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import (
func newStopCommand() *cli.Command {
return &cli.Command{
Name: "stop",
Usage: "dagu stop <config>",
Usage: "dagu stop <DAG file>",
Flags: globalFlags,
Action: func(c *cli.Context) error {
cfg, err := loadDAG(c.Args().Get(0), "")
cfg, err := loadDAG(c, c.Args().Get(0), "")
if err != nil {
return err
}
Expand Down
5 changes: 4 additions & 1 deletion internal/admin/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ func buildDAGsDir(cfg *Config, def *configDefinition) (err error) {
}

func buildBaseConfig(cfg *Config, def *configDefinition) (err error) {
cfg.BaseConfig = strings.TrimSpace(def.BaseConfig)
cfg.BaseConfig, err = utils.ParseVariable(strings.TrimSpace(def.BaseConfig))
if err != nil {
return err
}
if cfg.BaseConfig == "" {
cfg.BaseConfig = settings.MustGet(settings.SETTING__BASE_CONFIG)
}
Expand Down
1 change: 1 addition & 0 deletions tests/testdata/cmd_start_global_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
baseConfig: ${TEST_CONFIG_BASE}/cmd_start_global_config_base.yaml
2 changes: 2 additions & 0 deletions tests/testdata/cmd_start_global_config_base.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
env:
- GLOBAL_ENV: "GLOBAL_ENV_VAR"
3 changes: 3 additions & 0 deletions tests/testdata/cmd_start_global_config_check.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
steps:
- name: "1"
command: "echo ${GLOBAL_ENV}"

0 comments on commit 72d5de8

Please sign in to comment.