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

Commit cc0d5ee

Browse files
authored
Merge pull request #633 from rumpl/ux-init
Ux init
2 parents dc0d83c + c85f3d0 commit cc0d5ee

File tree

4 files changed

+40
-82
lines changed

4 files changed

+40
-82
lines changed

e2e/commands_test.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66
"io/ioutil"
7+
"os/user"
78
"path/filepath"
89
"regexp"
910
"strings"
@@ -76,25 +77,30 @@ func TestInit(t *testing.T) {
7677
cmd, cleanup := dockerCli.createTestCmd()
7778
defer cleanup()
7879

80+
userData, _ := user.Current()
81+
currentUser := ""
82+
if userData != nil {
83+
currentUser = userData.Username
84+
}
85+
7986
composeData := `version: "3.2"
8087
services:
8188
nginx:
8289
image: nginx:latest
8390
command: nginx $NGINX_ARGS ${NGINX_DRY_RUN}
8491
`
85-
meta := `# Version of the application
92+
meta := fmt.Sprintf(`# Version of the application
8693
version: 0.1.0
8794
# Name of the application
8895
name: app-test
8996
# A short description of the application
90-
description: my cool app
97+
description:
9198
# List of application maintainers with name and email for each
9299
maintainers:
93-
- name: dev1
100+
- name: %s
94101
email:
95-
- name: dev2
96-
97-
`
102+
`, currentUser)
103+
98104
envData := "# some comment\nNGINX_DRY_RUN=-t"
99105
tmpDir := fs.NewDir(t, "app_input",
100106
fs.WithFile(internal.ComposeFileName, composeData),
@@ -108,10 +114,7 @@ maintainers:
108114
cmd.Dir = tmpDir.Path()
109115
cmd.Command = dockerCli.Command("app",
110116
"init", testAppName,
111-
"--compose-file", tmpDir.Join(internal.ComposeFileName),
112-
"--description", "my cool app",
113-
"--maintainer", "dev1",
114-
"--maintainer", "dev2:[email protected]")
117+
"--compose-file", tmpDir.Join(internal.ComposeFileName))
115118
stdOut := icmd.RunCmd(cmd).Assert(t, icmd.Success).Combined()
116119
golden.Assert(t, stdOut, "init-output.golden")
117120

