X Tutup
Skip to content

Commit a65b9a3

Browse files
committed
💅 simplify filesystem operations while creating extensions
- doing mkdir before `git init <dir>` is not necessary; git will create the directory if it doesn't exist - passing both `-C` and `--git-dir` is not needed for git invocations - reading the current working directory isn't necessary for specifying nested paths to operate in - use `git add .` instead of explicitly naming all paths; we generate a gitignore for binaries that we want to avoid adding - generate cross-platform gitignore for Go binary extensions - add helpers to simplify writing files, tests
1 parent e506910 commit a65b9a3

File tree

3 files changed

+140
-172
lines changed

3 files changed

+140
-172
lines changed

pkg/cmd/extension/ext_tmpls/script.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ echo "Hello %[1]s!"
2929
# '
3030
# TEMPLATE='
3131
# {{- range $repo := .data.viewer.repositories.nodes -}}
32-
# {{- printf "name: %[2]s - stargazers: %[3]s\n" $repo.nameWithOwner $repo.stargazerCount -}}
32+
# {{- printf "name: %%s - stargazers: %%v\n" $repo.nameWithOwner $repo.stargazerCount -}}
3333
# {{- end -}}
3434
# '
3535
# exec gh api graphql -f query="${QUERY}" --paginate --template="${TEMPLATE}"

pkg/cmd/extension/manager.go

Lines changed: 28 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -510,17 +510,14 @@ func (m *Manager) upgradeGitExtension(ext Extension, force bool) error {
510510
if err != nil {
511511
return err
512512
}
513-
var cmds []*exec.Cmd
514513
dir := filepath.Dir(ext.path)
515514
if force {
516-
fetchCmd := m.newCommand(exe, "-C", dir, "--git-dir="+filepath.Join(dir, ".git"), "fetch", "origin", "HEAD")
517-
resetCmd := m.newCommand(exe, "-C", dir, "--git-dir="+filepath.Join(dir, ".git"), "reset", "--hard", "origin/HEAD")
518-
cmds = []*exec.Cmd{fetchCmd, resetCmd}
519-
} else {
520-
pullCmd := m.newCommand(exe, "-C", dir, "--git-dir="+filepath.Join(dir, ".git"), "pull", "--ff-only")
521-
cmds = []*exec.Cmd{pullCmd}
515+
if err := m.newCommand(exe, "-C", dir, "fetch", "origin", "HEAD").Run(); err != nil {
516+
return err
517+
}
518+
return m.newCommand(exe, "-C", dir, "reset", "--hard", "origin/HEAD").Run()
522519
}
523-
return runCmds(cmds)
520+
return m.newCommand(exe, "-C", dir, "pull", "--ff-only").Run()
524521
}
525522

526523
func (m *Manager) upgradeBinExtension(ext Extension) error {
@@ -564,14 +561,7 @@ func (m *Manager) Create(name string, tmplType extensions.ExtTemplateType) error
564561
return err
565562
}
566563

567-
err = os.Mkdir(name, 0755)
568-
if err != nil {
569-
return err
570-
}
571-
572-
initCmd := m.newCommand(exe, "init", "--quiet", name)
573-
err = initCmd.Run()
574-
if err != nil {
564+
if err := m.newCommand(exe, "init", "--quiet", name).Run(); err != nil {
575565
return err
576566
}
577567

@@ -581,85 +571,42 @@ func (m *Manager) Create(name string, tmplType extensions.ExtTemplateType) error
581571
return m.otherBinScaffolding(exe, name)
582572
}
583573

584-
script := fmt.Sprintf(scriptTmpl, name, "%s", "%v")
585-
filePath := filepath.Join(name, name)
586-
err = ioutil.WriteFile(filePath, []byte(script), 0755)
587-
if err != nil {
574+
script := fmt.Sprintf(scriptTmpl, name)
575+
if err := writeFile(filepath.Join(name, name), []byte(script), 0755); err != nil {
588576
return err
589577
}
590578

591-
wd, err := os.Getwd()
592-
if err != nil {
593-
return err
594-
}
595-
dir := filepath.Join(wd, name)
596-
addCmd := m.newCommand(exe, "-C", dir, "--git-dir="+filepath.Join(dir, ".git"), "add", name, "--chmod=+x")
597-
return addCmd.Run()
579+
return m.newCommand(exe, "-C", name, "add", name, "--chmod=+x").Run()
598580
}
599581

