github.com/paketoio/libpak@v1.3.1/internal/toml_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  
    24  	"github.com/BurntSushi/toml"
    25  	"github.com/buildpacks/libcnb"
    26  	"github.com/paketoio/libpak/bard"
    27  )
    28  
    29  // TOMLWriter is an implementation of the libcnb.TOMLWriter interface.
    30  type TOMLWriter struct {
    31  	logger bard.Logger
    32  }
    33  
    34  // TOMLWriterOption is a function for configuring a TOMLWriter instance.
    35  type TOMLWriterOption func(writer TOMLWriter) TOMLWriter
    36  
    37  // WithTOMLWriterLogger creates an TOMLWriterOption that configures the logger.
    38  func WithTOMLWriterLogger(logger bard.Logger) TOMLWriterOption {
    39  	return func(writer TOMLWriter) TOMLWriter {
    40  		writer.logger = logger
    41  		return writer
    42  	}
    43  }
    44  
    45  // NewTOMLWriter creates a new instance that writes to the filesystem and writes to the default bard.Logger.
    46  func NewTOMLWriter(options ...TOMLWriterOption) TOMLWriter {
    47  	w := TOMLWriter{
    48  		logger: bard.NewLogger(os.Stdout),
    49  	}
    50  
    51  	for _, option := range options {
    52  		w = option(w)
    53  	}
    54  
    55  	return w
    56  }
    57  
    58  // Write creates the path's parent directories, and creates a new file or truncates an existing file and then marshals
    59  // the value to the file.
    60  func (t TOMLWriter) Write(path string, value interface{}) error {
    61  	if value == nil {
    62  		return nil
    63  	}
    64  
    65  	switch v := value.(type) {
    66  	case libcnb.Launch:
    67  		t.logger.Header("%s", LaunchFormatter(v))
    68  	case libcnb.Store:
    69  		if len(v.Metadata) > 0 {
    70  			t.logger.Header("%s", StoreFormatter(v))
    71  		}
    72  	}
    73  
    74  	d := filepath.Dir(path)
    75  	if err := os.MkdirAll(d, 0755); err != nil {
    76  		return fmt.Errorf("unable to mkdir %s: %w", d, err)
    77  	}
    78  
    79  	file, err := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
    80  	if err != nil {
    81  		return fmt.Errorf("unable to open file %s: %w", path, err)
    82  	}
    83  	defer file.Close()
    84  
    85  	return toml.NewEncoder(file).Encode(value)
    86  }