From f190274bce91ded5b37afdd8b75207f20a866994 Mon Sep 17 00:00:00 2001 From: restitux Date: Tue, 7 Mar 2023 23:09:49 -0700 Subject: [PATCH] Utilify runner tag printing function --- pipeline_api/pipeline_api.go | 14 ++++---------- runnermanager/runnermanager.go | 13 +++---------- util/util.go | 18 ++++++++++++++++++ 3 files changed, 25 insertions(+), 20 deletions(-) create mode 100644 util/util.go diff --git a/pipeline_api/pipeline_api.go b/pipeline_api/pipeline_api.go index faaf160..0d76276 100644 --- a/pipeline_api/pipeline_api.go +++ b/pipeline_api/pipeline_api.go @@ -10,6 +10,7 @@ import ( apiv2 "git.ohea.xyz/cursorius/pipeline-api/go/api/v2" "git.ohea.xyz/cursorius/pipeline-api/go/api/v2/apiv2connect" "git.ohea.xyz/cursorius/server/runnermanager" + "git.ohea.xyz/cursorius/server/util" "github.com/bufbuild/connect-go" "github.com/google/uuid" "github.com/op/go-logging" @@ -61,22 +62,15 @@ func (s *ApiServer) GetRunner( RespChan: respChan, } - var runnerTagsStr strings.Builder - if len(req.Msg.Tags) > 0 { - fmt.Fprintf(&runnerTagsStr, "[%v", req.Msg.Tags[0]) - for _, tag := range req.Msg.Tags[1:] { - fmt.Fprintf(&runnerTagsStr, ", %v", tag) - } - fmt.Fprintf(&runnerTagsStr, "]") - } + tagsStr := util.FormatTags(req.Msg.Tags) response := <-respChan if response.Err != nil { - log.Errorf("Could not get runner with tags \"%v\": %v", runnerTagsStr.String(), response.Err) + log.Errorf("Could not get runner with tags \"%v\": %v", tagsStr, response.Err) return nil, connect.NewError(connect.CodeNotFound, fmt.Errorf("Could not get runner")) } - log.Infof("Got runner with tags: %v", runnerTagsStr.String()) + log.Infof("Got runner with tags: %v", tagsStr) runnerUuid := uuid.New() diff --git a/runnermanager/runnermanager.go b/runnermanager/runnermanager.go index 9b4d9b3..b67adba 100644 --- a/runnermanager/runnermanager.go +++ b/runnermanager/runnermanager.go @@ -3,7 +3,6 @@ package runnermanager import ( "context" "fmt" - "strings" "time" "github.com/google/uuid" @@ -13,6 +12,7 @@ import ( "git.ohea.xyz/cursorius/server/config" "git.ohea.xyz/cursorius/server/database" + "git.ohea.xyz/cursorius/server/util" runner_api "git.ohea.xyz/cursorius/runner-api/go/api/v2" ) @@ -51,15 +51,8 @@ type runnerJob struct { } func (r *runnerManager) processRequest(req GetRunnerRequest) { - var runnerTagsStr strings.Builder - if len(req.Tags) > 0 { - fmt.Fprintf(&runnerTagsStr, "[%v", req.Tags[0]) - for _, tag := range req.Tags[1:] { - fmt.Fprintf(&runnerTagsStr, ", %v", tag) - } - fmt.Fprintf(&runnerTagsStr, "]") - } - log.Infof("Got request for runner with tags \"%v\"", runnerTagsStr.String()) + tagsStr := util.FormatTags(req.Tags) + log.Infof("Got request for runner with tags \"%v\"", tagsStr) log.Debugf("Finding runner with tags %v", runnerTagsStr.String()) diff --git a/util/util.go b/util/util.go new file mode 100644 index 0000000..405e105 --- /dev/null +++ b/util/util.go @@ -0,0 +1,18 @@ +package util + +import ( + "fmt" + "strings" +) + +func FormatTags(tags []string) string { + var tagsStr strings.Builder + if len(tags) > 0 { + fmt.Fprintf(&tagsStr, "[%v", tags[0]) + for _, tag := range tags[1:] { + fmt.Fprintf(&tagsStr, ", %v", tag) + } + fmt.Fprintf(&tagsStr, "]") + } + return tagsStr.String() +}