Skip to content

Commit

Permalink
cmd/cue: implement cue mod init --source
Browse files Browse the repository at this point in the history
It seems to be really common to invoke `cue mod init`
followed by setting the source field, so make that flow
more straightforward by adding a `--source` flag to
`cue mod init`.

Also update the e2e tests to use it.

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: I610a851e05b36bf828a21ff6d7bb13daf3cc748b
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1193590
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
  • Loading branch information
rogpeppe committed Apr 23, 2024
1 parent edda26d commit 2ff5afa
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 21 deletions.
1 change: 1 addition & 0 deletions cmd/cue/cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const (
flagRecursive flagName = "recursive"
flagSchema flagName = "schema"
flagSimplify flagName = "simplify"
flagSource flagName = "source"
flagStrict flagName = "strict"
flagTrace flagName = "trace"
flagVerbose flagName = "verbose"
Expand Down
12 changes: 6 additions & 6 deletions cmd/cue/cmd/modedit.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ func (c *modEditCmd) addEdit(f func(*modfile.File) error) {
}

func (c *modEditCmd) flagSource(arg string) error {
if arg != "git" && arg != "self" {
return fmt.Errorf("unrecognized source kind %q", arg)
src := &modfile.Source{
Kind: arg,
}
if err := src.Validate(); err != nil {
return err
}
c.addEdit(func(f *modfile.File) error {
if f.Source == nil {
f.Source = &modfile.Source{}
}
f.Source.Kind = arg
f.Source = src
return nil
})
return nil
Expand Down
10 changes: 9 additions & 1 deletion cmd/cue/cmd/modinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ in the module.
}

cmd.Flags().BoolP(string(flagForce), "f", false, "force moving old-style cue.mod file")

cmd.Flags().String(string(flagSource), "", "set the source field")
return cmd
}

Expand Down Expand Up @@ -87,6 +87,14 @@ func runModInit(cmd *Command, args []string) (err error) {
mf := &modfile.File{
Module: modulePath,
}
if s := flagSource.String(cmd); s != "" {
mf.Source = &modfile.Source{
Kind: s,
}
if err := mf.Source.Validate(); err != nil {
return err
}
}
vers := versionForModFile()
if vers == "" {
// Shouldn't happen because we should use the
Expand Down
45 changes: 45 additions & 0 deletions cmd/cue/cmd/testdata/script/modinit_withsource.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Test that we can use the --source flag to cue mod init.

env CUE_VERSION_OVERRIDE=v0.9.0-alpha.2

mkdir $WORK/test0
cd $WORK/test0
exec cue mod init --source self test.example
cmp cue.mod/module.cue $WORK/want-module.cue-0

mkdir $WORK/test1
cd $WORK/test1
exec cue mod init --source git test.example
cmp cue.mod/module.cue $WORK/want-module.cue-1

mkdir $WORK/test2
cd $WORK/test2
! exec cue mod init --source bad test.example
cmp stderr $WORK/want-stderr

-- want-module.cue-0 --
module: "test.example"
language: {
version: "v0.9.0-alpha.2"
}
source: {
kind: "self"
}
-- want-module.cue-0 --
module: "test.example"
language: {
version: "v0.9.0-alpha.2"
}
source: {
kind: "self"
}
-- want-module.cue-1 --
module: "test.example"
language: {
version: "v0.9.0-alpha.2"
}
source: {
kind: "git"
}
-- want-stderr --
unrecognized source kind "bad"
6 changes: 2 additions & 4 deletions internal/_e2e/testdata/script/gcloud_upload.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ env MODVER=${MODULE}@v0

cd publish

exec cue mod init ${MODVER}
exec cue mod edit --source self
exec cue mod init --source self ${MODVER}
exec cue mod publish ${VERSION}

cd ../depend

env-fill out_foo.cue
exec cue mod init depend.localhost
exec cue mod edit --source self
exec cue mod init --source self depend.localhost
exec cue mod tidy
exec cue export
cmp stdout export.golden
Expand Down
6 changes: 2 additions & 4 deletions internal/_e2e/testdata/script/github_app_private.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ env MODVER=${MODULE}@v0

cd publish

exec cue mod init ${MODVER}
exec cue mod edit --source self
exec cue mod init --source self ${MODVER}
exec cue mod publish ${VERSION}

cd ../depend

env-fill out_foo.cue
exec cue mod init depend.localhost
exec cue mod edit --source self
exec cue mod init --source self depend.localhost
exec cue mod tidy
exec cue export
cmp stdout export.golden
Expand Down
9 changes: 3 additions & 6 deletions internal/_e2e/testdata/script/github_app_public.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ env MODVER=${MODULE}@v0

cd publish

exec cue mod init ${MODVER}
exec cue mod edit --source self
exec cue mod init --source self ${MODVER}
exec cue mod publish ${VERSION}

cd ../depend

env-fill out_foo.cue
exec cue mod init depend.localhost
exec cue mod edit --source self
exec cue mod init --source self depend.localhost
exec cue mod tidy
exec cue export
cmp stdout export.golden
Expand All @@ -35,8 +33,7 @@ exec cue mod publish ${VERSION}
# so it's not a stable string. We should give the user a short and stable error,
# and test for it here with a regular expression.
cd ../publish-different
exec cue mod init ${MODVER}
exec cue mod edit --source self
exec cue mod init --source self ${MODVER}
! exec cue mod publish ${VERSION}
stderr 'cannot tag.*400 Bad Request'

Expand Down
9 changes: 9 additions & 0 deletions mod/modfile/modfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ type Source struct {
Kind string `json:"kind"`
}

// Validate checks that src is well formed.
func (src *Source) Validate() error {
switch src.Kind {
case "git", "self":
return nil
}
return fmt.Errorf("unrecognized source kind %q", src.Kind)
}

// Format returns a formatted representation of f
// in CUE syntax.
func (f *File) Format() ([]byte, error) {
Expand Down

0 comments on commit 2ff5afa

Please sign in to comment.