-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
147 lines (125 loc) · 3.27 KB
/
main.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/alecthomas/kong"
"github.com/meigma/omashu/cli/pkg"
"github.com/meigma/omashu/cli/pkg/parsers"
"github.com/meigma/omashu/cli/pkg/scanners"
"github.com/spf13/afero"
)
var cli struct {
Images imagesCmd `cmd:"" help:"Find the images generated by an Earthfile target."`
Scan scanCmd `cmd:"" help:"Scan for Earthfiles."`
}
type imagesCmd struct {
JSONOutput bool `short:"j" long:"json" help:"Output in JSON format"`
Path string ` help:"path to Earthfile" arg:"" type:"path"`
Target string `short:"t" help:"The target to search for images" required:"true"`
}
func (c *imagesCmd) Run() error {
parser := parsers.NewEarthlyParser()
earthfile, err := parser.Parse(c.Path)
if err != nil {
return err
}
images, err := earthfile.GetImages(c.Target)
if err != nil {
return err
}
if c.JSONOutput {
jsonImages, err := json.Marshal(images)
if err != nil {
return err
}
fmt.Println(string(jsonImages))
return nil
}
for _, image := range images {
fmt.Println(image)
}
return nil
}
type scanCmd struct {
JSONOutput bool `short:"j" long:"json" help:"Output in JSON format"`
Images bool `short:"i" long:"images" help:"Also output images for the target of each Earthfile (requires -t option)"`
Paths []string ` help:"paths to scan for Earthfiles" arg:"" type:"path"`
Target string `short:"t" help:"filter by Earthfiles that include this target" default:""`
}
func (c *scanCmd) Run() error {
parser := parsers.NewEarthlyParser()
scanner := scanners.NewFileScanner(c.Paths, parser, afero.NewOsFs())
var files []pkg.Earthfile
var err error
if c.Target != "" {
files, err = scanner.ScanForTarget(c.Target)
} else {
files, err = scanner.Scan()
}
if err != nil {
return err
}
if c.Images {
if c.Target == "" {
return fmt.Errorf(
"the --images (-i) option requires the --target (-t) option",
)
}
var output = make(map[string][]string)
for _, file := range files {
images, err := file.GetImages(c.Target)
if err != nil {
return err
}
output[filepath.Dir(file.Path)] = images
}
if c.JSONOutput {
var outFinal []interface{}
for path, images := range output {
out := struct {
Images []string `json:"images"`
Path string `json:"path"`
}{
Images: images,
Path: path,
}
outFinal = append(outFinal, out)
}
jsonOutput, err := json.Marshal(outFinal)
if err != nil {
return err
}
fmt.Println(string(jsonOutput))
} else {
for path, images := range output {
fmt.Printf("%s %s\n", path, strings.Join(images, ","))
}
}
return nil
}
if c.JSONOutput {
var paths []string
for _, file := range files {
paths = append(paths, filepath.Dir(file.Path))
}
jsonFiles, err := json.Marshal(paths)
if err != nil {
return err
}
fmt.Println(string(jsonFiles))
} else {
for _, file := range files {
fmt.Println(filepath.Dir(file.Path))
}
}
return nil
}
func main() {
ctx := kong.Parse(&cli)
err := ctx.Run()
ctx.FatalIfErrorf(err)
os.Exit(0)
}