Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 503a5df

Browse files
author
Matthieu Nottale
committed
init: Add options to set description and maintainers.
Signed-off-by: Matthieu Nottale <[email protected]>
1 parent e9a51f2 commit 503a5df

File tree

4 files changed

+53
-22
lines changed

4 files changed

+53
-22
lines changed

cmd/init.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,24 @@ import (
88

99
// initCmd represents the init command
1010
var initCmd = &cobra.Command{
11-
Use: "init <app-name> [-c <compose-file>]",
11+
Use: "init <app-name> [-c <compose-file>] [-d <description>] [-m name:email ...]",
1212
Short: "Initialize an app package in the current working directory",
1313
Long: `This command initializes a new app package. If the -c option is used, or
1414
if a file named docker-compose.yml is found in the current directory, this file
1515
and the associated .env file if found will be used as the base for this application.`,
1616
Args: cli.ExactArgs(1),
1717
RunE: func(cmd *cobra.Command, args []string) error {
18-
return packager.Init(args[0], composeFile)
18+
return packager.Init(args[0], initComposeFile, initDescription, initMaintainers)
1919
},
2020
}
2121

22-
var composeFile string
22+
var initComposeFile string
23+
var initDescription string
24+
var initMaintainers []string
2325

2426
func init() {
2527
rootCmd.AddCommand(initCmd)
26-
initCmd.Flags().StringVarP(&composeFile, "compose-file", "c", "", "Initial Compose file (optional)")
28+
initCmd.Flags().StringVarP(&initComposeFile, "compose-file", "c", "", "Initial Compose file (optional)")
29+
initCmd.Flags().StringVarP(&initDescription, "description", "d", "", "Initial description (optional)")
30+
initCmd.Flags().StringArrayVarP(&initMaintainers, "maintainer", "m", []string{}, "Maintainer (name:email) (optional)")
2731
}

e2e/binary_test.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,18 @@ func TestInitBinary(t *testing.T) {
117117
nginx:
118118
image: nginx:${NGINX_VERSION}
119119
command: nginx $NGINX_ARGS
120+
`
121+
meta := `version: 0.1.0
122+
name: app_test
123+
description: my cool app
124+
maintainers:
125+
- name: bob
126+
email: ""
127+
- name: joe
128+
129+
targets:
130+
swarm: true
131+
kubernetes: true
120132
`
121133
envData := "# some comment\nNGINX_VERSION=latest"
122134
inputDir := randomName("app_input_")
@@ -125,7 +137,7 @@ func TestInitBinary(t *testing.T) {
125137
ioutil.WriteFile(filepath.Join(inputDir, ".env"), []byte(envData), 0644)
126138
defer os.RemoveAll(inputDir)
127139

128-
testAppName := randomName("app_")
140+
testAppName := "app_test"
129141
dirName := utils.DirNameFromAppName(testAppName)
130142
defer os.RemoveAll(dirName)
131143

@@ -134,19 +146,21 @@ func TestInitBinary(t *testing.T) {
134146
testAppName,
135147
"-c",
136148
filepath.Join(inputDir, "docker-compose.yml"),
149+
"-d",
150+
"my cool app",
151+
"-m", "bob",
152+
"-m", "joe:[email protected]",
137153
}
138154
cmd := exec.Command(dockerApp, args...)
139155
output, err := cmd.CombinedOutput()
140156
if err != nil {
141157
fmt.Println(output)
142158
}
143159
assert.NilError(t, err)
144-
meta, err := ioutil.ReadFile(filepath.Join(dirName, "metadata.yml"))
145-
assert.NilError(t, err)
146160
manifest := fs.Expected(
147161
t,
148162
fs.WithMode(0755),
149-
fs.WithFile("metadata.yml", string(meta), fs.WithMode(0644)), // too many variables, cheating
163+
fs.WithFile("metadata.yml", meta, fs.WithMode(0644)), // too many variables, cheating
150164
fs.WithFile("docker-compose.yml", composeData, fs.WithMode(0644)),
151165
fs.WithFile("settings.yml", "NGINX_ARGS: FILL ME\nNGINX_VERSION: latest\n", fs.WithMode(0644)),
152166
)

packager/init.go

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717

1818
// Init is the entrypoint initialization function.
1919
// It generates a new application package based on the provided parameters.
20-
func Init(name string, composeFile string) error {
20+
func Init(name string, composeFile string, description string, maintainers []string) error {
2121
if err := utils.ValidateAppName(name); err != nil {
2222
return err
2323
}
@@ -31,7 +31,7 @@ func Init(name string, composeFile string) error {
3131
os.RemoveAll(dirName)
3232
}
3333
}()
34-
if err = writeMetadataFile(name, dirName); err != nil {
34+
if err = writeMetadataFile(name, dirName, description, maintainers); err != nil {
3535
return err
3636
}
3737

@@ -199,28 +199,41 @@ func composeFileFromScratch() ([]byte, error) {
199199
return yaml.Marshal(fileStruct)
200200
}
201201

202-
func writeMetadataFile(name, dirName string) error {
203-
data, err := yaml.Marshal(newMetadata(name))
202+
func writeMetadataFile(name, dirName string, description string, maintainers []string) error {
203+
data, err := yaml.Marshal(newMetadata(name, description, maintainers))
204204
if err != nil {
205205
return err
206206
}
207207
return utils.CreateFileWithData(filepath.Join(dirName, "metadata.yml"), data)
208208
}
209209

210-
func newMetadata(name string) types.AppMetadata {
211-
var userName string
210+
func newMetadata(name string, description string, maintainers []string) types.AppMetadata {
212211
target := types.ApplicationTarget{
213212
Swarm: true,
214213
Kubernetes: true,
215214
}
216-
userData, _ := user.Current()
217-
if userData != nil {
218-
userName = userData.Username
219-
}
220-
return types.AppMetadata{
215+
res := types.AppMetadata{
221216
Version: "0.1.0",
222217
Targets: target,
223218
Name: name,
224-
Maintainers: []types.Maintainer{{Name: userName}},
219+
Description: description,
220+
}
221+
if len(maintainers) == 0 {
222+
var userName string
223+
userData, _ := user.Current()
224+
if userData != nil {
225+
userName = userData.Username
226+
}
227+
res.Maintainers = []types.Maintainer{{Name: userName}}
228+
} else {
229+
for _, m := range maintainers {
230+
ne := strings.Split(m, ":")
231+
email := ""
232+
if len(ne) > 1 {
233+
email = ne[1]
234+
}
235+
res.Maintainers = append(res.Maintainers, types.Maintainer{Name: ne[0], Email: email})
236+
}
225237
}
238+
return res
226239
}

packager/init_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ func TestWriteMetadataFile(t *testing.T) {
7272
tmpdir := fs.NewDir(t, appName)
7373
defer tmpdir.Remove()
7474

75-
err := writeMetadataFile(appName, tmpdir.Path())
75+
err := writeMetadataFile(appName, tmpdir.Path(), "", nil)
7676
assert.NilError(t, err)
7777

78-
data, err := yaml.Marshal(newMetadata(appName))
78+
data, err := yaml.Marshal(newMetadata(appName, "", nil))
7979
assert.NilError(t, err)
8080

8181
manifest := fs.Expected(

0 commit comments

Comments
 (0)