forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
describer.go
120 lines (110 loc) · 3.5 KB
/
describer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package describe
import (
"bytes"
"fmt"
"io"
"os"
"strings"
"text/tabwriter"
"github.com/openshift/source-to-image/pkg/api"
"github.com/openshift/source-to-image/pkg/build"
)
// Config returns the Config object in nice readable, tabbed format.
func DescribeConfig(config *api.Config) string {
out, err := tabbedString(func(out io.Writer) error {
if len(config.DisplayName) > 0 {
fmt.Fprintf(out, "Application Name:\t%s\n", config.DisplayName)
}
if len(config.Description) > 0 {
fmt.Fprintf(out, "Description:\t%s\n", config.Description)
}
describeBuilderImage(config, config.BuilderImage, out)
fmt.Fprintf(out, "Source:\t%s\n", config.Source)
if len(config.Ref) > 0 {
fmt.Fprintf(out, "Source Ref:\t%s\n", config.Ref)
}
if len(config.ContextDir) > 0 {
fmt.Fprintf(out, "Context Directory:\t%s\n", config.ContextDir)
}
fmt.Fprintf(out, "Output Image Tag:\t%s\n", config.Tag)
printEnv(out, config.Environment)
if len(config.EnvironmentFile) > 0 {
fmt.Fprintf(out, "Environment File:\t%s\n", config.EnvironmentFile)
}
fmt.Fprintf(out, "Incremental Build:\t%s\n", printBool(config.Incremental))
fmt.Fprintf(out, "Remove Old Build:\t%s\n", printBool(config.RemovePreviousImage))
fmt.Fprintf(out, "Force Pull:\t%s\n", printBool(config.ForcePull))
fmt.Fprintf(out, "Quiet:\t%s\n", printBool(config.Quiet))
fmt.Fprintf(out, "Layered Build:\t%s\n", printBool(config.LayeredBuild))
if len(config.Destination) > 0 {
fmt.Fprintf(out, "Artifacts Destination:\t%s\n", config.Destination)
}
if len(config.CallbackURL) > 0 {
fmt.Fprintf(out, "Callback URL:\t%s\n", config.CallbackURL)
}
if len(config.ScriptsURL) > 0 {
fmt.Fprintf(out, "STI Scripts URL:\t%s\n", config.ScriptsURL)
}
if len(config.WorkingDir) > 0 {
fmt.Fprintf(out, "Workdir:\t%s\n", config.WorkingDir)
}
fmt.Fprintf(out, "Docker Endpoint:\t%s\n", config.DockerConfig.Endpoint)
if _, err := os.Open(config.DockerCfgPath); err == nil {
fmt.Fprintf(out, "Docker Pull Config:\t%s\n", config.DockerCfgPath)
fmt.Fprintf(out, "Docker Pull User:\t%s\n", config.PullAuthentication.Username)
}
return nil
})
if err != nil {
fmt.Printf("ERROR: %v", err)
}
return out
}
func describeBuilderImage(config *api.Config, image string, out io.Writer) {
c := &api.Config{
DockerConfig: config.DockerConfig,
PullAuthentication: config.PullAuthentication,
BuilderImage: config.BuilderImage,
ForcePull: config.ForcePull,
Tag: config.Tag,
}
build.GenerateConfigFromLabels(image, c)
if len(c.DisplayName) > 0 {
fmt.Fprintf(out, "Builder Name:\t%s\n", c.DisplayName)
}
fmt.Fprintf(out, "Builder Image:\t%s\n", config.BuilderImage)
if len(c.BuilderImageVersion) > 0 {
fmt.Fprintf(out, "Builder Image Version:\t%s\n", c.BuilderImageVersion)
}
if len(c.BuilderBaseImageVersion) > 0 {
fmt.Fprintf(out, "Builder Base Version:\t%s\n", c.BuilderBaseImageVersion)
}
}
func printEnv(out io.Writer, env map[string]string) {
if len(env) == 0 {
return
}
result := []string{}
for k, v := range env {
result = append(result, fmt.Sprintf("%s=%s", k, v))
}
fmt.Fprintf(out, "Environment:\t%s\n", strings.Join(result, ","))
}
func printBool(b bool) string {
if b {
return "\033[1menabled\033[0m"
}
return "disabled"
}
func tabbedString(f func(io.Writer) error) (string, error) {
out := new(tabwriter.Writer)
buf := &bytes.Buffer{}
out.Init(buf, 0, 8, 1, '\t', 0)
err := f(out)
if err != nil {
return "", err
}
out.Flush()
str := string(buf.String())
return str, nil
}