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

Commit c98a1fd

Browse files
committed
init: Output name of created application definition
Signed-off-by: Christopher Crone <[email protected]>
1 parent 543a5a2 commit c98a1fd

File tree

5 files changed

+30
-18
lines changed

5 files changed

+30
-18
lines changed

e2e/commands_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ maintainers:
131131
"--description", "my cool app",
132132
"--maintainer", "dev1",
133133
"--maintainer", "dev2:[email protected]")
134-
icmd.RunCmd(cmd).Assert(t, icmd.Success)
134+
stdOut := icmd.RunCmd(cmd).Assert(t, icmd.Success).Combined()
135+
golden.Assert(t, stdOut, "init-output.golden")
135136

136137
manifest := fs.Expected(
137138
t,

e2e/testdata/init-output.golden

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
You will need to edit parameters.yml to fill in default values.
2+
Created "app-test.dockerapp"

internal/commands/init.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package commands
22

33
import (
4+
"fmt"
5+
46
"github.com/docker/app/internal/packager"
57
"github.com/docker/cli/cli"
8+
"github.com/docker/cli/cli/command"
69
"github.com/spf13/cobra"
710
)
811

@@ -13,15 +16,20 @@ var (
1316
initSingleFile bool
1417
)
1518

16-
func initCmd() *cobra.Command {
19+
func initCmd(dockerCli command.Cli) *cobra.Command {
1720
cmd := &cobra.Command{
1821
Use: "init APP_NAME [--compose-file COMPOSE_FILE] [--description DESCRIPTION] [--maintainer NAME:EMAIL ...] [OPTIONS]",
1922
Short: "Initialize Docker Application definition",
2023
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.`,
2124
Example: `$ docker app init myapp --description "a useful description"`,
2225
Args: cli.ExactArgs(1),
2326
RunE: func(cmd *cobra.Command, args []string) error {
24-
return packager.Init(args[0], initComposeFile, initDescription, initMaintainers, initSingleFile)
27+
created, err := packager.Init(args[0], initComposeFile, initDescription, initMaintainers, initSingleFile)
28+
if err != nil {
29+
return err
30+
}
31+
fmt.Fprintf(dockerCli.Out(), "Created %q\n", created)
32+
return nil
2533
},
2634
}
2735
cmd.Flags().StringVar(&initComposeFile, "compose-file", "", "Compose file to use as application base (optional)")

internal/commands/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func addCommands(cmd *cobra.Command, dockerCli command.Cli) {
2727
upgradeCmd(dockerCli),
2828
uninstallCmd(dockerCli),
2929
statusCmd(dockerCli),
30-
initCmd(),
30+
initCmd(dockerCli),
3131
inspectCmd(dockerCli),
3232
mergeCmd(dockerCli),
3333
renderCmd(dockerCli),

internal/packager/init.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,15 @@ func prependToFile(filename, text string) error {
3232
}
3333

3434
// Init is the entrypoint initialization function.
35-
// It generates a new application package based on the provided parameters.
36-
func Init(name string, composeFile string, description string, maintainers []string, singleFile bool) error {
35+
// It generates a new application definition based on the provided parameters
36+
// and returns the path to the created application definition.
37+
func Init(name string, composeFile string, description string, maintainers []string, singleFile bool) (string, error) {
3738
if err := internal.ValidateAppName(name); err != nil {
38-
return err
39+
return "", err
3940
}
4041
dirName := internal.DirNameFromAppName(name)
4142
if err := os.Mkdir(dirName, 0755); err != nil {
42-
return errors.Wrap(err, "failed to create application directory")
43+
return "", errors.Wrap(err, "failed to create application directory")
4344
}
4445
var err error
4546
defer func() {
@@ -48,7 +49,7 @@ func Init(name string, composeFile string, description string, maintainers []str
4849
}
4950
}()
5051
if err = writeMetadataFile(name, dirName, description, maintainers); err != nil {
51-
return err
52+
return "", err
5253
}
5354

5455
if composeFile == "" {
@@ -62,40 +63,40 @@ func Init(name string, composeFile string, description string, maintainers []str
6263
err = initFromComposeFile(name, composeFile)
6364
}
6465
if err != nil {
65-
return err
66+
return "", err
6667
}
6768
if !singleFile {
68-
return nil
69+
return dirName, nil
6970
}
7071
// Merge as a single file
7172
// Add some helfpful comments to distinguish the sections
7273
if err := prependToFile(filepath.Join(dirName, internal.ComposeFileName), "# This section contains the Compose file that describes your application services.\n"); err != nil {
73-
return err
74+
return "", err
7475
}
7576
if err := prependToFile(filepath.Join(dirName, internal.ParametersFileName), "# This section contains the default values for your application parameters.\n"); err != nil {
76-
return err
77+
return "", err
7778
}
7879
if err := prependToFile(filepath.Join(dirName, internal.MetadataFileName), "# This section contains your application metadata.\n"); err != nil {
79-
return err
80+
return "", err
8081
}
8182

8283
temp := "_temp_dockerapp__.dockerapp"
8384
err = os.Rename(dirName, temp)
8485
if err != nil {
85-
return err
86+
return "", err
8687
}
8788
defer os.RemoveAll(temp)
8889
var target io.Writer
8990
target, err = os.Create(dirName)
9091
if err != nil {
91-
return err
92+
return "", err
9293
}
9394
defer target.(io.WriteCloser).Close()
9495
app, err := loader.LoadFromDirectory(temp)
9596
if err != nil {
96-
return err
97+
return "", err
9798
}
98-
return Merge(app, target)
99+
return dirName, Merge(app, target)
99100
}
100101

101102
func initFromScratch(name string) error {

0 commit comments

Comments
 (0)