github.com/containerd/nerdctl/v2@v2.0.0-beta.5.0.20240520001846-b5758f54fa28/pkg/composer/projectloader/projectloader.go (about)

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package projectloader
    18  
    19  import (
    20  	"os"
    21  	"path/filepath"
    22  
    23  	"github.com/compose-spec/compose-go/loader"
    24  	compose "github.com/compose-spec/compose-go/types"
    25  )
    26  
    27  // Load is used only for unit testing.
    28  // TODO: Remove
    29  func Load(fileName, projectName string, envMap map[string]string) (*compose.Project, error) {
    30  	if envMap == nil {
    31  		envMap = make(map[string]string)
    32  	}
    33  	b, err := os.ReadFile(fileName)
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  
    38  	wd, err := filepath.Abs(filepath.Dir(fileName))
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  	var files []compose.ConfigFile
    43  	files = append(files, compose.ConfigFile{Filename: fileName, Content: b})
    44  	return loader.Load(compose.ConfigDetails{
    45  		WorkingDir:  wd,
    46  		ConfigFiles: files,
    47  		Environment: envMap,
    48  	}, withProjectName(projectName))
    49  }
    50  
    51  func withProjectName(name string) func(*loader.Options) {
    52  	return func(lOpts *loader.Options) {
    53  		lOpts.SetProjectName(name, true)
    54  	}
    55  }