...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/util/fileutil/fileutil.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/pkg/util/fileutil

     1  // Copyright 2022 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package fileutil
    16  
    17  import (
    18  	"fmt"
    19  	"io/ioutil"
    20  	"os"
    21  	"path/filepath"
    22  	"strings"
    23  )
    24  
    25  // DirExists determines if the given path points to a directory that exists.
    26  func DirExists(path string) (bool, error) {
    27  	fileInfo, err := os.Stat(path)
    28  	if err != nil {
    29  		return false, err
    30  	}
    31  	return fileInfo.IsDir(), nil
    32  }
    33  
    34  // HasSubdirs determines if a given path contains at least one subdirectory.
    35  func HasSubdirs(path string) (bool, error) {
    36  	subdirs, err := SubdirsIn(path)
    37  	if err != nil {
    38  		return false, err
    39  	}
    40  	return len(subdirs) > 0, nil
    41  }
    42  
    43  // SubdirsIn gets the names of subdirectories found in a given path.
    44  func SubdirsIn(path string) ([]string, error) {
    45  	fileInfos, err := ioutil.ReadDir(path)
    46  	if err != nil {
    47  		return []string{}, fmt.Errorf("error reading directory '%v': %v", path, err)
    48  	}
    49  	subdirNames := make([]string, 0)
    50  	for _, fi := range fileInfos {
    51  		if fi.IsDir() {
    52  			subdirNames = append(subdirNames, fi.Name())
    53  		}
    54  	}
    55  	return subdirNames, nil
    56  }
    57  
    58  // FileNamesWithSuffixInDir gets all the filenames in the directory at the
    59  // given path which end with the given suffix.
    60  func FileNamesWithSuffixInDir(path, suffix string) (names []string, err error) {
    61  	fileInfos, err := ioutil.ReadDir(path)
    62  	if err != nil {
    63  		return []string{}, fmt.Errorf("error reading directory '%v': %v", path, err)
    64  	}
    65  	names = make([]string, 0)
    66  	for _, fi := range fileInfos {
    67  		if fi.IsDir() {
    68  			continue
    69  		}
    70  		if strings.HasSuffix(fi.Name(), suffix) {
    71  			names = append(names, fi.Name())
    72  		}
    73  	}
    74  	return names, nil
    75  }
    76  
    77  // NewEmptyFile creates an empty file at the given path.
    78  func NewEmptyFile(path string) (*os.File, error) {
    79  	if err := ensureParentDirectoryExists(path); err != nil {
    80  		return nil, fmt.Errorf("error ensuring parent directory of %v exists: %v", path, err)
    81  	}
    82  	return os.Create(path)
    83  }
    84  
    85  func ensureParentDirectoryExists(path string) error {
    86  	dir := filepath.Dir(path)
    87  	if err := os.MkdirAll(dir, 0700); err != nil {
    88  		return fmt.Errorf("error creating directory %v and its parents: %v", dir, err)
    89  	}
    90  	return nil
    91  }
    92  

View as plain text