-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcommand.go
71 lines (60 loc) · 1.72 KB
/
command.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
package main
import (
"fmt"
"github.com/nilotpaul/gospur/config"
"github.com/nilotpaul/gospur/util"
"github.com/spf13/cobra"
)
// handleInitCmd handles the `init` command for gospur CLI.
func handleInitCmd(cmd *cobra.Command, args []string) {
targetPath, err := util.GetProjectPath(args)
if err != nil {
fmt.Println(config.ErrMsg(err))
return
}
// Building the stack config by talking user prompts.
stackCfg, err := util.GetStackConfig()
if err != nil {
fmt.Println(config.ErrMsg(err))
return
}
cfg := *stackCfg
// Asking for the go mod path from user.
goModPath, err := util.GetGoModulePath()
if err != nil {
fmt.Println(config.ErrMsg(err))
return
}
// Creating the target project directory.
// It'll check if the dir already exist and is empty or not (strict).
if err := util.CreateTargetDir(targetPath.Path, true); err != nil {
fmt.Println(config.ErrMsg(err))
return
}
// Creating the project files in the target directory.
// Passing the go mod path for resolving Go imports.
err = util.CreateProject(
targetPath.Path,
cfg,
util.MakeProjectCtx(cfg, goModPath),
)
if err != nil {
fmt.Println(config.ErrMsg(err))
return
}
// Running `go mod init` with the specified name.
if err := util.RunGoModInit(targetPath.FullPath, goModPath); err != nil {
fmt.Println(config.ErrMsg(err))
return
}
util.PrintSuccessMsg(targetPath.Path)
}
// handleVersionCmd handles the `version` command for gospur CLI.
func handleVersionCmd(cmd *cobra.Command, args []string) {
version, err := config.GetVersion()
if err != nil {
fmt.Println("Version:", config.ErrMsg(err))
return
}
fmt.Println("Version:", config.NormalMsg(version))
}