This repository has been archived by the owner on Feb 20, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
project.go
172 lines (142 loc) · 3.56 KB
/
project.go
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package create
import (
"errors"
"fmt"
"os"
"os/exec"
"github.com/AlecAivazis/survey/v2"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"goyave.dev/gyv/internal/command"
"goyave.dev/gyv/internal/fs"
"goyave.dev/gyv/internal/git"
"goyave.dev/gyv/internal/mod"
)
const (
defaultZipFileName = "goyave_template.zip"
)
// Project command for project generation
type Project struct {
GoyaveVersion string
ModuleName string
}
// BuildCobraCommand builds the cobra command for this action
func (c *Project) BuildCobraCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "project",
Short: "Create a Goyave project",
Long: `Command to create Goyave project.
You need go and git to be installed on your system in order de run this command.
The flags --module-name and --goyave-version are required.`,
RunE: command.GenerateRunFunc(c),
}
c.setFlags(cmd.Flags())
return cmd
}
// BuildSurvey builds a survey for this action
func (c *Project) BuildSurvey() ([]*survey.Question, error) {
tags, err := git.GetAllTags()
if err != nil {
return nil, err
}
versions, err := git.GetVersions(tags)
if err != nil {
return nil, err
}
filteredVersions, err := git.GetTopVersions(versions)
if err != nil {
return nil, err
}
return []*survey.Question{
{
Name: "moduleName",
Prompt: &survey.Input{Message: "Go module name"},
Validate: survey.Required,
},
{
Name: "goyaveVersion",
Prompt: &survey.Select{
Message: "Goyave version number",
Options: git.VersionsToStrings(filteredVersions),
Default: filteredVersions[0].String(),
},
},
}, nil
}
// Execute the command's behavior
func (c *Project) Execute() error {
tags, err := git.GetAllTags()
if err != nil {
return err
}
projectName := mod.ProjectNameFromModuleName(c.ModuleName)
info, err := os.Stat(projectName)
if info != nil {
return errors.New("A directory with this name already exists")
}
if !os.IsNotExist(err) {
return err
}
tag, err := git.GetTagByName(c.GoyaveVersion, tags)
if err != nil {
return err
}
if err := git.DownloadFile(tag.ZipballURL, defaultZipFileName); err != nil {
return err
}
if _, err := fs.ExtractZip(defaultZipFileName, projectName); err != nil {
return err
}
if err := fs.ReplaceAll(projectName, c.ModuleName); err != nil {
return err
}
if err := git.ProjectGitInit(projectName); err != nil {
return err
}
if err := os.Remove(defaultZipFileName); err != nil {
return err
}
currentWorkingDirectory, err := os.Getwd()
if err != nil {
return err
}
tidyCommand := exec.Command("go", "mod", "tidy")
projectPath := fmt.Sprintf("%s%c%s", currentWorkingDirectory, os.PathSeparator, projectName)
tidyCommand.Dir = projectPath
if err := tidyCommand.Run(); err != nil {
return err
}
err = fs.CopyFile(
fmt.Sprintf("%s%c%s", projectPath, os.PathSeparator, "config.example.json"),
fmt.Sprintf("%s%c%s", projectPath, os.PathSeparator, "config.json"),
)
if err != nil {
return err
}
fmt.Println("✅ Project created!")
fmt.Printf("➡️ Get started by navigating to \"%s\"\n", projectName)
return nil
}
// Validate check if required flags are definded
func (c *Project) Validate() error {
if c.GoyaveVersion == "" || c.ModuleName == "" {
return errors.New("required flag(s) \"goyave-version\" and \"module-name\" aren't set")
}
return nil
}
func (c *Project) setFlags(flags *pflag.FlagSet) {
flags.StringVarP(
&c.ModuleName,
"module-name",
"n",
"",
"The name of your module",
)
flags.StringVarP(
&c.GoyaveVersion,
"goyave-version",
"g",
"",
"The Goyave version used in this project",
)
}