aboutsummaryrefslogtreecommitdiff
path: root/checkdir.go
diff options
context:
space:
mode:
authorJakob Unterwurzacher2015-10-11 18:33:28 +0200
committerJakob Unterwurzacher2015-10-11 18:33:28 +0200
commitd1d444435cf4351fc50ffd02c203cf4a2209b8e5 (patch)
tree773ca8a914f9a5cc4165060da22c7810eda51ca8 /checkdir.go
parenta863a6569c294cb06916cbf303a1230a04e73f76 (diff)
main: check directories for existence early
This prevents that the user enters the password only to get an error later.
Diffstat (limited to 'checkdir.go')
-rw-r--r--checkdir.go33
1 files changed, 33 insertions, 0 deletions
diff --git a/checkdir.go b/checkdir.go
new file mode 100644
index 0000000..182bf97
--- /dev/null
+++ b/checkdir.go
@@ -0,0 +1,33 @@
+package main
+
+import (
+ "io/ioutil"
+ "fmt"
+ "os"
+)
+
+func checkDirEmpty(dir string) error {
+ err := checkDir(dir)
+ if err != nil {
+ return err
+ }
+ entries, err := ioutil.ReadDir(dir)
+ if err != nil {
+ return err
+ }
+ if len(entries) == 0 {
+ return nil
+ }
+ return fmt.Errorf("directory %s not empty", dir)
+}
+
+func checkDir(dir string) error {
+ fi, err := os.Stat(dir)
+ if err != nil {
+ return err
+ }
+ if ! fi.IsDir() {
+ return fmt.Errorf("%s is not a directory")
+ }
+ return nil
+}