|
2 | 2 | package imagemeta |
3 | 3 |
|
4 | 4 | import ( |
| 5 | + "bytes" |
5 | 6 | "encoding/json" |
| 7 | + "fmt" |
| 8 | + "slices" |
6 | 9 |
|
7 | 10 | "github.com/devcontainers/cli/internal/log" |
8 | 11 | ) |
@@ -41,6 +44,64 @@ type Entry struct { |
41 | 44 | Entrypoint string `json:"entrypoint,omitempty"` |
42 | 45 | } |
43 | 46 |
|
| 47 | +// featureEntryKeys is the order TS writes a Feature's entry in: id, then |
| 48 | +// pickFeatureProperties. |
| 49 | +var featureEntryKeys = []string{ |
| 50 | + "id", "onCreateCommand", "updateContentCommand", "postCreateCommand", "postStartCommand", "postAttachCommand", |
| 51 | + "init", "privileged", "capAdd", "securityOpt", "entrypoint", "mounts", "customizations", |
| 52 | +} |
| 53 | + |
| 54 | +// configEntryKeys is the order TS writes the configuration's entry in |
| 55 | +// (pickConfigProperties); entrypoint, which only a Feature's entry has, last. |
| 56 | +var configEntryKeys = []string{ |
| 57 | + "onCreateCommand", "updateContentCommand", "postCreateCommand", "postStartCommand", "postAttachCommand", |
| 58 | + "waitFor", "customizations", "mounts", "containerEnv", "containerUser", "init", "privileged", "capAdd", |
| 59 | + "securityOpt", "remoteUser", "userEnvProbe", "remoteEnv", "overrideCommand", "portsAttributes", |
| 60 | + "otherPortsAttributes", "forwardPorts", "shutdownAction", "updateRemoteUserUID", "hostRequirements", |
| 61 | + "entrypoint", |
| 62 | +} |
| 63 | + |
| 64 | +// MarshalJSON writes the entry's properties in the order TS writes them: a |
| 65 | +// Feature's entry (one with an id) in pickFeatureProperties order, then the |
| 66 | +// other properties in configEntryKeys order; any other entry in |
| 67 | +// pickConfigProperties order (configEntryKeys names every property but id). |
| 68 | +// Object values are written with their keys sorted (a Go map keeps no order). |
| 69 | +// &, < and > are not escaped, as JSON.stringify does not escape them. |
| 70 | +func (e Entry) MarshalJSON() ([]byte, error) { |
| 71 | + type plain Entry |
| 72 | + var data bytes.Buffer |
| 73 | + enc := json.NewEncoder(&data) |
| 74 | + enc.SetEscapeHTML(false) |
| 75 | + if err := enc.Encode(plain(e)); err != nil { |
| 76 | + return nil, err |
| 77 | + } |
| 78 | + var fields map[string]json.RawMessage |
| 79 | + if err := json.Unmarshal(data.Bytes(), &fields); err != nil { |
| 80 | + return nil, err |
| 81 | + } |
| 82 | + order := configEntryKeys |
| 83 | + if e.ID != "" { |
| 84 | + order = append(slices.Clone(featureEntryKeys), configEntryKeys...) |
| 85 | + } |
| 86 | + var out bytes.Buffer |
| 87 | + out.WriteByte('{') |
| 88 | + written := 0 |
| 89 | + for _, k := range order { |
| 90 | + v, ok := fields[k] |
| 91 | + if !ok { |
| 92 | + continue |
| 93 | + } |
| 94 | + delete(fields, k) // a Feature's order names some keys twice |
| 95 | + if written > 0 { |
| 96 | + out.WriteByte(',') |
| 97 | + } |
| 98 | + fmt.Fprintf(&out, "%q:%s", k, v) |
| 99 | + written++ |
| 100 | + } |
| 101 | + out.WriteByte('}') |
| 102 | + return out.Bytes(), nil |
| 103 | +} |
| 104 | + |
44 | 105 | // ReadMetadataFromLabels extracts metadata entries from Docker image/container labels. |
45 | 106 | func ReadMetadataFromLabels(labels map[string]string, logger log.Logger) []Entry { |
46 | 107 | raw, ok := labels[MetadataLabel] |
@@ -74,7 +135,10 @@ func GenerateMetadataLabel(entries []Entry) string { |
74 | 135 | } |
75 | 136 | // 0.88 (#1199): always write the devcontainer.metadata label as a JSON array, |
76 | 137 | // even for a single entry — tools that expect an array (e.g. Zed) fail on a |
77 | | - // bare object. |
78 | | - data, _ := json.Marshal(entries) |
79 | | - return string(data) |
| 138 | + // bare object. JSON.stringify leaves &, < and > as they are. |
| 139 | + var data bytes.Buffer |
| 140 | + enc := json.NewEncoder(&data) |
| 141 | + enc.SetEscapeHTML(false) |
| 142 | + _ = enc.Encode(entries) // an Entry holds JSON values only |
| 143 | + return string(bytes.TrimSuffix(data.Bytes(), []byte("\n"))) |
80 | 144 | } |
0 commit comments