From 2d7ff405a2404bc7c5b4379b9c13f6f1ba1c3c7f Mon Sep 17 00:00:00 2001 From: 012e Date: Mon, 8 Aug 2022 19:27:02 +0700 Subject: [PATCH 1/3] auto infer module outside GOPATH --- internal/cli/create/create.go | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/internal/cli/create/create.go b/internal/cli/create/create.go index 2e5131ab..1631d258 100644 --- a/internal/cli/create/create.go +++ b/internal/cli/create/create.go @@ -9,7 +9,6 @@ import ( "github.com/livebud/bud/internal/bail" "github.com/livebud/bud/internal/cli/bud" - "github.com/livebud/bud/internal/format" "github.com/livebud/bud/internal/scaffold" "github.com/livebud/bud/internal/versions" mod "github.com/livebud/bud/package/gomod" @@ -111,14 +110,9 @@ func (c *Command) loadModule() *Module { if module.Name == "" { // Try inferring the module name from the directory module.Name = mod.Infer(c.absDir) + // if can't infer then default it to `changeme` if module.Name == "" { - // Fail that you need to pass in a module path - c.bail.Bail(format.Errorf(` - Unable to infer a module name. Try again using the module name. - - For example, - bud create --module=github.com/my/app %s - `, c.Dir)) + module.Name = "changeme" } } // Autoquote the module name since From 8cd67f30be14b2fe1749ce27e28223e1c239cf79 Mon Sep 17 00:00:00 2001 From: 012e Date: Mon, 8 Aug 2022 19:28:55 +0700 Subject: [PATCH 2/3] fix create test --- internal/cli/create/create_test.go | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/internal/cli/create/create_test.go b/internal/cli/create/create_test.go index 05e489ff..fbe208bd 100644 --- a/internal/cli/create/create_test.go +++ b/internal/cli/create/create_test.go @@ -1,7 +1,9 @@ package create_test import ( + "bufio" "context" + "os" "testing" "github.com/livebud/bud/internal/cli/testcli" @@ -10,7 +12,15 @@ import ( "golang.org/x/mod/modfile" ) -func TestCreateOutsideGoPathError(t *testing.T) { +func fileFirstLine(filePath string) string { + file, _ := os.Open(filePath) + defer file.Close() + scanner := bufio.NewReader(file) + line, _ := scanner.ReadString('\n') + return line +} + +func TestCreateOutsideGoPath(t *testing.T) { is := is.New(t) ctx := context.Background() dir := t.TempDir() @@ -20,10 +30,13 @@ func TestCreateOutsideGoPathError(t *testing.T) { cli := testcli.New(dir) is.NoErr(td.NotExists(".gitignore")) result, err := cli.Run(ctx, "create", dir) - is.In(err.Error(), `Try again using the module name.`) is.Equal(result.Stdout(), "") is.Equal(result.Stderr(), "") - is.NoErr(td.NotExists(".gitignore")) + is.Equal(fileFirstLine(dir+"/go.mod"), "module changeme\n") + is.NoErr(td.Exists(".gitignore")) + is.NoErr(td.Exists("go.sum")) + is.NoErr(td.Exists("package.json")) + is.NoErr(td.Exists("package-lock.json")) } func TestCreateOutsideGoPathModulePath(t *testing.T) { @@ -37,6 +50,7 @@ func TestCreateOutsideGoPathModulePath(t *testing.T) { is.NoErr(err) is.Equal(result.Stdout(), "") is.Equal(result.Stderr(), "") + is.Equal(fileFirstLine(dir+"/go.mod"), "module github.com/my/app\n") is.NoErr(td.Exists(".gitignore")) is.NoErr(td.Exists("go.sum")) is.NoErr(td.Exists("package.json")) From 15d3cbbe1e252487a6bb41a99ff7d17a8eb74770 Mon Sep 17 00:00:00 2001 From: 012e Date: Tue, 9 Aug 2022 21:46:08 +0700 Subject: [PATCH 3/3] default module name to `change.me` --- internal/cli/create/create.go | 4 ++-- internal/cli/create/create_test.go | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/internal/cli/create/create.go b/internal/cli/create/create.go index 1631d258..f12f9cbe 100644 --- a/internal/cli/create/create.go +++ b/internal/cli/create/create.go @@ -110,9 +110,9 @@ func (c *Command) loadModule() *Module { if module.Name == "" { // Try inferring the module name from the directory module.Name = mod.Infer(c.absDir) - // if can't infer then default it to `changeme` + // if can't infer then default it to `change.me` if module.Name == "" { - module.Name = "changeme" + module.Name = "change.me" } } // Autoquote the module name since diff --git a/internal/cli/create/create_test.go b/internal/cli/create/create_test.go index fbe208bd..657e73c0 100644 --- a/internal/cli/create/create_test.go +++ b/internal/cli/create/create_test.go @@ -4,6 +4,7 @@ import ( "bufio" "context" "os" + "path/filepath" "testing" "github.com/livebud/bud/internal/cli/testcli" @@ -32,7 +33,7 @@ func TestCreateOutsideGoPath(t *testing.T) { result, err := cli.Run(ctx, "create", dir) is.Equal(result.Stdout(), "") is.Equal(result.Stderr(), "") - is.Equal(fileFirstLine(dir+"/go.mod"), "module changeme\n") + is.Equal(fileFirstLine(filepath.Join(dir, "go.mod")), "module change.me\n") is.NoErr(td.Exists(".gitignore")) is.NoErr(td.Exists("go.sum")) is.NoErr(td.Exists("package.json")) @@ -50,7 +51,7 @@ func TestCreateOutsideGoPathModulePath(t *testing.T) { is.NoErr(err) is.Equal(result.Stdout(), "") is.Equal(result.Stderr(), "") - is.Equal(fileFirstLine(dir+"/go.mod"), "module github.com/my/app\n") + is.Equal(fileFirstLine(filepath.Join(dir, "go.mod")), "module github.com/my/app\n") is.NoErr(td.Exists(".gitignore")) is.NoErr(td.Exists("go.sum")) is.NoErr(td.Exists("package.json"))