"Fossies" - the Fresh Open Source Software Archive

Member "gdrive-2.1.1/util.go" (28 May 2021, 1307 Bytes) of package /linux/misc/gdrive-2.1.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Go source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 package main
    2 
    3 import (
    4     "crypto/md5"
    5     "encoding/json"
    6     "fmt"
    7     "io"
    8     "os"
    9     "path/filepath"
   10     "runtime"
   11 )
   12 
   13 func GetDefaultConfigDir() string {
   14     return filepath.Join(Homedir(), ".gdrive")
   15 }
   16 
   17 func ConfigFilePath(basePath, name string) string {
   18     return filepath.Join(basePath, name)
   19 }
   20 
   21 func Homedir() string {
   22     if runtime.GOOS == "windows" {
   23         return os.Getenv("APPDATA")
   24     }
   25     return os.Getenv("HOME")
   26 }
   27 
   28 func equal(a, b []string) bool {
   29     if a == nil && b == nil {
   30         return true
   31     }
   32 
   33     if a == nil || b == nil {
   34         return false
   35     }
   36 
   37     if len(a) != len(b) {
   38         return false
   39     }
   40 
   41     for i := range a {
   42         if a[i] != b[i] {
   43             return false
   44         }
   45     }
   46 
   47     return true
   48 }
   49 
   50 func ExitF(format string, a ...interface{}) {
   51     fmt.Fprintf(os.Stderr, format, a...)
   52     fmt.Println("")
   53     os.Exit(1)
   54 }
   55 
   56 func checkErr(err error) {
   57     if err != nil {
   58         fmt.Println(err)
   59         os.Exit(1)
   60     }
   61 }
   62 
   63 func writeJson(path string, data interface{}) error {
   64     tmpFile := path + ".tmp"
   65     f, err := os.Create(tmpFile)
   66     if err != nil {
   67         return err
   68     }
   69 
   70     err = json.NewEncoder(f).Encode(data)
   71     f.Close()
   72     if err != nil {
   73         os.Remove(tmpFile)
   74         return err
   75     }
   76 
   77     return os.Rename(tmpFile, path)
   78 }
   79 
   80 func md5sum(path string) string {
   81     h := md5.New()
   82     f, err := os.Open(path)
   83     if err != nil {
   84         return ""
   85     }
   86     defer f.Close()
   87 
   88     io.Copy(h, f)
   89     return fmt.Sprintf("%x", h.Sum(nil))
   90 }