-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_module.go
128 lines (111 loc) 路 3.18 KB
/
create_module.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
// Copyright 2022 Outreach Corporation. All Rights Reserved.
// Description: This file contains code for the create templaterepository command
package main
import (
"fmt"
"os"
"os/exec"
"path"
"github.com/AlecAivazis/survey/v2"
"github.com/pkg/errors"
"github.com/rgst-io/stencil/pkg/configuration"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v3"
)
// NewCreateModule returns a new urfave/cli.Command for the
// create module command
func NewCreateModule() *cli.Command {
return &cli.Command{
Name: "module",
Description: "Creates a module with the provided name in the current directory",
ArgsUsage: "create module <name>",
Aliases: []string{"templaterepository"},
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "native-extension",
Usage: "Generates a native extension",
},
},
Action: func(c *cli.Context) error {
var manifestFileName = "service.yaml"
// ensure we have a name
if c.NArg() != 1 {
return errors.New("must provide a name for the module")
}
allowedFiles := []string{".", "..", ".git"}
files, err := os.ReadDir(".")
if err != nil {
return err
}
// ensure we don't have any files in the current directory, except for
// the allowed files
for _, file := range files {
found := false
for _, af := range allowedFiles {
if file.Name() == af {
found = true
continue
}
}
if !found {
return errors.New("must be in a directory with no files")
}
}
var reportingTeam string
if err := survey.AskOne(&survey.Input{
Message: "What is the reporting team for this module in the form of a GitHub slug (used in CODEOWNERS)?",
}, &reportingTeam); err != nil {
return errors.Wrap(err, "ask for reporting team")
}
var description string
if err := survey.AskOne(&survey.Input{
Message: "Enter a description for the module.",
}, &description); err != nil {
return errors.Wrap(err, "ask for description")
}
releaseOpts := map[string]interface{}{
"enablePrereleases": true,
}
tm := &configuration.ServiceManifest{
Name: path.Base(c.Args().Get(0)),
Modules: []*configuration.TemplateRepository{{
Name: "github.com/rgst-io/stencil-template-base",
}, {
Name: "github.com/rgst-io/stencil-base",
}, {
Name: "github.com/rgst-io/stencil-circleci",
}},
Arguments: map[string]interface{}{
"reportingTeam": reportingTeam,
"description": description,
},
}
if c.Bool("native-extension") {
tm.Arguments["plugin"] = true
releaseOpts["force"] = true
}
tm.Arguments["releaseOptions"] = releaseOpts
if _, err := os.Stat(manifestFileName); err == nil {
return fmt.Errorf("%s already exists", manifestFileName)
}
f, err := os.Create(manifestFileName)
if err != nil {
return err
}
defer f.Close()
enc := yaml.NewEncoder(f)
if err := enc.Encode(tm); err != nil {
return err
}
if err := enc.Close(); err != nil {
return err
}
//nolint:gosec // Why: intentional
cmd := exec.CommandContext(c.Context, os.Args[0])
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
return errors.Wrap(cmd.Run(), "failed to run stencil")
},
}
}