@@ -64,16 +64,13 @@ type Options struct {
64
64
projectName string
65
65
// Indicates when the projectName was imperatively set or guessed from path
66
66
projectNameImperativelySet bool
67
- // The value of projectName before normalization
68
- projectNameBeforeNormalization string
69
67
// Profiles set profiles to enable
70
68
Profiles []string
71
69
}
72
70
73
71
func (o * Options ) SetProjectName (name string , imperativelySet bool ) {
74
- o .projectName = NormalizeProjectName ( name )
72
+ o .projectName = name
75
73
o .projectNameImperativelySet = imperativelySet
76
- o .projectNameBeforeNormalization = name
77
74
}
78
75
79
76
func (o Options ) GetProjectName () (string , bool ) {
@@ -267,49 +264,54 @@ func Load(configDetails types.ConfigDetails, options ...func(*Options)) (*types.
267
264
return project , err
268
265
}
269
266
270
- func CheckOriginalProjectNameIsNormalized (original , normalized string ) error {
271
- if original != normalized {
272
- return fmt .Errorf ("%q is not a valid project name: it must contain only " +
273
- "characters from [a-z0-9_-] and start with [a-z0-9]" , original )
274
- } else if normalized == "" {
275
- return fmt .Errorf ("project name must not be empty" )
276
- }
277
- return nil
267
+ func InvalidProjectNameErr (v string ) error {
268
+ return fmt .Errorf (
269
+ "%q is not a valid project name: it must contain only " +
270
+ "characters from [a-z0-9_-] and start with [a-z0-9]" , v ,
271
+ )
278
272
}
279
273
280
274
func projectName (details types.ConfigDetails , opts * Options ) (string , error ) {
281
275
projectName , projectNameImperativelySet := opts .GetProjectName ()
282
- projectNameBeforeNormalization := opts .projectNameBeforeNormalization
283
276
284
- var pjNameFromConfigFile string
285
-
286
- for _ , configFile := range details .ConfigFiles {
287
- yml , err := ParseYAML (configFile .Content )
288
- if err != nil {
289
- return "" , nil
277
+ // if user did NOT provide a name explicitly, then see if one is defined
278
+ // in any of the config files
279
+ if ! projectNameImperativelySet {
280
+ var pjNameFromConfigFile string
281
+ for _ , configFile := range details .ConfigFiles {
282
+ yml , err := ParseYAML (configFile .Content )
283
+ if err != nil {
284
+ return "" , nil
285
+ }
286
+ if val , ok := yml ["name" ]; ok && val != "" {
287
+ pjNameFromConfigFile = yml ["name" ].(string )
288
+ }
290
289
}
291
- if val , ok := yml ["name" ]; ok && val != "" {
292
- pjNameFromConfigFile = yml ["name" ].(string )
290
+ if ! opts .SkipInterpolation {
291
+ interpolated , err := interp .Interpolate (
292
+ map [string ]interface {}{"name" : pjNameFromConfigFile },
293
+ * opts .Interpolate ,
294
+ )
295
+ if err != nil {
296
+ return "" , err
297
+ }
298
+ pjNameFromConfigFile = interpolated ["name" ].(string )
293
299
}
294
- }
295
- if ! opts .SkipInterpolation {
296
- interpolated , err := interp .Interpolate (map [string ]interface {}{"name" : pjNameFromConfigFile }, * opts .Interpolate )
297
- if err != nil {
298
- return "" , err
300
+ pjNameFromConfigFile = NormalizeProjectName (pjNameFromConfigFile )
301
+ if pjNameFromConfigFile != "" {
302
+ projectName = pjNameFromConfigFile
299
303
}
300
- pjNameFromConfigFile = interpolated ["name" ].(string )
301
304
}
302
- pjNameFromConfigFileNormalized := NormalizeProjectName (pjNameFromConfigFile )
303
- if ! projectNameImperativelySet && pjNameFromConfigFileNormalized != "" {
304
- projectName = pjNameFromConfigFileNormalized
305
- projectNameBeforeNormalization = pjNameFromConfigFile
305
+
306
+ if projectName == "" {
307
+ return "" , errors .New ("project name must not be empty" )
306
308
}
307
309
308
- if err := CheckOriginalProjectNameIsNormalized (
309
- projectNameBeforeNormalization , projectName ); err != nil {
310
- return "" , err
310
+ if NormalizeProjectName (projectName ) != projectName {
311
+ return "" , InvalidProjectNameErr (projectName )
311
312
}
312
313
314
+ // TODO(milas): this should probably ALWAYS set (overriding any existing)
313
315
if _ , ok := details .Environment [consts .ComposeProjectName ]; ! ok && projectName != "" {
314
316
details .Environment [consts .ComposeProjectName ] = projectName
315
317
}
0 commit comments