From bddccecdc8d708f4b51501615e3e024137786bfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A0=95=EA=B2=BD=EC=9B=90?= Date: Tue, 26 Nov 2024 21:51:20 +0900 Subject: [PATCH 1/2] fix: resolve command incompatibility issue on Windows --- .../internal/hugogenerator/hugo_gen_setup.go | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/wp2hugo/internal/hugogenerator/hugo_gen_setup.go b/src/wp2hugo/internal/hugogenerator/hugo_gen_setup.go index aebcc1e..1afd4ac 100644 --- a/src/wp2hugo/internal/hugogenerator/hugo_gen_setup.go +++ b/src/wp2hugo/internal/hugogenerator/hugo_gen_setup.go @@ -9,6 +9,7 @@ import ( "path" "strings" "time" + "runtime" "github.com/ashishb/wp2hugo/src/wp2hugo/internal/hugogenerator/hugopage" "github.com/ashishb/wp2hugo/src/wp2hugo/internal/nginxgenerator" @@ -164,17 +165,18 @@ func (g Generator) setupHugo(outputDirPath string) (*string, error) { return nil, fmt.Errorf("hugo not found, install it from https://gohugo.io/: %s", err) } + // Create output directory + os.MkdirAll(outputDirPath, 0700) + commands := []string{ "git version", "hugo version", - fmt.Sprintf("mkdir -p %s", outputDirPath), // Use YAML file as it is easier to edit it afterward than TOML fmt.Sprintf("cd %s && hugo new site %s --format yaml", outputDirPath, siteName), fmt.Sprintf("cd %s && git clone https://github.com/adityatelange/hugo-PaperMod themes/PaperMod --depth=1", path.Join(outputDirPath, siteName)), - fmt.Sprintf("cd %s && rm -rf themes/PaperMod/.git themes/PaperMod/.github", path.Join(outputDirPath, siteName)), // Set theme to PaperMod - fmt.Sprintf(`echo "theme: 'PaperMod'">> %s/hugo.yaml`, path.Join(outputDirPath, siteName)), + fmt.Sprintf(`echo theme: 'PaperMod'>> %s/hugo.yaml`, path.Join(outputDirPath, siteName)), // Verify that the site is set up correctly fmt.Sprintf("cd %s && hugo", path.Join(outputDirPath, siteName)), } @@ -184,7 +186,16 @@ func (g Generator) setupHugo(outputDirPath string) (*string, error) { Int("totalSteps", len(commands)). Str("cmd", command). Msg("Running Hugo setup command") - output, err := exec.Command("bash", "-c", command).Output() + var ( + output []byte + err error + ) + if runtime.GOOS == "windows" { + output, err = exec.Command("cmd", "/C", command).Output() + } else { + // mac & Linux + output, err = exec.Command("bash", "-c", command).Output() + } if err != nil { log.Error(). Err(err). @@ -196,6 +207,10 @@ func (g Generator) setupHugo(outputDirPath string) (*string, error) { log.Debug().Msgf("Hugo setup output: %s", output) } + // Delete .git directory + os.RemoveAll(path.Join(outputDirPath, siteName, "themes/PaperMod/.git")) + os.RemoveAll(path.Join(outputDirPath, siteName, "themes/PaperMod/.github")) + siteDir := path.Join(outputDirPath, siteName) log.Info(). Str("location", siteDir). From fd86fe2d9e6f1fa7fa5450dd035bfeb0fbc22632 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A0=95=EA=B2=BD=EC=9B=90?= Date: Wed, 27 Nov 2024 08:31:52 +0900 Subject: [PATCH 2/2] fix: fix code formatting issues to pass lint checks --- .../internal/hugogenerator/hugo_gen_setup.go | 29 +++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/src/wp2hugo/internal/hugogenerator/hugo_gen_setup.go b/src/wp2hugo/internal/hugogenerator/hugo_gen_setup.go index 1afd4ac..d64144d 100644 --- a/src/wp2hugo/internal/hugogenerator/hugo_gen_setup.go +++ b/src/wp2hugo/internal/hugogenerator/hugo_gen_setup.go @@ -7,9 +7,9 @@ import ( "os" "os/exec" "path" + "runtime" "strings" "time" - "runtime" "github.com/ashishb/wp2hugo/src/wp2hugo/internal/hugogenerator/hugopage" "github.com/ashishb/wp2hugo/src/wp2hugo/internal/nginxgenerator" @@ -166,7 +166,14 @@ func (g Generator) setupHugo(outputDirPath string) (*string, error) { } // Create output directory - os.MkdirAll(outputDirPath, 0700) + err = os.MkdirAll(outputDirPath, 0700) + if err != nil { + log.Fatal(). + Err(err). + Str("outputDirPath", outputDirPath). + Msg("error creating output directory") + return nil, fmt.Errorf("error creating output directory '%s': %s", outputDirPath, err) + } commands := []string{ "git version", @@ -188,7 +195,7 @@ func (g Generator) setupHugo(outputDirPath string) (*string, error) { Msg("Running Hugo setup command") var ( output []byte - err error + err error ) if runtime.GOOS == "windows" { output, err = exec.Command("cmd", "/C", command).Output() @@ -208,8 +215,20 @@ func (g Generator) setupHugo(outputDirPath string) (*string, error) { } // Delete .git directory - os.RemoveAll(path.Join(outputDirPath, siteName, "themes/PaperMod/.git")) - os.RemoveAll(path.Join(outputDirPath, siteName, "themes/PaperMod/.github")) + deleteDirs := []string{ + path.Join(outputDirPath, siteName, ".git"), + path.Join(outputDirPath, siteName, "themes/PaperMod/.git"), + } + for _, dir := range deleteDirs { + err := os.RemoveAll(dir) + if err != nil { + log.Error(). + Err(err). + Str("dir", dir). + Msg("error removing directory") + return nil, fmt.Errorf("error removing directory '%s': %s", dir, err) + } + } siteDir := path.Join(outputDirPath, siteName) log.Info().