179 lines
3.6 KiB
Go
179 lines
3.6 KiB
Go
package mega
|
|
|
|
import (
|
|
"fmt"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"sort"
|
|
"strings"
|
|
)
|
|
|
|
const (
|
|
CP = "/usr/bin/mega-cp"
|
|
FIND = "/usr/bin/mega-find"
|
|
GET = "/usr/bin/mega-get"
|
|
LS = "/usr/bin/mega-ls"
|
|
MKDIR = "/usr/bin/mega-mkdir"
|
|
MV = "/usr/bin/mega-mv"
|
|
RM = "/usr/bin/mega-rm"
|
|
)
|
|
|
|
type FileInfo struct {
|
|
Dir string
|
|
Name string
|
|
FQN string
|
|
}
|
|
|
|
// List returns an array of mega.FileInfo restricted by specified filetype. Pass an empty
|
|
// string array for all files. The array will be sorted by name based on the sortorder parameter.
|
|
// Valid values are:
|
|
//
|
|
// "asc|desc"
|
|
//
|
|
// Passing an empty string will default to "asc"
|
|
func List(dir string, filetypes []string, sortorder string) (files []FileInfo, err error) {
|
|
|
|
out, err := exec.Command(LS, dir).Output()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
lines := strings.Split(string(out), "\n")
|
|
for _, line := range lines {
|
|
|
|
if len(filetypes) == 0 {
|
|
files = append(files, FileInfo{dir, line, filepath.Join(dir, line)})
|
|
} else {
|
|
for _, filetype := range filetypes {
|
|
if strings.HasSuffix(line, filetype) {
|
|
files = append(files, FileInfo{dir, line, filepath.Join(dir, line)})
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
if len(sortorder) == 0 {
|
|
sortorder = "asc"
|
|
}
|
|
|
|
sort.Slice(files, func(i, j int) bool {
|
|
iVal := strings.ToLower(files[i].Name)
|
|
jVal := strings.ToLower(files[j].Name)
|
|
if sortorder == "asc" {
|
|
return iVal < jVal
|
|
}
|
|
return iVal > jVal
|
|
})
|
|
|
|
return
|
|
}
|
|
|
|
// Backwards compatability
|
|
func Files(dir string, filetypes []string, sortorder string) (files []FileInfo, err error) {
|
|
return List(dir, filetypes, sortorder)
|
|
}
|
|
|
|
// Find returns an array of mega.FileInfo
|
|
//
|
|
// To specify a sort order pass either of the following:
|
|
//
|
|
// --sort=asc | --sort=desc
|
|
//
|
|
// If sort order is not specified it will default to "asc"
|
|
func Find(dir string, patterns ...string) (files []FileInfo, err error) {
|
|
|
|
sortorder := "asc"
|
|
_params := []string{}
|
|
for _, pattern := range patterns {
|
|
if strings.HasPrefix(pattern, "--sort=") {
|
|
_toks := strings.Split(pattern, "=")
|
|
if len(_toks) == 2 && (_toks[1] == "asc" || _toks[1] == "desc") {
|
|
sortorder = _toks[1]
|
|
}
|
|
continue
|
|
}
|
|
_params = append(_params, "--pattern="+pattern)
|
|
}
|
|
|
|
for _, param := range _params {
|
|
params := []string{dir, param}
|
|
fmt.Printf("cmd: %s %s\n", FIND, params)
|
|
out, err := exec.Command(FIND, params...).Output()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
lines := strings.Split(string(out), "\n")
|
|
for _, line := range lines {
|
|
files = append(files, FileInfo{filepath.Dir(line), filepath.Base(line), line})
|
|
}
|
|
|
|
}
|
|
|
|
if len(sortorder) == 0 {
|
|
sortorder = "asc"
|
|
}
|
|
|
|
sort.Slice(files, func(i, j int) bool {
|
|
iVal := strings.ToLower(files[i].Name)
|
|
jVal := strings.ToLower(files[j].Name)
|
|
if sortorder == "asc" {
|
|
return iVal < jVal
|
|
}
|
|
return iVal > jVal
|
|
})
|
|
|
|
return
|
|
}
|
|
|
|
// Remove executes mega-rm src
|
|
func Remove(src string) (err error) {
|
|
_, err = exec.Command(RM, src).Output()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// Copy executes mega-cp src dest
|
|
func Copy(src, dest string) (err error) {
|
|
_, err = exec.Command(CP, src, dest).Output()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// Move executes mega-mv src dest
|
|
func Move(src, dest string) (err error) {
|
|
_, err = exec.Command(MV, src, dest).Output()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// Get executes mega-get src dest
|
|
func Get(src, dest string) (err error) {
|
|
_, err = exec.Command(GET, src, dest).Output()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// Mkdir executes mega-mkdir src dest
|
|
func Mkdir(path string) (err error) {
|
|
_, err = exec.Command(MKDIR, path).Output()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return
|
|
}
|