Skip to content

Commit

Permalink
cmd/cue: get go: add --package flag
Browse files Browse the repository at this point in the history
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 <mpvl@golang.org>
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
  • Loading branch information
myitcv authored and mpvl committed Aug 18, 2020
1 parent 1a9b88d commit ba5708b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
9 changes: 8 additions & 1 deletion cmd/cue/cmd/get_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ the more restrictive enum interpretation of Switch remains.
cmd.Flags().Bool(string(flagLocal), false,
"generates files in the main module locally")

cmd.Flags().StringP(string(flagPackage), "p", "", "package name for generated CUE files")

return cmd
}

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

pkg := &cueast.Package{Name: e.ident(p.Name, false)}
pName := flagPackage.String(e.cmd)
if pName == "" {
pName = p.Name
}

pkg := &cueast.Package{Name: e.ident(pName, false)}
addDoc(f.Doc, pkg)

f := &cueast.File{Decls: []cueast.Decl{
Expand Down
20 changes: 20 additions & 0 deletions cmd/cue/cmd/testdata/script/get_go_basic.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ env LOCALAPPDATA=$WORK${/}appdata
cue get go --local
cmp blah_go_gen.cue all.cue.golden

# Use an alternative package name
cue get go --local -p other
cmp blah_go_gen.cue other.cue.golden

-- go.mod --
module mod.com/blah
-- blah.go --
Expand Down Expand Up @@ -43,3 +47,19 @@ package main

#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()"
#IntConst: "test"
-- other.cue.golden --
// Code generated by cue get go. DO NOT EDIT.

//cue:generate cue get go mod.com/blah

package other

#S: {
Name: string
T: #T
}

#T: Age: int

#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()"
#IntConst: "test"

0 comments on commit ba5708b

Please sign in to comment.