-
Notifications
You must be signed in to change notification settings - Fork 2
/
reference.go
86 lines (77 loc) · 2.11 KB
/
reference.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
package main
import (
"encoding/json"
"fmt"
"gopkg.in/yaml.v3"
"os"
"strings"
"text/tabwriter"
)
type DockerfileFromReference struct {
Reference string `json:"image,omitempty" yaml:"image"`
Path string `json:"path,omitempty"`
Branch string `json:"branch,omitempty"`
Newer []string `json:"newer,omitempty"`
}
type DockerfileFromReferences []*DockerfileFromReference
func (r DockerfileFromReferences) ExtractReferences() []string {
refs := make(map[string]bool, len(r))
for _, reference := range r {
refs[reference.Reference] = true
}
names := make([]string, 0, len(refs))
for name, _ := range refs {
names = append(names, name)
}
return names
}
func (r DockerfileFromReferences) OutputOnlyReferences(format string, noHeader bool) {
result := r.ExtractReferences()
if format == "json" {
encoder := json.NewEncoder(os.Stdout)
_ = encoder.Encode(r)
} else if format == "yaml" {
encoder := yaml.NewEncoder(os.Stdout)
_ = encoder.Encode(r)
} else {
w := tabwriter.NewWriter(os.Stdout, 1, 8, 0, '\t', tabwriter.TabIndent)
if !noHeader {
fmt.Fprintf(w, "%s\n", "REFERENCE")
}
for _, reference := range result {
fmt.Fprintf(w, "%s\n", reference)
}
w.Flush()
}
}
func (r DockerfileFromReferences) Output(format string, noHeader bool) {
if format == "json" {
encoder := json.NewEncoder(os.Stdout)
_ = encoder.Encode(r)
} else if format == "yaml" {
encoder := yaml.NewEncoder(os.Stdout)
_ = encoder.Encode(r)
} else {
w := tabwriter.NewWriter(os.Stdout, 1, 8, 1, '\t', tabwriter.TabIndent)
if !noHeader {
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", "IMAGE", "PATH", "BRANCH", "NEWER")
}
for _, reference := range r {
var newer = "-"
if reference.Newer != nil {
newer = strings.Join(reference.Newer, ",")
}
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", reference.Reference, reference.Path, reference.Branch, newer)
}
w.Flush()
}
}
func (r DockerfileFromReferences) FilterOutOfDate() DockerfileFromReferences {
result := make(DockerfileFromReferences, 0, len(r))
for _, ref := range r {
if len(ref.Newer) > 0 {
result = append(result, ref)
}
}
return result
}