-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
65 lines (55 loc) · 1.54 KB
/
init.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
package cmd
import (
"path"
"github.com/foomo/posh/embed"
scaffold2 "github.com/foomo/posh/integration/scaffold"
"github.com/foomo/posh/internal/util/git"
"github.com/foomo/posh/pkg/env"
"github.com/spf13/cobra"
)
var (
initCmdFlagDry bool
initCmdFlagOverride bool
)
// initCmd represents the init command
var initCmd = &cobra.Command{
Use: "init",
Short: "Initialize a Project Oriented Shell",
Long: `Initialize (posh init) will create a new Project Oriented Shell with the appropriate structure.
Posh init must be run inside of a go module (please run "go mod init <MODNAME> first)"`,
SilenceErrors: true,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
data := map[string]interface{}{}
// define module
if value, err := git.OriginURL(); err != nil {
l.Debug("failed to retrieve git origin url:", err.Error())
data["module"] = path.Base(env.ProjectRoot())
} else {
data["module"] = value
}
fs, err := embed.Scaffold("init")
if err != nil {
return err
}
sc, err := scaffold2.New(
l,
scaffold2.WithDry(initCmdFlagDry),
scaffold2.WithOverride(initCmdFlagOverride),
scaffold2.WithDirectories(scaffold2.Directory{
Source: fs,
Target: env.ProjectRoot(),
Data: data,
}),
)
if err != nil {
return err
}
return sc.Render(cmd.Context())
},
}
func init() {
rootCmd.AddCommand(initCmd)
initCmd.Flags().BoolVar(&initCmdFlagDry, "dry", false, "don't render files")
initCmd.Flags().BoolVar(&initCmdFlagOverride, "override", false, "override existing files")
}