internal/commands/init.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,17 @@ import (
1111

1212
var (
1313
initComposeFile string
14-
initDescription string
15-
initMaintainers []string
1614
)
1715

1816
func initCmd(dockerCli command.Cli) *cobra.Command {
1917
cmd := &cobra.Command{
20-
Use: "init APP_NAME [--compose-file COMPOSE_FILE] [--description DESCRIPTION] [--maintainer NAME:EMAIL ...] [OPTIONS]",
18+
Use: "init APP_NAME [--compose-file COMPOSE_FILE] [OPTIONS]",
2119
Short: "Initialize Docker Application definition",
2220
Long: `Start building a Docker Application package. If there is a docker-compose.yml file in the current directory it will be copied and used.`,
23-
Example: `$ docker app init myapp --description "a useful description"`,
21+
Example: `$ docker app init myapp`,
2422
Args: cli.ExactArgs(1),
2523
RunE: func(cmd *cobra.Command, args []string) error {
26-
created, err := packager.Init(args[0], initComposeFile, initDescription, initMaintainers)
24+
created, err := packager.Init(args[0], initComposeFile)
2725
if err != nil {
2826
return err
2927
}
@@ -32,7 +30,5 @@ func initCmd(dockerCli command.Cli) *cobra.Command {
3230
},
3331
}
3432
cmd.Flags().StringVar(&initComposeFile, "compose-file", "", "Compose file to use as application base (optional)")
35-
cmd.Flags().StringVar(&initDescription, "description", "", "Human readable description of your application (optional)")
36-
cmd.Flags().StringArrayVar(&initMaintainers, "maintainer", []string{}, "Name and email address of person responsible for the application (name:email) (optional)")
3733
return cmd
3834
}

internal/packager/init.go

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
// Init is the entrypoint initialization function.
2828
// It generates a new application definition based on the provided parameters
2929
// and returns the path to the created application definition.
30-
func Init(name string, composeFile string, description string, maintainers []string) (string, error) {
30+
func Init(name string, composeFile string) (string, error) {
3131
if err := internal.ValidateAppName(name); err != nil {
3232
return "", err
3333
}
@@ -41,15 +41,10 @@ func Init(name string, composeFile string, description string, maintainers []str
4141
os.RemoveAll(dirName)
4242
}
4343
}()
44-
if err = writeMetadataFile(name, dirName, description, maintainers); err != nil {
44+
if err = writeMetadataFile(name, dirName); err != nil {
4545
return "", err
4646
}
4747

48-
if composeFile == "" {
49-
if _, err := os.Stat(internal.ComposeFileName); err == nil {
50-
composeFile = internal.ComposeFileName
51-
}
52-
}
5348
if composeFile == "" {
5449
err = initFromScratch(name)
5550
} else {
@@ -183,8 +178,8 @@ description: {{ .Description }}
183178
184179
{{ end }}`
185180

186-
func writeMetadataFile(name, dirName string, description string, maintainers []string) error {
187-
meta := newMetadata(name, description, maintainers)
181+
func writeMetadataFile(name, dirName string) error {
182+
meta := newMetadata(name)
188183
tmpl, err := template.New("metadata").Parse(metaTemplate)
189184
if err != nil {
190185
return errors.Wrap(err, "internal error parsing metadata template")
@@ -196,34 +191,14 @@ func writeMetadataFile(name, dirName string, description string, maintainers []s
196191
return ioutil.WriteFile(filepath.Join(dirName, internal.MetadataFileName), resBuf.Bytes(), 0644)
197192
}
198193

199-
// parseMaintainersData parses user-provided data through the maintainers flag and returns
200-
// a slice of Maintainer instances
201-
func parseMaintainersData(maintainers []string) []metadata.Maintainer {
202-
var res []metadata.Maintainer
203-
for _, m := range maintainers {
204-
ne := strings.SplitN(m, ":", 2)
205-
var email string
206-
if len(ne) > 1 {
207-
email = ne[1]
208-
}
209-
res = append(res, metadata.Maintainer{Name: ne[0], Email: email})
210-
}
211-
return res
212-
}
213-
214-
func newMetadata(name string, description string, maintainers []string) metadata.AppMetadata {
194+
func newMetadata(name string) metadata.AppMetadata {
215195
res := metadata.AppMetadata{
216-
Version: "0.1.0",
217-
Name: name,
218-
Description: description,
219-
}
220-
if len(maintainers) == 0 {
221-
userData, _ := user.Current()
222-
if userData != nil && userData.Username != "" {
223-
res.Maintainers = []metadata.Maintainer{{Name: userData.Username}}
224-
}
225-
} else {
226-
res.Maintainers = parseMaintainersData(maintainers)
196+
Version: "0.1.0",
197+
Name: name,
198+
}
199+
userData, _ := user.Current()
200+
if userData != nil && userData.Username != "" {
201+
res.Maintainers = []metadata.Maintainer{{Name: userData.Username}}
227202
}
228203
return res
229204
}

internal/packager/init_test.go

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package packager
22

33
import (
4+
"fmt"
5+
"os/user"
46
"testing"
57

68
"github.com/docker/app/internal"
7-
"github.com/docker/app/types/metadata"
89
"gotest.tools/assert"
910
"gotest.tools/fs"
1011
)
@@ -159,46 +160,29 @@ func TestWriteMetadataFile(t *testing.T) {
159160
tmpdir := fs.NewDir(t, appName)
160161
defer tmpdir.Remove()
161162

162-
err := writeMetadataFile(appName, tmpdir.Path(), "", []string{"dev:[email protected]"})
163+
err := writeMetadataFile(appName, tmpdir.Path())
163164
assert.NilError(t, err)
164165

165-
data := `# Version of the application
166+
userData, _ := user.Current()
167+
currentUser := ""
168+
if userData != nil {
169+
currentUser = userData.Username
170+
}
171+
172+
data := fmt.Sprintf(`# Version of the application
166173
version: 0.1.0
167174
# Name of the application
168175
name: writemetadata_test
169176
# A short description of the application
170177
description:
171178
# List of application maintainers with name and email for each
172179
maintainers:
173-
- name: dev
174-
175-
`
180+
- name: %s
181+
email:
182+
`, currentUser)
176183

177184
manifest := fs.Expected(t,
178185
fs.WithFile(internal.MetadataFileName, data, fs.WithMode(0644)),
179186
)
180187
assert.Assert(t, fs.Equal(tmpdir.Path(), manifest))
181188
}
182-
183-
func TestParseMaintainersData(t *testing.T) {
184-
input := []string{
185-
186-
"marisa.kirisame",
187-
"Reimu Hakurei",
188-
"Hong Meiling:[email protected]",
189-
" : ",
190-
"perfect:cherry:blossom",
191-
}
192-
193-
expectedOutput := []metadata.Maintainer{
194-
{Name: "sakuya", Email: "[email protected]"},
195-
{Name: "marisa.kirisame", Email: ""},
196-
{Name: "Reimu Hakurei", Email: ""},
197-
{Name: "Hong Meiling", Email: "[email protected]"},
198-
{Name: " ", Email: " "},
199-
{Name: "perfect", Email: "cherry:blossom"},
200-
}
201-
output := parseMaintainersData(input)
202-
203-
assert.DeepEqual(t, output, expectedOutput)
204-
}

0 commit comments

Comments
 (0)