From d9a2892145a3381ff8a9254c05d749c58aadd6b3 Mon Sep 17 00:00:00 2001 From: Sergey Rubanov Date: Thu, 4 Jul 2024 06:18:51 +0200 Subject: [PATCH] give go module the same name as it's directory name --- cli/init.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ cli/init_test.go | 43 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) diff --git a/cli/init.go b/cli/init.go index d2f6644..1234781 100644 --- a/cli/init.go +++ b/cli/init.go @@ -2,6 +2,7 @@ package cli import ( "archive/tar" + "bufio" "compress/gzip" "encoding/json" "fmt" @@ -256,6 +257,46 @@ func copyFile(srcFile string, dstFile string) error { return os.Chmod(dstFile, srcInfo.Mode()) } +func prepareGoTemplate(path string) error { + moduleName := filepath.Base(path) + goModPath := filepath.Join(path, "go.mod") + moduleHeader := fmt.Sprintf("module %s", moduleName) + + // Update the go.mod file with the correct module name + file, err := os.Open(goModPath) + if err != nil { + return err + } + defer file.Close() + + // Read all lines from the file + scanner := bufio.NewScanner(file) + var lines []string + for scanner.Scan() { + lines = append(lines, scanner.Text()) + } + + if err := scanner.Err(); err != nil { + return err + } + + // replace the module header + lines[0] = moduleHeader + + // Join the lines back into a single string with newlines + output := strings.Join(lines, "\n") + "\n" + + // Write the modified content back to the file + err = os.WriteFile(goModPath, []byte(output), 0644) + if err != nil { + return err + } + + // TODO: create .gitignore file? + + return nil +} + func initRunE(cmd *cobra.Command, args []string) error { // get or create the Dispatch templates directory dispatchUserDirPath, err := getAppDataDir(dispatchUserDir) @@ -403,6 +444,15 @@ func initRunE(cmd *cobra.Command, args []string) error { return fmt.Errorf("failed to copy template: %w", err) } + switch wantedTemplate { + case "go": + err := prepareGoTemplate(path) + if err != nil { + cmd.SilenceUsage = true + return fmt.Errorf("failed to prepare Go template: %w", err) + } + } + return nil } diff --git a/cli/init_test.go b/cli/init_test.go index 143e0a5..e420aa2 100644 --- a/cli/init_test.go +++ b/cli/init_test.go @@ -1,6 +1,7 @@ package cli import ( + "bufio" "os" "testing" @@ -150,4 +151,46 @@ func TestInitCommand(t *testing.T) { assert.Nil(t, err) assert.ElementsMatch(t, []string{"dir1", "dir2"}, dirs) }) + + t.Run("prepareGoTemplate updates go.mod", func(t *testing.T) { + t.Parallel() + + tempDir := t.TempDir() + projectName := "alpha" + + // create project directory and go.mod file + projectDir := tempDir + "/" + projectName + goModFile := projectDir + "/go.mod" + + err := os.Mkdir(projectDir, 0755) + assert.Nil(t, err) + + file, err := os.Create(goModFile) + assert.Nil(t, err) + + // write some content to the file + _, err = file.WriteString("module randommodule") + assert.Nil(t, err) + + // Clean up + err = file.Close() + assert.Nil(t, err) + + err = prepareGoTemplate(projectDir) + assert.Nil(t, err) + + // read first line of the file using scanner + file, err = os.Open(goModFile) + assert.Nil(t, err) + + scanner := bufio.NewScanner(file) + scanner.Scan() + firstLine := scanner.Text() + + assert.Equal(t, "module "+projectName, firstLine) + + // Clean up + err = file.Close() + assert.Nil(t, err) + }) }