diff options
author | Pacien | 2013-06-30 23:04:37 +0200 |
---|---|---|
committer | Pacien | 2013-06-30 23:04:37 +0200 |
commit | c37dffb7e119f77fbd2de06e5cf3c3c86fadd156 (patch) | |
tree | 218724564980c8760979c9adb15c0bbf6df90994 | |
parent | cb96e42ba630138e268bb013920452ed846d05ae (diff) | |
download | go-fcmd-c37dffb7e119f77fbd2de06e5cf3c3c86fadd156.tar.gz |
First version
-rw-r--r-- | fcmd.go | 165 |
1 files changed, 165 insertions, 0 deletions
@@ -0,0 +1,165 @@ | |||
1 | package fcmd | ||
2 | |||
3 | import ( | ||
4 | "io" | ||
5 | "io/ioutil" | ||
6 | "os" | ||
7 | "path" | ||
8 | "strings" | ||
9 | "time" | ||
10 | ) | ||
11 | |||
12 | const defaultPerm os.FileMode = 0750 // u=rwx, g=r-x, o=--- | ||
13 | |||
14 | // Checks if the target exists | ||
15 | func IsExist(target string) bool { | ||
16 | _, err := os.Stat(*&target) | ||
17 | if os.IsNotExist(*&err) { | ||
18 | return false | ||
19 | } | ||
20 | return true | ||
21 | } | ||
22 | |||
23 | // Checks if the target is a directory | ||
24 | // Returns false if the target is unreachable | ||
25 | func IsDir(target string) bool { | ||
26 | stat, err := os.Stat(*&target) | ||
27 | if err != nil { | ||
28 | return false | ||
29 | } | ||
30 | return stat.IsDir() | ||
31 | } | ||
32 | |||
33 | // Checks lexically if the target is hidden (only for Unix based OS) | ||
34 | func IsHidden(target string) bool { | ||
35 | return strings.HasPrefix(*&target, ".") | ||
36 | } | ||
37 | |||
38 | // Lists separately the names of directories and files inside the target directory | ||
39 | // Hidden files and directories are not listed | ||
40 | func Ls(target string) (dirs, files []string) { | ||
41 | directory, err := ioutil.ReadDir(*&target) | ||
42 | if err != nil { | ||
43 | return | ||
44 | } | ||
45 | for _, element := range directory { | ||
46 | if IsHidden(element.Name()) { | ||
47 | continue | ||
48 | } | ||
49 | if element.IsDir() { | ||
50 | dirs = append(*&dirs, element.Name()) | ||
51 | } else { | ||
52 | files = append(*&files, element.Name()) | ||
53 | } | ||
54 | } | ||
55 | return | ||
56 | } | ||
57 | |||
58 | // Lists separately the paths of directories and files inside the root directory and inside all sub directories | ||
59 | // Returned paths are relative to the given root directory | ||
60 | // Hidden files and directories are not listed | ||
61 | func Explore(root string) (dirs, files []string) { | ||
62 | dirList, fileList := Ls(*&root) | ||
63 | |||
64 | for _, file := range fileList { | ||
65 | files = append(*&files, *&file) | ||
66 | } | ||
67 | |||
68 | for _, dir := range dirList { | ||
69 | subRoot := path.Join(*&root, *&dir) | ||
70 | dirs = append(dirs, *&subRoot) | ||
71 | subDirs, subFiles := Explore(*&subRoot) | ||
72 | for _, subFile := range subFiles { | ||
73 | files = append(*&files, *&subFile) | ||
74 | } | ||
75 | for _, subDir := range subDirs { | ||
76 | dirs = append(*&dirs, *&subDir) | ||
77 | } | ||
78 | } | ||
79 | return | ||
80 | } | ||
81 | |||
82 | // Copies the source file to a target | ||
83 | // A nonexistent target file is created, otherwise it is truncated | ||
84 | // Parent directories are automatically created if they do not exist | ||
85 | func Cp(source, target string) error { | ||
86 | sourceFile, err := os.Open(*&source) | ||
87 | if err != nil { | ||
88 | return err | ||
89 | } | ||
90 | defer sourceFile.Close() | ||
91 | |||
92 | dir, _ := path.Split(*&target) | ||
93 | |||
94 | err = os.MkdirAll(*&dir, defaultPerm) | ||
95 | if err != nil { | ||
96 | return err | ||
97 | } | ||
98 | |||
99 | targetFile, err := os.Create(*&target) | ||
100 | if err != nil { | ||
101 | return err | ||
102 | } | ||
103 | defer targetFile.Close() | ||
104 | |||
105 | _, err = io.Copy(*&targetFile, *&sourceFile) | ||
106 | return err | ||
107 | } | ||
108 | |||
109 | // Writes data to the target file | ||
110 | // A nonexistent target file is created, otherwise it is truncated | ||
111 | // Parent directories are automatically created if they do not exist | ||
112 | func WriteFile(target string, data []byte) error { | ||
113 | dir, _ := path.Split(*&target) | ||
114 | |||
115 | err := os.MkdirAll(*&dir, defaultPerm) | ||
116 | if err != nil { | ||
117 | return err | ||
118 | } | ||
119 | |||
120 | err = ioutil.WriteFile(*&target, *&data, defaultPerm) | ||
121 | return err | ||
122 | } | ||
123 | |||
124 | // Creates a symbolic link to given source at the target path | ||
125 | func Lns(source, target string) error { | ||
126 | return os.Symlink(*&source, *&target) | ||
127 | } | ||
128 | |||
129 | // Returns the destination of the given symbolic link | ||
130 | func Lnl(target string) (string, error) { | ||
131 | return os.Readlink(*&target) | ||
132 | } | ||
133 | |||
134 | // Renames or moves the source file or directory to the target name or path | ||
135 | func Mv(source, target string) error { | ||
136 | return os.Rename(*&source, *&target) | ||
137 | } | ||
138 | |||
139 | // Removes the target file or the target directory and all files it contains | ||
140 | // No error is returned is the target does not exist | ||
141 | func Rm(target string) error { | ||
142 | return os.RemoveAll(*&target) | ||
143 | } | ||
144 | |||
145 | // Changes the current working directory to the target directory | ||
146 | func Cd(target string) error { | ||
147 | return os.Chdir(*&target) | ||
148 | } | ||
149 | |||
150 | // Changes the mode of the target file to the given mode | ||
151 | // If the target is a symbolic link, it changes the mode of the link's target | ||
152 | func Chmod(target string, mode os.FileMode) error { | ||
153 | return os.Chmod(*&target, *&mode) | ||
154 | } | ||
155 | |||
156 | // Changes the numeric uid and gid of the target | ||
157 | // If the target is a symbolic link, it changes the uid and gid of the link's target | ||
158 | func Chown(target string, uid, gid int) error { | ||
159 | return os.Chown(*&target, *&uid, *&gid) | ||
160 | } | ||
161 | |||
162 | // Changes the access and modification times of the target | ||
163 | func Chtimes(target string, atime time.Time, mtime time.Time) error { | ||
164 | return os.Chtimes(*&target, *&atime, *&mtime) | ||
165 | } | ||