600582
func (m *Manager) otherBinScaffolding(gitExe, name string) error {
601-
err := os.MkdirAll(filepath.Join(name, ".github", "workflows"), 0755)
602-
if err != nil {
603-
return err
604-
}
605-
workflowPath := filepath.Join(".github", "workflows", "release.yml")
606-
err = ioutil.WriteFile(filepath.Join(name, workflowPath), otherBinWorkflow, 0644)
607-
if err != nil {
608-
return err
609-
}
610-
err = os.Mkdir(filepath.Join(name, "script"), 0755)
611-
if err != nil {
583+
if err := writeFile(filepath.Join(name, ".github", "workflows", "release.yml"), otherBinWorkflow, 0644); err != nil {
612584
return err
613585
}
614586
buildScriptPath := filepath.Join("script", "build.sh")
615-
err = ioutil.WriteFile(filepath.Join(name, buildScriptPath), buildScript, 0755)
616-
if err != nil {
617-
return err
618-
}
619-
620-
wd, err := os.Getwd()
621-
if err != nil {
587+
if err := writeFile(filepath.Join(name, buildScriptPath), buildScript, 0755); err != nil {
622588
return err
623589
}
624-
dir := filepath.Join(wd, name)
625-
addCmd := m.newCommand(gitExe, "-C", dir, "--git-dir="+filepath.Join(dir, ".git"), "add", buildScriptPath, "--chmod=+x")
626-
err = addCmd.Run()
627-
if err != nil {
590+
if err := m.newCommand(gitExe, "-C", name, "add", buildScriptPath, "--chmod=+x").Run(); err != nil {
628591
return err
629592
}
630-
631-
addCmd = m.newCommand(gitExe, "-C", dir, "--git-dir="+filepath.Join(dir, ".git"), "add", workflowPath)
632-
return addCmd.Run()
593+
return m.newCommand(gitExe, "-C", name, "add", ".").Run()
633594
}
634595

635596
func (m *Manager) goBinScaffolding(gitExe, name string) error {
636597
goExe, err := m.lookPath("go")
637598
if err != nil {
638599
return fmt.Errorf("go is required for creating Go extensions: %w", err)
639600
}
640-
err = os.MkdirAll(filepath.Join(name, ".github", "workflows"), 0755)
641-
if err != nil {
642-
return err
643-
}
644-
workflowPath := filepath.Join(".github", "workflows", "release.yml")
645-
err = ioutil.WriteFile(filepath.Join(name, workflowPath), goBinWorkflow, 0644)
646-
if err != nil {
647-
return err
648-
}
649-
650-
mainGo := fmt.Sprintf(mainGoTmpl, name)
651-
mainPath := "main.go"
652601

653-
err = ioutil.WriteFile(filepath.Join(name, mainPath), []byte(mainGo), 0644)
654-
if err != nil {
602+
if err := writeFile(filepath.Join(name, ".github", "workflows", "release.yml"), goBinWorkflow, 0644); err != nil {
655603
return err
656604
}
657605

658-
wd, err := os.Getwd()
659-
if err != nil {
606+
mainGo := fmt.Sprintf(mainGoTmpl, name)
607+
if err := writeFile(filepath.Join(name, "main.go"), []byte(mainGo), 0644); err != nil {
660608
return err
661609
}
662-
dir := filepath.Join(wd, name)
663610

664611
host, err := m.config.DefaultHost()
665612
if err != nil {
@@ -677,49 +624,33 @@ func (m *Manager) goBinScaffolding(gitExe, name string) error {
677624
{"build"},
678625
}
679626

680-
_, ext := m.platform()
681-
ignore := name + ext
682-
683-
err = ioutil.WriteFile(filepath.Join(name, ".gitignore"), []byte(ignore), 0644)
684-
if err != nil {
627+
ignore := fmt.Sprintf("/%[1]s\n/%[1]s.exe\n", name)
628+
if err := writeFile(filepath.Join(name, ".gitignore"), []byte(ignore), 0644); err != nil {
685629
return err
686630
}
687631

688632
for _, args := range goCmds {
689633
goCmd := m.newCommand(goExe, args...)
690-
goCmd.Dir = dir
691-
err = goCmd.Run()
692-
if err != nil {
634+
goCmd.Dir = name
635+
if err := goCmd.Run(); err != nil {
693636
return fmt.Errorf("failed to set up go module: %w", err)
694637
}
695638
}
696639

697-
addArgs := []string{
698-
"-C", dir,
699-
"--git-dir=" + filepath.Join(dir, ".git"),
700-
"add",
701-
workflowPath,
702-
mainPath,
703-
".gitignore",
704-
"go.mod",
705-
"go.sum",
706-
}
640+
return m.newCommand(gitExe, "-C", name, "add", ".").Run()
641+
}
707642

708-
addCmd := m.newCommand(gitExe, addArgs...)
709-
return addCmd.Run()
643+
func isSymlink(m os.FileMode) bool {
644+
return m&os.ModeSymlink != 0
710645
}
711646

712-
func runCmds(cmds []*exec.Cmd) error {
713-
for _, cmd := range cmds {
714-
if err := cmd.Run(); err != nil {
647+
func writeFile(p string, contents []byte, mode os.FileMode) error {
648+
if dir := filepath.Dir(p); dir != "." {
649+
if err := os.MkdirAll(dir, 0755); err != nil {
715650
return err
716651
}
717652
}
718-
return nil
719-
}
720-
721-
func isSymlink(m os.FileMode) bool {
722-
return m&os.ModeSymlink != 0
653+
return os.WriteFile(p, contents, mode)
723654
}
724655

725656
// reads the product of makeSymlink on Windows

0 commit comments

Comments
 (0)
X Tutup