From 3e10f2cf6d54e4049489ed5edd8ca2710643f985 Mon Sep 17 00:00:00 2001 From: mlsmaycon Date: Thu, 13 Aug 2026 18:59:53 +0000 Subject: [PATCH] [client] Cut cognitive complexity of the up request builders The --allow-remote-jobs branch pushed setupSetConfigReq and setupLoginRequest to cognitive complexity 26 (SonarCloud gate is 25). Extract the repeated "if cmd.Flag(x).Changed { field = &val }" shape into setBoolPtrIfChanged and use it for the remote-jobs flag in all three request builders, dropping each flagged method back to 25. No behavior change. --- client/cmd/up.go | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/client/cmd/up.go b/client/cmd/up.go index 91c204d9a..ca7875885 100644 --- a/client/cmd/up.go +++ b/client/cmd/up.go @@ -398,6 +398,17 @@ func doDaemonUp(ctx context.Context, cmd *cobra.Command, client proto.DaemonServ return nil } +// setBoolPtrIfChanged points dst at a copy of val when the named bool flag was +// explicitly set on cmd. It collapses the repeated +// "if cmd.Flag(x).Changed { field = &val }" pattern in the request builders into +// a single call, keeping their cognitive complexity within bounds. +func setBoolPtrIfChanged(cmd *cobra.Command, name string, dst **bool, val bool) { + if cmd.Flag(name).Changed { + dst2 := val + *dst = &dst2 + } +} + func setupSetConfigReq(customDNSAddressConverted []byte, cmd *cobra.Command, profileName, username string) *proto.SetConfigRequest { var req proto.SetConfigRequest req.ProfileName = profileName @@ -421,9 +432,7 @@ func setupSetConfigReq(customDNSAddressConverted []byte, cmd *cobra.Command, pro if cmd.Flag(serverSSHAllowedFlag).Changed { req.ServerSSHAllowed = &serverSSHAllowed } - if cmd.Flag(remoteJobsAllowedFlag).Changed { - req.RemoteJobsAllowed = &remoteJobsAllowed - } + setBoolPtrIfChanged(cmd, remoteJobsAllowedFlag, &req.RemoteJobsAllowed, remoteJobsAllowed) if cmd.Flag(enableSSHRootFlag).Changed { req.EnableSSHRoot = &enableSSHRoot } @@ -526,9 +535,7 @@ func setupConfig(customDNSAddressConverted []byte, cmd *cobra.Command, configFil if cmd.Flag(serverSSHAllowedFlag).Changed { ic.ServerSSHAllowed = &serverSSHAllowed } - if cmd.Flag(remoteJobsAllowedFlag).Changed { - ic.RemoteJobsAllowed = &remoteJobsAllowed - } + setBoolPtrIfChanged(cmd, remoteJobsAllowedFlag, &ic.RemoteJobsAllowed, remoteJobsAllowed) if cmd.Flag(enableSSHRootFlag).Changed { ic.EnableSSHRoot = &enableSSHRoot @@ -654,9 +661,7 @@ func setupLoginRequest(providedSetupKey string, customDNSAddressConverted []byte if cmd.Flag(serverSSHAllowedFlag).Changed { loginRequest.ServerSSHAllowed = &serverSSHAllowed } - if cmd.Flag(remoteJobsAllowedFlag).Changed { - loginRequest.RemoteJobsAllowed = &remoteJobsAllowed - } + setBoolPtrIfChanged(cmd, remoteJobsAllowedFlag, &loginRequest.RemoteJobsAllowed, remoteJobsAllowed) if cmd.Flag(enableSSHRootFlag).Changed { loginRequest.EnableSSHRoot = &enableSSHRoot