2021-09-19 12:00:00 +00:00
|
|
|
// Copyright (c) 2019, Drone IO Inc.
|
|
|
|
// Copyright (c) 2021, Robert Kaussow <mail@thegeeklab.de>
|
2019-02-10 19:00:16 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
package pretty
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
"github.com/drone/drone-yaml/yaml"
|
|
|
|
)
|
|
|
|
|
|
|
|
// helper function pretty prints the container mapping.
|
|
|
|
func printContainer(w writer, v *yaml.Container) {
|
2021-09-19 12:00:00 +00:00
|
|
|
w.IndentIncrease()
|
2019-01-22 23:44:17 +00:00
|
|
|
w.WriteTagValue("name", v.Name)
|
|
|
|
w.WriteTagValue("pull", v.Pull)
|
|
|
|
w.WriteTagValue("image", v.Image)
|
|
|
|
|
|
|
|
if v.Build != nil {
|
|
|
|
printBuild(w, v.Build)
|
|
|
|
}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
if v.Push != nil {
|
|
|
|
w.WriteTagValue("push", v.Push.Image)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteTagValue("detach", v.Detach)
|
2019-04-11 02:31:22 +00:00
|
|
|
w.WriteTagValue("user", v.User)
|
2019-01-22 23:44:17 +00:00
|
|
|
w.WriteTagValue("shell", v.Shell)
|
|
|
|
w.WriteTagValue("entrypoint", v.Entrypoint)
|
|
|
|
w.WriteTagValue("command", v.Command)
|
|
|
|
w.WriteTagValue("commands", v.Commands)
|
|
|
|
w.WriteTagValue("dns", v.DNS)
|
|
|
|
w.WriteTagValue("dns_search", v.DNSSearch)
|
|
|
|
w.WriteTagValue("extra_hosts", v.ExtraHosts)
|
2023-02-08 09:14:20 +00:00
|
|
|
w.WriteTagValue("network_mode", v.NetworkMode)
|
2019-01-22 23:44:17 +00:00
|
|
|
|
|
|
|
if len(v.Settings) > 0 {
|
|
|
|
printSettings(w, v.Settings)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(v.Environment) > 0 {
|
|
|
|
printEnviron(w, v.Environment)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteTagValue("failure", v.Failure)
|
|
|
|
w.WriteTagValue("privileged", v.Privileged)
|
|
|
|
w.WriteTagValue("working_dir", v.WorkingDir)
|
|
|
|
|
|
|
|
if len(v.Devices) > 0 {
|
|
|
|
printDeviceMounts(w, v.Devices)
|
|
|
|
}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
if len(v.Ports) > 0 {
|
|
|
|
printPorts(w, v.Ports)
|
|
|
|
}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
if v.Resources != nil {
|
|
|
|
printResources(w, v.Resources)
|
|
|
|
}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
if len(v.Volumes) > 0 {
|
|
|
|
printVolumeMounts(w, v.Volumes)
|
|
|
|
}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
if !isConditionsEmpty(v.When) {
|
|
|
|
printConditions(w, "when", v.When)
|
|
|
|
}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
if len(v.DependsOn) > 0 {
|
|
|
|
printDependsOn(w, v.DependsOn)
|
|
|
|
}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2022-04-25 10:56:05 +00:00
|
|
|
_ = w.WriteByte('\n')
|
2021-09-19 12:00:00 +00:00
|
|
|
w.IndentDecrease()
|
2019-01-22 23:44:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// helper function pretty prints the build node.
|
|
|
|
func printBuild(w writer, v *yaml.Build) {
|
|
|
|
if shortBuild(v) {
|
|
|
|
w.WriteTagValue("build", v.Image)
|
|
|
|
} else {
|
|
|
|
w.WriteTag("build")
|
|
|
|
w.IndentIncrease()
|
|
|
|
w.WriteTagValue("image", v.Image)
|
|
|
|
w.WriteTagValue("args", v.Args)
|
|
|
|
w.WriteTagValue("cache_from", v.CacheFrom)
|
|
|
|
w.WriteTagValue("context", v.Context)
|
|
|
|
w.WriteTagValue("dockerfile", v.Dockerfile)
|
|
|
|
w.WriteTagValue("labels", v.Labels)
|
|
|
|
w.IndentDecrease()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// helper function pretty prints the depends_on sequence.
|
|
|
|
func printDependsOn(w writer, v []string) {
|
|
|
|
w.WriteTagValue("depends_on", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// helper function pretty prints the device sequence.
|
|
|
|
func printDeviceMounts(w writer, v []*yaml.VolumeDevice) {
|
|
|
|
w.WriteTag("devices")
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
for _, v := range v {
|
|
|
|
s := new(indexWriter)
|
|
|
|
s.writer = w
|
|
|
|
s.IndentIncrease()
|
|
|
|
s.WriteTagValue("name", v.Name)
|
2023-02-08 09:14:20 +00:00
|
|
|
s.WriteTagValue("path", v.Path)
|
2019-01-22 23:44:17 +00:00
|
|
|
s.IndentDecrease()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// helper function pretty prints the environment mapping.
|
|
|
|
func printEnviron(w writer, v map[string]*yaml.Variable) {
|
2023-02-08 09:14:20 +00:00
|
|
|
keys := make([]string, 0)
|
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
for k := range v {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
sort.Strings(keys)
|
|
|
|
|
|
|
|
w.WriteTag("environment")
|
|
|
|
w.IndentIncrease()
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
for _, k := range keys {
|
|
|
|
v := v[k]
|
2023-02-08 09:14:20 +00:00
|
|
|
if v.FromSecret == "" {
|
2019-01-22 23:44:17 +00:00
|
|
|
w.WriteTagValue(k, v.Value)
|
|
|
|
} else {
|
|
|
|
w.WriteTag(k)
|
|
|
|
w.IndentIncrease()
|
2023-02-08 09:14:20 +00:00
|
|
|
w.WriteTagValue("from_secret", v.FromSecret)
|
2019-01-22 23:44:17 +00:00
|
|
|
w.IndentDecrease()
|
|
|
|
}
|
|
|
|
}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
w.IndentDecrease()
|
|
|
|
}
|
|
|
|
|
|
|
|
// helper function pretty prints the port sequence.
|
|
|
|
func printPorts(w writer, v []*yaml.Port) {
|
|
|
|
w.WriteTag("ports")
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
for _, v := range v {
|
|
|
|
if shortPort(v) {
|
2022-04-25 10:56:05 +00:00
|
|
|
_ = w.WriteByte('\n')
|
2019-01-22 23:44:17 +00:00
|
|
|
w.Indent()
|
2022-04-25 10:56:05 +00:00
|
|
|
_ = w.WriteByte('-')
|
|
|
|
_ = w.WriteByte(' ')
|
2019-01-22 23:44:17 +00:00
|
|
|
writeInt(w, v.Port)
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
s := new(indexWriter)
|
|
|
|
s.writer = w
|
|
|
|
s.IndentIncrease()
|
|
|
|
s.WriteTagValue("port", v.Port)
|
|
|
|
s.WriteTagValue("host", v.Host)
|
|
|
|
s.WriteTagValue("protocol", v.Protocol)
|
|
|
|
s.IndentDecrease()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-24 21:31:55 +00:00
|
|
|
// helper function pretty prints the resource mapping.
|
2019-01-22 23:44:17 +00:00
|
|
|
func printResources(w writer, v *yaml.Resources) {
|
|
|
|
w.WriteTag("resources")
|
|
|
|
w.IndentIncrease()
|
|
|
|
|
|
|
|
if v.Limits != nil {
|
|
|
|
w.WriteTag("limits")
|
|
|
|
w.IndentIncrease()
|
|
|
|
w.WriteTagValue("cpu", v.Limits.CPU)
|
|
|
|
w.WriteTagValue("memory", v.Limits.Memory)
|
|
|
|
w.IndentDecrease()
|
|
|
|
}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
if v.Requests != nil {
|
|
|
|
w.WriteTag("requests")
|
|
|
|
w.IndentIncrease()
|
|
|
|
w.WriteTagValue("cpu", v.Requests.CPU)
|
|
|
|
w.WriteTagValue("memory", v.Requests.Memory)
|
|
|
|
w.IndentDecrease()
|
|
|
|
}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
w.IndentDecrease()
|
|
|
|
}
|
|
|
|
|
|
|
|
// helper function pretty prints the resoure mapping.
|
|
|
|
func printSettings(w writer, v map[string]*yaml.Parameter) {
|
2023-02-08 09:14:20 +00:00
|
|
|
keys := make([]string, 0)
|
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
for k := range v {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
sort.Strings(keys)
|
|
|
|
|
|
|
|
w.WriteTag("settings")
|
|
|
|
w.IndentIncrease()
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
for _, k := range keys {
|
|
|
|
v := v[k]
|
2023-02-08 09:14:20 +00:00
|
|
|
if v.FromSecret == "" {
|
2019-09-24 21:31:55 +00:00
|
|
|
w.IncludeZero()
|
2019-01-22 23:44:17 +00:00
|
|
|
w.WriteTagValue(k, v.Value)
|
2019-09-24 21:31:55 +00:00
|
|
|
w.ExcludeZero()
|
2019-01-22 23:44:17 +00:00
|
|
|
} else {
|
|
|
|
w.WriteTag(k)
|
|
|
|
w.IndentIncrease()
|
2023-02-08 09:14:20 +00:00
|
|
|
w.WriteTagValue("from_secret", v.FromSecret)
|
2019-01-22 23:44:17 +00:00
|
|
|
w.IndentDecrease()
|
|
|
|
}
|
|
|
|
}
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
w.IndentDecrease()
|
|
|
|
}
|
|
|
|
|
|
|
|
// helper function pretty prints the volume sequence.
|
|
|
|
func printVolumeMounts(w writer, v []*yaml.VolumeMount) {
|
|
|
|
w.WriteTag("volumes")
|
2023-02-08 09:14:20 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
for _, v := range v {
|
|
|
|
s := new(indexWriter)
|
|
|
|
s.writer = w
|
2021-09-19 12:00:00 +00:00
|
|
|
w.IndentIncrease()
|
2019-01-22 23:44:17 +00:00
|
|
|
s.IndentIncrease()
|
2021-09-19 12:00:00 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
s.WriteTagValue("name", v.Name)
|
2023-02-08 09:14:20 +00:00
|
|
|
s.WriteTagValue("path", v.Path)
|
2021-09-19 12:00:00 +00:00
|
|
|
|
2019-01-22 23:44:17 +00:00
|
|
|
s.IndentDecrease()
|
2021-09-19 12:00:00 +00:00
|
|
|
w.IndentDecrease()
|
2019-01-22 23:44:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// helper function returns true if the Build block should
|
|
|
|
// be printed in short form.
|
|
|
|
func shortBuild(b *yaml.Build) bool {
|
|
|
|
return len(b.Args) == 0 &&
|
|
|
|
len(b.CacheFrom) == 0 &&
|
|
|
|
len(b.Context) == 0 &&
|
|
|
|
len(b.Dockerfile) == 0 &&
|
|
|
|
len(b.Labels) == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// helper function returns true if the Port block should
|
|
|
|
// be printed in short form.
|
|
|
|
func shortPort(p *yaml.Port) bool {
|
|
|
|
return p.Host == 0 && len(p.Protocol) == 0
|
|
|
|
}
|