github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/base/hiddenfile/hidden_file.go (about)

     1  // +build !windows
     2  
     3  package hiddenfile
     4  
     5  import (
     6  	"fmt"
     7  	"io/ioutil"
     8  	"path/filepath"
     9  	"strings"
    10  )
    11  
    12  // SetFileHidden ensures the filename begins with a dot. Other OSes may do more
    13  func SetFileHidden(path string) error {
    14  	basename := filepath.Base(path)
    15  	if !strings.HasPrefix(basename, ".") {
    16  		return fmt.Errorf("hidden files must begin with \".\"")
    17  	}
    18  	return nil
    19  }
    20  
    21  // WriteHiddenFile ensures the filename begins with a dot, and writes content to it. Other
    22  // Operating Systems may do more
    23  func WriteHiddenFile(path, content string) error {
    24  	// Ensure the filename begins with a dot
    25  	basename := filepath.Base(path)
    26  	if !strings.HasPrefix(basename, ".") {
    27  		return fmt.Errorf("hidden files must begin with \".\"")
    28  	}
    29  	// Write the contents
    30  	if err := ioutil.WriteFile(path, []byte(content), 0644); err != nil {
    31  		return err
    32  	}
    33  	return nil
    34  }