this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

cmd/cue: support explicit encodings in `exp writefs`

So that one can write a file with an encoding that isn't derived
from the filename extension, much like `@embed` supports a "type" flag.
Note that we don't use the field name "type" here,
as that is already used to distinguish regular files from symlinks.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: I60ba9a0efade0fa62fb4354d5868d7e77baa796e
Reviewed-on: https://cue.gerrithub.io/c/cue-lang/cue/+/1232054
Reviewed-by: Paul Jolly <paul@myitcv.io>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>

+16 -9
+9 -6
cmd/cue/cmd/exp.go
··· 158 158 } | *{ 159 159 type: "file" 160 160 161 - // If filepath has a supported file extension, such as .yaml or .json, 162 - // this is an arbitrary concrete value written in that encoding. 163 - // Otherwise, this is a string written as-is. 161 + // encoding can be set to a filetype like "text", "json", or "yaml" 162 + // to control how the arbitrary concrete value in contents is encoded. 163 + // When unset, the filepath extension is used to infer an encoding, 164 + // with a fallback to "text" for filepaths with no extension. 165 + encoding?: string 164 166 contents!: _ 165 167 } 166 168 ··· 186 188 187 189 type writefsFile struct { 188 190 Type string `json:"type"` 191 + Encoding string `json:"encoding"` 189 192 Contents json.RawMessage `json:"contents"` 190 193 } 191 194 ··· 240 243 return fmt.Errorf("failed to symlink %s -> %s: %v", fp, target, err) 241 244 } 242 245 case "file", "": // empty if omitted, as it's the default 243 - fenc := "" 244 - if filepath.Ext(fp) == "" { 245 - // Fall back to text when there is no extension, which is a useful default 246 + fenc := f.Encoding 247 + if fenc == "" && filepath.Ext(fp) == "" { 248 + // Fall back to text when there is no encoding nor extension, which is a useful default 246 249 // without causing issues when we add more encodings with extensions in the future. 247 250 fenc = "text" 248 251 }
+2
cmd/cue/cmd/testdata/script/exp_writefs.txtar
··· 11 11 cmp out/foo.toml expect-toml 12 12 cmp out/foo.txt expect-txt 13 13 cmp out/foo-noext expect-txt 14 + cmp out/foo.custom expect-yaml 14 15 cmp out/link.yaml expect-yaml 15 16 cmp out/sub/dir/foo.yaml expect-nested-yaml 16 17 ··· 38 39 "out/foo.toml": {contents: foo: "as toml"} 39 40 "out/foo.txt": {contents: "plain text"} 40 41 "out/foo-noext": {contents: "plain text"} 42 + "out/foo.custom": {encoding: "yaml", contents: foo: "as yaml"} 41 43 "out/link.yaml": {type: "symlink", contents: "foo.yaml"} 42 44 "out/sub/dir/foo.yaml": {type: "file", contents: foo: "in nested dir"} 43 45 }
+5 -3
internal/ci/base/write.cue
··· 64 64 // and this carries forward the semantics we've had in a few repos for a while. 65 65 type: "file" 66 66 67 - // If filepath has a supported file extension, such as .yaml or .json, 68 - // this is an arbitrary concrete value written in that encoding. 69 - // Otherwise, this is a string written as-is. 67 + // encoding can be set to a filetype like "text", "json", or "yaml" 68 + // to control how the arbitrary concrete value in contents is encoded. 69 + // When unset, the filepath extension is used to infer an encoding, 70 + // with a fallback to "text" for filepaths with no extension. 71 + encoding?: string 70 72 contents!: _ 71 73 } 72 74 }