mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-07-04 01:07:49 +02:00
cmd/cli: simplifying console logging initialization
By using PersistentPreRun with root command, so we don't have to write the same code for each child commands.
This commit is contained in:
committed by
Cuong Manh Le
parent
c2556a8e39
commit
4769da4ef4
+2
-30
@@ -86,7 +86,7 @@ var rootCmd = &cobra.Command{
|
|||||||
Use: "ctrld",
|
Use: "ctrld",
|
||||||
Short: strings.TrimLeft(rootShortDesc, "\n"),
|
Short: strings.TrimLeft(rootShortDesc, "\n"),
|
||||||
Version: curVersion(),
|
Version: curVersion(),
|
||||||
PreRun: func(cmd *cobra.Command, args []string) {
|
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
||||||
initConsoleLogging()
|
initConsoleLogging()
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -127,9 +127,6 @@ func initCLI() {
|
|||||||
Use: "run",
|
Use: "run",
|
||||||
Short: "Run the DNS proxy server",
|
Short: "Run the DNS proxy server",
|
||||||
Args: cobra.NoArgs,
|
Args: cobra.NoArgs,
|
||||||
PreRun: func(cmd *cobra.Command, args []string) {
|
|
||||||
initConsoleLogging()
|
|
||||||
},
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
RunCobraCommand(cmd)
|
RunCobraCommand(cmd)
|
||||||
},
|
},
|
||||||
@@ -158,7 +155,6 @@ func initCLI() {
|
|||||||
|
|
||||||
startCmd := &cobra.Command{
|
startCmd := &cobra.Command{
|
||||||
PreRun: func(cmd *cobra.Command, args []string) {
|
PreRun: func(cmd *cobra.Command, args []string) {
|
||||||
initConsoleLogging()
|
|
||||||
checkHasElevatedPrivilege()
|
checkHasElevatedPrivilege()
|
||||||
},
|
},
|
||||||
Use: "start",
|
Use: "start",
|
||||||
@@ -405,9 +401,6 @@ func initCLI() {
|
|||||||
|
|
||||||
routerCmd := &cobra.Command{
|
routerCmd := &cobra.Command{
|
||||||
Use: "setup",
|
Use: "setup",
|
||||||
PreRun: func(cmd *cobra.Command, args []string) {
|
|
||||||
initConsoleLogging()
|
|
||||||
},
|
|
||||||
Run: func(cmd *cobra.Command, _ []string) {
|
Run: func(cmd *cobra.Command, _ []string) {
|
||||||
exe, err := os.Executable()
|
exe, err := os.Executable()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -435,7 +428,6 @@ func initCLI() {
|
|||||||
|
|
||||||
stopCmd := &cobra.Command{
|
stopCmd := &cobra.Command{
|
||||||
PreRun: func(cmd *cobra.Command, args []string) {
|
PreRun: func(cmd *cobra.Command, args []string) {
|
||||||
initConsoleLogging()
|
|
||||||
checkHasElevatedPrivilege()
|
checkHasElevatedPrivilege()
|
||||||
},
|
},
|
||||||
Use: "stop",
|
Use: "stop",
|
||||||
@@ -467,7 +459,6 @@ func initCLI() {
|
|||||||
|
|
||||||
restartCmd := &cobra.Command{
|
restartCmd := &cobra.Command{
|
||||||
PreRun: func(cmd *cobra.Command, args []string) {
|
PreRun: func(cmd *cobra.Command, args []string) {
|
||||||
initConsoleLogging()
|
|
||||||
checkHasElevatedPrivilege()
|
checkHasElevatedPrivilege()
|
||||||
},
|
},
|
||||||
Use: "restart",
|
Use: "restart",
|
||||||
@@ -506,7 +497,6 @@ func initCLI() {
|
|||||||
|
|
||||||
reloadCmd := &cobra.Command{
|
reloadCmd := &cobra.Command{
|
||||||
PreRun: func(cmd *cobra.Command, args []string) {
|
PreRun: func(cmd *cobra.Command, args []string) {
|
||||||
initConsoleLogging()
|
|
||||||
checkHasElevatedPrivilege()
|
checkHasElevatedPrivilege()
|
||||||
},
|
},
|
||||||
Use: "reload",
|
Use: "reload",
|
||||||
@@ -552,9 +542,6 @@ func initCLI() {
|
|||||||
Use: "status",
|
Use: "status",
|
||||||
Short: "Show status of the ctrld service",
|
Short: "Show status of the ctrld service",
|
||||||
Args: cobra.NoArgs,
|
Args: cobra.NoArgs,
|
||||||
PreRun: func(cmd *cobra.Command, args []string) {
|
|
||||||
initConsoleLogging()
|
|
||||||
},
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
s, err := newService(&prog{}, svcConfig)
|
s, err := newService(&prog{}, svcConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -582,14 +569,12 @@ func initCLI() {
|
|||||||
if runtime.GOOS == "darwin" {
|
if runtime.GOOS == "darwin" {
|
||||||
// On darwin, running status command without privileges may return wrong information.
|
// On darwin, running status command without privileges may return wrong information.
|
||||||
statusCmd.PreRun = func(cmd *cobra.Command, args []string) {
|
statusCmd.PreRun = func(cmd *cobra.Command, args []string) {
|
||||||
initConsoleLogging()
|
|
||||||
checkHasElevatedPrivilege()
|
checkHasElevatedPrivilege()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uninstallCmd := &cobra.Command{
|
uninstallCmd := &cobra.Command{
|
||||||
PreRun: func(cmd *cobra.Command, args []string) {
|
PreRun: func(cmd *cobra.Command, args []string) {
|
||||||
initConsoleLogging()
|
|
||||||
checkHasElevatedPrivilege()
|
checkHasElevatedPrivilege()
|
||||||
},
|
},
|
||||||
Use: "uninstall",
|
Use: "uninstall",
|
||||||
@@ -624,9 +609,6 @@ NOTE: Uninstalling will set DNS to values provided by DHCP.`,
|
|||||||
Use: "list",
|
Use: "list",
|
||||||
Short: "List network interfaces of the host",
|
Short: "List network interfaces of the host",
|
||||||
Args: cobra.NoArgs,
|
Args: cobra.NoArgs,
|
||||||
PreRun: func(cmd *cobra.Command, args []string) {
|
|
||||||
initConsoleLogging()
|
|
||||||
},
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
err := interfaces.ForeachInterface(func(i interfaces.Interface, prefixes []netip.Prefix) {
|
err := interfaces.ForeachInterface(func(i interfaces.Interface, prefixes []netip.Prefix) {
|
||||||
fmt.Printf("Index : %d\n", i.Index)
|
fmt.Printf("Index : %d\n", i.Index)
|
||||||
@@ -687,7 +669,6 @@ NOTE: Uninstalling will set DNS to values provided by DHCP.`,
|
|||||||
rootCmd.AddCommand(serviceCmd)
|
rootCmd.AddCommand(serviceCmd)
|
||||||
startCmdAlias := &cobra.Command{
|
startCmdAlias := &cobra.Command{
|
||||||
PreRun: func(cmd *cobra.Command, args []string) {
|
PreRun: func(cmd *cobra.Command, args []string) {
|
||||||
initConsoleLogging()
|
|
||||||
checkHasElevatedPrivilege()
|
checkHasElevatedPrivilege()
|
||||||
},
|
},
|
||||||
Use: "start",
|
Use: "start",
|
||||||
@@ -705,7 +686,6 @@ NOTE: Uninstalling will set DNS to values provided by DHCP.`,
|
|||||||
rootCmd.AddCommand(startCmdAlias)
|
rootCmd.AddCommand(startCmdAlias)
|
||||||
stopCmdAlias := &cobra.Command{
|
stopCmdAlias := &cobra.Command{
|
||||||
PreRun: func(cmd *cobra.Command, args []string) {
|
PreRun: func(cmd *cobra.Command, args []string) {
|
||||||
initConsoleLogging()
|
|
||||||
checkHasElevatedPrivilege()
|
checkHasElevatedPrivilege()
|
||||||
},
|
},
|
||||||
Use: "stop",
|
Use: "stop",
|
||||||
@@ -724,7 +704,6 @@ NOTE: Uninstalling will set DNS to values provided by DHCP.`,
|
|||||||
|
|
||||||
restartCmdAlias := &cobra.Command{
|
restartCmdAlias := &cobra.Command{
|
||||||
PreRun: func(cmd *cobra.Command, args []string) {
|
PreRun: func(cmd *cobra.Command, args []string) {
|
||||||
initConsoleLogging()
|
|
||||||
checkHasElevatedPrivilege()
|
checkHasElevatedPrivilege()
|
||||||
},
|
},
|
||||||
Use: "restart",
|
Use: "restart",
|
||||||
@@ -737,7 +716,6 @@ NOTE: Uninstalling will set DNS to values provided by DHCP.`,
|
|||||||
|
|
||||||
reloadCmdAlias := &cobra.Command{
|
reloadCmdAlias := &cobra.Command{
|
||||||
PreRun: func(cmd *cobra.Command, args []string) {
|
PreRun: func(cmd *cobra.Command, args []string) {
|
||||||
initConsoleLogging()
|
|
||||||
checkHasElevatedPrivilege()
|
checkHasElevatedPrivilege()
|
||||||
},
|
},
|
||||||
Use: "reload",
|
Use: "reload",
|
||||||
@@ -752,16 +730,12 @@ NOTE: Uninstalling will set DNS to values provided by DHCP.`,
|
|||||||
Use: "status",
|
Use: "status",
|
||||||
Short: "Show status of the ctrld service",
|
Short: "Show status of the ctrld service",
|
||||||
Args: cobra.NoArgs,
|
Args: cobra.NoArgs,
|
||||||
PreRun: func(cmd *cobra.Command, args []string) {
|
Run: statusCmd.Run,
|
||||||
initConsoleLogging()
|
|
||||||
},
|
|
||||||
Run: statusCmd.Run,
|
|
||||||
}
|
}
|
||||||
rootCmd.AddCommand(statusCmdAlias)
|
rootCmd.AddCommand(statusCmdAlias)
|
||||||
|
|
||||||
uninstallCmdAlias := &cobra.Command{
|
uninstallCmdAlias := &cobra.Command{
|
||||||
PreRun: func(cmd *cobra.Command, args []string) {
|
PreRun: func(cmd *cobra.Command, args []string) {
|
||||||
initConsoleLogging()
|
|
||||||
checkHasElevatedPrivilege()
|
checkHasElevatedPrivilege()
|
||||||
},
|
},
|
||||||
Use: "uninstall",
|
Use: "uninstall",
|
||||||
@@ -786,7 +760,6 @@ NOTE: Uninstalling will set DNS to values provided by DHCP.`,
|
|||||||
Short: "List clients that ctrld discovered",
|
Short: "List clients that ctrld discovered",
|
||||||
Args: cobra.NoArgs,
|
Args: cobra.NoArgs,
|
||||||
PreRun: func(cmd *cobra.Command, args []string) {
|
PreRun: func(cmd *cobra.Command, args []string) {
|
||||||
initConsoleLogging()
|
|
||||||
checkHasElevatedPrivilege()
|
checkHasElevatedPrivilege()
|
||||||
},
|
},
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
@@ -874,7 +847,6 @@ NOTE: Uninstalling will set DNS to values provided by DHCP.`,
|
|||||||
ValidArgs: []string{upgradeChannelDev, upgradeChannelProd},
|
ValidArgs: []string{upgradeChannelDev, upgradeChannelProd},
|
||||||
Args: cobra.MaximumNArgs(1),
|
Args: cobra.MaximumNArgs(1),
|
||||||
PreRun: func(cmd *cobra.Command, args []string) {
|
PreRun: func(cmd *cobra.Command, args []string) {
|
||||||
initConsoleLogging()
|
|
||||||
checkHasElevatedPrivilege()
|
checkHasElevatedPrivilege()
|
||||||
},
|
},
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
|||||||
Reference in New Issue
Block a user