fix: address P0-2b review issues - build tag and actionType guard
1. Add //go:build !cgo to control_host_test.go so the stub-testing tests only run in non-CGO builds, matching the stub.go build constraint. Clean up dead nil check in SignatureCompiles test. 2. Replace two independent if-statements for actionType mapping in ControlHost with a map lookup + explicit guard, consistent with ControlQueue's ActionMap pattern. Prevents silent zero-value actionType if SupportedHostActions expands later.
This commit is contained in:
parent
8124cda83e
commit
5807a05cbb
2 changed files with 11 additions and 9 deletions
|
|
@ -1,3 +1,5 @@
|
||||||
|
//go:build !cgo
|
||||||
|
|
||||||
package backend
|
package backend
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
@ -18,8 +20,5 @@ func TestControlHost_StubReturnsError(t *testing.T) {
|
||||||
// signature matches: func ControlHost(hostname string, action int) error.
|
// signature matches: func ControlHost(hostname string, action int) error.
|
||||||
// If the signature changes this test will not compile.
|
// If the signature changes this test will not compile.
|
||||||
func TestControlHost_SignatureCompiles(t *testing.T) {
|
func TestControlHost_SignatureCompiles(t *testing.T) {
|
||||||
var fn func(string, int) error = ControlHost
|
var _ func(string, int) error = ControlHost
|
||||||
if fn == nil {
|
|
||||||
t.Fatal("ControlHost must not be nil")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -407,12 +407,15 @@ func (h *HostHandler) ControlHost(ctx *gin.Context) {
|
||||||
ReturnError(ctx, common.ERROR_CODE, err.Error(), nil)
|
ReturnError(ctx, common.ERROR_CODE, err.Error(), nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var actionType int
|
hostActionMap := map[string]int{
|
||||||
if baseAction == "close" {
|
"open": models.HOST_OPEN,
|
||||||
actionType = models.HOST_CLOSE
|
"close": models.HOST_CLOSE,
|
||||||
}
|
}
|
||||||
if baseAction == "open" {
|
actionType, exists := hostActionMap[baseAction]
|
||||||
actionType = models.HOST_OPEN
|
if !exists {
|
||||||
|
logger.Error("Unsupported action type", zap.String("baseAction", baseAction))
|
||||||
|
ReturnError(ctx, common.INVALID_PARAMS_CODE, "Unsupported action type: "+baseAction, nil)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
successHosts := []map[string]interface{}{}
|
successHosts := []map[string]interface{}{}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue