mirror of
https://github.com/netbirdio/netbird.git
synced 2026-10-26 06:19:08 +01:00
[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.
This commit is contained in:
+14
-9
@@ -398,6 +398,17 @@ func doDaemonUp(ctx context.Context, cmd *cobra.Command, client proto.DaemonServ
|
|||||||
return nil
|
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 {
|
func setupSetConfigReq(customDNSAddressConverted []byte, cmd *cobra.Command, profileName, username string) *proto.SetConfigRequest {
|
||||||
var req proto.SetConfigRequest
|
var req proto.SetConfigRequest
|
||||||
req.ProfileName = profileName
|
req.ProfileName = profileName
|
||||||
@@ -421,9 +432,7 @@ func setupSetConfigReq(customDNSAddressConverted []byte, cmd *cobra.Command, pro
|
|||||||
if cmd.Flag(serverSSHAllowedFlag).Changed {
|
if cmd.Flag(serverSSHAllowedFlag).Changed {
|
||||||
req.ServerSSHAllowed = &serverSSHAllowed
|
req.ServerSSHAllowed = &serverSSHAllowed
|
||||||
}
|
}
|
||||||
if cmd.Flag(remoteJobsAllowedFlag).Changed {
|
setBoolPtrIfChanged(cmd, remoteJobsAllowedFlag, &req.RemoteJobsAllowed, remoteJobsAllowed)
|
||||||
req.RemoteJobsAllowed = &remoteJobsAllowed
|
|
||||||
}
|
|
||||||
if cmd.Flag(enableSSHRootFlag).Changed {
|
if cmd.Flag(enableSSHRootFlag).Changed {
|
||||||
req.EnableSSHRoot = &enableSSHRoot
|
req.EnableSSHRoot = &enableSSHRoot
|
||||||
}
|
}
|
||||||
@@ -526,9 +535,7 @@ func setupConfig(customDNSAddressConverted []byte, cmd *cobra.Command, configFil
|
|||||||
if cmd.Flag(serverSSHAllowedFlag).Changed {
|
if cmd.Flag(serverSSHAllowedFlag).Changed {
|
||||||
ic.ServerSSHAllowed = &serverSSHAllowed
|
ic.ServerSSHAllowed = &serverSSHAllowed
|
||||||
}
|
}
|
||||||
if cmd.Flag(remoteJobsAllowedFlag).Changed {
|
setBoolPtrIfChanged(cmd, remoteJobsAllowedFlag, &ic.RemoteJobsAllowed, remoteJobsAllowed)
|
||||||
ic.RemoteJobsAllowed = &remoteJobsAllowed
|
|
||||||
}
|
|
||||||
|
|
||||||
if cmd.Flag(enableSSHRootFlag).Changed {
|
if cmd.Flag(enableSSHRootFlag).Changed {
|
||||||
ic.EnableSSHRoot = &enableSSHRoot
|
ic.EnableSSHRoot = &enableSSHRoot
|
||||||
@@ -654,9 +661,7 @@ func setupLoginRequest(providedSetupKey string, customDNSAddressConverted []byte
|
|||||||
if cmd.Flag(serverSSHAllowedFlag).Changed {
|
if cmd.Flag(serverSSHAllowedFlag).Changed {
|
||||||
loginRequest.ServerSSHAllowed = &serverSSHAllowed
|
loginRequest.ServerSSHAllowed = &serverSSHAllowed
|
||||||
}
|
}
|
||||||
if cmd.Flag(remoteJobsAllowedFlag).Changed {
|
setBoolPtrIfChanged(cmd, remoteJobsAllowedFlag, &loginRequest.RemoteJobsAllowed, remoteJobsAllowed)
|
||||||
loginRequest.RemoteJobsAllowed = &remoteJobsAllowed
|
|
||||||
}
|
|
||||||
|
|
||||||
if cmd.Flag(enableSSHRootFlag).Changed {
|
if cmd.Flag(enableSSHRootFlag).Changed {
|
||||||
loginRequest.EnableSSHRoot = &enableSSHRoot
|
loginRequest.EnableSSHRoot = &enableSSHRoot
|
||||||
|
|||||||
Reference in New Issue
Block a user