Skip to content

Commit c9435a8

Browse files
authored
Fix command flag duplication issue (#522)
* fix command flag duplication issue * fix test
1 parent 33f0cf9 commit c9435a8

File tree

12 files changed

+71
-25
lines changed

12 files changed

+71
-25
lines changed

cmd/common_test.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@ package cmd
22

33
import (
44
"bytes"
5-
"github.com/dagu-dev/dagu/internal/config"
6-
"github.com/dagu-dev/dagu/internal/persistence"
7-
"github.com/dagu-dev/dagu/internal/persistence/client"
85
"io"
96
"log"
107
"os"
118
"path"
129
"testing"
1310
"time"
1411

12+
"github.com/dagu-dev/dagu/internal/config"
13+
"github.com/dagu-dev/dagu/internal/persistence"
14+
"github.com/dagu-dev/dagu/internal/persistence/client"
15+
1516
"github.com/dagu-dev/dagu/internal/engine"
1617
"github.com/dagu-dev/dagu/internal/scheduler"
1718
"github.com/dagu-dev/dagu/internal/utils"
@@ -34,9 +35,10 @@ func setupTest(t *testing.T) (string, engine.Engine, persistence.DataStoreFactor
3435
return tmpDir, e, ds
3536
}
3637

37-
func changeHomeDir(homeDir string) {
38-
_ = os.Setenv("HOME", homeDir)
39-
_ = config.LoadConfig(homeDir)
38+
func changeHomeDir(dir string) {
39+
homeDir = dir
40+
_ = os.Setenv("HOME", dir)
41+
_ = config.LoadConfig(dir)
4042
}
4143

4244
type cmdTest struct {

cmd/dry.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ func dryCmd() *cobra.Command {
1313
Short: "Dry-runs specified DAG",
1414
Long: `dagu dry [--params="param1 param2"] <DAG file>`,
1515
Args: cobra.ExactArgs(1),
16+
PreRun: func(cmd *cobra.Command, args []string) {
17+
cobra.CheckErr(config.LoadConfig(homeDir))
18+
},
1619
Run: func(cmd *cobra.Command, args []string) {
1720
df := client.NewDataStoreFactory(config.Get())
1821
e := engine.NewFactory(df, config.Get()).Create()

cmd/restart.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package cmd
22

33
import (
4+
"log"
5+
"time"
6+
47
"github.com/dagu-dev/dagu/internal/config"
58
"github.com/dagu-dev/dagu/internal/dag"
69
"github.com/dagu-dev/dagu/internal/engine"
710
"github.com/dagu-dev/dagu/internal/persistence/client"
811
"github.com/dagu-dev/dagu/internal/scheduler"
912
"github.com/spf13/cobra"
10-
"log"
11-
"time"
1213
)
1314

1415
func restartCmd() *cobra.Command {
@@ -17,6 +18,9 @@ func restartCmd() *cobra.Command {
1718
Short: "Restart the DAG",
1819
Long: `dagu restart <DAG file>`,
1920
Args: cobra.ExactArgs(1),
21+
PreRun: func(cmd *cobra.Command, args []string) {
22+
cobra.CheckErr(config.LoadConfig(homeDir))
23+
},
2024
Run: func(cmd *cobra.Command, args []string) {
2125
dagFile := args[0]
2226
loadedDAG, err := loadDAG(dagFile, "")

cmd/retry.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package cmd
22

33
import (
4+
"path/filepath"
5+
46
"github.com/dagu-dev/dagu/internal/agent"
57
"github.com/dagu-dev/dagu/internal/config"
68
"github.com/dagu-dev/dagu/internal/engine"
79
"github.com/dagu-dev/dagu/internal/persistence/client"
810
"github.com/spf13/cobra"
9-
"path/filepath"
1011
)
1112

1213
func retryCmd() *cobra.Command {
@@ -15,6 +16,9 @@ func retryCmd() *cobra.Command {
1516
Short: "Retry the DAG execution",
1617
Long: `dagu retry --req=<request-id> <DAG file>`,
1718
Args: cobra.ExactArgs(1),
19+
PreRun: func(cmd *cobra.Command, args []string) {
20+
cobra.CheckErr(config.LoadConfig(homeDir))
21+
},
1822
Run: func(cmd *cobra.Command, args []string) {
1923
f, _ := filepath.Abs(args[0])
2024
reqID, err := cmd.Flags().GetString("req")

cmd/root.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,27 @@ func Execute() {
3636
}
3737

3838
func init() {
39-
cobra.OnInitialize(initConfig)
40-
4139
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.dagu/admin.yaml)")
4240

41+
cobra.OnInitialize(initConfig)
42+
4343
registerCommands(rootCmd)
4444
}
4545

46-
func initConfig() {
46+
var (
47+
homeDir string
48+
)
49+
50+
func init() {
4751
home, err := os.UserHomeDir()
48-
cobra.CheckErr(err)
49-
setConfigFile(home)
50-
cobra.CheckErr(config.LoadConfig(home))
52+
if err != nil {
53+
cobra.CheckErr(err)
54+
}
55+
homeDir = home
56+
}
57+
58+
func initConfig() {
59+
setConfigFile(homeDir)
5160
}
5261

5362
func setConfigFile(home string) {

cmd/scheduler.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ func createSchedulerCommand() *cobra.Command {
1313
Use: "scheduler",
1414
Short: "Start the scheduler",
1515
Long: `dagu scheduler [--dags=<DAGs dir>]`,
16+
PreRun: func(cmd *cobra.Command, args []string) {
17+
cobra.CheckErr(config.LoadConfig(homeDir))
18+
},
1619
Run: func(cmd *cobra.Command, args []string) {
1720
config.Get().DAGs = getFlagString(cmd, "dags", config.Get().DAGs)
1821

cmd/server.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"github.com/dagu-dev/dagu/app"
5+
"github.com/dagu-dev/dagu/internal/config"
56
"github.com/spf13/cobra"
67
"github.com/spf13/viper"
78
)
@@ -11,6 +12,12 @@ func serverCmd() *cobra.Command {
1112
Use: "server",
1213
Short: "Start the server",
1314
Long: `dagu server [--dags=<DAGs dir>] [--host=<host>] [--port=<port>]`,
15+
PreRun: func(cmd *cobra.Command, args []string) {
16+
_ = viper.BindPFlag("port", cmd.Flags().Lookup("port"))
17+
_ = viper.BindPFlag("host", cmd.Flags().Lookup("host"))
18+
_ = viper.BindPFlag("dags", cmd.Flags().Lookup("dags"))
19+
cobra.CheckErr(config.LoadConfig(homeDir))
20+
},
1421
Run: func(cmd *cobra.Command, args []string) {
1522
service := app.NewFrontendService()
1623
err := service.Start(cmd.Context())
@@ -25,8 +32,4 @@ func bindServerCommandFlags(cmd *cobra.Command) {
2532
cmd.Flags().StringP("dags", "d", "", "location of DAG files (default is $HOME/.dagu/dags)")
2633
cmd.Flags().StringP("host", "s", "", "server host (default is localhost)")
2734
cmd.Flags().StringP("port", "p", "", "server port (default is 8080)")
28-
29-
_ = viper.BindPFlag("port", cmd.Flags().Lookup("port"))
30-
_ = viper.BindPFlag("host", cmd.Flags().Lookup("host"))
31-
_ = viper.BindPFlag("dags", cmd.Flags().Lookup("dags"))
3235
}

cmd/start.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ func startCmd() *cobra.Command {
1313
Short: "Runs the DAG",
1414
Long: `dagu start [--params="param1 param2"] <DAG file>`,
1515
Args: cobra.ExactArgs(1),
16+
PreRun: func(cmd *cobra.Command, args []string) {
17+
cobra.CheckErr(config.LoadConfig(homeDir))
18+
},
1619
Run: func(cmd *cobra.Command, args []string) {
1720
ds := client.NewDataStoreFactory(config.Get())
1821
e := engine.NewFactory(ds, config.Get()).Create()

cmd/start_all.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ func startAllCmd() *cobra.Command {
1515
Use: "start-all",
1616
Short: "Launches both the Dagu web UI server and the scheduler process.",
1717
Long: `dagu start-all [--dags=<DAGs dir>] [--host=<host>] [--port=<port>]`,
18+
PreRun: func(cmd *cobra.Command, args []string) {
19+
_ = viper.BindPFlag("port", cmd.Flags().Lookup("port"))
20+
_ = viper.BindPFlag("host", cmd.Flags().Lookup("host"))
21+
_ = viper.BindPFlag("dags", cmd.Flags().Lookup("dags"))
22+
cobra.CheckErr(config.LoadConfig(homeDir))
23+
},
1824
Run: func(cmd *cobra.Command, args []string) {
1925
ctx := cmd.Context()
2026

@@ -39,8 +45,4 @@ func bindStartAllCommandFlags(cmd *cobra.Command) {
3945
cmd.Flags().StringP("dags", "d", "", "location of DAG files (default is $HOME/.dagu/dags)")
4046
cmd.Flags().StringP("host", "s", "", "server host (default is localhost)")
4147
cmd.Flags().StringP("port", "p", "", "server port (default is 8080)")
42-
43-
_ = viper.BindPFlag("port", cmd.Flags().Lookup("port"))
44-
_ = viper.BindPFlag("host", cmd.Flags().Lookup("host"))
45-
_ = viper.BindPFlag("dags", cmd.Flags().Lookup("dags"))
4648
}

cmd/status.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package cmd
22

33
import (
4+
"log"
5+
46
"github.com/dagu-dev/dagu/internal/config"
57
"github.com/dagu-dev/dagu/internal/engine"
68
"github.com/dagu-dev/dagu/internal/persistence/client"
79
"github.com/dagu-dev/dagu/internal/persistence/model"
810
"github.com/spf13/cobra"
9-
"log"
1011
)
1112

1213
func createStatusCommand() *cobra.Command {
@@ -15,6 +16,9 @@ func createStatusCommand() *cobra.Command {
1516
Short: "Display current status of the DAG",
1617
Long: `dagu status <DAG file>`,
1718
Args: cobra.ExactArgs(1),
19+
PreRun: func(cmd *cobra.Command, args []string) {
20+
cobra.CheckErr(config.LoadConfig(homeDir))
21+
},
1822
Run: func(cmd *cobra.Command, args []string) {
1923
loadedDAG, err := loadDAG(args[0], "")
2024
checkError(err)

0 commit comments

Comments
 (0)