Skip to content

Commit 0db574e

Browse files
authored
Merge pull request #372 from milas/proj-with-name
2 parents a4da4da + 8d95f08 commit 0db574e

File tree

2 files changed

+74
-13
lines changed

2 files changed

+74
-13
lines changed

cli/options.go

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,59 @@ import (
2323
"path/filepath"
2424
"strings"
2525

26+
"github.com/pkg/errors"
27+
"github.com/sirupsen/logrus"
28+
2629
"github.com/compose-spec/compose-go/consts"
2730
"github.com/compose-spec/compose-go/dotenv"
2831
"github.com/compose-spec/compose-go/errdefs"
2932
"github.com/compose-spec/compose-go/loader"
3033
"github.com/compose-spec/compose-go/types"
3134
"github.com/compose-spec/compose-go/utils"
32-
"github.com/pkg/errors"
33-
"github.com/sirupsen/logrus"
3435
)
3536

36-
// ProjectOptions groups the command line options recommended for a Compose implementation
37+
// ProjectOptions provides common configuration for loading a project.
3738
type ProjectOptions struct {
38-
Name string
39-
WorkingDir string
39+
// Name is a valid Compose project name to be used or empty.
40+
//
41+
// If empty, the project loader will automatically infer a reasonable
42+
// project name if possible.
43+
Name string
44+
45+
// WorkingDir is a file path to use as the project directory or empty.
46+
//
47+
// If empty, the project loader will automatically infer a reasonable
48+
// working directory if possible.
49+
WorkingDir string
50+
51+
// ConfigPaths are file paths to one or more Compose files.
52+
//
53+
// These are applied in order by the loader following the merge logic
54+
// as described in the spec.
55+
//
56+
// The first entry is required and is the primary Compose file.
57+
// For convenience, WithConfigFileEnv and WithDefaultConfigPath
58+
// are provided to populate this in a predictable manner.
4059
ConfigPaths []string
60+
61+
// Environment are additional environment variables to make available
62+
// for interpolation.
63+
//
64+
// NOTE: For security, the loader does not automatically expose any
65+
// process environment variables. For convenience, WithOsEnv can be
66+
// used if appropriate.
4167
Environment map[string]string
42-
EnvFiles []string
68+
69+
// EnvFiles are file paths to ".env" files with additional environment
70+
// variable data.
71+
//
72+
// These are loaded in-order, so it is possible to override variables or
73+
// in subsequent files.
74+
//
75+
// This field is optional, but any file paths that are included here must
76+
// exist or an error will be returned during load.
77+
EnvFiles []string
78+
4379
loadOptions []func(*loader.Options)
4480
}
4581

@@ -63,9 +99,15 @@ func NewProjectOptions(configs []string, opts ...ProjectOptionsFn) (*ProjectOpti
6399
// WithName defines ProjectOptions' name
64100
func WithName(name string) ProjectOptionsFn {
65101
return func(o *ProjectOptions) error {
66-
normalized := loader.NormalizeProjectName(name)
67-
if err := loader.CheckOriginalProjectNameIsNormalized(name, normalized); err != nil {
68-
return err
102+
// a project (once loaded) cannot have an empty name
103+
// however, on the options object, the name is optional: if unset,
104+
// a name will be inferred by the loader, so it's legal to set the
105+
// name to an empty string here
106+
if name != "" {
107+
normalized := loader.NormalizeProjectName(name)
108+
if err := loader.CheckOriginalProjectNameIsNormalized(name, normalized); err != nil {
109+
return err
110+
}
69111
}
70112
o.Name = name
71113
return nil

cli/options_test.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ import (
2222
"path/filepath"
2323
"testing"
2424

25+
"gotest.tools/v3/assert"
26+
2527
"github.com/compose-spec/compose-go/consts"
2628
"github.com/compose-spec/compose-go/utils"
27-
"gotest.tools/v3/assert"
2829
)
2930

3031
func TestProjectName(t *testing.T) {
@@ -52,9 +53,27 @@ func TestProjectName(t *testing.T) {
5253
assert.Equal(t, p.Name, "42my_project_env")
5354
})
5455

55-
t.Run("by name must not be empty", func(t *testing.T) {
56-
_, err := NewProjectOptions([]string{"testdata/simple/compose.yaml"}, WithName(""))
57-
assert.ErrorContains(t, err, `project name must not be empty`)
56+
t.Run("by name empty", func(t *testing.T) {
57+
opts, err := NewProjectOptions(
58+
[]string{"testdata/simple/compose.yaml"},
59+
WithName(""),
60+
)
61+
assert.NilError(t, err)
62+
p, err := ProjectFromOptions(opts)
63+
assert.NilError(t, err)
64+
assert.Equal(t, p.Name, "simple")
65+
})
66+
67+
t.Run("by name empty working dir", func(t *testing.T) {
68+
opts, err := NewProjectOptions(
69+
[]string{"testdata/simple/compose.yaml"},
70+
WithName(""),
71+
WithWorkingDirectory("/path/to/proj"),
72+
)
73+
assert.NilError(t, err)
74+
p, err := ProjectFromOptions(opts)
75+
assert.NilError(t, err)
76+
assert.Equal(t, p.Name, "proj")
5877
})
5978

6079
t.Run("by name must not come from root directory", func(t *testing.T) {

0 commit comments

Comments
 (0)