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

Fix command flag duplication issue #522

Merged
merged 2 commits into from
Jan 7, 2024
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
14 changes: 8 additions & 6 deletions cmd/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package cmd

import (
"bytes"
"github.com/dagu-dev/dagu/internal/config"
"github.com/dagu-dev/dagu/internal/persistence"
"github.com/dagu-dev/dagu/internal/persistence/client"
"io"
"log"
"os"
"path"
"testing"
"time"

"github.com/dagu-dev/dagu/internal/config"
"github.com/dagu-dev/dagu/internal/persistence"
"github.com/dagu-dev/dagu/internal/persistence/client"

"github.com/dagu-dev/dagu/internal/engine"
"github.com/dagu-dev/dagu/internal/scheduler"
"github.com/dagu-dev/dagu/internal/utils"
Expand All @@ -34,9 +35,10 @@ func setupTest(t *testing.T) (string, engine.Engine, persistence.DataStoreFactor
return tmpDir, e, ds
}

func changeHomeDir(homeDir string) {
_ = os.Setenv("HOME", homeDir)
_ = config.LoadConfig(homeDir)
func changeHomeDir(dir string) {
homeDir = dir
_ = os.Setenv("HOME", dir)
_ = config.LoadConfig(dir)
}

type cmdTest struct {
Expand Down
3 changes: 3 additions & 0 deletions cmd/dry.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ func dryCmd() *cobra.Command {
Short: "Dry-runs specified DAG",
Long: `dagu dry [--params="param1 param2"] <DAG file>`,
Args: cobra.ExactArgs(1),
PreRun: func(cmd *cobra.Command, args []string) {
cobra.CheckErr(config.LoadConfig(homeDir))
},
Run: func(cmd *cobra.Command, args []string) {
df := client.NewDataStoreFactory(config.Get())
e := engine.NewFactory(df, config.Get()).Create()
Expand Down
8 changes: 6 additions & 2 deletions cmd/restart.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package cmd

import (
"log"
"time"

"github.com/dagu-dev/dagu/internal/config"
"github.com/dagu-dev/dagu/internal/dag"
"github.com/dagu-dev/dagu/internal/engine"
"github.com/dagu-dev/dagu/internal/persistence/client"
"github.com/dagu-dev/dagu/internal/scheduler"
"github.com/spf13/cobra"
"log"
"time"
)

func restartCmd() *cobra.Command {
Expand All @@ -17,6 +18,9 @@ func restartCmd() *cobra.Command {
Short: "Restart the DAG",
Long: `dagu restart <DAG file>`,
Args: cobra.ExactArgs(1),
PreRun: func(cmd *cobra.Command, args []string) {
cobra.CheckErr(config.LoadConfig(homeDir))
},
Run: func(cmd *cobra.Command, args []string) {
dagFile := args[0]
loadedDAG, err := loadDAG(dagFile, "")
Expand Down
6 changes: 5 additions & 1 deletion cmd/retry.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package cmd

import (
"path/filepath"

"github.com/dagu-dev/dagu/internal/agent"
"github.com/dagu-dev/dagu/internal/config"
"github.com/dagu-dev/dagu/internal/engine"
"github.com/dagu-dev/dagu/internal/persistence/client"
"github.com/spf13/cobra"
"path/filepath"
)

func retryCmd() *cobra.Command {
Expand All @@ -15,6 +16,9 @@ func retryCmd() *cobra.Command {
Short: "Retry the DAG execution",
Long: `dagu retry --req=<request-id> <DAG file>`,
Args: cobra.ExactArgs(1),
PreRun: func(cmd *cobra.Command, args []string) {
cobra.CheckErr(config.LoadConfig(homeDir))
},
Run: func(cmd *cobra.Command, args []string) {
f, _ := filepath.Abs(args[0])
reqID, err := cmd.Flags().GetString("req")
Expand Down
21 changes: 15 additions & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,27 @@ func Execute() {
}

func init() {
cobra.OnInitialize(initConfig)

rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.dagu/admin.yaml)")

cobra.OnInitialize(initConfig)

registerCommands(rootCmd)
}

func initConfig() {
var (
homeDir string
)

func init() {
home, err := os.UserHomeDir()
cobra.CheckErr(err)
setConfigFile(home)
cobra.CheckErr(config.LoadConfig(home))
if err != nil {
cobra.CheckErr(err)
}
homeDir = home
}

func initConfig() {
setConfigFile(homeDir)
}

func setConfigFile(home string) {
Expand Down
3 changes: 3 additions & 0 deletions cmd/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ func createSchedulerCommand() *cobra.Command {
Use: "scheduler",
Short: "Start the scheduler",
Long: `dagu scheduler [--dags=<DAGs dir>]`,
PreRun: func(cmd *cobra.Command, args []string) {
cobra.CheckErr(config.LoadConfig(homeDir))
},
Run: func(cmd *cobra.Command, args []string) {
config.Get().DAGs = getFlagString(cmd, "dags", config.Get().DAGs)

Expand Down
11 changes: 7 additions & 4 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"github.com/dagu-dev/dagu/app"
"github.com/dagu-dev/dagu/internal/config"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand All @@ -11,6 +12,12 @@ func serverCmd() *cobra.Command {
Use: "server",
Short: "Start the server",
Long: `dagu server [--dags=<DAGs dir>] [--host=<host>] [--port=<port>]`,
PreRun: func(cmd *cobra.Command, args []string) {
_ = viper.BindPFlag("port", cmd.Flags().Lookup("port"))
_ = viper.BindPFlag("host", cmd.Flags().Lookup("host"))
_ = viper.BindPFlag("dags", cmd.Flags().Lookup("dags"))
cobra.CheckErr(config.LoadConfig(homeDir))
},
Run: func(cmd *cobra.Command, args []string) {
service := app.NewFrontendService()
err := service.Start(cmd.Context())
Expand All @@ -25,8 +32,4 @@ func bindServerCommandFlags(cmd *cobra.Command) {
cmd.Flags().StringP("dags", "d", "", "location of DAG files (default is $HOME/.dagu/dags)")
cmd.Flags().StringP("host", "s", "", "server host (default is localhost)")
cmd.Flags().StringP("port", "p", "", "server port (default is 8080)")

_ = viper.BindPFlag("port", cmd.Flags().Lookup("port"))
_ = viper.BindPFlag("host", cmd.Flags().Lookup("host"))
_ = viper.BindPFlag("dags", cmd.Flags().Lookup("dags"))
}
3 changes: 3 additions & 0 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ func startCmd() *cobra.Command {
Short: "Runs the DAG",
Long: `dagu start [--params="param1 param2"] <DAG file>`,
Args: cobra.ExactArgs(1),
PreRun: func(cmd *cobra.Command, args []string) {
cobra.CheckErr(config.LoadConfig(homeDir))
},
Run: func(cmd *cobra.Command, args []string) {
ds := client.NewDataStoreFactory(config.Get())
e := engine.NewFactory(ds, config.Get()).Create()
Expand Down
10 changes: 6 additions & 4 deletions cmd/start_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ func startAllCmd() *cobra.Command {
Use: "start-all",
Short: "Launches both the Dagu web UI server and the scheduler process.",
Long: `dagu start-all [--dags=<DAGs dir>] [--host=<host>] [--port=<port>]`,
PreRun: func(cmd *cobra.Command, args []string) {
_ = viper.BindPFlag("port", cmd.Flags().Lookup("port"))
_ = viper.BindPFlag("host", cmd.Flags().Lookup("host"))
_ = viper.BindPFlag("dags", cmd.Flags().Lookup("dags"))
cobra.CheckErr(config.LoadConfig(homeDir))
},
Run: func(cmd *cobra.Command, args []string) {
ctx := cmd.Context()

Expand All @@ -39,8 +45,4 @@ func bindStartAllCommandFlags(cmd *cobra.Command) {
cmd.Flags().StringP("dags", "d", "", "location of DAG files (default is $HOME/.dagu/dags)")
cmd.Flags().StringP("host", "s", "", "server host (default is localhost)")
cmd.Flags().StringP("port", "p", "", "server port (default is 8080)")

_ = viper.BindPFlag("port", cmd.Flags().Lookup("port"))
_ = viper.BindPFlag("host", cmd.Flags().Lookup("host"))
_ = viper.BindPFlag("dags", cmd.Flags().Lookup("dags"))
}
6 changes: 5 additions & 1 deletion cmd/status.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package cmd

import (
"log"

"github.com/dagu-dev/dagu/internal/config"
"github.com/dagu-dev/dagu/internal/engine"
"github.com/dagu-dev/dagu/internal/persistence/client"
"github.com/dagu-dev/dagu/internal/persistence/model"
"github.com/spf13/cobra"
"log"
)

func createStatusCommand() *cobra.Command {
Expand All @@ -15,6 +16,9 @@ func createStatusCommand() *cobra.Command {
Short: "Display current status of the DAG",
Long: `dagu status <DAG file>`,
Args: cobra.ExactArgs(1),
PreRun: func(cmd *cobra.Command, args []string) {
cobra.CheckErr(config.LoadConfig(homeDir))
},
Run: func(cmd *cobra.Command, args []string) {
loadedDAG, err := loadDAG(args[0], "")
checkError(err)
Expand Down
6 changes: 5 additions & 1 deletion cmd/stop.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package cmd

import (
"log"

"github.com/dagu-dev/dagu/internal/config"
"github.com/dagu-dev/dagu/internal/engine"
"github.com/dagu-dev/dagu/internal/persistence/client"
"github.com/spf13/cobra"
"log"
)

func stopCmd() *cobra.Command {
Expand All @@ -14,6 +15,9 @@ func stopCmd() *cobra.Command {
Short: "Stop the running DAG",
Long: `dagu stop <DAG file>`,
Args: cobra.ExactArgs(1),
PreRun: func(cmd *cobra.Command, args []string) {
cobra.CheckErr(config.LoadConfig(homeDir))
},
Run: func(cmd *cobra.Command, args []string) {
loadedDAG, err := loadDAG(args[0], "")
checkError(err)
Expand Down
5 changes: 5 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package cmd

import (
"fmt"

"github.com/dagu-dev/dagu/internal/config"
"github.com/dagu-dev/dagu/internal/constants"
"github.com/spf13/cobra"
)
Expand All @@ -11,6 +13,9 @@ func versionCmd() *cobra.Command {
Use: "version",
Short: "Display the binary version",
Long: `dagu version`,
PreRun: func(cmd *cobra.Command, args []string) {
cobra.CheckErr(config.LoadConfig(homeDir))
},
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(constants.Version)
},
Expand Down
Loading