-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
181 lines (153 loc) · 4.7 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
Copyright 2025 Chainguard, Inc.
SPDX-License-Identifier: Apache-2.0
*/
package main
import (
"bytes"
"context"
_ "embed"
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
"github.com/chainguard-dev/dfc/pkg/dfc"
)
//go:embed packages.yaml
var packagesYamlBytes []byte
var (
// Version is the semantic version (added at compile time via -X main.Version=$VERSION)
Version string
// Revision is the git commit id (added at compile time via -X main.Revision=$REVISION)
Revision string
)
func main() {
// inspired by https://github.com/jonjohnsonjr/apkrane/blob/main/main.go
if err := cli().ExecuteContext(context.Background()); err != nil {
log.Fatal(err)
}
}
func cli() *cobra.Command {
var j bool
var inPlace bool
var org string
var registry string
var mappingsFile string
v := "dev"
if Version != "" {
v = Version
if Revision != "" {
v += fmt.Sprintf(" (%s)", Revision)
}
}
cmd := &cobra.Command{
Use: "dfc",
Example: "dfc <path_to_dockerfile>",
Args: cobra.ExactArgs(1),
Version: v,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
// Allow for piping into the CLI if first arg is "-"
input := cmd.InOrStdin()
isFile := args[0] != "-"
var path string
if isFile {
path = args[0]
file, err := os.Open(filepath.Clean(path))
if err != nil {
return fmt.Errorf("failed open file: %s: %w", path, err)
}
defer file.Close()
input = file
}
buf := new(bytes.Buffer)
if _, err := buf.ReadFrom(input); err != nil {
return fmt.Errorf("failed to read input: %w", err)
}
raw := buf.Bytes()
// Use dfc2 to parse the Dockerfile
dockerfile, err := dfc.ParseDockerfile(ctx, raw)
if err != nil {
return fmt.Errorf("unable to parse dockerfile: %w", err)
}
// Try to parse and merge additional mappings from packages.yaml or custom mappings file
var packageMap dfc.PackageMap
var mappingsBytes []byte
// Use custom mappings file if provided
if mappingsFile != "" {
var err error
mappingsBytes, err = os.ReadFile(mappingsFile)
if err != nil {
return fmt.Errorf("reading mappings file %s: %w", mappingsFile, err)
}
log.Printf("using custom mappings file: %s", mappingsFile)
} else {
// Use embedded packages.yaml
mappingsBytes = packagesYamlBytes
}
if err := yaml.Unmarshal(mappingsBytes, &packageMap); err != nil {
return fmt.Errorf("unmarshalling package mappings: %w", err)
}
// Setup conversion options
opts := dfc.Options{
Organization: org,
Registry: registry,
PackageMap: packageMap,
}
// Convert the Dockerfile
convertedDockerfile, err := dockerfile.Convert(ctx, opts)
if err != nil {
return fmt.Errorf("converting dockerfile: %w", err)
}
// Output the Dockerfile as JSON
if j {
if inPlace {
return fmt.Errorf("unable to use --in-place and --json flag at same time")
}
// Output the Dockerfile as JSON
b, err := json.Marshal(convertedDockerfile)
if err != nil {
return fmt.Errorf("marshalling dockerfile to json: %w", err)
}
fmt.Println(string(b))
return nil
}
// Get the string representation
result := convertedDockerfile.String()
// modify file in place
if inPlace {
if !isFile {
return fmt.Errorf("unable to use --in-place flag when processing stdin")
}
// Get original file info to preserve permissions
fileInfo, err := os.Stat(path)
if err != nil {
return fmt.Errorf("getting file info for %s: %w", path, err)
}
originalMode := fileInfo.Mode().Perm()
backupPath := path + ".bak"
log.Printf("saving dockerfile backup to %s", backupPath)
if err := os.WriteFile(backupPath, raw, originalMode); err != nil {
return fmt.Errorf("saving dockerfile backup to %s: %w", backupPath, err)
}
log.Printf("overwriting %s", path)
if err := os.WriteFile(path, []byte(result), originalMode); err != nil {
return fmt.Errorf("overwriting %s: %w", path, err)
}
return nil
}
// Print to stdout
fmt.Print(result)
return nil
},
}
cmd.Flags().StringVar(&org, "org", dfc.DefaultOrg, "the organization for cgr.dev/<org>/<image> (defaults to ORG)")
cmd.Flags().StringVar(®istry, "registry", "", "an alternate registry and root namepace (e.g. r.example.com/cg-mirror)")
cmd.Flags().BoolVarP(&inPlace, "in-place", "i", false, "modified the Dockerfile in place (vs. stdout), saving original in a .bak file")
cmd.Flags().BoolVarP(&j, "json", "j", false, "print dockerfile as json (before conversion)")
cmd.Flags().StringVarP(&mappingsFile, "mappings", "m", "", "path to a custom package mappings YAML file (instead of the default)")
return cmd
}