This repository has been archived on 2024-02-15. You can view files and clone it, but cannot push or open issues or pull requests.
config-mapper/internal/files.go

100 lines
2.3 KiB
Go
Raw Normal View History

2022-02-26 15:46:14 +01:00
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 {
2022-02-27 18:13:27 +01:00
pterm.DefaultSection.Println("Save files into saved location")
2022-02-26 15:46:14 +01:00
haveErr := false
2022-02-27 18:13:27 +01:00
p, _ := pterm.DefaultProgressbar.WithTotal(len(files)).Start()
2022-02-26 15:46:14 +01:00
for _, f := range files {
2022-02-27 18:13:27 +01:00
src, dst, err := configPaths(f, location)
if err != nil {
if err == ErrUnsupportedOS {
return err
2022-02-26 15:46:14 +01:00
}
2022-02-27 18:13:27 +01:00
pterm.Error.Println(fmt.Sprintf("failed to destination resolve path \"%s\": %v", f.Linux, err))
haveErr = true
continue
2022-02-26 15:46:14 +01:00
}
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))
2022-02-27 14:00:55 +01:00
if err := copyFile(src, dst); err != nil {
2022-02-26 15:46:14 +01:00
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))
2022-02-27 18:13:27 +01:00
p.Increment()
2022-02-26 15:46:14 +01:00
}
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 {
2022-02-27 18:13:27 +01:00
dst, src, err := configPaths(f, location)
2022-02-27 14:00:55 +01:00
if err != nil {
if err == ErrUnsupportedOS {
return err
2022-02-26 15:46:14 +01:00
}
2022-02-27 14:00:55 +01:00
pterm.Error.Println(fmt.Sprintf("failed to destination resolve path \"%s\": %v", f.Linux, err))
haveErr = true
continue
2022-02-26 15:46:14 +01:00
}
2022-02-27 14:00:55 +01:00
p.UpdateTitle(fmt.Sprintf("copying \"%s\"", src))
2022-02-26 15:46:14 +01:00
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
}
2022-02-27 14:00:55 +01:00
if err := copyFile(src, dst); err != nil {
2022-02-26 15:46:14 +01:00
pterm.Error.Println(fmt.Sprintf("failed to load file from \"%s\" to \"%s\": %v", src, dst, err))
haveErr = true
continue
}
2022-02-27 14:00:55 +01:00
pterm.Success.Println(fmt.Sprintf("\"%s\" copied", src))
2022-02-26 15:46:14 +01:00
p.Increment()
}
p.Stop()
if haveErr {
return ErrCopy
}
return nil
}