2017-06-10 21:38:09 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-06-10 23:51:46 +02:00
|
|
|
"fmt"
|
2017-06-12 23:12:40 +02:00
|
|
|
"io"
|
2017-06-10 21:38:09 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-06-13 17:00:24 +02:00
|
|
|
"syscall"
|
2017-06-10 21:38:09 +02:00
|
|
|
)
|
|
|
|
|
2017-06-12 23:12:40 +02:00
|
|
|
// copyFile implements non-atomic copy without copying metadata.
|
|
|
|
// This is sufficient for its use within this package.
|
|
|
|
func copyFile(dst, src string, perm os.FileMode) error {
|
|
|
|
inf, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer inf.Close()
|
|
|
|
outf, err := os.Create(dst)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err = io.Copy(outf, inf); err != nil {
|
|
|
|
_ = os.Remove(dst)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return outf.Close()
|
|
|
|
}
|
|
|
|
|
2017-06-11 01:32:39 +02:00
|
|
|
func getBool(m map[interface{}]interface{}, k string, defaultValue bool) bool {
|
|
|
|
if val, found := m[k]; found {
|
|
|
|
if v, ok := val.(bool); ok {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return defaultValue
|
|
|
|
}
|
|
|
|
|
2017-06-11 17:51:25 +02:00
|
|
|
func getString(m map[interface{}]interface{}, k string, defaultValue string) string {
|
|
|
|
if val, found := m[k]; found {
|
|
|
|
if v, ok := val.(string); ok {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return defaultValue
|
|
|
|
}
|
|
|
|
|
2017-06-10 21:38:09 +02:00
|
|
|
// alternative to http://left-pad.io
|
|
|
|
func leftPad(s string, n int) string {
|
2017-06-11 22:00:03 +02:00
|
|
|
if n <= len(s) {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
ws := make([]byte, n-len(s))
|
2017-06-10 21:38:09 +02:00
|
|
|
for i := range ws {
|
|
|
|
ws[i] = ' '
|
|
|
|
}
|
|
|
|
return string(ws) + s
|
|
|
|
}
|
|
|
|
|
2017-06-10 23:51:46 +02:00
|
|
|
func mergeMaps(a map[interface{}]interface{}, b map[interface{}]interface{}) map[interface{}]interface{} {
|
|
|
|
result := map[interface{}]interface{}{}
|
|
|
|
for k, v := range a {
|
|
|
|
result[k] = v
|
|
|
|
}
|
|
|
|
for k, v := range b {
|
|
|
|
result[k] = v
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2017-06-11 22:00:03 +02:00
|
|
|
// stringMap returns a string-indexed map with the same values as its argument.
|
|
|
|
// Non-strings keys are converted to strings.
|
2017-06-10 23:51:46 +02:00
|
|
|
func stringMap(m map[interface{}]interface{}) map[string]interface{} {
|
|
|
|
result := map[string]interface{}{}
|
|
|
|
for k, v := range m {
|
|
|
|
stringer, ok := k.(fmt.Stringer)
|
|
|
|
if ok {
|
|
|
|
result[stringer.String()] = v
|
|
|
|
} else {
|
|
|
|
result[fmt.Sprintf("%v", k)] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2017-06-10 21:38:09 +02:00
|
|
|
func postfixWalk(path string, walkFn filepath.WalkFunc) error {
|
2017-06-12 00:12:33 +02:00
|
|
|
if files, err := ioutil.ReadDir(path); err == nil {
|
|
|
|
for _, stat := range files {
|
|
|
|
if stat.IsDir() {
|
|
|
|
if err = postfixWalk(filepath.Join(path, stat.Name()), walkFn); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-06-10 21:38:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
info, err := os.Stat(path)
|
2017-06-12 02:05:17 +02:00
|
|
|
return walkFn(path, info, err)
|
2017-06-10 21:38:09 +02:00
|
|
|
}
|
|
|
|
|
2017-06-13 17:00:24 +02:00
|
|
|
func IsNotEmpty(err error) bool {
|
|
|
|
if err, ok := err.(*os.PathError); ok {
|
|
|
|
return err.Err.(syscall.Errno) == syscall.ENOTEMPTY
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveEmptyDirectories recursively removes empty directories.
|
|
|
|
func RemoveEmptyDirectories(path string) error {
|
2017-06-10 21:38:09 +02:00
|
|
|
walkFn := func(path string, info os.FileInfo, err error) error {
|
2017-06-13 17:00:24 +02:00
|
|
|
switch {
|
|
|
|
case err != nil && os.IsNotExist(err):
|
|
|
|
return nil
|
|
|
|
case err != nil:
|
2017-06-10 21:38:09 +02:00
|
|
|
return err
|
2017-06-13 17:00:24 +02:00
|
|
|
case info.IsDir():
|
|
|
|
err := os.Remove(path)
|
|
|
|
switch {
|
|
|
|
case err == nil:
|
|
|
|
return nil
|
|
|
|
case os.IsNotExist(err):
|
|
|
|
return nil
|
|
|
|
case IsNotEmpty(err):
|
|
|
|
return nil
|
|
|
|
default:
|
2017-06-12 00:12:33 +02:00
|
|
|
return err
|
|
|
|
}
|
2017-06-10 21:38:09 +02:00
|
|
|
}
|
2017-06-12 00:12:33 +02:00
|
|
|
return nil
|
2017-06-10 21:38:09 +02:00
|
|
|
}
|
2017-06-12 02:05:17 +02:00
|
|
|
return postfixWalk(path, walkFn)
|
2017-06-10 21:38:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func stringArrayToMap(strings []string) map[string]bool {
|
|
|
|
stringMap := map[string]bool{}
|
|
|
|
for _, s := range strings {
|
|
|
|
stringMap[s] = true
|
|
|
|
}
|
|
|
|
return stringMap
|
|
|
|
}
|