Skip to content

Commit e4280a0

Browse files
authored
Merge pull request #81 from dispatchrun/modify_go_template
Give Go module initialized with `dispatch init` the same name as it's directory name
2 parents 1a83d3f + d9a2892 commit e4280a0

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

cli/init.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cli
22

33
import (
44
"archive/tar"
5+
"bufio"
56
"compress/gzip"
67
"encoding/json"
78
"fmt"
@@ -256,6 +257,46 @@ func copyFile(srcFile string, dstFile string) error {
256257
return os.Chmod(dstFile, srcInfo.Mode())
257258
}
258259

260+
func prepareGoTemplate(path string) error {
261+
moduleName := filepath.Base(path)
262+
goModPath := filepath.Join(path, "go.mod")
263+
moduleHeader := fmt.Sprintf("module %s", moduleName)
264+
265+
// Update the go.mod file with the correct module name
266+
file, err := os.Open(goModPath)
267+
if err != nil {
268+
return err
269+
}
270+
defer file.Close()
271+
272+
// Read all lines from the file
273+
scanner := bufio.NewScanner(file)
274+
var lines []string
275+
for scanner.Scan() {
276+
lines = append(lines, scanner.Text())
277+
}
278+
279+
if err := scanner.Err(); err != nil {
280+
return err
281+
}
282+
283+
// replace the module header
284+
lines[0] = moduleHeader
285+
286+
// Join the lines back into a single string with newlines
287+
output := strings.Join(lines, "\n") + "\n"
288+
289+
// Write the modified content back to the file
290+
err = os.WriteFile(goModPath, []byte(output), 0644)
291+
if err != nil {
292+
return err
293+
}
294+
295+
// TODO: create .gitignore file?
296+
297+
return nil
298+
}
299+
259300
func initRunE(cmd *cobra.Command, args []string) error {
260301
// get or create the Dispatch templates directory
261302
dispatchUserDirPath, err := getAppDataDir(dispatchUserDir)
@@ -403,6 +444,15 @@ func initRunE(cmd *cobra.Command, args []string) error {
403444
return fmt.Errorf("failed to copy template: %w", err)
404445
}
405446

447+
switch wantedTemplate {
448+
case "go":
449+
err := prepareGoTemplate(path)
450+
if err != nil {
451+
cmd.SilenceUsage = true
452+
return fmt.Errorf("failed to prepare Go template: %w", err)
453+
}
454+
}
455+
406456
return nil
407457
}
408458

cli/init_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cli
22

33
import (
4+
"bufio"
45
"os"
56
"testing"
67

@@ -150,4 +151,46 @@ func TestInitCommand(t *testing.T) {
150151
assert.Nil(t, err)
151152
assert.ElementsMatch(t, []string{"dir1", "dir2"}, dirs)
152153
})
154+
155+
t.Run("prepareGoTemplate updates go.mod", func(t *testing.T) {
156+
t.Parallel()
157+
158+
tempDir := t.TempDir()
159+
projectName := "alpha"
160+
161+
// create project directory and go.mod file
162+
projectDir := tempDir + "/" + projectName
163+
goModFile := projectDir + "/go.mod"
164+
165+
err := os.Mkdir(projectDir, 0755)
166+
assert.Nil(t, err)
167+
168+
file, err := os.Create(goModFile)
169+
assert.Nil(t, err)
170+
171+
// write some content to the file
172+
_, err = file.WriteString("module randommodule")
173+
assert.Nil(t, err)
174+
175+
// Clean up
176+
err = file.Close()
177+
assert.Nil(t, err)
178+
179+
err = prepareGoTemplate(projectDir)
180+
assert.Nil(t, err)
181+
182+
// read first line of the file using scanner
183+
file, err = os.Open(goModFile)
184+
assert.Nil(t, err)
185+
186+
scanner := bufio.NewScanner(file)
187+
scanner.Scan()
188+
firstLine := scanner.Text()
189+
190+
assert.Equal(t, "module "+projectName, firstLine)
191+
192+
// Clean up
193+
err = file.Close()
194+
assert.Nil(t, err)
195+
})
153196
}

0 commit comments

Comments
 (0)