github.com/paketo-buildpacks/libpak@v1.70.0/internal/environment_writer.go (about)

     1  /*
     2   * Copyright 2018-2020 the original author or 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   *      https://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 internal
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  	"path/filepath"
    23  	"sort"
    24  
    25  	"github.com/paketo-buildpacks/libpak/bard"
    26  )
    27  
    28  // EnvironmentWriter is an implementation of the libcnb.EnvironmentWriter interface.
    29  type EnvironmentWriter struct {
    30  	logger bard.Logger
    31  }
    32  
    33  // EnvironmentWriterOption is a function for configuring a EnvironmentWriter instance.
    34  type EnvironmentWriterOption func(writer EnvironmentWriter) EnvironmentWriter
    35  
    36  // WithEnvironmentWriterLogger creates an EnvironmentWriterOption that configures the logger.
    37  func WithEnvironmentWriterLogger(logger bard.Logger) EnvironmentWriterOption {
    38  	return func(writer EnvironmentWriter) EnvironmentWriter {
    39  		writer.logger = logger
    40  		return writer
    41  	}
    42  }
    43  
    44  // NewEnvironmentWriter creates a new instance that writes to the filesystem and writes to the default bard.Logger.
    45  func NewEnvironmentWriter(options ...EnvironmentWriterOption) EnvironmentWriter {
    46  	w := EnvironmentWriter{
    47  		logger: bard.NewLogger(os.Stdout),
    48  	}
    49  
    50  	for _, option := range options {
    51  		w = option(w)
    52  	}
    53  
    54  	return w
    55  }
    56  
    57  // Write creates the path directory, and creates a new file for each key with the value as the contents of each file.
    58  func (w EnvironmentWriter) Write(path string, environment map[string]string) error {
    59  	if len(environment) == 0 {
    60  		return nil
    61  	}
    62  
    63  	if err := os.MkdirAll(path, 0755); err != nil {
    64  		return fmt.Errorf("unable to mkdir %s\n%w", path, err)
    65  	}
    66  
    67  	var keys []string
    68  	for k := range environment {
    69  		keys = append(keys, k)
    70  	}
    71  	sort.Strings(keys)
    72  
    73  	base := filepath.Base(path)
    74  	for _, k := range keys {
    75  		w.logger.Bodyf("Writing %s/%s", base, k)
    76  		f := filepath.Join(path, k)
    77  
    78  		if err := os.MkdirAll(filepath.Dir(f), 0755); err != nil {
    79  			return fmt.Errorf("unable to mkdir from key %s\n%w", filepath.Dir(f), err)
    80  		}
    81  
    82  		if err := os.WriteFile(f, []byte(environment[k]), 0644); err != nil {
    83  			return fmt.Errorf("unable to write file %s\n%w", f, err)
    84  		}
    85  	}
    86  
    87  	return nil
    88  }