Refactor into package structure

This commit is contained in:
2020-11-11 01:46:48 +01:00
parent 04fa50d9ae
commit 27993e9fa0
13 changed files with 329 additions and 179 deletions

View File

@ -0,0 +1,56 @@
package docker
import (
"fmt"
"strings"
)
// Extract the repository owner (if any), repository and tag (if any) from a docker image name
func (lc LabeledContainer) SplitImageParts() (*string, string, *string) {
name := lc.Container.Image
var repository string
var owner *string
var tag *string
slashIndex := strings.Index(name, "/")
if slashIndex >= 0 {
tmp := name[:slashIndex]
owner = &tmp
name = name[slashIndex+1:]
}
colonIndex := strings.Index(name, ":")
if colonIndex >= 0 {
tmp := name[colonIndex+1:]
tag = &tmp
repository = name[:colonIndex]
} else {
repository = name
}
return owner, repository, tag
}
func (lc LabeledContainer) GetName() string {
if len(lc.Container.Names) >= 0 {
// trim prefixed "/"
return lc.Container.Names[0][1:]
} else {
return lc.Container.ID[:10]
}
}
func CombineImageParts(owner *string, repository string, tag *string) string {
image := repository
if owner != nil {
image = fmt.Sprintf("%s/%s", *owner, image)
}
if tag != nil {
image = fmt.Sprintf("%s:%s", image, *tag)
}
return image
}