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

Commit fdb9864

Browse files
author
Matthieu Nottale
committed
save, push: Use version and repository_prefix from metadata by default.
Signed-off-by: Matthieu Nottale <[email protected]>
1 parent e8a6c21 commit fdb9864

File tree

9 files changed

+45
-23
lines changed

9 files changed

+45
-23
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ It should look like this:
4646
version: 0.1.0
4747
name: hello
4848
description: ""
49+
repository_prefix: ""
4950
maintainers:
5051
- name: yourusername
5152
email: ""

cmd/docker-app/push.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,10 @@ func pushCmd() *cobra.Command {
1717
Short: "Push the application to a registry",
1818
Args: cli.RequiresMaxArgs(1),
1919
RunE: func(cmd *cobra.Command, args []string) error {
20-
if pushTag == "" {
21-
pushTag = "latest"
22-
}
2320
return packager.Push(firstOrEmpty(args), pushPrefix, pushTag)
2421
},
2522
}
26-
cmd.Flags().StringVarP(&pushPrefix, "prefix", "p", "", "prefix to use")
27-
cmd.Flags().StringVarP(&pushTag, "tag", "t", "latest", "tag to use")
23+
cmd.Flags().StringVarP(&pushPrefix, "prefix", "p", "", "prefix to use (default: repository_prefix in metadata)")
24+
cmd.Flags().StringVarP(&pushTag, "tag", "t", "", "tag to use (default: version in metadata")
2825
return cmd
2926
}

cmd/docker-app/save.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@ func saveCmd() *cobra.Command {
1717
Short: "Save the application as an image to the docker daemon(in preparation for push)",
1818
Args: cli.RequiresMaxArgs(1),
1919
RunE: func(cmd *cobra.Command, args []string) error {
20-
if saveTag == "" {
21-
saveTag = "latest"
22-
}
23-
return packager.Save(firstOrEmpty(args), savePrefix, saveTag)
20+
_, err := packager.Save(firstOrEmpty(args), savePrefix, saveTag)
21+
return err
2422
},
2523
}
26-
cmd.Flags().StringVarP(&savePrefix, "prefix", "p", "", "prefix to use")
27-
cmd.Flags().StringVarP(&saveTag, "tag", "t", "latest", "tag to use")
24+
cmd.Flags().StringVarP(&savePrefix, "prefix", "p", "", "prefix to use (default: repository_prefix in metadata)")
25+
cmd.Flags().StringVarP(&saveTag, "tag", "t", "", "tag to use (default: version in metadata)")
2826
return cmd
2927
}

e2e/binary_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ func TestInitBinary(t *testing.T) {
134134
meta := `version: 0.1.0
135135
name: app_test
136136
description: my cool app
137+
repository_prefix: ""
137138
maintainers:
138139
- name: bob
139140
email: ""

e2e/testdata/init-singlefile.dockerapp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
version: 0.1.0
33
name: tac
44
description: my cool app
5+
repository_prefix: ""
56
maintainers:
67
- name: bob
78
email: ""

examples/hello-world/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ $ cat hello-world.dockerapp
1818
version: 0.1.0
1919
name: hello-world
2020
description: ""
21+
repository_prefix: ""
2122
maintainers:
2223
- name: dimrok
2324
email: ""

examples/hello-world/hello-world.dockerapp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
version: 0.1.0
55
name: hello-world
66
description: "Hello world!"
7+
repository_prefix: "myhubuser/"
78
maintainers:
89
- name: user
910

packager/registry.go

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,41 @@ import (
1212
"strings"
1313

1414
"github.com/docker/lunchbox/constants"
15+
"github.com/docker/lunchbox/types"
1516
"github.com/docker/lunchbox/utils"
1617
"github.com/pkg/errors"
18+
yaml "gopkg.in/yaml.v2"
1719
)
1820

1921
func appName(appname string) string {
2022
return utils.AppNameFromDir(appname)
2123
}
2224

23-
// Save saves an app to docker
24-
func Save(appname, prefix, tag string) error {
25+
// Save saves an app to docker and returns the image name.
26+
func Save(appname, prefix, tag string) (string, error) {
2527
appname, cleanup, err := Extract(appname)
2628
if err != nil {
27-
return err
29+
return "", err
2830
}
2931
defer cleanup()
32+
if prefix == "" || tag == "" {
33+
metaFile := filepath.Join(appname, "metadata.yml")
34+
metaContent, err := ioutil.ReadFile(metaFile)
35+
if err != nil {
36+
return "", err
37+
}
38+
var meta types.AppMetadata
39+
err = yaml.Unmarshal(metaContent, &meta)
40+
if err != nil {
41+
return "", err
42+
}
43+
if tag == "" {
44+
tag = meta.Version
45+
}
46+
if prefix == "" {
47+
prefix = meta.RepositoryPrefix
48+
}
49+
}
3050
dockerfile := `
3151
FROM scratch
3252
COPY / /
@@ -35,15 +55,16 @@ COPY / /
3555
ioutil.WriteFile(df, []byte(dockerfile), 0644)
3656
di := filepath.Join(appname, ".dockerignore")
3757
ioutil.WriteFile(di, []byte("__Dockerfile-docker-app__\n.dockerignore"), 0644)
38-
args := []string{"build", "-t", prefix + appName(appname) + constants.AppExtension + ":" + tag, "-f", df, appname}
58+
imageName := prefix + appName(appname) + constants.AppExtension + ":" + tag
59+
args := []string{"build", "-t", imageName, "-f", df, appname}
3960
cmd := exec.Command("docker", args...)
4061
output, err := cmd.CombinedOutput()
4162
os.Remove(df)
4263
os.Remove(di)
4364
if err != nil {
4465
fmt.Println(string(output))
4566
}
46-
return err
67+
return imageName, err
4768
}
4869

4970
// Load loads an app from docker
@@ -89,11 +110,11 @@ func Push(appname, prefix, tag string) error {
89110
return err
90111
}
91112
defer cleanup()
92-
err = Save(appname, prefix, tag)
113+
imageName, err := Save(appname, prefix, tag)
93114
if err != nil {
94115
return err
95116
}
96-
cmd := exec.Command("docker", "push", prefix+appName(appname)+constants.AppExtension+":"+tag)
117+
cmd := exec.Command("docker", "push", imageName)
97118
output, err := cmd.CombinedOutput()
98119
if err != nil {
99120
return errors.Wrapf(err, "error from docker push command: %s", string(output))

types/metadata.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ type Maintainer struct {
88

99
// AppMetadata is the format of the data found inside the metadata.yml file
1010
type AppMetadata struct {
11-
Version string
12-
Name string
13-
Description string
14-
Maintainers []Maintainer
15-
Targets ApplicationTarget
11+
Version string
12+
Name string
13+
Description string
14+
RepositoryPrefix string `yaml:"repository_prefix"`
15+
Maintainers []Maintainer
16+
Targets ApplicationTarget
1617
}
1718

1819
// ApplicationTarget represents which platform(s) / orchestrator(s) the

0 commit comments

Comments
 (0)