github.com/quantumghost/awgo@v0.15.0/util/util.go (about)

     1  //
     2  // Copyright (c) 2016 Dean Jackson <deanishe@deanishe.net>
     3  //
     4  // MIT Licence. See http://opensource.org/licenses/MIT
     5  //
     6  
     7  /*
     8  
     9  Package util contains general helper functions for workflow (library) authors.
    10  
    11  The functions can be divided into roughly three groups: paths, formatting
    12  and scripting.
    13  
    14  
    15  Paths
    16  
    17  There are a couple of convenience path functions, MustExist and
    18  ClearDirectory.
    19  
    20  
    21  Formatting
    22  
    23  PrettyPath for user-friendly paths, and the Pad* functions for padding
    24  strings.
    25  
    26  Scripting
    27  
    28  QuoteAS quotes strings for insertion into AppleScript code and there
    29  are several Run* functions for executing script code and files.
    30  
    31  	Run()     // run a script file or executable & return output
    32  	RunAS()   // run AppleScript code & return output
    33  	RunJS()   // run JXA code & return output
    34  	RunCmd()  // run *exec.Cmd & return output
    35  
    36  Run takes the path to a script or executable. If file is executable,
    37  it runs the file directly. If it's a script file, it tries to guess the
    38  appropriate interpreter.
    39  
    40  See Runner for more information.
    41  
    42  */
    43  package util
    44  
    45  import (
    46  	"fmt"
    47  	"log"
    48  	"os"
    49  	"time"
    50  )
    51  
    52  // Timed logs the duration since start & title. Use it with defer.
    53  //
    54  //    func doSomething() {
    55  //        defer Timed(time.Now(), "long running task")
    56  //        // do thing here
    57  //        // and another thing
    58  //    }
    59  //    // Output: ... long running task
    60  //
    61  func Timed(start time.Time, title string) {
    62  	log.Printf("%s \U000029D7 %s", time.Now().Sub(start), title)
    63  }
    64  
    65  // MustExist takes and returns a directory path, creating the directory
    66  // if necessary. Any created directories have permission set to 700.
    67  // Panics if the directory cannot be created.
    68  func MustExist(dirpath string) string {
    69  	err := os.MkdirAll(dirpath, 0700)
    70  	if err != nil {
    71  		panic(fmt.Sprintf("Couldn't create directory `%s` : %v", dirpath, err))
    72  	}
    73  	return dirpath
    74  }
    75  
    76  // PathExists checks for the existence of path.
    77  // Panics if an error is encountered.
    78  func PathExists(path string) bool {
    79  	_, err := os.Stat(path)
    80  	if err == nil {
    81  		return true
    82  	}
    83  	if os.IsNotExist(err) {
    84  		return false
    85  	}
    86  	panic(err)
    87  }
    88  
    89  // ClearDirectory deletes all files within a directory, but not directory itself.
    90  func ClearDirectory(p string) error {
    91  	if !PathExists(p) {
    92  		return nil
    93  	}
    94  	err := os.RemoveAll(p)
    95  	MustExist(p)
    96  	if err == nil {
    97  		log.Printf("deleted contents of `%s`", p)
    98  	}
    99  	return err
   100  }