aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--fcmd.go63
1 files changed, 30 insertions, 33 deletions
diff --git a/fcmd.go b/fcmd.go
index 2193f71..e057587 100644
--- a/fcmd.go
+++ b/fcmd.go
@@ -38,17 +38,14 @@ var DefaultPerm os.FileMode = 0750 // u=rwx, g=r-x, o=---
38 38
39// Checks if the target exists. 39// Checks if the target exists.
40func IsExist(target string) bool { 40func IsExist(target string) bool {
41 _, err := os.Stat(*&target) 41 _, err := os.Stat(target)
42 if os.IsNotExist(*&err) { 42 return os.IsNotExist(err)
43 return false
44 }
45 return true
46} 43}
47 44
48// Checks if the target is a directory. 45// Checks if the target is a directory.
49// Returns false if the target is unreachable. 46// Returns false if the target is unreachable.
50func IsDir(target string) bool { 47func IsDir(target string) bool {
51 stat, err := os.Stat(*&target) 48 stat, err := os.Stat(target)
52 if err != nil { 49 if err != nil {
53 return false 50 return false
54 } 51 }
@@ -57,13 +54,13 @@ func IsDir(target string) bool {
57 54
58// Checks lexically if the target is hidden (only for Unix based OS). 55// Checks lexically if the target is hidden (only for Unix based OS).
59func IsHidden(target string) bool { 56func IsHidden(target string) bool {
60 return strings.HasPrefix(*&target, ".") 57 return strings.HasPrefix(target, ".")
61} 58}
62 59
63// Lists separately the names of directories and files inside the target directory. 60// Lists separately the names of directories and files inside the target directory.
64// Hidden files and directories are not listed. 61// Hidden files and directories are not listed.
65func Ls(target string) (dirs, files []string) { 62func Ls(target string) (dirs, files []string) {
66 directory, err := ioutil.ReadDir(*&target) 63 directory, err := ioutil.ReadDir(target)
67 if err != nil { 64 if err != nil {
68 return 65 return
69 } 66 }
@@ -72,9 +69,9 @@ func Ls(target string) (dirs, files []string) {
72 continue 69 continue
73 } 70 }
74 if element.IsDir() { 71 if element.IsDir() {
75 dirs = append(*&dirs, element.Name()) 72 dirs = append(dirs, element.Name())
76 } else { 73 } else {
77 files = append(*&files, element.Name()) 74 files = append(files, element.Name())
78 } 75 }
79 } 76 }
80 return 77 return
@@ -84,21 +81,21 @@ func Ls(target string) (dirs, files []string) {
84// Returned paths are relative to the given root directory. 81// Returned paths are relative to the given root directory.
85// Hidden files and directories are not listed. 82// Hidden files and directories are not listed.
86func Explore(root string) (dirs, files []string) { 83func Explore(root string) (dirs, files []string) {
87 dirList, fileList := Ls(*&root) 84 dirList, fileList := Ls(root)
88 85
89 for _, file := range fileList { 86 for _, file := range fileList {
90 files = append(*&files, *&file) 87 files = append(files, file)
91 } 88 }
92 89
93 for _, dir := range dirList { 90 for _, dir := range dirList {
94 subRoot := path.Join(*&root, *&dir) 91 subRoot := path.Join(root, dir)
95 dirs = append(dirs, *&subRoot) 92 dirs = append(dirs, subRoot)
96 subDirs, subFiles := Explore(*&subRoot) 93 subDirs, subFiles := Explore(subRoot)
97 for _, subFile := range subFiles { 94 for _, subFile := range subFiles {
98 files = append(*&files, *&subFile) 95 files = append(files, subFile)
99 } 96 }
100 for _, subDir := range subDirs { 97 for _, subDir := range subDirs {
101 dirs = append(*&dirs, *&subDir) 98 dirs = append(dirs, subDir)
102 } 99 }
103 } 100 }
104 return 101 return
@@ -108,26 +105,26 @@ func Explore(root string) (dirs, files []string) {
108// A nonexistent target file is created, otherwise it is truncated. 105// A nonexistent target file is created, otherwise it is truncated.
109// Parent directories are automatically created if they do not exist. 106// Parent directories are automatically created if they do not exist.
110func Cp(source, target string) error { 107func Cp(source, target string) error {
111 sourceFile, err := os.Open(*&source) 108 sourceFile, err := os.Open(source)
112 if err != nil { 109 if err != nil {
113 return err 110 return err
114 } 111 }
115 defer sourceFile.Close() 112 defer sourceFile.Close()
116 113
117 dir, _ := path.Split(*&target) 114 dir, _ := path.Split(target)
118 115
119 err = os.MkdirAll(*&dir, DefaultPerm) 116 err = os.MkdirAll(dir, DefaultPerm)
120 if err != nil { 117 if err != nil {
121 return err 118 return err
122 } 119 }
123 120
124 targetFile, err := os.Create(*&target) 121 targetFile, err := os.Create(target)
125 if err != nil { 122 if err != nil {
126 return err 123 return err
127 } 124 }
128 defer targetFile.Close() 125 defer targetFile.Close()
129 126
130 _, err = io.Copy(*&targetFile, *&sourceFile) 127 _, err = io.Copy(targetFile, sourceFile)
131 return err 128 return err
132} 129}
133 130
@@ -135,56 +132,56 @@ func Cp(source, target string) error {
135// A nonexistent target file is created, otherwise it is truncated. 132// A nonexistent target file is created, otherwise it is truncated.
136// Parent directories are automatically created if they do not exist. 133// Parent directories are automatically created if they do not exist.
137func WriteFile(target string, data []byte) error { 134func WriteFile(target string, data []byte) error {
138 dir, _ := path.Split(*&target) 135 dir, _ := path.Split(target)
139 136
140 err := os.MkdirAll(*&dir, DefaultPerm) 137 err := os.MkdirAll(dir, DefaultPerm)
141 if err != nil { 138 if err != nil {
142 return err 139 return err
143 } 140 }
144 141
145 err = ioutil.WriteFile(*&target, *&data, DefaultPerm) 142 err = ioutil.WriteFile(target, data, DefaultPerm)
146 return err 143 return err
147} 144}
148 145
149// Creates a symbolic link to given source at the target path. 146// Creates a symbolic link to given source at the target path.
150func Lns(source, target string) error { 147func Lns(source, target string) error {
151 return os.Symlink(*&source, *&target) 148 return os.Symlink(source, target)
152} 149}
153 150
154// Returns the destination of the given symbolic link. 151// Returns the destination of the given symbolic link.
155func Lnl(target string) (string, error) { 152func Lnl(target string) (string, error) {
156 return os.Readlink(*&target) 153 return os.Readlink(target)
157} 154}
158 155
159// Renames or moves the source file or directory to the target name or path. 156// Renames or moves the source file or directory to the target name or path.
160func Mv(source, target string) error { 157func Mv(source, target string) error {
161 return os.Rename(*&source, *&target) 158 return os.Rename(source, target)
162} 159}
163 160
164// Removes the target file or the target directory and all files it contains. 161// Removes the target file or the target directory and all files it contains.
165// No error is returned is the target does not exist. 162// No error is returned is the target does not exist.
166func Rm(target string) error { 163func Rm(target string) error {
167 return os.RemoveAll(*&target) 164 return os.RemoveAll(target)
168} 165}
169 166
170// Changes the current working directory to the target directory. 167// Changes the current working directory to the target directory.
171func Cd(target string) error { 168func Cd(target string) error {
172 return os.Chdir(*&target) 169 return os.Chdir(target)
173} 170}
174 171
175// Changes the mode of the target file to the given mode. 172// Changes the mode of the target file to the given mode.
176// If the target is a symbolic link, it changes the mode of the link's target. 173// If the target is a symbolic link, it changes the mode of the link's target.
177func Chmod(target string, mode os.FileMode) error { 174func Chmod(target string, mode os.FileMode) error {
178 return os.Chmod(*&target, *&mode) 175 return os.Chmod(target, mode)
179} 176}
180 177
181// Changes the numeric uid and gid of the target. 178// Changes the numeric uid and gid of the target.
182// If the target is a symbolic link, it changes the uid and gid of the link's target. 179// If the target is a symbolic link, it changes the uid and gid of the link's target.
183func Chown(target string, uid, gid int) error { 180func Chown(target string, uid, gid int) error {
184 return os.Chown(*&target, *&uid, *&gid) 181 return os.Chown(target, uid, gid)
185} 182}
186 183
187// Changes the access and modification times of the target. 184// Changes the access and modification times of the target.
188func Chtimes(target string, atime time.Time, mtime time.Time) error { 185func Chtimes(target string, atime time.Time, mtime time.Time) error {
189 return os.Chtimes(*&target, *&atime, *&mtime) 186 return os.Chtimes(target, atime, mtime)
190} 187}