-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b66dd6a
commit 6bf145d
Showing
6 changed files
with
160 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,17 @@ | ||
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= | ||
github.com/evanw/esbuild v0.19.12 h1:p5WGo4o6TCN+kt+uZtYSGS3ZHPa+iIZ0SX+ys8UnP10= | ||
github.com/evanw/esbuild v0.19.12/go.mod h1:D2vIQZqV/vIf/VRHtViaUtViZmG7o+kKmlBfVQuRi48= | ||
github.com/gomarkdown/markdown v0.0.0-20231222211730-1d6d20845b47 h1:k4Tw0nt6lwro3Uin8eqoET7MDA4JnT8YgbCjc/g5E3k= | ||
github.com/gomarkdown/markdown v0.0.0-20231222211730-1d6d20845b47/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA= | ||
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= | ||
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= | ||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= | ||
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0= | ||
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho= | ||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= | ||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= | ||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU= | ||
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package cmd | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var initCmd = &cobra.Command{ | ||
Use: "init", | ||
Short: "Initialize a project", | ||
Long: "Initialize a project", | ||
Run: initialize, | ||
} | ||
|
||
func init() { | ||
RootCmd.AddCommand(initCmd) | ||
} | ||
|
||
const BASE_PROJECT_PATH = "baseProject" | ||
const REPO_URL = "https://github.com/K-Sato1995/go-simple-ssg" | ||
|
||
func initialize(cmd *cobra.Command, args []string) { | ||
fmt.Println("Initializing project...") | ||
reader := bufio.NewReader(os.Stdin) | ||
fmt.Print("Enter a directory name for the project (default is 'myProject'): ") | ||
dirName, _ := reader.ReadString('\n') | ||
dirName = strings.TrimSpace(dirName) | ||
if dirName == "" { | ||
dirName = "myProject" // default directory name | ||
} | ||
|
||
if _, err := os.Stat(dirName); os.IsNotExist(err) { | ||
err := os.Mkdir(dirName, os.ModePerm) | ||
if err != nil { | ||
fmt.Println("Error creating project directory:", err) | ||
return | ||
} | ||
} | ||
tempDir, err := ioutil.TempDir("", "repoClone") | ||
if err != nil { | ||
fmt.Println("Error creating a temporary directory:", err) | ||
return | ||
} | ||
defer os.RemoveAll(tempDir) // Clean up | ||
|
||
err = gitClone(REPO_URL, tempDir) | ||
if err != nil { | ||
fmt.Println("Error cloning repository:", err) | ||
return | ||
} | ||
|
||
baseProjectPath := filepath.Join(tempDir, BASE_PROJECT_PATH) | ||
projectDir := filepath.Join(".", dirName) | ||
|
||
err = copyDir(baseProjectPath, projectDir) | ||
if err != nil { | ||
fmt.Println("Error copying baseProject:", err) | ||
return | ||
} | ||
|
||
fmt.Println("Project initialized successfully in directory:", dirName) | ||
} | ||
|
||
func gitClone(repoURL, directoryName string) error { | ||
cmd := exec.Command("git", "clone", repoURL, directoryName) | ||
cmd.Stdout = os.Stdout | ||
cmd.Stderr = os.Stderr | ||
return cmd.Run() | ||
} | ||
|
||
func copyDir(srcPath, dstPath string) error { | ||
return filepath.Walk(srcPath, func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
relativePath, err := filepath.Rel(srcPath, path) | ||
if err != nil { | ||
return err | ||
} | ||
dst := filepath.Join(dstPath, relativePath) | ||
|
||
if info.IsDir() { | ||
return os.MkdirAll(dst, info.Mode()) | ||
} else { | ||
if _, err := os.Stat(dst); err == nil { | ||
return nil | ||
} | ||
return copyFile(path, dst) | ||
} | ||
}) | ||
} | ||
|
||
func copyFile(srcFile, dstFile string) error { | ||
in, err := os.Open(srcFile) | ||
if err != nil { | ||
return err | ||
} | ||
defer in.Close() | ||
|
||
out, err := os.Create(dstFile) | ||
if err != nil { | ||
return err | ||
} | ||
defer out.Close() | ||
|
||
_, err = io.Copy(out, in) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package cmd | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
// rootCmd represents the base command when called without any subcommands | ||
var RootCmd = &cobra.Command{ | ||
Use: "go-simple-ssg", | ||
Short: "Simple static site generator written in Go", | ||
Long: "This is a simple static site generator written in Go", | ||
} | ||
|
||
func Execute() { | ||
err := RootCmd.Execute() | ||
if err != nil { | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package main | ||
|
||
import "go-simple-ssg/simple-ssg-cli/cmd" | ||
|
||
func main() { | ||
cmd.Execute() | ||
} |