diff options
Diffstat (limited to 'fcmd.go')
-rw-r--r-- | fcmd.go | 60 |
1 files changed, 30 insertions, 30 deletions
@@ -9,9 +9,9 @@ import ( | |||
9 | "time" | 9 | "time" |
10 | ) | 10 | ) |
11 | 11 | ||
12 | const defaultPerm os.FileMode = 0750 // u=rwx, g=r-x, o=--- | 12 | var DefaultPerm os.FileMode = 0750 // u=rwx, g=r-x, o=--- |
13 | 13 | ||
14 | // Checks if the target exists | 14 | // Checks if the target exists. |
15 | func IsExist(target string) bool { | 15 | func IsExist(target string) bool { |
16 | _, err := os.Stat(*&target) | 16 | _, err := os.Stat(*&target) |
17 | if os.IsNotExist(*&err) { | 17 | if os.IsNotExist(*&err) { |
@@ -20,8 +20,8 @@ func IsExist(target string) bool { | |||
20 | return true | 20 | return true |
21 | } | 21 | } |
22 | 22 | ||
23 | // Checks if the target is a directory | 23 | // Checks if the target is a directory. |
24 | // Returns false if the target is unreachable | 24 | // Returns false if the target is unreachable. |
25 | func IsDir(target string) bool { | 25 | func IsDir(target string) bool { |
26 | stat, err := os.Stat(*&target) | 26 | stat, err := os.Stat(*&target) |
27 | if err != nil { | 27 | if err != nil { |
@@ -30,13 +30,13 @@ func IsDir(target string) bool { | |||
30 | return stat.IsDir() | 30 | return stat.IsDir() |
31 | } | 31 | } |
32 | 32 | ||
33 | // Checks lexically if the target is hidden (only for Unix based OS) | 33 | // Checks lexically if the target is hidden (only for Unix based OS). |
34 | func IsHidden(target string) bool { | 34 | func IsHidden(target string) bool { |
35 | return strings.HasPrefix(*&target, ".") | 35 | return strings.HasPrefix(*&target, ".") |
36 | } | 36 | } |
37 | 37 | ||
38 | // Lists separately the names of directories and files inside the target directory | 38 | // Lists separately the names of directories and files inside the target directory. |
39 | // Hidden files and directories are not listed | 39 | // Hidden files and directories are not listed. |
40 | func Ls(target string) (dirs, files []string) { | 40 | func Ls(target string) (dirs, files []string) { |
41 | directory, err := ioutil.ReadDir(*&target) | 41 | directory, err := ioutil.ReadDir(*&target) |
42 | if err != nil { | 42 | if err != nil { |
@@ -55,9 +55,9 @@ func Ls(target string) (dirs, files []string) { | |||
55 | return | 55 | return |
56 | } | 56 | } |
57 | 57 | ||
58 | // Lists separately the paths of directories and files inside the root directory and inside all sub directories | 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 | 59 | // Returned paths are relative to the given root directory. |
60 | // Hidden files and directories are not listed | 60 | // Hidden files and directories are not listed. |
61 | func Explore(root string) (dirs, files []string) { | 61 | func Explore(root string) (dirs, files []string) { |
62 | dirList, fileList := Ls(*&root) | 62 | dirList, fileList := Ls(*&root) |
63 | 63 | ||
@@ -79,9 +79,9 @@ func Explore(root string) (dirs, files []string) { | |||
79 | return | 79 | return |
80 | } | 80 | } |
81 | 81 | ||
82 | // Copies the source file to a target | 82 | // Copies the source file to a target. |
83 | // A nonexistent target file is created, otherwise it is truncated | 83 | // A nonexistent target file is created, otherwise it is truncated. |
84 | // Parent directories are automatically created if they do not exist | 84 | // Parent directories are automatically created if they do not exist. |
85 | func Cp(source, target string) error { | 85 | func Cp(source, target string) error { |
86 | sourceFile, err := os.Open(*&source) | 86 | sourceFile, err := os.Open(*&source) |
87 | if err != nil { | 87 | if err != nil { |
@@ -91,7 +91,7 @@ func Cp(source, target string) error { | |||
91 | 91 | ||
92 | dir, _ := path.Split(*&target) | 92 | dir, _ := path.Split(*&target) |
93 | 93 | ||
94 | err = os.MkdirAll(*&dir, defaultPerm) | 94 | err = os.MkdirAll(*&dir, DefaultPerm) |
95 | if err != nil { | 95 | if err != nil { |
96 | return err | 96 | return err |
97 | } | 97 | } |
@@ -106,60 +106,60 @@ func Cp(source, target string) error { | |||
106 | return err | 106 | return err |
107 | } | 107 | } |
108 | 108 | ||
109 | // Writes data to the target file | 109 | // Writes data to the target file. |
110 | // A nonexistent target file is created, otherwise it is truncated | 110 | // A nonexistent target file is created, otherwise it is truncated. |
111 | // Parent directories are automatically created if they do not exist | 111 | // Parent directories are automatically created if they do not exist. |
112 | func WriteFile(target string, data []byte) error { | 112 | func WriteFile(target string, data []byte) error { |
113 | dir, _ := path.Split(*&target) | 113 | dir, _ := path.Split(*&target) |
114 | 114 | ||
115 | err := os.MkdirAll(*&dir, defaultPerm) | 115 | err := os.MkdirAll(*&dir, DefaultPerm) |
116 | if err != nil { | 116 | if err != nil { |
117 | return err | 117 | return err |
118 | } | 118 | } |
119 | 119 | ||
120 | err = ioutil.WriteFile(*&target, *&data, defaultPerm) | 120 | err = ioutil.WriteFile(*&target, *&data, DefaultPerm) |
121 | return err | 121 | return err |
122 | } | 122 | } |
123 | 123 | ||
124 | // Creates a symbolic link to given source at the target path | 124 | // Creates a symbolic link to given source at the target path. |
125 | func Lns(source, target string) error { | 125 | func Lns(source, target string) error { |
126 | return os.Symlink(*&source, *&target) | 126 | return os.Symlink(*&source, *&target) |
127 | } | 127 | } |
128 | 128 | ||
129 | // Returns the destination of the given symbolic link | 129 | // Returns the destination of the given symbolic link. |
130 | func Lnl(target string) (string, error) { | 130 | func Lnl(target string) (string, error) { |
131 | return os.Readlink(*&target) | 131 | return os.Readlink(*&target) |
132 | } | 132 | } |
133 | 133 | ||
134 | // Renames or moves the source file or directory to the target name or path | 134 | // Renames or moves the source file or directory to the target name or path. |
135 | func Mv(source, target string) error { | 135 | func Mv(source, target string) error { |
136 | return os.Rename(*&source, *&target) | 136 | return os.Rename(*&source, *&target) |
137 | } | 137 | } |
138 | 138 | ||
139 | // Removes the target file or the target directory and all files it contains | 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 | 140 | // No error is returned is the target does not exist. |
141 | func Rm(target string) error { | 141 | func Rm(target string) error { |
142 | return os.RemoveAll(*&target) | 142 | return os.RemoveAll(*&target) |
143 | } | 143 | } |
144 | 144 | ||
145 | // Changes the current working directory to the target directory | 145 | // Changes the current working directory to the target directory. |
146 | func Cd(target string) error { | 146 | func Cd(target string) error { |
147 | return os.Chdir(*&target) | 147 | return os.Chdir(*&target) |
148 | } | 148 | } |
149 | 149 | ||
150 | // Changes the mode of the target file to the given mode | 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 | 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 { | 152 | func Chmod(target string, mode os.FileMode) error { |
153 | return os.Chmod(*&target, *&mode) | 153 | return os.Chmod(*&target, *&mode) |
154 | } | 154 | } |
155 | 155 | ||
156 | // Changes the numeric uid and gid of the target | 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 | 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 { | 158 | func Chown(target string, uid, gid int) error { |
159 | return os.Chown(*&target, *&uid, *&gid) | 159 | return os.Chown(*&target, *&uid, *&gid) |
160 | } | 160 | } |
161 | 161 | ||
162 | // Changes the access and modification times of the target | 162 | // Changes the access and modification times of the target. |
163 | func Chtimes(target string, atime time.Time, mtime time.Time) error { | 163 | func Chtimes(target string, atime time.Time, mtime time.Time) error { |
164 | return os.Chtimes(*&target, *&atime, *&mtime) | 164 | return os.Chtimes(*&target, *&atime, *&mtime) |
165 | } | 165 | } |