refactor: convert rootCmd from global to local variable

- Add appVersion variable to store curVersion() result during init
- Change initCLI() to return *cobra.Command
- Move rootCmd creation inside initCLI() as local variable
- Replace all rootCmd.Version usage with appVersion variable
- Update Main() function to capture returned rootCmd from initCLI()
- Remove sync.Once guard from tests and use initCLI() directly
- Remove sync import from test file as it's no longer needed

This refactoring improves encapsulation by eliminating global state,
reduces version computation overhead, and simplifies test setup by
removing the need for sync.Once guards. All tests pass and the
application builds successfully.
This commit is contained in:
Cuong Manh Le
2025-08-05 14:37:21 +07:00
committed by Cuong Manh Le
parent 4d8e10ca0d
commit b187ec98a3
6 changed files with 32 additions and 39 deletions
+18 -14
View File
@@ -61,6 +61,8 @@ var (
defaultConfigFile = "ctrld.toml"
rootCertPool *x509.CertPool
errSelfCheckNoAnswer = errors.New("no response from ctrld listener. You can try to re-launch with flag --skip_self_checks")
// Store version once during init to avoid repeated calls to curVersion()
appVersion = curVersion()
)
var basicModeFlags = []string{"listen", "primary_upstream", "secondary_upstream", "domains"}
@@ -83,15 +85,6 @@ _/ ___\ __\_ __ \ | / __ |
\/ dns forwarding proxy \/
`
var rootCmd = &cobra.Command{
Use: "ctrld",
Short: strings.TrimLeft(rootShortDesc, "\n"),
Version: curVersion(),
PersistentPreRun: func(cmd *cobra.Command, args []string) {
initConsoleLogging()
},
}
func curVersion() string {
if version != "dev" && !strings.HasPrefix(version, "v") {
version = "v" + version
@@ -105,12 +98,21 @@ func curVersion() string {
return fmt.Sprintf("%s-%s", version, commit)
}
func initCLI() {
func initCLI() *cobra.Command {
// Enable opening via explorer.exe on Windows.
// See: https://github.com/spf13/cobra/issues/844.
cobra.MousetrapHelpText = ""
cobra.EnableCommandSorting = false
rootCmd := &cobra.Command{
Use: "ctrld",
Short: strings.TrimLeft(rootShortDesc, "\n"),
Version: appVersion,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
initConsoleLogging()
},
}
rootCmd.PersistentFlags().CountVarP(
&verbose,
"verbose",
@@ -132,6 +134,8 @@ func initCLI() {
InitClientsCmd(rootCmd)
InitUpgradeCmd(rootCmd)
InitLogCmd(rootCmd)
return rootCmd
}
// isMobile reports whether the current OS is a mobile platform.
@@ -603,12 +607,12 @@ func processCDFlags(cfg *ctrld.Config) (*controld.ResolverConfig, error) {
bo := backoff.NewBackoff("processCDFlags", logf, 30*time.Second)
bo.LogLongerThan = 30 * time.Second
ctx := ctrld.LoggerCtx(context.Background(), logger)
resolverConfig, err := controld.FetchResolverConfig(ctx, cdUID, rootCmd.Version, cdDev)
resolverConfig, err := controld.FetchResolverConfig(ctx, cdUID, appVersion, cdDev)
for {
if errUrlNetworkError(err) {
bo.BackOff(ctx, err)
logger.Warn().Msg("could not fetch resolver using bootstrap DNS, retrying...")
resolverConfig, err = controld.FetchResolverConfig(ctx, cdUID, rootCmd.Version, cdDev)
resolverConfig, err = controld.FetchResolverConfig(ctx, cdUID, appVersion, cdDev)
continue
}
break
@@ -1391,7 +1395,7 @@ func cdUIDFromProvToken() string {
req := &controld.UtilityOrgRequest{ProvToken: cdOrg, Hostname: customHostname}
// Process provision token if provided.
loggerCtx := ctrld.LoggerCtx(context.Background(), mainLog.Load())
resolverConfig, err := controld.FetchResolverUID(loggerCtx, req, rootCmd.Version, cdDev)
resolverConfig, err := controld.FetchResolverUID(loggerCtx, req, appVersion, cdDev)
if err != nil {
mainLog.Load().Fatal().Err(err).Msgf("failed to fetch resolver uid with provision token: %s", cdOrg)
}
@@ -1715,7 +1719,7 @@ func runningIface(s service.Service) *ifaceResponse {
// doValidateCdRemoteConfig fetches and validates custom config for cdUID.
func doValidateCdRemoteConfig(cdUID string, fatal bool) error {
loggerCtx := ctrld.LoggerCtx(context.Background(), mainLog.Load())
rc, err := controld.FetchResolverConfig(loggerCtx, cdUID, rootCmd.Version, cdDev)
rc, err := controld.FetchResolverConfig(loggerCtx, cdUID, appVersion, cdDev)
if err != nil {
logger := mainLog.Load().Fatal()
if !fatal {