github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/pkg/file/establish.go (about)

     1  // Copyright 2021 The TrueBlocks Authors. All rights reserved.
     2  // Use of this source code is governed by a license that can
     3  // be found in the LICENSE file.
     4  
     5  package file
     6  
     7  import (
     8  	"os"
     9  	"path/filepath"
    10  )
    11  
    12  // EstablishFolders creates the rootPath and any subfolders
    13  func EstablishFolders(rootPath string, folders []string) error {
    14  	err := EstablishFolder(rootPath)
    15  	if err != nil {
    16  		return err
    17  	}
    18  
    19  	for _, folder := range folders {
    20  		err := EstablishFolder(filepath.Join(rootPath, folder))
    21  		if err != nil {
    22  			return err
    23  		}
    24  	}
    25  
    26  	return nil
    27  }
    28  
    29  // EstablishFolder creates folders given a list of folders
    30  func EstablishFolder(rootPath string) error {
    31  	_, err := os.Stat(rootPath)
    32  	if err != nil {
    33  		if os.IsNotExist(err) {
    34  			err = os.MkdirAll(rootPath, 0755)
    35  			if err != nil {
    36  				return err
    37  			}
    38  		} else {
    39  			// If there's an error other than not exist...we fail
    40  			return err
    41  		}
    42  	}
    43  	return nil
    44  }