-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce
include
to load sub-compose projects as dependencies
Signed-off-by: Nicolas De Loof <[email protected]>
- Loading branch information
Showing
10 changed files
with
336 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
Copyright 2020 The Compose Specification Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package dotenv | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
func GetEnvFromFile(currentEnv map[string]string, workingDir string, filenames []string) (map[string]string, error) { | ||
envMap := make(map[string]string) | ||
|
||
dotEnvFiles := filenames | ||
if len(dotEnvFiles) == 0 { | ||
dotEnvFiles = append(dotEnvFiles, filepath.Join(workingDir, ".env")) | ||
} | ||
for _, dotEnvFile := range dotEnvFiles { | ||
abs, err := filepath.Abs(dotEnvFile) | ||
if err != nil { | ||
return envMap, err | ||
} | ||
dotEnvFile = abs | ||
|
||
s, err := os.Stat(dotEnvFile) | ||
if os.IsNotExist(err) { | ||
if len(filenames) == 0 { | ||
return envMap, nil | ||
} | ||
return envMap, errors.Errorf("Couldn't find env file: %s", dotEnvFile) | ||
} | ||
if err != nil { | ||
return envMap, err | ||
} | ||
|
||
if s.IsDir() { | ||
if len(filenames) == 0 { | ||
return envMap, nil | ||
} | ||
return envMap, errors.Errorf("%s is a directory", dotEnvFile) | ||
} | ||
|
||
b, err := os.ReadFile(dotEnvFile) | ||
if os.IsNotExist(err) { | ||
return nil, errors.Errorf("Couldn't read env file: %s", dotEnvFile) | ||
} | ||
if err != nil { | ||
return envMap, err | ||
} | ||
|
||
env, err := ParseWithLookup(bytes.NewReader(b), func(k string) (string, bool) { | ||
v, ok := currentEnv[k] | ||
if ok { | ||
return v, true | ||
} | ||
v, ok = envMap[k] | ||
return v, ok | ||
}) | ||
if err != nil { | ||
return envMap, errors.Wrapf(err, "failed to read %s", dotEnvFile) | ||
} | ||
for k, v := range env { | ||
envMap[k] = v | ||
} | ||
} | ||
|
||
return envMap, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
Copyright 2020 The Compose Specification Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package loader | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
|
||
"github.com/compose-spec/compose-go/dotenv" | ||
"github.com/compose-spec/compose-go/types" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// LoadIncludeConfig parse the require config from raw yaml | ||
func LoadIncludeConfig(source []interface{}) ([]types.IncludeConfig, error) { | ||
var requires []types.IncludeConfig | ||
err := Transform(source, &requires) | ||
return requires, err | ||
} | ||
|
||
var transformRequireConfig TransformerFunc = func(data interface{}) (interface{}, error) { | ||
switch value := data.(type) { | ||
case string: | ||
return map[string]interface{}{"path": value}, nil | ||
case map[string]interface{}: | ||
return value, nil | ||
default: | ||
return data, errors.Errorf("invalid type %T for `require` configuration", value) | ||
} | ||
} | ||
|
||
func loadInclude(configDetails types.ConfigDetails, model *types.Config, options []func(*Options)) (*types.Config, error) { | ||
for _, r := range model.Include { | ||
for i, p := range r.Path { | ||
if !filepath.IsAbs(p) { | ||
r.Path[i] = filepath.Join(configDetails.WorkingDir, p) | ||
} | ||
} | ||
if r.ProjectDirectory == "" { | ||
r.ProjectDirectory = filepath.Dir(r.Path[0]) | ||
} | ||
|
||
loadOptions := []func(*Options){ | ||
func(options *Options) { | ||
options.SetProjectName(model.Name, true) | ||
options.ResolvePaths = true | ||
options.SkipNormalization = true | ||
options.SkipConsistencyCheck = true | ||
}, | ||
} | ||
loadOptions = append(loadOptions, options...) | ||
|
||
env, err := dotenv.GetEnvFromFile(configDetails.Environment, r.ProjectDirectory, r.EnvFile) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
imported, err := Load(types.ConfigDetails{ | ||
WorkingDir: r.ProjectDirectory, | ||
ConfigFiles: types.ToConfigFiles(r.Path), | ||
Environment: env, | ||
}, loadOptions...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = importResources(model, imported, r.Path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
model.Include = nil | ||
return model, nil | ||
} | ||
|
||
// importResources import into model all resources defined by imported, and report error on conflict | ||
func importResources(model *types.Config, imported *types.Project, path []string) error { | ||
services := mapByName(model.Services) | ||
for _, service := range imported.Services { | ||
if _, ok := services[service.Name]; ok { | ||
return fmt.Errorf("imported compose file %s defines conflicting service %s", path, service.Name) | ||
} | ||
model.Services = append(model.Services, service) | ||
} | ||
for n, network := range imported.Networks { | ||
if _, ok := model.Networks[n]; ok { | ||
return fmt.Errorf("imported compose file %s defines conflicting network %s", path, n) | ||
} | ||
model.Networks[n] = network | ||
} | ||
for n, volume := range imported.Volumes { | ||
if _, ok := model.Volumes[n]; ok { | ||
return fmt.Errorf("imported compose file %s defines conflicting volume %s", path, n) | ||
} | ||
model.Volumes[n] = volume | ||
} | ||
for n, secret := range imported.Secrets { | ||
if _, ok := model.Secrets[n]; ok { | ||
return fmt.Errorf("imported compose file %s defines conflicting secret %s", path, n) | ||
} | ||
model.Secrets[n] = secret | ||
} | ||
for n, config := range imported.Configs { | ||
if _, ok := model.Configs[n]; ok { | ||
return fmt.Errorf("imported compose file %s defines conflicting config %s", path, n) | ||
} | ||
model.Configs[n] = config | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.