-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaths.go
More file actions
48 lines (40 loc) · 829 Bytes
/
Copy pathpaths.go
File metadata and controls
48 lines (40 loc) · 829 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main
import (
"fmt"
"os"
"path/filepath"
)
func defaultDirectory() (string, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(homeDir, "Downloads"), nil
}
func resolveTargetDirectory(args []string) (string, error) {
if len(args) > 1 {
return "", fmt.Errorf("expected zero or one directory, received %d", len(args))
}
directory := ""
if len(args) == 0 {
var err error
directory, err = defaultDirectory()
if err != nil {
return "", err
}
} else {
directory = args[0]
}
absDirectory, err := filepath.Abs(directory)
if err != nil {
return "", err
}
info, err := os.Stat(absDirectory)
if err != nil {
return "", err
}
if !info.IsDir() {
return "", fmt.Errorf("%s is not a directory", absDirectory)
}
return absDirectory, nil
}