mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-08-10 13:20:33 +02:00
cmd: refactoring Run function
So it's easier, more clear, more isolation between code on non-mobile and mobile platforms.
This commit is contained in:
+44
-30
@@ -80,7 +80,7 @@ var rootCmd = &cobra.Command{
|
|||||||
Short: strings.TrimLeft(rootShortDesc, "\n"),
|
Short: strings.TrimLeft(rootShortDesc, "\n"),
|
||||||
Version: curVersion(),
|
Version: curVersion(),
|
||||||
PreRun: func(cmd *cobra.Command, args []string) {
|
PreRun: func(cmd *cobra.Command, args []string) {
|
||||||
InitConsoleLogging()
|
initConsoleLogging()
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,10 +121,10 @@ func initCLI() {
|
|||||||
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) {
|
PreRun: func(cmd *cobra.Command, args []string) {
|
||||||
InitConsoleLogging()
|
initConsoleLogging()
|
||||||
},
|
},
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
Run(cmd, nil, nil, nil)
|
RunCobraCommand(cmd)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
runCmd.Flags().BoolVarP(&daemon, "daemon", "d", false, "Run as daemon")
|
runCmd.Flags().BoolVarP(&daemon, "daemon", "d", false, "Run as daemon")
|
||||||
@@ -149,7 +149,7 @@ func initCLI() {
|
|||||||
|
|
||||||
startCmd := &cobra.Command{
|
startCmd := &cobra.Command{
|
||||||
PreRun: func(cmd *cobra.Command, args []string) {
|
PreRun: func(cmd *cobra.Command, args []string) {
|
||||||
InitConsoleLogging()
|
initConsoleLogging()
|
||||||
checkHasElevatedPrivilege()
|
checkHasElevatedPrivilege()
|
||||||
},
|
},
|
||||||
Use: "start",
|
Use: "start",
|
||||||
@@ -227,7 +227,7 @@ func initCLI() {
|
|||||||
mainLog.Load().Fatal().Msgf("failed to unmarshal config: %v", err)
|
mainLog.Load().Fatal().Msgf("failed to unmarshal config: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
InitLogging()
|
initLogging()
|
||||||
|
|
||||||
// Explicitly passing config, so on system where home directory could not be obtained,
|
// Explicitly passing config, so on system where home directory could not be obtained,
|
||||||
// or sub-process env is different with the parent, we still behave correctly and use
|
// or sub-process env is different with the parent, we still behave correctly and use
|
||||||
@@ -297,7 +297,7 @@ func initCLI() {
|
|||||||
routerCmd := &cobra.Command{
|
routerCmd := &cobra.Command{
|
||||||
Use: "setup",
|
Use: "setup",
|
||||||
PreRun: func(cmd *cobra.Command, args []string) {
|
PreRun: func(cmd *cobra.Command, args []string) {
|
||||||
InitConsoleLogging()
|
initConsoleLogging()
|
||||||
},
|
},
|
||||||
Run: func(cmd *cobra.Command, _ []string) {
|
Run: func(cmd *cobra.Command, _ []string) {
|
||||||
exe, err := os.Executable()
|
exe, err := os.Executable()
|
||||||
@@ -326,7 +326,7 @@ func initCLI() {
|
|||||||
|
|
||||||
stopCmd := &cobra.Command{
|
stopCmd := &cobra.Command{
|
||||||
PreRun: func(cmd *cobra.Command, args []string) {
|
PreRun: func(cmd *cobra.Command, args []string) {
|
||||||
InitConsoleLogging()
|
initConsoleLogging()
|
||||||
checkHasElevatedPrivilege()
|
checkHasElevatedPrivilege()
|
||||||
},
|
},
|
||||||
Use: "stop",
|
Use: "stop",
|
||||||
@@ -341,7 +341,7 @@ func initCLI() {
|
|||||||
mainLog.Load().Error().Msg(err.Error())
|
mainLog.Load().Error().Msg(err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
InitLogging()
|
initLogging()
|
||||||
if doTasks([]task{{s.Stop, true}}) {
|
if doTasks([]task{{s.Stop, true}}) {
|
||||||
p.router.Cleanup()
|
p.router.Cleanup()
|
||||||
p.resetDNS()
|
p.resetDNS()
|
||||||
@@ -353,7 +353,7 @@ func initCLI() {
|
|||||||
|
|
||||||
restartCmd := &cobra.Command{
|
restartCmd := &cobra.Command{
|
||||||
PreRun: func(cmd *cobra.Command, args []string) {
|
PreRun: func(cmd *cobra.Command, args []string) {
|
||||||
InitConsoleLogging()
|
initConsoleLogging()
|
||||||
checkHasElevatedPrivilege()
|
checkHasElevatedPrivilege()
|
||||||
},
|
},
|
||||||
Use: "restart",
|
Use: "restart",
|
||||||
@@ -365,7 +365,7 @@ func initCLI() {
|
|||||||
mainLog.Load().Error().Msg(err.Error())
|
mainLog.Load().Error().Msg(err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
InitLogging()
|
initLogging()
|
||||||
|
|
||||||
tasks := []task{
|
tasks := []task{
|
||||||
{s.Stop, false},
|
{s.Stop, false},
|
||||||
@@ -391,7 +391,7 @@ func initCLI() {
|
|||||||
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) {
|
PreRun: func(cmd *cobra.Command, args []string) {
|
||||||
InitConsoleLogging()
|
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)
|
||||||
@@ -420,14 +420,14 @@ 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()
|
initConsoleLogging()
|
||||||
checkHasElevatedPrivilege()
|
checkHasElevatedPrivilege()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uninstallCmd := &cobra.Command{
|
uninstallCmd := &cobra.Command{
|
||||||
PreRun: func(cmd *cobra.Command, args []string) {
|
PreRun: func(cmd *cobra.Command, args []string) {
|
||||||
InitConsoleLogging()
|
initConsoleLogging()
|
||||||
checkHasElevatedPrivilege()
|
checkHasElevatedPrivilege()
|
||||||
},
|
},
|
||||||
Use: "uninstall",
|
Use: "uninstall",
|
||||||
@@ -458,7 +458,7 @@ NOTE: Uninstalling will set DNS to values provided by DHCP.`,
|
|||||||
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) {
|
PreRun: func(cmd *cobra.Command, args []string) {
|
||||||
InitConsoleLogging()
|
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) {
|
||||||
@@ -518,7 +518,7 @@ 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()
|
initConsoleLogging()
|
||||||
checkHasElevatedPrivilege()
|
checkHasElevatedPrivilege()
|
||||||
},
|
},
|
||||||
Use: "start",
|
Use: "start",
|
||||||
@@ -536,7 +536,7 @@ 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()
|
initConsoleLogging()
|
||||||
checkHasElevatedPrivilege()
|
checkHasElevatedPrivilege()
|
||||||
},
|
},
|
||||||
Use: "stop",
|
Use: "stop",
|
||||||
@@ -555,7 +555,7 @@ 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()
|
initConsoleLogging()
|
||||||
checkHasElevatedPrivilege()
|
checkHasElevatedPrivilege()
|
||||||
},
|
},
|
||||||
Use: "restart",
|
Use: "restart",
|
||||||
@@ -571,7 +571,7 @@ NOTE: Uninstalling will set DNS to values provided by DHCP.`,
|
|||||||
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) {
|
PreRun: func(cmd *cobra.Command, args []string) {
|
||||||
InitConsoleLogging()
|
initConsoleLogging()
|
||||||
},
|
},
|
||||||
Run: statusCmd.Run,
|
Run: statusCmd.Run,
|
||||||
}
|
}
|
||||||
@@ -579,7 +579,7 @@ NOTE: Uninstalling will set DNS to values provided by DHCP.`,
|
|||||||
|
|
||||||
uninstallCmdAlias := &cobra.Command{
|
uninstallCmdAlias := &cobra.Command{
|
||||||
PreRun: func(cmd *cobra.Command, args []string) {
|
PreRun: func(cmd *cobra.Command, args []string) {
|
||||||
InitConsoleLogging()
|
initConsoleLogging()
|
||||||
checkHasElevatedPrivilege()
|
checkHasElevatedPrivilege()
|
||||||
},
|
},
|
||||||
Use: "uninstall",
|
Use: "uninstall",
|
||||||
@@ -604,7 +604,7 @@ 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()
|
initConsoleLogging()
|
||||||
checkHasElevatedPrivilege()
|
checkHasElevatedPrivilege()
|
||||||
},
|
},
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
@@ -665,15 +665,30 @@ func isMobile() bool {
|
|||||||
return runtime.GOOS == "android" || runtime.GOOS == "ios"
|
return runtime.GOOS == "android" || runtime.GOOS == "ios"
|
||||||
}
|
}
|
||||||
|
|
||||||
func Run(cmd *cobra.Command, appConfig *AppConfig, appCallback *AppCallback, stopCh chan struct{}) {
|
// RunCobraCommand runs ctrld cli.
|
||||||
if appConfig != nil {
|
func RunCobraCommand(cmd *cobra.Command) {
|
||||||
homedir = appConfig.HomeDir
|
noConfigStart = isNoConfigStart(cmd)
|
||||||
verbose = appConfig.Verbose
|
run(nil, make(chan struct{}))
|
||||||
cdUID = appConfig.CdUID
|
}
|
||||||
logPath = appConfig.LogPath
|
|
||||||
|
// RunMobile runs the ctrld cli on mobile platforms.
|
||||||
|
func RunMobile(appConfig *AppConfig, appCallback *AppCallback, stopCh chan struct{}) {
|
||||||
|
if appConfig == nil {
|
||||||
|
panic("appConfig is nil")
|
||||||
}
|
}
|
||||||
|
initConsoleLogging()
|
||||||
|
noConfigStart = false
|
||||||
|
homedir = appConfig.HomeDir
|
||||||
|
verbose = appConfig.Verbose
|
||||||
|
cdUID = appConfig.CdUID
|
||||||
|
logPath = appConfig.LogPath
|
||||||
|
run(appCallback, stopCh)
|
||||||
|
}
|
||||||
|
|
||||||
|
// run runs ctrld cli with given app callback and stop channel.
|
||||||
|
func run(appCallback *AppCallback, stopCh chan struct{}) {
|
||||||
if stopCh == nil {
|
if stopCh == nil {
|
||||||
stopCh = make(chan struct{})
|
mainLog.Load().Fatal().Msg("stopCh is nil")
|
||||||
}
|
}
|
||||||
waitCh := make(chan struct{})
|
waitCh := make(chan struct{})
|
||||||
p := &prog{
|
p := &prog{
|
||||||
@@ -713,7 +728,6 @@ func Run(cmd *cobra.Command, appConfig *AppConfig, appCallback *AppCallback, sto
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
noConfigStart := cmd != nil && isNoConfigStart(cmd)
|
|
||||||
writeDefaultConfig := !noConfigStart && configBase64 == ""
|
writeDefaultConfig := !noConfigStart && configBase64 == ""
|
||||||
tryReadingConfig(writeDefaultConfig)
|
tryReadingConfig(writeDefaultConfig)
|
||||||
|
|
||||||
@@ -727,7 +741,7 @@ func Run(cmd *cobra.Command, appConfig *AppConfig, appCallback *AppCallback, sto
|
|||||||
|
|
||||||
// Log config do not have thing to validate, so it's safe to init log here,
|
// Log config do not have thing to validate, so it's safe to init log here,
|
||||||
// so it's able to log information in processCDFlags.
|
// so it's able to log information in processCDFlags.
|
||||||
InitLogging()
|
initLogging()
|
||||||
|
|
||||||
mainLog.Load().Info().Msgf("starting ctrld %s", curVersion())
|
mainLog.Load().Info().Msgf("starting ctrld %s", curVersion())
|
||||||
mainLog.Load().Info().Msgf("os: %s", osVersion())
|
mainLog.Load().Info().Msgf("os: %s", osVersion())
|
||||||
@@ -1353,7 +1367,7 @@ func uninstall(p *prog, s service.Service) {
|
|||||||
{s.Stop, false},
|
{s.Stop, false},
|
||||||
{s.Uninstall, true},
|
{s.Uninstall, true},
|
||||||
}
|
}
|
||||||
InitLogging()
|
initLogging()
|
||||||
if doTasks(tasks) {
|
if doTasks(tasks) {
|
||||||
if err := p.router.ConfigureService(svcConfig); err != nil {
|
if err := p.router.ConfigureService(svcConfig); err != nil {
|
||||||
mainLog.Load().Fatal().Err(err).Msg("could not configure service")
|
mainLog.Load().Fatal().Err(err).Msg("could not configure service")
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package cli
|
package cli
|
||||||
|
|
||||||
|
// AppCallback provides hooks for injecting certain functionalities
|
||||||
|
// from mobile platforms to main ctrld cli.
|
||||||
type AppCallback struct {
|
type AppCallback struct {
|
||||||
HostName func() string
|
HostName func() string
|
||||||
LanIp func() string
|
LanIp func() string
|
||||||
@@ -7,6 +9,7 @@ type AppCallback struct {
|
|||||||
Exit func(error string)
|
Exit func(error string)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AppConfig allows overwriting ctrld cli flags from mobile platforms.
|
||||||
type AppConfig struct {
|
type AppConfig struct {
|
||||||
CdUID string
|
CdUID string
|
||||||
HomeDir string
|
HomeDir string
|
||||||
|
|||||||
+5
-3
@@ -35,6 +35,7 @@ var (
|
|||||||
|
|
||||||
mainLog atomic.Pointer[zerolog.Logger]
|
mainLog atomic.Pointer[zerolog.Logger]
|
||||||
consoleWriter zerolog.ConsoleWriter
|
consoleWriter zerolog.ConsoleWriter
|
||||||
|
noConfigStart bool
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -65,7 +66,8 @@ func normalizeLogFilePath(logFilePath string) string {
|
|||||||
return filepath.Join(dir, logFilePath)
|
return filepath.Join(dir, logFilePath)
|
||||||
}
|
}
|
||||||
|
|
||||||
func InitConsoleLogging() {
|
// initConsoleLogging initializes console logging, then storing to mainLog.
|
||||||
|
func initConsoleLogging() {
|
||||||
consoleWriter = zerolog.NewConsoleWriter(func(w *zerolog.ConsoleWriter) {
|
consoleWriter = zerolog.NewConsoleWriter(func(w *zerolog.ConsoleWriter) {
|
||||||
w.TimeFormat = time.StampMilli
|
w.TimeFormat = time.StampMilli
|
||||||
})
|
})
|
||||||
@@ -84,8 +86,8 @@ func InitConsoleLogging() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// InitLogging initializes global logging setup.
|
// initLogging initializes global logging setup.
|
||||||
func InitLogging() {
|
func initLogging() {
|
||||||
initLoggingWithBackup(true)
|
initLoggingWithBackup(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -31,7 +31,6 @@ type AppCallback interface {
|
|||||||
func (c *Controller) Start(CdUID string, HomeDir string, logLevel int, logPath string) {
|
func (c *Controller) Start(CdUID string, HomeDir string, logLevel int, logPath string) {
|
||||||
if c.stopCh == nil {
|
if c.stopCh == nil {
|
||||||
c.stopCh = make(chan struct{})
|
c.stopCh = make(chan struct{})
|
||||||
cli.InitConsoleLogging()
|
|
||||||
c.Config = cli.AppConfig{
|
c.Config = cli.AppConfig{
|
||||||
CdUID: CdUID,
|
CdUID: CdUID,
|
||||||
HomeDir: HomeDir,
|
HomeDir: HomeDir,
|
||||||
@@ -39,7 +38,7 @@ func (c *Controller) Start(CdUID string, HomeDir string, logLevel int, logPath s
|
|||||||
LogPath: logPath,
|
LogPath: logPath,
|
||||||
}
|
}
|
||||||
appCallback := mapCallback(c.AppCallback)
|
appCallback := mapCallback(c.AppCallback)
|
||||||
cli.Run(nil, &c.Config, &appCallback, c.stopCh)
|
cli.RunMobile(&c.Config, &appCallback, c.stopCh)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user