github.com/drone/runner-go@v1.12.0/labels/labels.go (about) 1 // Copyright 2019 Drone.IO Inc. All rights reserved. 2 // Use of this source code is governed by the Polyform License 3 // that can be found in the LICENSE file. 4 5 package labels 6 7 import ( 8 "fmt" 9 "time" 10 11 "github.com/drone/drone-go/drone" 12 ) 13 14 // now returns the current time. 15 var now = time.Now 16 17 // FromRepo returns container labels derived from the 18 // Repository metadata. 19 func FromRepo(v *drone.Repo) map[string]string { 20 return map[string]string{ 21 "io.drone.repo.namespace": v.Namespace, 22 "io.drone.repo.name": v.Name, 23 "io.drone.repo.slug": v.Slug, 24 } 25 } 26 27 // FromBuild returns container labels derived from the 28 // Build metadata. 29 func FromBuild(v *drone.Build) map[string]string { 30 return map[string]string{ 31 "io.drone.build.number": fmt.Sprint(v.Number), 32 } 33 } 34 35 // FromStage returns container labels derived from the 36 // Stage metadata. 37 func FromStage(v *drone.Stage) map[string]string { 38 return map[string]string{ 39 "io.drone.stage.name": v.Name, 40 "io.drone.stage.number": fmt.Sprint(v.Number), 41 } 42 } 43 44 // FromStep returns container labels derived from the 45 // Step metadata. 46 func FromStep(v *drone.Step) map[string]string { 47 return map[string]string{ 48 "io.drone.step.number": fmt.Sprint(v.Number), 49 "io.drone.step.name": v.Name, 50 } 51 } 52 53 // FromSystem returns container labels derived from the 54 // System metadata. 55 func FromSystem(v *drone.System) map[string]string { 56 return map[string]string{ 57 "io.drone": "true", 58 "io.drone.protected": "false", 59 "io.drone.system.host": v.Host, 60 "io.drone.system.proto": v.Proto, 61 "io.drone.system.version": v.Version, 62 } 63 } 64 65 // WithTimeout returns container labels that define 66 // timeout and expiration values. 67 func WithTimeout(v *drone.Repo) map[string]string { 68 return map[string]string{ 69 "io.drone.ttl": fmt.Sprint(time.Duration(v.Timeout) * time.Minute), 70 "io.drone.expires": fmt.Sprint(now().Add(time.Duration(v.Timeout)*time.Minute + time.Hour).Unix()), 71 "io.drone.created": fmt.Sprint(now().Unix()), 72 } 73 } 74 75 // Combine is a helper function combines one or more maps of 76 // labels into a single map. 77 func Combine(labels ...map[string]string) map[string]string { 78 c := map[string]string{} 79 for _, e := range labels { 80 for k, v := range e { 81 c[k] = v 82 } 83 } 84 return c 85 }