apputil.go raw

   1  package apputil
   2  
   3  import (
   4  	"os"
   5  	"path/filepath"
   6  	"runtime"
   7  )
   8  
   9  // EnsureDir checks a file could be written to a path, creates the directories as needed
  10  func EnsureDir(fileName string) {
  11  	dirName := filepath.Dir(fileName)
  12  	if _, serr := os.Stat(dirName); serr != nil {
  13  		merr := os.MkdirAll(dirName, os.ModePerm)
  14  		if merr != nil {
  15  			panic(merr)
  16  		}
  17  	}
  18  }
  19  
  20  // FileExists reports whether the named file or directory exists.
  21  func FileExists(filePath string) bool {
  22  	_, e := os.Stat(filePath)
  23  	return e == nil
  24  }
  25  
  26  // MinUint32 is a helper function to return the minimum of two uint32s. This avoids a math import and the need to cast
  27  // to floats.
  28  func MinUint32(a, b uint32) uint32 {
  29  	if a < b {
  30  		return a
  31  	}
  32  	return b
  33  }
  34  
  35  // PrependForWindows runs a command with a terminal
  36  func PrependForWindows(args []string) []string {
  37  	if runtime.GOOS == "windows" {
  38  		args = append(
  39  			[]string{
  40  				"cmd.exe",
  41  				"/C",
  42  			},
  43  			args...,
  44  		)
  45  	}
  46  	return args
  47  }
  48  
  49  // PrependForWindowsWithStart runs a process independently
  50  func PrependForWindowsWithStart(args []string) []string {
  51  	if runtime.GOOS == "windows" {
  52  		args = append(
  53  			[]string{
  54  				"cmd.exe",
  55  				"/C",
  56  				"start",
  57  			},
  58  			args...,
  59  		)
  60  	}
  61  	return args
  62  }
  63