refactor(archi): reduce base code to one struct
This commit is contained in:
parent
569532ad42
commit
759604ebda
36
cmd/cli.go
36
cmd/cli.go
|
@ -59,18 +59,20 @@ var loadCmd = &cobra.Command{
|
|||
os.Exit(1)
|
||||
}
|
||||
|
||||
el := mapper.NewElement([]mapper.ItemLocation{}, config.Storage.Location)
|
||||
|
||||
if !viper.GetBool("load-disable-files") {
|
||||
if err := mapper.LoadFiles(config.Files, config.Storage.Location); err != nil {
|
||||
errLogger.Printf(pterm.Red(err))
|
||||
os.Exit(1)
|
||||
}
|
||||
el.AddItems(config.Files)
|
||||
}
|
||||
if !viper.GetBool("load-disable-folders") {
|
||||
if err := mapper.LoadFolders(config.Folders, config.Storage.Location); err != nil {
|
||||
errLogger.Printf(pterm.Red(err))
|
||||
os.Exit(1)
|
||||
}
|
||||
el.AddItems(config.Folders)
|
||||
}
|
||||
|
||||
if err := el.Action("load"); err != nil {
|
||||
errLogger.Printf(pterm.Red(err))
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if !viper.GetBool("load-disable-pkgs") {
|
||||
if err := mapper.LoadPkgs(config.PackageManagers); err != nil {
|
||||
errLogger.Printf(pterm.Red(err))
|
||||
|
@ -92,18 +94,20 @@ var saveCmd = &cobra.Command{
|
|||
os.Exit(1)
|
||||
}
|
||||
|
||||
el := mapper.NewElement([]mapper.ItemLocation{}, config.Storage.Location)
|
||||
|
||||
if !viper.GetBool("save-disable-files") {
|
||||
if err := mapper.SaveFiles(config.Files, config.Storage.Location); err != nil {
|
||||
errLogger.Printf(pterm.Red(err))
|
||||
os.Exit(1)
|
||||
}
|
||||
el.AddItems(config.Files)
|
||||
}
|
||||
if !viper.GetBool("save-disable-folders") {
|
||||
if err := mapper.SaveFolders(config.Folders, config.Storage.Location); err != nil {
|
||||
errLogger.Printf(pterm.Red(err))
|
||||
os.Exit(1)
|
||||
}
|
||||
el.AddItems(config.Folders)
|
||||
}
|
||||
|
||||
if err := el.Action("save"); err != nil {
|
||||
errLogger.Printf(pterm.Red(err))
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if !viper.GetBool("save-disable-pkgs") {
|
||||
if err := mapper.SavePkgs(config); err != nil {
|
||||
errLogger.Printf(pterm.Red(err))
|
||||
|
|
|
@ -0,0 +1,136 @@
|
|||
package mapper
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/pterm/pterm"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrCopy = errors.New("failed to copy some files")
|
||||
ErrUnsupportedOS = errors.New("unsupported OS. Please, contact the maintainer")
|
||||
)
|
||||
|
||||
type Items struct {
|
||||
locations []ItemLocation
|
||||
storage string
|
||||
progessBar *pterm.ProgressbarPrinter
|
||||
runErr bool
|
||||
}
|
||||
|
||||
type ItemsActions interface {
|
||||
Action(action string) error
|
||||
AddItems(items []ItemLocation)
|
||||
copy(src, dst string) error
|
||||
}
|
||||
|
||||
func NewElement(l []ItemLocation, storage string) ItemsActions {
|
||||
return &Items{
|
||||
l,
|
||||
storage,
|
||||
pterm.DefaultProgressbar.WithTotal(len(l)),
|
||||
false,
|
||||
}
|
||||
}
|
||||
|
||||
func (e *Items) Action(a string) error {
|
||||
pterm.DefaultSection.Println(fmt.Sprintf("%s items", strings.Title(a)))
|
||||
|
||||
e.progessBar.Start()
|
||||
|
||||
for _, f := range e.locations {
|
||||
var src, dst string
|
||||
storagePath, systemPath, err := configPaths(f, e.storage)
|
||||
if err != nil {
|
||||
pterm.Error.Println(fmt.Sprintf("failed to resolve item paths \"%v\": %v", f, err))
|
||||
e.runErr = true
|
||||
continue
|
||||
}
|
||||
if a == "save" {
|
||||
src = systemPath
|
||||
dst = storagePath
|
||||
} else {
|
||||
src = storagePath
|
||||
dst = systemPath
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(path.Dir(dst), 0755); err != nil {
|
||||
pterm.Error.Printfln(fmt.Sprintf("failed to create directory architecture for destination path \"%s\": %v", path.Dir(dst), err))
|
||||
e.runErr = true
|
||||
continue
|
||||
}
|
||||
|
||||
s, err := os.Stat(src)
|
||||
if err != nil {
|
||||
pterm.Error.Println(fmt.Sprintf("failed to check if source path is a folder \"%s\": %v", src, err))
|
||||
e.runErr = true
|
||||
continue
|
||||
}
|
||||
|
||||
e.progessBar.UpdateTitle(fmt.Sprintf("copying %s", src))
|
||||
|
||||
if s.IsDir() {
|
||||
if err := os.Mkdir(dst, 0755); err != nil {
|
||||
if !os.IsExist(err) {
|
||||
pterm.Error.Println(fmt.Sprintf("failed to create destination folder \"%s\": %v", dst, err))
|
||||
e.runErr = true
|
||||
continue
|
||||
}
|
||||
}
|
||||
if err := copyFolder(src, dst); err != nil {
|
||||
pterm.Error.Println(fmt.Sprintf("failed to %s folder from \"%s\" to \"%s\": %v", a, src, dst, err))
|
||||
e.runErr = true
|
||||
continue
|
||||
}
|
||||
} else {
|
||||
if err := copyFile(src, dst); err != nil {
|
||||
pterm.Error.Println(fmt.Sprintf("failed to %s file from \"%s\" to \"%s\": %v", a, src, dst, err))
|
||||
e.runErr = true
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
pterm.Success.Println(fmt.Sprintf("%s copied", src))
|
||||
e.progessBar.Increment()
|
||||
}
|
||||
|
||||
e.progessBar.Stop()
|
||||
if e.runErr {
|
||||
e.runErr = false
|
||||
return ErrCopy
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *Items) AddItems(items []ItemLocation) {
|
||||
e.locations = append(e.locations, items...)
|
||||
}
|
||||
|
||||
func (e *Items) copy(src, dst string) error {
|
||||
s, err := os.Stat(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
e.progessBar.UpdateTitle(fmt.Sprintf("copying %s", src))
|
||||
|
||||
if s.IsDir() {
|
||||
if err := copyFolder(src, dst); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if err := copyFile(src, dst); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
pterm.Success.Println(fmt.Sprintf("\"%s\" copied", src))
|
||||
e.progessBar.Increment()
|
||||
|
||||
return nil
|
||||
}
|
|
@ -1,99 +0,0 @@
|
|||
package mapper
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"github.com/pterm/pterm"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrCopy = errors.New("failed to copy some files")
|
||||
ErrUnsupportedOS = errors.New("unsupported OS. Please, contact the maintainer")
|
||||
)
|
||||
|
||||
func LoadFiles(files []ItemLocation, location string) error {
|
||||
pterm.DefaultSection.Println("Save files into saved location")
|
||||
haveErr := false
|
||||
p, _ := pterm.DefaultProgressbar.WithTotal(len(files)).Start()
|
||||
|
||||
for _, f := range files {
|
||||
src, dst, err := configPaths(f, location)
|
||||
if err != nil {
|
||||
if err == ErrUnsupportedOS {
|
||||
return err
|
||||
}
|
||||
pterm.Error.Println(fmt.Sprintf("failed to destination resolve path \"%s\": %v", f.Linux, err))
|
||||
haveErr = true
|
||||
continue
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(path.Dir(dst), 0755); err != nil {
|
||||
pterm.Error.Printfln(fmt.Sprintf("failed to create directory architecture for destination path \"%s\": %v", dst, err))
|
||||
haveErr = true
|
||||
continue
|
||||
}
|
||||
|
||||
p.UpdateTitle(fmt.Sprintf("copying %s", src))
|
||||
|
||||
if err := copyFile(src, dst); err != nil {
|
||||
pterm.Error.Println(fmt.Sprintf("failed to load file from \"%s\" to \"%s\": %v", src, dst, err))
|
||||
haveErr = true
|
||||
continue
|
||||
}
|
||||
|
||||
pterm.Success.Println(fmt.Sprintf("%s copied", src))
|
||||
p.Increment()
|
||||
}
|
||||
|
||||
p.Stop()
|
||||
if haveErr {
|
||||
return ErrCopy
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func SaveFiles(files []ItemLocation, location string) error {
|
||||
haveErr := false
|
||||
pterm.DefaultSection.Println("Save files into saved location")
|
||||
p, _ := pterm.DefaultProgressbar.WithTotal(len(files)).Start()
|
||||
|
||||
for _, f := range files {
|
||||
dst, src, err := configPaths(f, location)
|
||||
if err != nil {
|
||||
if err == ErrUnsupportedOS {
|
||||
return err
|
||||
}
|
||||
pterm.Error.Println(fmt.Sprintf("failed to destination resolve path \"%s\": %v", f.Linux, err))
|
||||
haveErr = true
|
||||
continue
|
||||
}
|
||||
|
||||
p.UpdateTitle(fmt.Sprintf("copying \"%s\"", src))
|
||||
|
||||
if err := os.MkdirAll(path.Dir(dst), 0755); err != nil {
|
||||
pterm.Error.Printfln(fmt.Sprintf("failed to create directory architecture for destination path \"%s\": %v", dst, err))
|
||||
haveErr = true
|
||||
continue
|
||||
}
|
||||
|
||||
if err := copyFile(src, dst); err != nil {
|
||||
pterm.Error.Println(fmt.Sprintf("failed to load file from \"%s\" to \"%s\": %v", src, dst, err))
|
||||
haveErr = true
|
||||
continue
|
||||
}
|
||||
|
||||
pterm.Success.Println(fmt.Sprintf("\"%s\" copied", src))
|
||||
p.Increment()
|
||||
}
|
||||
|
||||
p.Stop()
|
||||
if haveErr {
|
||||
return ErrCopy
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
|
@ -1,150 +0,0 @@
|
|||
package mapper
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/pterm/pterm"
|
||||
)
|
||||
|
||||
var ErrFolderCopy = errors.New("failed to copy some folders")
|
||||
|
||||
func LoadFolders(folders []ItemLocation, location string) error {
|
||||
haveErr := false
|
||||
pterm.DefaultSection.Println("Load folders into saved location")
|
||||
p, _ := pterm.DefaultProgressbar.WithTotal(len(folders)).Start()
|
||||
|
||||
for _, f := range folders {
|
||||
src, dst, err := configPaths(f, location)
|
||||
if err != nil {
|
||||
if err == ErrUnsupportedOS {
|
||||
return err
|
||||
}
|
||||
pterm.Error.Println(fmt.Sprintf("failed to destination resolve path \"%s\": %v", f.Linux, err))
|
||||
haveErr = true
|
||||
continue
|
||||
}
|
||||
|
||||
s, err := os.Stat(src)
|
||||
if err != nil {
|
||||
pterm.Error.Println(fmt.Sprintf("failed to check if source path is a folder \"%s\": %v", src, err))
|
||||
haveErr = true
|
||||
continue
|
||||
}
|
||||
if !s.IsDir() {
|
||||
pterm.Error.Println(fmt.Sprintf("source path is a file \"%s\"", src))
|
||||
haveErr = true
|
||||
continue
|
||||
}
|
||||
|
||||
p.UpdateTitle(fmt.Sprintf("copying folder \"%s\"", src))
|
||||
|
||||
if err := os.MkdirAll(dst, 0755); err != nil {
|
||||
pterm.Error.Printfln(fmt.Sprintf("failed to create directory architecture for destination path \"%s\": %v", dst, err))
|
||||
haveErr = true
|
||||
continue
|
||||
}
|
||||
|
||||
if err := copyFolder(src, dst); err != nil {
|
||||
pterm.Error.Println(fmt.Sprintf("failed to load folder from \"%s\" to \"%s\": %v", src, dst, err))
|
||||
haveErr = true
|
||||
continue
|
||||
}
|
||||
|
||||
pterm.Success.Println(fmt.Sprintf("\"%s\" copied", src))
|
||||
p.Increment()
|
||||
}
|
||||
|
||||
p.Stop()
|
||||
if haveErr {
|
||||
return ErrCopy
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func SaveFolders(folders []ItemLocation, location string) error {
|
||||
haveErr := false
|
||||
pterm.DefaultSection.Println("Save folders into saved location")
|
||||
p, _ := pterm.DefaultProgressbar.WithTotal(len(folders)).Start()
|
||||
|
||||
for _, f := range folders {
|
||||
dst, src, err := configPaths(f, location)
|
||||
if err != nil {
|
||||
if err == ErrUnsupportedOS {
|
||||
return err
|
||||
}
|
||||
pterm.Error.Println(fmt.Sprintf("failed to destination resolve path \"%s\": %v", f.Linux, err))
|
||||
haveErr = true
|
||||
continue
|
||||
}
|
||||
|
||||
s, err := os.Stat(src)
|
||||
if err != nil {
|
||||
pterm.Error.Println(fmt.Sprintf("failed to check if source path is a folder \"%s\": %v", src, err))
|
||||
haveErr = true
|
||||
continue
|
||||
}
|
||||
if !s.IsDir() {
|
||||
pterm.Error.Println(fmt.Sprintf("source path is a file \"%s\"", src))
|
||||
haveErr = true
|
||||
continue
|
||||
}
|
||||
|
||||
p.UpdateTitle(fmt.Sprintf("copying folder \"%s\"", src))
|
||||
|
||||
if err := os.MkdirAll(dst, 0755); err != nil {
|
||||
pterm.Error.Printfln(fmt.Sprintf("failed to create directory architecture for destination path \"%s\": %v", dst, err))
|
||||
haveErr = true
|
||||
continue
|
||||
}
|
||||
|
||||
if err := copyFolder(src, dst); err != nil {
|
||||
pterm.Error.Println(fmt.Sprintf("failed to save folder from \"%s\" to \"%s\": %v", src, dst, err))
|
||||
haveErr = true
|
||||
continue
|
||||
}
|
||||
|
||||
pterm.Success.Println(fmt.Sprintf("\"%s\" copied", src))
|
||||
p.Increment()
|
||||
}
|
||||
|
||||
p.Stop()
|
||||
if haveErr {
|
||||
return ErrCopy
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func copyFolder(src, dst string) error {
|
||||
var haveErr bool
|
||||
|
||||
items, err := os.ReadDir(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, i := range items {
|
||||
itemName := i.Name()
|
||||
srcItem := fmt.Sprintf("%s/%s", src, itemName)
|
||||
dstItem := fmt.Sprintf("%s/%s", dst, itemName)
|
||||
|
||||
if i.IsDir() {
|
||||
os.Mkdir(dstItem, i.Type().Perm())
|
||||
copyFolder(srcItem, dstItem)
|
||||
continue
|
||||
}
|
||||
|
||||
if err := copyFile(srcItem, dstItem); err != nil {
|
||||
haveErr = true
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
if haveErr {
|
||||
return ErrFolderCopy
|
||||
}
|
||||
return nil
|
||||
}
|
|
@ -104,3 +104,31 @@ func configPaths(f ItemLocation, location string) (string, string, error) {
|
|||
|
||||
return src, dst, nil
|
||||
}
|
||||
|
||||
func copyFolder(src, dst string) error {
|
||||
items, err := os.ReadDir(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, i := range items {
|
||||
itemName := i.Name()
|
||||
srcItem := fmt.Sprintf("%s/%s", src, itemName)
|
||||
dstItem := fmt.Sprintf("%s/%s", dst, itemName)
|
||||
|
||||
if i.IsDir() {
|
||||
if err := os.Mkdir(dstItem, i.Type().Perm()); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := copyFolder(srcItem, dstItem); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if err := copyFile(srcItem, dstItem); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
Reference in New Issue