1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
  | 
package cmd
import (
	"fmt"
	"io/fs"
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"
	"github.com/TwinProduction/go-color"
	"github.com/go-git/go-git/v5"
	"github.com/spf13/cobra"
)
var (
	InitializeCommand = &cobra.Command{
		Use:   "init",
		Short: "Initialize a new nova based project",
		Run:   initNovaRepo,
	}
)
func initNovaRepo(cmd *cobra.Command, args []string) {
	url := "https://github.com/libgit2/git2go.git"
	path := ""
	name := "test"
	if name == "" {
		fmt.Print(
			color.Ize(color.Red, "A name must be specified"),
		)
		return
	}
	if path == "" {
		path = fmt.Sprintf("./%s", name)
	}
	cw, err := os.Getwd()
	if err != nil {
		return
	}
	path = filepath.Join(cw, path)
	fmt.Println(
		color.Ize(color.Green, fmt.Sprintf("Initializing a new nova project at %s", path)),
	)
	fmt.Println(
		color.Ize(color.Gray, fmt.Sprintf("Using the %s template", url)),
	)
	// clone the repo
	_, err = git.PlainClone(path, false, &git.CloneOptions{
		URL:      url,
		Progress: os.Stdout,
	})
	if err != nil {
		fmt.Println(
			color.Ize(color.Red, fmt.Sprintf("Failed to initialize the repository: %s", err.Error())),
		)
		return
	}
	fmt.Println(
		color.Ize(color.Green, "Cloned the repository..."),
	)
	filepath.WalkDir(path, func(path string, d fs.DirEntry, err error) error {
		if d.IsDir() || err != nil {
			return nil
		}
		read, err := ioutil.ReadFile(path)
		if err != nil {
		}
		content := strings.ReplaceAll(string(read), "%PROJECT%", name)
		err = ioutil.WriteFile(path, []byte(content), 0)
		if err != nil {
			return err
		}
		return nil
	})
	err = os.RemoveAll(filepath.Join(path, ".git"))
}
  |