From e3dfd9947b4222e71eaab26df16f7c604a0e37c0 Mon Sep 17 00:00:00 2001 From: niqdev Date: Thu, 31 Aug 2023 22:57:14 +0100 Subject: [PATCH] refactor labels --- internal/command/box/box.go | 14 +++---- internal/command/box/common.go | 30 +++++++-------- internal/command/box/start.go | 4 +- pkg/box/cloud/cloud.go | 2 +- pkg/box/cloud/cloud_test.go | 21 ++++++----- pkg/box/docker/docker.go | 2 +- pkg/box/kubernetes/kubernetes.go | 11 ++---- pkg/box/model/labels.go | 32 ++++++++++++++++ pkg/{common => box}/model/labels_test.go | 42 ++++++++++----------- pkg/box/model/types_test.go | 4 +- pkg/common/model/labels.go | 47 ++++++------------------ 11 files changed, 107 insertions(+), 102 deletions(-) rename pkg/{common => box}/model/labels_test.go (76%) diff --git a/internal/command/box/box.go b/internal/command/box/box.go index 227d4ea1..6a46fcf8 100644 --- a/internal/command/box/box.go +++ b/internal/command/box/box.go @@ -10,7 +10,7 @@ import ( commonCmd "github.com/hckops/hckctl/internal/command/common" commonFlag "github.com/hckops/hckctl/internal/command/common/flag" "github.com/hckops/hckctl/internal/command/config" - "github.com/hckops/hckctl/pkg/box/model" + boxModel "github.com/hckops/hckctl/pkg/box/model" commonModel "github.com/hckops/hckctl/pkg/common/model" "github.com/hckops/hckctl/pkg/template" ) @@ -19,7 +19,7 @@ type boxCmdOptions struct { configRef *config.ConfigRef sourceFlag *commonFlag.SourceFlag providerFlag *commonFlag.ProviderFlag - provider model.BoxProvider + provider boxModel.BoxProvider tunnelFlag *boxFlag.TunnelFlag } @@ -118,21 +118,21 @@ func (opts *boxCmdOptions) run(cmd *cobra.Command, args []string) error { path := args[0] log.Debug().Msgf("temporary box from local template: path=%s", path) - sourceLoader := template.NewLocalCachedLoader[model.BoxV1](path, opts.configRef.Config.Template.CacheDir) - return opts.temporaryBox(sourceLoader, opts.provider, commonModel.NewBoxLabels().AddDefaultLocal()) + sourceLoader := template.NewLocalCachedLoader[boxModel.BoxV1](path, opts.configRef.Config.Template.CacheDir) + return opts.temporaryBox(sourceLoader, opts.provider, boxModel.NewBoxLabels().AddDefaultLocal()) } else { name := args[0] log.Debug().Msgf("temporary box from git template: name=%s revision=%s", name, opts.sourceFlag.Revision) sourceOpts := commonCmd.NewGitSourceOptions(opts.configRef.Config.Template.CacheDir, opts.sourceFlag.Revision) - sourceLoader := template.NewGitLoader[model.BoxV1](sourceOpts, name) - labels := commonModel.NewBoxLabels().AddDefaultGit(sourceOpts.RepositoryUrl, sourceOpts.DefaultRevision, sourceOpts.CacheDirName()) + sourceLoader := template.NewGitLoader[boxModel.BoxV1](sourceOpts, name) + labels := boxModel.NewBoxLabels().AddDefaultGit(sourceOpts.RepositoryUrl, sourceOpts.DefaultRevision, sourceOpts.CacheDirName()) return opts.temporaryBox(sourceLoader, opts.provider, labels) } } -func (opts *boxCmdOptions) temporaryBox(sourceLoader template.SourceLoader[model.BoxV1], provider model.BoxProvider, labels commonModel.Labels) error { +func (opts *boxCmdOptions) temporaryBox(sourceLoader template.SourceLoader[boxModel.BoxV1], provider boxModel.BoxProvider, labels commonModel.Labels) error { temporaryClient := func(invokeOpts *invokeOptions) error { diff --git a/internal/command/box/common.go b/internal/command/box/common.go index 77169355..e5a069b3 100644 --- a/internal/command/box/common.go +++ b/internal/command/box/common.go @@ -11,7 +11,7 @@ import ( "github.com/hckops/hckctl/internal/command/config" "github.com/hckops/hckctl/internal/command/version" "github.com/hckops/hckctl/pkg/box" - "github.com/hckops/hckctl/pkg/box/model" + boxModel "github.com/hckops/hckctl/pkg/box/model" commonModel "github.com/hckops/hckctl/pkg/common/model" "github.com/hckops/hckctl/pkg/schema" "github.com/hckops/hckctl/pkg/template" @@ -19,12 +19,12 @@ import ( type invokeOptions struct { client box.BoxClient - template *template.TemplateInfo[model.BoxV1] + template *template.TemplateInfo[boxModel.BoxV1] loader *commonCmd.Loader } // start and temporary -func runBoxClient(sourceLoader template.SourceLoader[model.BoxV1], provider model.BoxProvider, configRef *config.ConfigRef, invokeClient func(*invokeOptions) error) error { +func runBoxClient(sourceLoader template.SourceLoader[boxModel.BoxV1], provider boxModel.BoxProvider, configRef *config.ConfigRef, invokeClient func(*invokeOptions) error) error { boxTemplate, err := sourceLoader.Read() if err != nil || boxTemplate.Value.Kind != schema.KindBoxV1 { @@ -56,7 +56,7 @@ func runBoxClient(sourceLoader template.SourceLoader[model.BoxV1], provider mode } // open, info and stop-one -func attemptRunBoxClients(configRef *config.ConfigRef, boxName string, invokeClient func(*invokeOptions, *model.BoxDetails) error) error { +func attemptRunBoxClients(configRef *config.ConfigRef, boxName string, invokeClient func(*invokeOptions, *boxModel.BoxDetails) error) error { loader := commonCmd.NewLoader() loader.Start("loading %s", boxName) @@ -107,17 +107,17 @@ func attemptRunBoxClients(configRef *config.ConfigRef, boxName string, invokeCli return errors.New("not found") } -func newSourceLoader(boxDetails *model.BoxDetails, cacheDir string) template.SourceLoader[model.BoxV1] { +func newSourceLoader(boxDetails *boxModel.BoxDetails, cacheDir string) template.SourceLoader[boxModel.BoxV1] { if boxDetails.TemplateInfo.IsCached() { - return template.NewLocalLoader[model.BoxV1](boxDetails.TemplateInfo.CachedTemplate.Path) + return template.NewLocalLoader[boxModel.BoxV1](boxDetails.TemplateInfo.CachedTemplate.Path) } else { sourceOpts := commonCmd.NewGitSourceOptions(cacheDir, boxDetails.TemplateInfo.GitTemplate.Commit) - return template.NewGitLoader[model.BoxV1](sourceOpts, boxDetails.TemplateInfo.GitTemplate.Name) + return template.NewGitLoader[boxModel.BoxV1](sourceOpts, boxDetails.TemplateInfo.GitTemplate.Name) } } -func newBoxClientOpts(provider model.BoxProvider, configRef *config.ConfigRef) *model.BoxClientOptions { - return &model.BoxClientOptions{ +func newBoxClientOpts(provider boxModel.BoxProvider, configRef *config.ConfigRef) *boxModel.BoxClientOptions { + return &boxModel.BoxClientOptions{ Provider: provider, DockerOpts: configRef.Config.Provider.Docker.ToDockerOptions(), KubeOpts: configRef.Config.Provider.Kube.ToKubeOptions(), @@ -125,7 +125,7 @@ func newBoxClientOpts(provider model.BoxProvider, configRef *config.ConfigRef) * } } -func newDefaultBoxClient(provider model.BoxProvider, configRef *config.ConfigRef, loader *commonCmd.Loader) (box.BoxClient, error) { +func newDefaultBoxClient(provider boxModel.BoxProvider, configRef *config.ConfigRef, loader *commonCmd.Loader) (box.BoxClient, error) { boxClientOpts := newBoxClientOpts(provider, configRef) boxClient, err := box.NewBoxClient(boxClientOpts) @@ -138,8 +138,8 @@ func newDefaultBoxClient(provider model.BoxProvider, configRef *config.ConfigRef return boxClient, nil } -func newCreateOptions(info *template.TemplateInfo[model.BoxV1], labels commonModel.Labels, sizeValue string) (*model.CreateOptions, error) { - size, err := model.ExistResourceSize(sizeValue) +func newCreateOptions(info *template.TemplateInfo[boxModel.BoxV1], labels commonModel.Labels, sizeValue string) (*boxModel.CreateOptions, error) { + size, err := boxModel.ExistResourceSize(sizeValue) if err != nil { return nil, err } @@ -147,12 +147,12 @@ func newCreateOptions(info *template.TemplateInfo[model.BoxV1], labels commonMod var allLabels commonModel.Labels switch info.SourceType { case template.Local: - allLabels = labels.AddBoxSize(size).AddLocal(info.Path) + allLabels = boxModel.AddBoxSize(labels, size).AddLocal(info.Path) case template.Git: - allLabels = labels.AddBoxSize(size).AddGit(info.Path, info.Revision) + allLabels = boxModel.AddBoxSize(labels, size).AddGit(info.Path, info.Revision) } - return &model.CreateOptions{ + return &boxModel.CreateOptions{ Template: &info.Value.Data, Size: size, Labels: allLabels, diff --git a/internal/command/box/start.go b/internal/command/box/start.go index 5367d90c..f3c64dcc 100644 --- a/internal/command/box/start.go +++ b/internal/command/box/start.go @@ -67,7 +67,7 @@ func (opts *boxStartCmdOptions) run(cmd *cobra.Command, args []string) error { log.Debug().Msgf("start box from local template: path=%s", path) sourceLoader := template.NewLocalCachedLoader[boxModel.BoxV1](path, opts.configRef.Config.Template.CacheDir) - return startBox(sourceLoader, opts.provider, opts.configRef, commonModel.NewBoxLabels().AddDefaultLocal()) + return startBox(sourceLoader, opts.provider, opts.configRef, boxModel.NewBoxLabels().AddDefaultLocal()) } else { name := args[0] @@ -75,7 +75,7 @@ func (opts *boxStartCmdOptions) run(cmd *cobra.Command, args []string) error { sourceOpts := commonCmd.NewGitSourceOptions(opts.configRef.Config.Template.CacheDir, opts.sourceFlag.Revision) sourceLoader := template.NewGitLoader[boxModel.BoxV1](sourceOpts, name) - labels := commonModel.NewBoxLabels().AddDefaultGit(sourceOpts.RepositoryUrl, sourceOpts.DefaultRevision, sourceOpts.CacheDirName()) + labels := boxModel.NewBoxLabels().AddDefaultGit(sourceOpts.RepositoryUrl, sourceOpts.DefaultRevision, sourceOpts.CacheDirName()) return startBox(sourceLoader, opts.provider, opts.configRef, labels) } } diff --git a/pkg/box/cloud/cloud.go b/pkg/box/cloud/cloud.go index 5730bb01..836acc8e 100644 --- a/pkg/box/cloud/cloud.go +++ b/pkg/box/cloud/cloud.go @@ -247,7 +247,7 @@ func toBoxDetails(response *v1.Message[v1.BoxDescribeResponseBody]) (*boxModel.B }, TemplateInfo: &boxModel.BoxTemplateInfo{ // TODO valid only if response.Body.Template.Public - GitTemplate: &boxModel.GitTemplateInfo{ + GitTemplate: &commonModel.GitTemplateInfo{ Url: response.Body.Template.Url, Revision: response.Body.Template.Revision, Commit: response.Body.Template.Commit, diff --git a/pkg/box/cloud/cloud_test.go b/pkg/box/cloud/cloud_test.go index 65dcf280..a3e38751 100644 --- a/pkg/box/cloud/cloud_test.go +++ b/pkg/box/cloud/cloud_test.go @@ -7,7 +7,8 @@ import ( "github.com/stretchr/testify/assert" v1 "github.com/hckops/hckctl/pkg/api/v1" - "github.com/hckops/hckctl/pkg/box/model" + boxModel "github.com/hckops/hckctl/pkg/box/model" + commonModel "github.com/hckops/hckctl/pkg/common/model" ) func TestToBoxDetails(t *testing.T) { @@ -30,29 +31,29 @@ func TestToBoxDetails(t *testing.T) { Env: []string{"KEY_1=VALUE_1", "KEY_2=VALUE_2", "INVALID", "=INVALID="}, Ports: []string{"alias-1/123", "alias-2/456", "INVALID", "/INVALID/"}, }) - expected := &model.BoxDetails{ - Info: model.BoxInfo{ + expected := &boxModel.BoxDetails{ + Info: boxModel.BoxInfo{ Id: "myId", Name: "myName", Healthy: true, }, - TemplateInfo: &model.BoxTemplateInfo{ - GitTemplate: &model.GitTemplateInfo{ + TemplateInfo: &boxModel.BoxTemplateInfo{ + GitTemplate: &commonModel.GitTemplateInfo{ Url: "infoUrl", Revision: "infoRevision", Commit: "infoCommit", Name: "infoName", }, }, - ProviderInfo: &model.BoxProviderInfo{ - Provider: model.Cloud, + ProviderInfo: &boxModel.BoxProviderInfo{ + Provider: boxModel.Cloud, }, - Size: model.Medium, - Env: []model.BoxEnv{ + Size: boxModel.Medium, + Env: []boxModel.BoxEnv{ {Key: "KEY_1", Value: "VALUE_1"}, {Key: "KEY_2", Value: "VALUE_2"}, }, - Ports: []model.BoxPort{ + Ports: []boxModel.BoxPort{ {Alias: "alias-1", Local: "none", Remote: "123", Public: false}, {Alias: "alias-2", Local: "none", Remote: "456", Public: false}, }, diff --git a/pkg/box/docker/docker.go b/pkg/box/docker/docker.go index 1c8ab053..5dded9ac 100644 --- a/pkg/box/docker/docker.go +++ b/pkg/box/docker/docker.go @@ -270,7 +270,7 @@ func toBoxDetails(container docker.ContainerDetails) (*boxModel.BoxDetails, erro labels := commonModel.Labels(container.Labels) - size, err := labels.ToBoxSize() + size, err := boxModel.ToBoxSize(labels) if err != nil { return nil, err } diff --git a/pkg/box/kubernetes/kubernetes.go b/pkg/box/kubernetes/kubernetes.go index a7aeb9cb..6c993015 100644 --- a/pkg/box/kubernetes/kubernetes.go +++ b/pkg/box/kubernetes/kubernetes.go @@ -144,13 +144,8 @@ func (box *KubeBoxClient) connectBox(opts *boxModel.ConnectOptions) error { } } -func BoxLabelSelector() string { - // value must be sanitized - return fmt.Sprintf("%s=%s", commonModel.LabelSchemaKind, common.ToKebabCase(schema.KindBoxV1.String())) -} - func boxNameLabelSelector(name string) string { - return fmt.Sprintf("%s,%s=%s", BoxLabelSelector(), kubernetes.LabelKubeName, name) + return fmt.Sprintf("%s,%s=%s", boxModel.BoxLabelSelector(), kubernetes.LabelKubeName, name) } func (box *KubeBoxClient) searchBox(name string) (*boxModel.BoxInfo, error) { @@ -291,7 +286,7 @@ func ToBoxDetails(deployment *kubernetes.DeploymentDetails, serviceInfo *kuberne labels := commonModel.Labels(deployment.Annotations) - size, err := labels.ToBoxSize() + size, err := boxModel.ToBoxSize(labels) if err != nil { return nil, err } @@ -336,7 +331,7 @@ func ToBoxDetails(deployment *kubernetes.DeploymentDetails, serviceInfo *kuberne func (box *KubeBoxClient) listBoxes() ([]boxModel.BoxInfo, error) { namespace := box.clientOpts.Namespace - deployments, err := box.client.DeploymentList(namespace, boxModel.BoxPrefixName, BoxLabelSelector()) + deployments, err := box.client.DeploymentList(namespace, boxModel.BoxPrefixName, boxModel.BoxLabelSelector()) if err != nil { return nil, err } diff --git a/pkg/box/model/labels.go b/pkg/box/model/labels.go index 156634d4..8b24e8eb 100644 --- a/pkg/box/model/labels.go +++ b/pkg/box/model/labels.go @@ -1,5 +1,37 @@ package model +import ( + "fmt" + "strings" + + "github.com/hckops/hckctl/pkg/client/common" + commonModel "github.com/hckops/hckctl/pkg/common/model" + "github.com/hckops/hckctl/pkg/schema" +) + const ( LabelBoxSize = "com.hckops.box.size" ) + +func NewBoxLabels() commonModel.Labels { + return map[string]string{ + commonModel.LabelSchemaKind: schema.KindBoxV1.String(), + } +} + +func AddBoxSize(labels commonModel.Labels, size ResourceSize) commonModel.Labels { + return labels.AddLabel(LabelBoxSize, strings.ToLower(size.String())) +} + +func ToBoxSize(labels commonModel.Labels) (ResourceSize, error) { + if label, err := labels.Exist(LabelBoxSize); err != nil { + return Small, err + } else { + return ExistResourceSize(label) + } +} + +func BoxLabelSelector() string { + // value must be sanitized + return fmt.Sprintf("%s=%s", commonModel.LabelSchemaKind, common.ToKebabCase(schema.KindBoxV1.String())) +} diff --git a/pkg/common/model/labels_test.go b/pkg/box/model/labels_test.go similarity index 76% rename from pkg/common/model/labels_test.go rename to pkg/box/model/labels_test.go index ff39ac35..c01e6740 100644 --- a/pkg/common/model/labels_test.go +++ b/pkg/box/model/labels_test.go @@ -5,12 +5,12 @@ import ( "github.com/stretchr/testify/assert" - "github.com/hckops/hckctl/pkg/box/model" + commonModel "github.com/hckops/hckctl/pkg/common/model" ) func TestLocalLabels(t *testing.T) { labels := NewBoxLabels().AddDefaultLocal() - expected := Labels{ + expected := commonModel.Labels{ "com.hckops.schema.kind": "box/v1", "com.hckops.template.local": "true", } @@ -21,7 +21,7 @@ func TestLocalLabels(t *testing.T) { func TestGitLabels(t *testing.T) { labels := NewBoxLabels().AddDefaultGit("myUrl", "myRevision", "myDir") - expected := Labels{ + expected := commonModel.Labels{ "com.hckops.schema.kind": "box/v1", "com.hckops.template.git": "true", "com.hckops.template.git.url": "myUrl", @@ -33,9 +33,9 @@ func TestGitLabels(t *testing.T) { assert.Equal(t, expected, labels) } -func TestAboxModelddLocalLabels(t *testing.T) { - labels := NewBoxLabels().AddBoxSize(model.Small).AddLocal("/tmp/cache") - expected := Labels{ +func TestAddLocalLabels(t *testing.T) { + labels := AddBoxSize(NewBoxLabels().AddDefaultLocal(), Small).AddLocal("/tmp/cache") + expected := commonModel.Labels{ "com.hckops.schema.kind": "box/v1", "com.hckops.template.local": "true", "com.hckops.template.cache.path": "/tmp/cache", @@ -57,8 +57,8 @@ func TestAddGitLabels(t *testing.T) { gitLabels := NewBoxLabels().AddDefaultGit("https://github.com/hckops/megalopolis", "main", "megalopolis") path := "/home/test/.cache/hck/megalopolis/box/base/arch.yml" - labels := gitLabels.AddBoxSize(model.Medium).AddGit(path, "myCommit") - expected := Labels{ + labels := AddBoxSize(gitLabels, Medium).AddGit(path, "myCommit") + expected := commonModel.Labels{ "com.hckops.schema.kind": "box/v1", "com.hckops.template.git": "true", "com.hckops.template.git.url": "https://github.com/hckops/megalopolis", @@ -82,45 +82,44 @@ func TestAddGitLabelsInvalid(t *testing.T) { } func TestExist(t *testing.T) { - labels := Labels{ + labels := commonModel.Labels{ "my.name": "myValue", } - value, err := labels.exist("my.name") + value, err := labels.Exist("my.name") assert.NoError(t, err) assert.Equal(t, "myValue", value) } func TestExistError(t *testing.T) { - _, err := Labels{}.exist("my.name") + _, err := commonModel.Labels{}.Exist("my.name") assert.EqualError(t, err, "label my.name not found") } func TestToSize(t *testing.T) { - labels := Labels{ + labels := commonModel.Labels{ "com.hckops.box.size": "xl", } - size, err := labels.ToBoxSize() + size, err := ToBoxSize(labels) assert.NoError(t, err) - assert.Equal(t, model.ExtraLarge, size) + assert.Equal(t, ExtraLarge, size) } func TestToSizeError(t *testing.T) { - _, errLabel := Labels{}.ToBoxSize() + _, errLabel := ToBoxSize(commonModel.Labels{}) assert.EqualError(t, errLabel, "label com.hckops.box.size not found") - _, errSize := Labels{"com.hckops.box.size": "abc"}.ToBoxSize() + _, errSize := ToBoxSize(commonModel.Labels{"com.hckops.box.size": "abc"}) assert.EqualError(t, errSize, "invalid resource size value=abc") } func TestToCachedTemplateInfo(t *testing.T) { - info := NewBoxLabels().AddDefaultLocal(). - AddBoxSize(model.Small). + info := AddBoxSize(NewBoxLabels().AddDefaultLocal(), Small). AddLocal("/tmp/cache"). ToCachedTemplateInfo() - expected := &CachedTemplateInfo{ + expected := &commonModel.CachedTemplateInfo{ Path: "/tmp/cache", } @@ -128,12 +127,11 @@ func TestToCachedTemplateInfo(t *testing.T) { } func TestToBoxTemplateInfo(t *testing.T) { - info := NewBoxLabels().AddDefaultGit("myUrl", "myRevision", "myDir"). - AddBoxSize(model.Medium). + info := AddBoxSize(NewBoxLabels().AddDefaultGit("myUrl", "myRevision", "myDir"), Medium). AddGit("myDir/myName", "myCommit"). ToGitTemplateInfo() - expected := &GitTemplateInfo{ + expected := &commonModel.GitTemplateInfo{ Url: "myUrl", Revision: "myRevision", Commit: "myCommit", diff --git a/pkg/box/model/types_test.go b/pkg/box/model/types_test.go index febfc86a..4ef92914 100644 --- a/pkg/box/model/types_test.go +++ b/pkg/box/model/types_test.go @@ -4,11 +4,13 @@ import ( "testing" "github.com/stretchr/testify/assert" + + commonModel "github.com/hckops/hckctl/pkg/common/model" ) func TestIsCached(t *testing.T) { info := &BoxTemplateInfo{ - CachedTemplate: &CachedTemplateInfo{ + CachedTemplate: &commonModel.CachedTemplateInfo{ Path: "/tmp/cache", }, } diff --git a/pkg/common/model/labels.go b/pkg/common/model/labels.go index 338a67fa..1163a5be 100644 --- a/pkg/common/model/labels.go +++ b/pkg/common/model/labels.go @@ -6,9 +6,6 @@ import ( "strings" "golang.org/x/exp/maps" - - boxModel "github.com/hckops/hckctl/pkg/box/model" - "github.com/hckops/hckctl/pkg/schema" ) type Labels map[string]string @@ -24,27 +21,19 @@ const ( LabelTemplateGitDir = "com.hckops.template.git.dir" LabelTemplateGitName = "com.hckops.template.git.name" LabelTemplateCachePath = "com.hckops.template.cache.path" - LabelBoxSize = "com.hckops.box.size" - LabelTaskSize = "com.hckops.task.size" // TODO not used ) -func NewBoxLabels() Labels { - return map[string]string{ - LabelSchemaKind: schema.KindBoxV1.String(), - } +func (l Labels) AddLabel(key string, value string) Labels { + return l.AddLabels(Labels{key: value}) } -func (l Labels) addLabel(key string, value string) Labels { - return l.addLabels(Labels{key: value}) -} - -func (l Labels) addLabels(labels Labels) Labels { +func (l Labels) AddLabels(labels Labels) Labels { // merge labels maps.Copy(labels, l) return labels } -func (l Labels) exist(name string) (string, error) { +func (l Labels) Exist(name string) (string, error) { if label, ok := l[name]; !ok { return "", fmt.Errorf("label %s not found", name) } else { @@ -53,11 +42,11 @@ func (l Labels) exist(name string) (string, error) { } func (l Labels) AddDefaultLocal() Labels { - return l.addLabel(LabelTemplateLocal, "true") + return l.AddLabel(LabelTemplateLocal, "true") } func (l Labels) AddDefaultGit(url, revision, dir string) Labels { - return l.addLabels(Labels{ + return l.AddLabels(Labels{ LabelTemplateGit: "true", LabelTemplateGitUrl: url, LabelTemplateGitRevision: revision, @@ -66,14 +55,14 @@ func (l Labels) AddDefaultGit(url, revision, dir string) Labels { } func (l Labels) AddLocal(path string) Labels { - if _, err := l.exist(LabelTemplateLocal); err != nil { + if _, err := l.Exist(LabelTemplateLocal); err != nil { return l } - return l.addLabel(LabelTemplateCachePath, path) + return l.AddLabel(LabelTemplateCachePath, path) } func (l Labels) AddGit(path string, commit string) Labels { - if _, err := l.exist(LabelTemplateGit); err != nil { + if _, err := l.Exist(LabelTemplateGit); err != nil { return l } @@ -81,7 +70,7 @@ func (l Labels) AddGit(path string, commit string) Labels { // unsafe assume valid path name := strings.TrimSuffix(strings.TrimPrefix(templatePath[1], "/"), filepath.Ext(path)) - return l.addLabels(Labels{ + return l.AddLabels(Labels{ LabelTemplateGitCommit: commit, LabelTemplateGitName: name, LabelTemplateCachePath: path, @@ -89,14 +78,14 @@ func (l Labels) AddGit(path string, commit string) Labels { } func (l Labels) ToCachedTemplateInfo() *CachedTemplateInfo { - if _, err := l.exist(LabelTemplateLocal); err != nil { + if _, err := l.Exist(LabelTemplateLocal); err != nil { return nil } return &CachedTemplateInfo{Path: l[LabelTemplateCachePath]} } func (l Labels) ToGitTemplateInfo() *GitTemplateInfo { - if _, err := l.exist(LabelTemplateGit); err != nil { + if _, err := l.Exist(LabelTemplateGit); err != nil { return nil } @@ -107,15 +96,3 @@ func (l Labels) ToGitTemplateInfo() *GitTemplateInfo { Name: l[LabelTemplateGitName], } } - -func (l Labels) AddBoxSize(size boxModel.ResourceSize) Labels { - return l.addLabel(LabelBoxSize, strings.ToLower(size.String())) -} - -func (l Labels) ToBoxSize() (boxModel.ResourceSize, error) { - if label, err := l.exist(LabelBoxSize); err != nil { - return boxModel.Small, err - } else { - return boxModel.ExistResourceSize(label) - } -}