github.com/Venafi/vcert/v5@v5.10.2/pkg/playbook/util/filehelper.go (about)

     1  /*
     2   * Copyright 2023 Venafi, Inc.
     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 util
    18  
    19  import (
    20  	"errors"
    21  	"fmt"
    22  	"io"
    23  	"os"
    24  	"path/filepath"
    25  
    26  	"go.uber.org/zap"
    27  )
    28  
    29  // FileExists returns true if  a file exists and is accessible on the given certPath
    30  func FileExists(certPath string) (bool, error) {
    31  	_, err := os.Stat(certPath)
    32  	if err != nil {
    33  		// Certificate does not exist in location. Install
    34  		if errors.Is(err, os.ErrNotExist) {
    35  			zap.L().Debug("certificate file does not exist at location", zap.String("location", certPath))
    36  			return false, nil
    37  		}
    38  		return false, err
    39  	}
    40  	return true, nil
    41  }
    42  
    43  // WriteFile saves the content in the given location. Creates any folders necessary for this action
    44  func WriteFile(location string, content []byte) error {
    45  	dirPath := filepath.Dir(location)
    46  	err := os.MkdirAll(dirPath, 0750)
    47  	if err != nil {
    48  		zap.L().Error("could not create certificate directory path", zap.String("location", location),
    49  			zap.Error(err))
    50  		return err
    51  	}
    52  
    53  	err = os.WriteFile(location, content, 0600)
    54  	if err != nil {
    55  		zap.L().Error("could not write certificate to file", zap.String("file", location), zap.Error(err))
    56  		return err
    57  	}
    58  	return nil
    59  }
    60  
    61  // CopyFile makes a copy of the given source to the given destination using Go's native copy function io.Copy
    62  func CopyFile(source string, destination string) error {
    63  	zap.L().Debug("checking file", zap.String("location", source))
    64  
    65  	sourceFileStat, err := os.Stat(source)
    66  	if err != nil {
    67  		zap.L().Error("failed to stat file", zap.String("file", source), zap.Error(err))
    68  		return err
    69  	}
    70  
    71  	if !sourceFileStat.Mode().IsRegular() {
    72  		m := "file is not a regular file"
    73  		zap.L().Error(m, zap.String("file", source))
    74  		return fmt.Errorf("%s: %s", m, source)
    75  	}
    76  
    77  	sourceFile, err := os.Open(source)
    78  	if err != nil {
    79  		zap.L().Error("failed to open file", zap.String("file", source), zap.Error(err))
    80  		return err
    81  	}
    82  	defer sourceFile.Close()
    83  
    84  	destinationFile, err := os.Create(destination)
    85  	if err != nil {
    86  		zap.L().Error("failed to create/truncate file", zap.String("file", destination), zap.Error(err))
    87  		return err
    88  	}
    89  	defer destinationFile.Close()
    90  
    91  	_, err = io.Copy(destinationFile, sourceFile)
    92  	if err != nil {
    93  		zap.L().Error("failed to copy file", zap.String("source", source),
    94  			zap.String("destination", destination), zap.Error(err))
    95  		return err
    96  	}
    97  	zap.L().Debug("file successfully copied", zap.String("source", source),
    98  		zap.String("destination", destination))
    99  
   100  	return nil
   101  }