cmd

package
v0.0.0-...-81c6b1d Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2024 License: Apache-2.0 Imports: 21 Imported by: 0

README

CLI 入口

使用 cobra-cli 生成代码,然后在生成的 command 下补充

cobra-cli create [command name]

主命令

root 为主命令,
其他命令为子命令然后添加到主命令下

Documentation

Index

Constants

View Source
const (
	CliName     = "dpi-analyze"
	Description = "dpi CLI"
	CliVersion  = "1.1.0.241211_beta"
)

Variables

View Source
var CleanCmd = &cobra.Command{
	Use:   "clean",
	Short: "清空所有数据",
	Run:   cleanRun,
}
View Source
var PsCmd = &cobra.Command{
	Use:   "ps",
	Short: "List DPI Server",
	Run: func(cmd *cobra.Command, args []string) {
		var processes [][]string

		entries, err := os.ReadDir(config.RunDir)
		if err != nil {
			fmt.Println("Error reading run directory:", err)
			return
		}

		for _, entry := range entries {
			if !entry.IsDir() {
				split := strings.Split(entry.Name(), ".")
				if split[len(split)-1] != "pid" {
					continue
				}

				pidFile := filepath.Join(config.RunDir, entry.Name())
				pidBytes, err := os.ReadFile(pidFile)
				if err != nil {
					fmt.Printf("Error reading PID file %s: %v\n", pidFile, err)
					continue
				}

				pidStr := strings.TrimSpace(string(pidBytes))
				pid, err := strconv.Atoi(pidStr)
				if err != nil {
					fmt.Printf("Invalid PID in file %s: %s\n", pidFile, pidStr)
					continue
				}

				processInfo := getProcessInfo(pid)
				if processInfo != nil {
					processes = append(processes, processInfo)
				}
			}
		}

		displayProcesses(processes)
	},
}
View Source
var RestartCmd = &cobra.Command{
	Use:   "restart",
	Short: "Restart one or more running server",
	PreRun: func(cmd *cobra.Command, args []string) {
		if len(args) == 0 {
			_ = cmd.Help()
			os.Exit(0)
		}
	},
}
View Source
var RunCmd = &cobra.Command{
	Use:   "run [command]",
	Short: "Create and run server",
	PreRun: func(cmd *cobra.Command, args []string) {
		if len(args) == 0 {
			_ = cmd.Help()
			os.Exit(0)
		}
	},
}
View Source
var StatusCmd = &cobra.Command{
	Use:   "status",
	Short: "Show service running status",
	PreRun: func(cmd *cobra.Command, args []string) {
		if len(args) == 0 {
			_ = cmd.Help()
			os.Exit(0)
		}
	},
}
View Source
var StopCmd = &cobra.Command{
	Use:   "stop",
	Short: "Stop one or more running server",
	PreRun: func(cmd *cobra.Command, args []string) {
		if len(args) == 0 {
			_ = cmd.Help()
			os.Exit(0)
		}
	},
}

Functions

func Execute

func Execute()

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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