this repo has no description
fork

Configure Feed

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

cmd/cue: implement cue mod init --source

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>

+77 -21
+1
cmd/cue/cmd/flags.go
··· 45 45 flagRecursive flagName = "recursive" 46 46 flagSchema flagName = "schema" 47 47 flagSimplify flagName = "simplify" 48 + flagSource flagName = "source" 48 49 flagStrict flagName = "strict" 49 50 flagTrace flagName = "trace" 50 51 flagVerbose flagName = "verbose"
+6 -6
cmd/cue/cmd/modedit.go
··· 108 108 } 109 109 110 110 func (c *modEditCmd) flagSource(arg string) error { 111 - if arg != "git" && arg != "self" { 112 - return fmt.Errorf("unrecognized source kind %q", arg) 111 + src := &modfile.Source{ 112 + Kind: arg, 113 + } 114 + if err := src.Validate(); err != nil { 115 + return err 113 116 } 114 117 c.addEdit(func(f *modfile.File) error { 115 - if f.Source == nil { 116 - f.Source = &modfile.Source{} 117 - } 118 - f.Source.Kind = arg 118 + f.Source = src 119 119 return nil 120 120 }) 121 121 return nil
+9 -1
cmd/cue/cmd/modinit.go
··· 46 46 } 47 47 48 48 cmd.Flags().BoolP(string(flagForce), "f", false, "force moving old-style cue.mod file") 49 - 49 + cmd.Flags().String(string(flagSource), "", "set the source field") 50 50 return cmd 51 51 } 52 52 ··· 86 86 } 87 87 mf := &modfile.File{ 88 88 Module: modulePath, 89 + } 90 + if s := flagSource.String(cmd); s != "" { 91 + mf.Source = &modfile.Source{ 92 + Kind: s, 93 + } 94 + if err := mf.Source.Validate(); err != nil { 95 + return err 96 + } 89 97 } 90 98 vers := versionForModFile() 91 99 if vers == "" {
+45
cmd/cue/cmd/testdata/script/modinit_withsource.txtar
··· 1 + # Test that we can use the --source flag to cue mod init. 2 + 3 + env CUE_VERSION_OVERRIDE=v0.9.0-alpha.2 4 + 5 + mkdir $WORK/test0 6 + cd $WORK/test0 7 + exec cue mod init --source self test.example 8 + cmp cue.mod/module.cue $WORK/want-module.cue-0 9 + 10 + mkdir $WORK/test1 11 + cd $WORK/test1 12 + exec cue mod init --source git test.example 13 + cmp cue.mod/module.cue $WORK/want-module.cue-1 14 + 15 + mkdir $WORK/test2 16 + cd $WORK/test2 17 + ! exec cue mod init --source bad test.example 18 + cmp stderr $WORK/want-stderr 19 + 20 + -- want-module.cue-0 -- 21 + module: "test.example" 22 + language: { 23 + version: "v0.9.0-alpha.2" 24 + } 25 + source: { 26 + kind: "self" 27 + } 28 + -- want-module.cue-0 -- 29 + module: "test.example" 30 + language: { 31 + version: "v0.9.0-alpha.2" 32 + } 33 + source: { 34 + kind: "self" 35 + } 36 + -- want-module.cue-1 -- 37 + module: "test.example" 38 + language: { 39 + version: "v0.9.0-alpha.2" 40 + } 41 + source: { 42 + kind: "git" 43 + } 44 + -- want-stderr -- 45 + unrecognized source kind "bad"
+2 -4
internal/_e2e/testdata/script/gcloud_upload.txtar
··· 11 11 12 12 cd publish 13 13 14 - exec cue mod init ${MODVER} 15 - exec cue mod edit --source self 14 + exec cue mod init --source self ${MODVER} 16 15 exec cue mod publish ${VERSION} 17 16 18 17 cd ../depend 19 18 20 19 env-fill out_foo.cue 21 - exec cue mod init depend.localhost 22 - exec cue mod edit --source self 20 + exec cue mod init --source self depend.localhost 23 21 exec cue mod tidy 24 22 exec cue export 25 23 cmp stdout export.golden
+2 -4
internal/_e2e/testdata/script/github_app_private.txtar
··· 9 9 10 10 cd publish 11 11 12 - exec cue mod init ${MODVER} 13 - exec cue mod edit --source self 12 + exec cue mod init --source self ${MODVER} 14 13 exec cue mod publish ${VERSION} 15 14 16 15 cd ../depend 17 16 18 17 env-fill out_foo.cue 19 - exec cue mod init depend.localhost 20 - exec cue mod edit --source self 18 + exec cue mod init --source self depend.localhost 21 19 exec cue mod tidy 22 20 exec cue export 23 21 cmp stdout export.golden
+3 -6
internal/_e2e/testdata/script/github_app_public.txtar
··· 9 9 10 10 cd publish 11 11 12 - exec cue mod init ${MODVER} 13 - exec cue mod edit --source self 12 + exec cue mod init --source self ${MODVER} 14 13 exec cue mod publish ${VERSION} 15 14 16 15 cd ../depend 17 16 18 17 env-fill out_foo.cue 19 - exec cue mod init depend.localhost 20 - exec cue mod edit --source self 18 + exec cue mod init --source self depend.localhost 21 19 exec cue mod tidy 22 20 exec cue export 23 21 cmp stdout export.golden ··· 35 33 # so it's not a stable string. We should give the user a short and stable error, 36 34 # and test for it here with a regular expression. 37 35 cd ../publish-different 38 - exec cue mod init ${MODVER} 39 - exec cue mod edit --source self 36 + exec cue mod init --source self ${MODVER} 40 37 ! exec cue mod publish ${VERSION} 41 38 stderr 'cannot tag.*400 Bad Request' 42 39
+9
mod/modfile/modfile.go
··· 73 73 Kind string `json:"kind"` 74 74 } 75 75 76 + // Validate checks that src is well formed. 77 + func (src *Source) Validate() error { 78 + switch src.Kind { 79 + case "git", "self": 80 + return nil 81 + } 82 + return fmt.Errorf("unrecognized source kind %q", src.Kind) 83 + } 84 + 76 85 // Format returns a formatted representation of f 77 86 // in CUE syntax. 78 87 func (f *File) Format() ([]byte, error) {