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

Commit 356151c

Browse files
Merge pull request #366 from chris-crone/settings-refactor
Expose settings functions externally
2 parents 2f1dbb8 + 6920676 commit 356151c

24 files changed

Lines changed: 268 additions & 297 deletions

cmd/docker-app/validate.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package main
22

33
import (
44
"github.com/docker/app/internal/packager"
5-
"github.com/docker/app/internal/validator"
5+
"github.com/docker/app/render"
66
"github.com/docker/app/types"
77
"github.com/docker/cli/cli"
88
cliopts "github.com/docker/cli/opts"
@@ -28,7 +28,8 @@ func validateCmd() *cobra.Command {
2828
}
2929
defer app.Cleanup()
3030
argSettings := cliopts.ConvertKVStringsToMap(validateEnv)
31-
return validator.Validate(app, argSettings)
31+
_, err = render.Render(app, argSettings)
32+
return err
3233
},
3334
}
3435
cmd.Flags().StringArrayVarP(&validateSettingsFile, "settings-files", "f", []string{}, "Override settings files")

internal/helm/helm.go

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ import (
1313
"github.com/docker/app/internal/helm/templateconversion"
1414
"github.com/docker/app/internal/helm/templateloader"
1515
"github.com/docker/app/internal/helm/templatev1beta2"
16-
"github.com/docker/app/internal/settings"
1716
"github.com/docker/app/internal/slices"
1817
"github.com/docker/app/internal/yaml"
1918
"github.com/docker/app/render"
2019
"github.com/docker/app/types"
2120
"github.com/docker/app/types/metadata"
21+
"github.com/docker/app/types/settings"
2222
"github.com/docker/cli/cli/command/stack/kubernetes"
2323
"github.com/docker/cli/cli/compose/loader"
2424
"github.com/docker/cli/kubernetes/compose/v1beta1"
@@ -49,8 +49,8 @@ func Helm(app *types.App, env map[string]string, shouldRender bool, stackVersion
4949
if err := os.MkdirAll(targetDir, 0755); err != nil {
5050
return errors.Wrap(err, "failed to create Chart directory")
5151
}
52-
err := makeChart(app.Metadata(), targetDir)
53-
if err != nil {
52+
meta := app.Metadata()
53+
if err := makeChart(&meta, targetDir); err != nil {
5454
return err
5555
}
5656
if shouldRender {
@@ -70,8 +70,7 @@ func Helm(app *types.App, env map[string]string, shouldRender bool, stackVersion
7070
for k := range vars {
7171
variables = append(variables, k)
7272
}
73-
err = makeStack(app.Name, targetDir, data, stackVersion)
74-
if err != nil {
73+
if err := makeStack(app.Name, targetDir, data, stackVersion); err != nil {
7574
return err
7675
}
7776
return makeValues(app, targetDir, env, variables)
@@ -80,11 +79,8 @@ func Helm(app *types.App, env map[string]string, shouldRender bool, stackVersion
8079
// makeValues updates helm values.yaml with used variables from settings and env
8180
func makeValues(app *types.App, targetDir string, env map[string]string, variables []string) error {
8281
// merge our variables into Values.yaml
83-
s, err := settings.LoadMultiple(app.Settings())
84-
if err != nil {
85-
return err
86-
}
87-
metaPrefixed, err := settings.Load(app.Metadata(), settings.WithPrefix("app"))
82+
s := app.Settings()
83+
metaPrefixed, err := settings.Load(app.MetadataRaw(), settings.WithPrefix("app"))
8884
if err != nil {
8985
return err
9086
}
@@ -210,13 +206,8 @@ func helmRender(app *types.App, targetDir string, env map[string]string, stackVe
210206
return ioutil.WriteFile(filepath.Join(targetDir, "templates", "stack.yaml"), stackData, 0644)
211207
}
212208

213-
func makeChart(data []byte, targetDir string) error {
214-
var meta metadata.AppMetadata
215-
err := yaml.Unmarshal(data, &meta)
216-
if err != nil {
217-
return errors.Wrap(err, "failed to parse application metadata")
218-
}
219-
hmeta, err := toHelmMeta(&meta)
209+
func makeChart(meta *metadata.AppMetadata, targetDir string) error {
210+
hmeta, err := toHelmMeta(meta)
220211
if err != nil {
221212
return errors.Wrap(err, "failed to convert application metadata")
222213
}

internal/inspect/inspect.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,17 @@ import (
66
"sort"
77
"text/tabwriter"
88

9-
"github.com/docker/app/internal/settings"
10-
"github.com/docker/app/internal/yaml"
119
"github.com/docker/app/types"
12-
"github.com/docker/app/types/metadata"
13-
"github.com/pkg/errors"
1410
)
1511

1612
// Inspect dumps the metadata of an app
1713
func Inspect(out io.Writer, app *types.App) error {
18-
var meta metadata.AppMetadata
19-
err := yaml.Unmarshal(app.Metadata(), &meta)
20-
if err != nil {
21-
return errors.Wrap(err, "failed to parse application metadata")
22-
}
14+
meta := app.Metadata()
2315
// extract settings
24-
s, err := settings.LoadMultiple(app.Settings())
25-
if err != nil {
26-
return errors.Wrap(err, "failed to load application settings")
27-
}
28-
fs := s.Flatten()
16+
settings := app.Settings().Flatten()
2917
// sort the keys to get consistent output
3018
var settingsKeys []string
31-
for k := range fs {
19+
for k := range settings {
3220
settingsKeys = append(settingsKeys, k)
3321
}
3422
sort.Slice(settingsKeys, func(i, j int) bool { return settingsKeys[i] < settingsKeys[j] })
@@ -47,7 +35,7 @@ func Inspect(out io.Writer, app *types.App) error {
4735
fmt.Fprintln(w, "Setting\tDefault")
4836
fmt.Fprintln(w, "-------\t-------")
4937
for _, k := range settingsKeys {
50-
fmt.Fprintf(w, "%s\t%s\n", k, fs[k])
38+
fmt.Fprintf(w, "%s\t%s\n", k, settings[k])
5139
}
5240
w.Flush()
5341
return nil

internal/inspect/inspect_test.go

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@ package inspect
33
import (
44
"bytes"
55
"fmt"
6-
"io/ioutil"
76
"testing"
87

98
"github.com/docker/app/internal"
109
"github.com/docker/app/types"
1110
"gotest.tools/assert"
12-
is "gotest.tools/assert/cmp"
1311
"gotest.tools/fs"
1412
"gotest.tools/golden"
1513
)
@@ -22,32 +20,6 @@ services:
2220
image: nginx`
2321
)
2422

25-
func TestInspectErrorsOnFiles(t *testing.T) {
26-
dir := fs.NewDir(t, "inspect-errors",
27-
fs.WithDir("unparseable-metadata-app",
28-
fs.WithFile(internal.ComposeFileName, composeYAML),
29-
fs.WithFile(internal.MetadataFileName, `something is wrong`),
30-
fs.WithFile(internal.SettingsFileName, "foo"),
31-
),
32-
fs.WithDir("unparseable-settings-app",
33-
fs.WithFile(internal.ComposeFileName, composeYAML),
34-
fs.WithFile(internal.MetadataFileName, `{}`),
35-
fs.WithFile(internal.SettingsFileName, "foo"),
36-
),
37-
)
38-
defer dir.Remove()
39-
40-
for appname, expectedError := range map[string]string{
41-
"unparseable-metadata-app": "failed to parse application metadat",
42-
"unparseable-settings-app": "failed to load application settings",
43-
} {
44-
app, err := types.NewAppFromDefaultFiles(dir.Join(appname))
45-
assert.NilError(t, err)
46-
err = Inspect(ioutil.Discard, app)
47-
assert.Check(t, is.ErrorContains(err, expectedError))
48-
}
49-
}
50-
5123
func TestInspect(t *testing.T) {
5224
dir := fs.NewDir(t, "inspect",
5325
fs.WithDir("no-maintainers",

internal/packager/init.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,12 +237,10 @@ func newMetadata(name string, description string, maintainers []string) metadata
237237
Description: description,
238238
}
239239
if len(maintainers) == 0 {
240-
var userName string
241240
userData, _ := user.Current()
242-
if userData != nil {
243-
userName = userData.Username
241+
if userData != nil && userData.Username != "" {
242+
res.Maintainers = []metadata.Maintainer{{Name: userData.Username}}
244243
}
245-
res.Maintainers = []metadata.Maintainer{{Name: userName}}
246244
} else {
247245
res.Maintainers = parseMaintainersData(maintainers)
248246
}

internal/packager/registry.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ import (
88
"strings"
99

1010
"github.com/docker/app/internal"
11-
"github.com/docker/app/internal/yaml"
1211
"github.com/docker/app/pkg/resto"
1312
"github.com/docker/app/types"
14-
"github.com/docker/app/types/metadata"
1513
"github.com/docker/distribution/reference"
1614
"github.com/pkg/errors"
1715
log "github.com/sirupsen/logrus"
@@ -70,14 +68,11 @@ func Pull(repotag string, outputDir string) (string, error) {
7068
// Push pushes an app to a registry. Returns the image digest.
7169
func Push(app *types.App, namespace, tag string) (string, error) {
7270
payload := make(map[string]string)
73-
payload[internal.MetadataFileName] = string(app.Metadata())
71+
payload[internal.MetadataFileName] = string(app.MetadataRaw())
7472
payload[internal.ComposeFileName] = string(app.Composes()[0])
75-
payload[internal.SettingsFileName] = string(app.Settings()[0])
73+
payload[internal.SettingsFileName] = string(app.SettingsRaw()[0])
7674
if namespace == "" || tag == "" {
77-
var metadata metadata.AppMetadata
78-
if err := yaml.Unmarshal(app.Metadata(), &metadata); err != nil {
79-
return "", errors.Wrap(err, "failed to parse application metadata")
80-
}
75+
metadata := app.Metadata()
8176
if namespace == "" {
8277
namespace = metadata.Namespace
8378
}

internal/packager/split.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ func Split(app *types.App, outputDir string) error {
1616
if len(app.Composes()) > 1 {
1717
return errors.New("split: multiple compose files is not supported")
1818
}
19-
if len(app.Settings()) > 1 {
19+
if len(app.SettingsRaw()) > 1 {
2020
return errors.New("split: multiple setting files is not supported")
2121
}
2222
err := os.MkdirAll(outputDir, 0755)
2323
if err != nil {
2424
return err
2525
}
2626
for file, data := range map[string][]byte{
27-
internal.MetadataFileName: app.Metadata(),
27+
internal.MetadataFileName: app.MetadataRaw(),
2828
internal.ComposeFileName: app.Composes()[0],
29-
internal.SettingsFileName: app.Settings()[0],
29+
internal.SettingsFileName: app.SettingsRaw()[0],
3030
} {
3131
if err := ioutil.WriteFile(filepath.Join(outputDir, file), data, 0644); err != nil {
3232
return err
@@ -40,15 +40,15 @@ func Merge(app *types.App, target io.Writer) error {
4040
if len(app.Composes()) > 1 {
4141
return errors.New("merge: multiple compose files is not supported")
4242
}
43-
if len(app.Settings()) > 1 {
43+
if len(app.SettingsRaw()) > 1 {
4444
return errors.New("merge: multiple setting files is not supported")
4545
}
4646
for _, data := range [][]byte{
47-
app.Metadata(),
47+
app.MetadataRaw(),
4848
[]byte(types.SingleFileSeparator),
4949
app.Composes()[0],
5050
[]byte(types.SingleFileSeparator),
51-
app.Settings()[0],
51+
app.SettingsRaw()[0],
5252
} {
5353
if _, err := target.Write(data); err != nil {
5454
return err

internal/validator/validate.go

Lines changed: 0 additions & 43 deletions
This file was deleted.

internal/validator/validate_test.go

Lines changed: 0 additions & 92 deletions
This file was deleted.

0 commit comments

Comments
 (0)