Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Give Go module initialized with dispatch init the same name as it's directory name #81

Merged
merged 1 commit into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions cli/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"archive/tar"
"bufio"
"compress/gzip"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -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?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add a .gitignore file to the template so we don't need this to be a special case?

Copy link
Contributor Author

@chicoxyzzy chicoxyzzy Jul 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I initially added it, but we never know what will be the -o flag value to ignore the binary. I made it equal to the directory name as well initially (when -o is omitted), but that's also dynamical.

What known in advance Go directories and files I should exclude?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's roll with these 👍


return nil
}

func initRunE(cmd *cobra.Command, args []string) error {
// get or create the Dispatch templates directory
dispatchUserDirPath, err := getAppDataDir(dispatchUserDir)
Expand Down Expand Up @@ -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
}

Expand Down
43 changes: 43 additions & 0 deletions cli/init_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"bufio"
"os"
"testing"

Expand Down Expand Up @@ -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)
})
}
Loading