Skip to content

Commit

Permalink
Merge pull request #81 from dispatchrun/modify_go_template
Browse files Browse the repository at this point in the history
Give Go module initialized with `dispatch init` the same name as it's directory name
  • Loading branch information
chicoxyzzy authored Jul 4, 2024
2 parents 1a83d3f + d9a2892 commit e4280a0
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
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?

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)
})
}

0 comments on commit e4280a0

Please sign in to comment.