You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
runner-go/labels/labels.go

86 lines
2.2 KiB
Go

// Copyright 2019 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by the Polyform License
// that can be found in the LICENSE file.
package labels
import (
"fmt"
"time"
"git.awesome-for.me/liuzhiguo/drone-go/drone"
)
// now returns the current time.
var now = time.Now
// FromRepo returns container labels derived from the
// Repository metadata.
func FromRepo(v *drone.Repo) map[string]string {
return map[string]string{
"io.drone.repo.namespace": v.Namespace,
"io.drone.repo.name": v.Name,
"io.drone.repo.slug": v.Slug,
}
}
// FromBuild returns container labels derived from the
// Build metadata.
func FromBuild(v *drone.Build) map[string]string {
return map[string]string{
"io.drone.build.number": fmt.Sprint(v.Number),
}
}
// FromStage returns container labels derived from the
// Stage metadata.
func FromStage(v *drone.Stage) map[string]string {
return map[string]string{
"io.drone.stage.name": v.Name,
"io.drone.stage.number": fmt.Sprint(v.Number),
}
}
// FromStep returns container labels derived from the
// Step metadata.
func FromStep(v *drone.Step) map[string]string {
return map[string]string{
"io.drone.step.number": fmt.Sprint(v.Number),
"io.drone.step.name": v.Name,
}
}
// FromSystem returns container labels derived from the
// System metadata.
func FromSystem(v *drone.System) map[string]string {
return map[string]string{
"io.drone": "true",
"io.drone.protected": "false",
"io.drone.system.host": v.Host,
"io.drone.system.proto": v.Proto,
"io.drone.system.version": v.Version,
}
}
// WithTimeout returns container labels that define
// timeout and expiration values.
func WithTimeout(v *drone.Repo) map[string]string {
return map[string]string{
"io.drone.ttl": fmt.Sprint(time.Duration(v.Timeout) * time.Minute),
"io.drone.expires": fmt.Sprint(now().Add(time.Duration(v.Timeout)*time.Minute + time.Hour).Unix()),
"io.drone.created": fmt.Sprint(now().Unix()),
}
}
// Combine is a helper function combines one or more maps of
// labels into a single map.
func Combine(labels ...map[string]string) map[string]string {
c := map[string]string{}
for _, e := range labels {
for k, v := range e {
c[k] = v
}
}
return c
}