cmd

package
v1.3.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 7, 2025 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AppTasks map[string]*procedure.Task
View Source
var CheckCmd = &cobra.Command{
	Use:   "check",
	Short: "Confirm the correctness of the YAML content",
	Long:  "Confirm the correctness of the YAML content: dgrkt check",
	Run: func(cmd *cobra.Command, args []string) {

		if dir, err := os.Getwd(); err == nil {
			message := fmt.Sprintf("dir: %s", dir)
			utils.SharedAppLogger.Info(message)
		}

		if err := CheckConfig(); err != nil {
			utils.SharedAppLogger.Fatal(err)
		}

		if len(config.AppConfig.Tasks) > 0 {

			if app.ShowDetail {
				utils.SharedAppLogger.Info(fmt.Sprintf("Found %d tasks in config.\n", len(config.AppConfig.Tasks)))
				jsonData, err := json.MarshalIndent(config.AppConfig.Tasks, "", "  ")
				if err != nil {
					utils.SharedAppLogger.Fatal(err)
				}
				utils.SharedAppLogger.Info(string(jsonData))
			}

			utils.SharedAppLogger.Success("configuration check success")

		} else {
			utils.SharedAppLogger.Warn("No applications defined in the configuration.")
		}

	},
}
View Source
var DownCmd = &cobra.Command{
	Use:   "down",
	Short: "Kill previous tasks processes",
	Long:  "Kill previous tasks processes with command: dgrkt down",
	PreRun: func(cmd *cobra.Command, args []string) {
		app.DetachMode = true
		procedure.InitializeSpinnerAgent()
		procedure.StartSpinnerAgent()
	},
	PostRun: func(cmd *cobra.Command, args []string) {
		procedure.StopSpinnerAgent()
	},
	Run: func(cmd *cobra.Command, args []string) {

		if dir, err := os.Getwd(); err == nil {
			var pidFilePath = filepath.Join(dir, procedure.PidFile)
			pidFile, err := os.ReadFile(pidFilePath)
			if err != nil && !os.IsNotExist(err) {
				utils.SharedAppLogger.Error(err)
				return
			}
			var pids procedure.TaskProcessLog

			if err = yaml.Unmarshal(pidFile, &pids); err != nil {
				utils.SharedAppLogger.Error(err)
				return
			}

			var shutdownProcess []ShutdownProcess

			var waitGroup = &sync.WaitGroup{}
			waitGroup.Add(len(pids.Tasks))

			for _, task := range pids.Tasks {
				procedure.TaskSpinner.RegisterSpinner(task.Name, task.Name+"|", "Shutting down")

				process, err := os.FindProcess(task.Pid)

				if err != nil {
					if spinner, ok := procedure.TaskSpinner.GetSpinner(task.Name); ok {
						spinner.ErrorWithMessagef("Error finding process: %s", err.Error())
					}
					continue
				} else {
					shutdownProcess = append(shutdownProcess, ShutdownProcess{process: process, name: task.Name})
				}
			}

			for _, process := range shutdownProcess {
				go process.kill(waitGroup)
			}

			waitGroup.Wait()
		}
	},
}
View Source
var InitCmd = &cobra.Command{
	Use:   "init",
	Short: "Generate minimal dgrkt.yaml file",
	Long:  "Generate minimal dgrkt.yaml in the current directory with a basic 'echo' task.",
	Run: func(cmd *cobra.Command, args []string) {

		if dir, err := os.Getwd(); err == nil {
			message := fmt.Sprintf("dir: %s", dir)
			utils.SharedAppLogger.Info(message)
		}

		var echoConfig map[string]any

		if app.InitCmdIsWindows {
			echoConfig = map[string]any{
				"tasks": []map[string]any{
					{
						"name":       "echo",
						"executable": "cmd.exe",
						"args":       []string{"/C", "echo hello world"},
					},
				},
			}
		} else {
			echoConfig = map[string]any{
				"tasks": []map[string]any{
					{
						"name":       "echo",
						"executable": "echo",
						"args":       []string{"hello", "world"},
					},
				},
			}
		}

		currentDir, err := os.Getwd()
		if err != nil {
			utils.SharedAppLogger.Fatal(err)
		}

		var outputFile = config.GetDefaultFileNameWithExtension()

		if app.InitCmdOutput != "" {
			outputFile = app.InitCmdOutput
		}

		outputFile = filepath.Join(currentDir, outputFile)

		if _, err := os.Stat(outputFile); err == nil {
			utils.SharedAppLogger.Warn(fmt.Sprintf("'dgrkt.yaml' already exists in '%s'. Aborting to prevent overwrite.\n", currentDir))
			utils.SharedAppLogger.Warn("If you wish to overwrite, please delete the existing file first.")
			return
		} else if !os.IsNotExist(err) {

			utils.SharedAppLogger.Fatal(err)
		}

		yamlContent, err := yaml.Marshal(echoConfig)
		if err != nil {
			utils.SharedAppLogger.Fatal(err)
		}

		err = os.WriteFile(outputFile, yamlContent, 0644)
		if err != nil {
			utils.SharedAppLogger.Fatal(err)
		}
		utils.SharedAppLogger.Info("Successfully generated dgrkt.yaml", "path", outputFile)
	},
}
View Source
var RootCmd = &cobra.Command{
	Use:   "dgrkt",
	Short: "A versatile application launcher with flexible configuration options",
	Long: `dgrkt is a versatile command-line tool designed to simplify the management of complex, multi-process applications or task sequences.
	
It allows you to define a series of commands (tasks) and their operational parameters within a single, human-readable YAML configuration file.
This includes specifying the executable, arguments, working directories, environment variables, and crucial inter-task dependencies.

A core feature of dgrkt is its robust health check mechanism. Each task can be configured with HTTP-based health checks (including JSON response validation via JSONPath) or command-line script-based health checks.
Tasks with dependencies will only start once their prerequisites are deemed healthy, ensuring a stable and reliable startup order for your services.

Whether you're spinning up a local development environment with multiple microservices, orchestrating integration tests, or automating complex workflows,
dgrkt provides a declarative and efficient way to manage your system's components.
	`,
}
View Source
var UpCmd = &cobra.Command{
	Use:   "up",
	Short: "Execute tasks according to the YAML configuration file.",
	Long:  "Execute tasks according to the YAML configuration file. with command: dgrkt up",
	PreRun: func(cmd *cobra.Command, args []string) {
		procedure.InitializeSpinnerAgent()
		procedure.StartSpinnerAgent()
	},
	PostRun: func(cmd *cobra.Command, args []string) {
		procedure.StopSpinnerAgent()
	},
	Run: func(cmd *cobra.Command, args []string) {
		if err := CheckConfig(); err != nil {
			utils.SharedAppLogger.Fatal(err)
		}

		AppTasks = make(map[string]*procedure.Task)

		for _, taskConfig := range config.AppTasksConfig {
			var task, err = procedure.CreateTask(taskConfig)
			if err != nil {
				utils.SharedAppLogger.Fatal(err)
			}
			AppTasks[taskConfig.Name] = task
		}
		for _, taskConfig := range config.AppTasksConfig {
			var task = AppTasks[taskConfig.Name]
			if len(taskConfig.DependsOn) > 0 {
				for _, dependency := range taskConfig.DependsOn {
					task.AppendDependencies(AppTasks[dependency])
				}
			}
		}

		var waitGroup = &sync.WaitGroup{}
		waitGroup.Add(len(AppTasks))

		for _, task := range AppTasks {
			go task.Start(waitGroup)
		}

		waitGroup.Wait()

		if len(os.Args) == 1 && app.Portable == "true" && runtime.GOOS == "windows" {
			utils.SharedAppLogger.Info("Program completed, Press ctrl-c to exit.")
			c := make(chan os.Signal, 1)
			signal.Notify(c, os.Interrupt, syscall.SIGTERM)
			<-c
		}
	},
}
View Source
var VersionCmd = &cobra.Command{
	Use:   "version",
	Short: "Show version number and build details of dgrkt",
	Run: func(cmd *cobra.Command, args []string) {

		var info = fmt.Sprintf("[%s] (Build: %s Commit: %s Portable: %s)",
			app.Version,
			app.BuildDate,
			app.CommitHash,
			app.Portable)
		utils.SharedAppLogger.Info(info)
	},
}

Functions

func CheckConfig

func CheckConfig() error

func Execute

func Execute()

Types

type ShutdownProcess

type ShutdownProcess struct {
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL