From 90692ab791af105eaacb8ec2415b06e4ee5cdb10 Mon Sep 17 00:00:00 2001 From: Black-Hole1 Date: Wed, 25 Oct 2023 22:06:43 +0800 Subject: [PATCH] fix(ci): lint failed --- pkg/rest/vf/vm_config.go | 4 ++-- pkg/util/strings.go | 7 +++++++ pkg/util/strings_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/pkg/rest/vf/vm_config.go b/pkg/rest/vf/vm_config.go index 4bd02a6b..b43f9e65 100644 --- a/pkg/rest/vf/vm_config.go +++ b/pkg/rest/vf/vm_config.go @@ -2,10 +2,10 @@ package rest import ( "net/http" - "strings" "github.com/Code-Hex/vz/v3" "github.com/crc-org/vfkit/pkg/rest/define" + "github.com/crc-org/vfkit/pkg/util" "github.com/gin-gonic/gin" "github.com/sirupsen/logrus" ) @@ -72,7 +72,7 @@ func (vm *VzVirtualMachine) CanOperate(c *gin.Context) { return } - can, err := vm.CanChangeState(define.StateChange(strings.Title(p.Op))) + can, err := vm.CanChangeState(define.StateChange(util.FirstUpper(p.Op))) if err != nil { logrus.Errorf("failed to check operation %s: %q", p.Op, err) c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) diff --git a/pkg/util/strings.go b/pkg/util/strings.go index a74fbfd3..dabcfd26 100644 --- a/pkg/util/strings.go +++ b/pkg/util/strings.go @@ -21,3 +21,10 @@ func StringInSlice(st string, sl []string) bool { } return false } + +func FirstUpper(s string) string { + if s == "" { + return "" + } + return strings.ToUpper(s[:1]) + s[1:] +} diff --git a/pkg/util/strings_test.go b/pkg/util/strings_test.go index 343925b3..3a434b9f 100644 --- a/pkg/util/strings_test.go +++ b/pkg/util/strings_test.go @@ -92,3 +92,43 @@ func TestTrimQuotes(t *testing.T) { }) } } + +func TestFirstUpper(t *testing.T) { + tests := []struct { + value string + want string + }{ + { + value: "foobar", + want: "Foobar", + }, + { + value: "Foobar", + want: "Foobar", + }, + { + value: "", + want: "", + }, + { + value: "f", + want: "F", + }, + { + value: "F", + want: "F", + }, + { + value: "1", + want: "1", + }, + } + + for _, tt := range tests { + t.Run(tt.value, func(t *testing.T) { + if got := FirstUpper(tt.value); got != tt.want { + t.Errorf("FirstUpper() = %v, want %v", got, tt.want) + } + }) + } +}