Added MEGA module

This commit is contained in:
cdmnky 2024-05-12 14:32:32 -04:00
parent 41783d965b
commit 87929750ae
2 changed files with 166 additions and 0 deletions

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module cd.cdmnky.dev/cdmnky/go
go 1.20

163
mega/mega.go Normal file
View File

@ -0,0 +1,163 @@
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 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 Find(dir, pattern, sortorder string) (files []FileInfo, err error) {
pattern = "--pattern=" + pattern
fmt.Println("cmd:", FIND, dir, pattern)
out, err := exec.Command(FIND, dir, pattern).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), 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
}
// 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
}