Skip to content

Commit ba5708b

Browse files
myitcvmpvl
authored andcommitted
cmd/cue: get go: add --package flag
Consider the following scenario: example.com/blah is a command (i.e. Go main package) that is configured via CUE files passed via -config flags. The author of the tool wants to make the CUE definitions used to validate the config available publicly and hence runs cue get go --local as part of a go:generate directive. Currently this results in *_gen.cue files which have a package name of main. This isn't particularly useful for a consumer of this CUE package because the import path is example.com/blah, hence an import would need to look like example.com/blah:main. Instead we now offer a --package (-p) flag for the get go command which allows overriding of the package name for the generated CUE files. Change-Id: Id04f0403b962c4f35da64a676a50d641f18a1eac Reviewed-on: https://cue-review.googlesource.com/c/cue/+/6801 Reviewed-by: Marcel van Lohuizen <[email protected]> Reviewed-by: CUE cueckoo <[email protected]>
1 parent 1a9b88d commit ba5708b

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

cmd/cue/cmd/get_go.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ the more restrictive enum interpretation of Switch remains.
214214
cmd.Flags().Bool(string(flagLocal), false,
215215
"generates files in the main module locally")
216216

217+
cmd.Flags().StringP(string(flagPackage), "p", "", "package name for generated CUE files")
218+
217219
return cmd
218220
}
219221

@@ -461,7 +463,12 @@ func (e *extractor) extractPkg(root string, p *packages.Package) error {
461463
}
462464
sort.Strings(pkgs)
463465

464-
pkg := &cueast.Package{Name: e.ident(p.Name, false)}
466+
pName := flagPackage.String(e.cmd)
467+
if pName == "" {
468+
pName = p.Name
469+
}
470+
471+
pkg := &cueast.Package{Name: e.ident(pName, false)}
465472
addDoc(f.Doc, pkg)
466473

467474
f := &cueast.File{Decls: []cueast.Decl{

cmd/cue/cmd/testdata/script/get_go_basic.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ env LOCALAPPDATA=$WORK${/}appdata
99
cue get go --local
1010
cmp blah_go_gen.cue all.cue.golden
1111

12+
# Use an alternative package name
13+
cue get go --local -p other
14+
cmp blah_go_gen.cue other.cue.golden
15+
1216
-- go.mod --
1317
module mod.com/blah
1418
-- blah.go --
@@ -43,3 +47,19 @@ package main
4347

4448
#LongStringConst: "This is a really long string. Why are we using a long string? Because that way it ensures we are using go/constant.Value.ExactString() instead of go/constant.Value.String()"
4549
#IntConst: "test"
50+
-- other.cue.golden --
51+
// Code generated by cue get go. DO NOT EDIT.
52+
53+
//cue:generate cue get go mod.com/blah
54+
55+
package other
56+
57+
#S: {
58+
Name: string
59+
T: #T
60+
}
61+
62+
#T: Age: int
63+
64+
#LongStringConst: "This is a really long string. Why are we using a long string? Because that way it ensures we are using go/constant.Value.ExactString() instead of go/constant.Value.String()"
65+
#IntConst: "test"

0 commit comments

Comments
 (0)