fix(pkgs): update command building
This commit is contained in:
parent
8051912b35
commit
f85fc9d39c
|
@ -46,18 +46,18 @@ func NewItemsActions(items []configuration.OSLocation, storage string, repositor
|
|||
//
|
||||
// If the performed action is "save", it'll also write the `.index` file with all new items.
|
||||
func (e *Items) Action(action string) {
|
||||
color.Blue("# %s files and folders\n", action)
|
||||
fmt.Printf("# %s files and folders\n", action)
|
||||
newLines := []string{}
|
||||
|
||||
for i, l := range e.locations {
|
||||
var src string
|
||||
storagePath, systemPath, err := misc.ConfigPaths(l, e.storage)
|
||||
if err != nil {
|
||||
PrintError("[%d] failed to resolve item paths \"%v\": %v", i, l, err)
|
||||
PrintError("❌ failed to resolve item paths \"%v\": %v", i, l, err)
|
||||
continue
|
||||
}
|
||||
if storagePath == "" && systemPath == "" {
|
||||
color.Blue("[%d] file doesn't have configuration path for current OS. Skipping...", i)
|
||||
fmt.Printf("⛔ Skipping %s\n", src)
|
||||
continue
|
||||
}
|
||||
|
||||
|
@ -74,7 +74,7 @@ func (e *Items) Action(action string) {
|
|||
e.loadItem(storagePath, systemPath, i)
|
||||
}
|
||||
|
||||
color.Green("[%d] %s copied", i, src)
|
||||
fmt.Printf("✔️ %s\n", src)
|
||||
}
|
||||
|
||||
if action == "save" && !viper.GetBool("disable-index-update") {
|
||||
|
@ -94,13 +94,13 @@ func (e *Items) Action(action string) {
|
|||
// (E.g: /home/user/.config => .config)
|
||||
func (e *Items) saveItem(src, dst string, index int) string {
|
||||
if err := os.MkdirAll(path.Dir(dst), 0755); err != nil {
|
||||
PrintError("[%d] failed to create directory architecture for destination path \"%s\": %v", index, path.Dir(dst), err)
|
||||
PrintError("❌ failed to create directory architecture for destination path \"%s\": %v", index, path.Dir(dst), err)
|
||||
return ""
|
||||
}
|
||||
|
||||
s, err := os.Stat(src)
|
||||
if err != nil {
|
||||
PrintError("[%d] failed to check if source path is a folder \"%s\": %v", index, src, err)
|
||||
PrintError("❌ failed to check if source path is a folder \"%s\": %v", index, src, err)
|
||||
return ""
|
||||
}
|
||||
|
||||
|
@ -109,7 +109,7 @@ func (e *Items) saveItem(src, dst string, index int) string {
|
|||
s, err := os.Stat(dst)
|
||||
if err != nil {
|
||||
if !os.IsNotExist(err) {
|
||||
PrintError("[%d] failed to check if destination folder \"%s\" exists: %v", index, dst, err)
|
||||
PrintError("❌ failed to check if destination folder \"%s\" exists: %v", index, dst, err)
|
||||
return ""
|
||||
}
|
||||
} else {
|
||||
|
@ -118,29 +118,29 @@ func (e *Items) saveItem(src, dst string, index int) string {
|
|||
|
||||
// remove the destination if it exists. It cleans up the saved location from unused files
|
||||
if err := os.RemoveAll(dst); err != nil {
|
||||
PrintError("[%d] failed to truncate destination folder \"%s\": %v", index, dst, err)
|
||||
PrintError("❌ failed to truncate destination folder \"%s\": %v", index, dst, err)
|
||||
}
|
||||
|
||||
if err := os.Mkdir(dst, dstPerms); err != nil {
|
||||
if !os.IsExist(err) {
|
||||
PrintError("[%d] failed to create destination folder \"%s\": %v", index, dst, err)
|
||||
PrintError("❌ failed to create destination folder \"%s\": %v", index, dst, err)
|
||||
return ""
|
||||
}
|
||||
}
|
||||
if err := misc.CopyFolder(src, dst, true); err != nil {
|
||||
PrintError("[%d] failed to save folder from \"%s\" to \"%s\": %v", index, src, dst, err)
|
||||
PrintError("❌ failed to save folder from \"%s\" to \"%s\": %v", index, src, dst, err)
|
||||
return ""
|
||||
}
|
||||
} else {
|
||||
if err := misc.CopyFile(src, dst); err != nil {
|
||||
PrintError("[%d] failed to save file from \"%s\" to \"%s\": %v", index, src, dst, err)
|
||||
PrintError("❌ failed to save file from \"%s\" to \"%s\": %v", index, src, dst, err)
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
p, err := misc.AbsolutePath(e.storage)
|
||||
if err != nil {
|
||||
PrintError("[%d] failed resolve absolute path from configuration storage: %v", index, err)
|
||||
PrintError("❌ failed resolve absolute path from configuration storage: %v", index, err)
|
||||
return ""
|
||||
}
|
||||
|
||||
|
@ -153,13 +153,13 @@ func (e *Items) saveItem(src, dst string, index int) string {
|
|||
// (meaning the item hasn't been saved) and prints the error in STDERR.
|
||||
func (e *Items) loadItem(src, dst string, index int) {
|
||||
if err := os.MkdirAll(path.Dir(dst), 0755); err != nil {
|
||||
PrintError("[%d] failed to create directory architecture for destination path \"%s\": %v", index, path.Dir(dst), err)
|
||||
PrintError("❌ failed to create directory architecture for destination path \"%s\": %v", index, path.Dir(dst), err)
|
||||
return
|
||||
}
|
||||
|
||||
s, err := os.Stat(src)
|
||||
if err != nil {
|
||||
PrintError("[%d] failed to check if source path is a folder \"%s\": %v", index, src, err)
|
||||
PrintError("❌ failed to check if source path is a folder \"%s\": %v", index, src, err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -168,7 +168,7 @@ func (e *Items) loadItem(src, dst string, index int) {
|
|||
s, err := os.Stat(dst)
|
||||
if err != nil {
|
||||
if !os.IsNotExist(err) {
|
||||
PrintError("[%d] failed to check if destination folder \"%s\" exists: %v", index, dst, err)
|
||||
PrintError("❌ failed to check if destination folder \"%s\" exists: %v", index, dst, err)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
|
@ -177,17 +177,17 @@ func (e *Items) loadItem(src, dst string, index int) {
|
|||
|
||||
if err := os.Mkdir(dst, dstPerms); err != nil {
|
||||
if !os.IsExist(err) {
|
||||
PrintError("[%d] failed to create destination folder \"%s\": %v", index, dst, err)
|
||||
PrintError("❌ failed to create destination folder \"%s\": %v", index, dst, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
if err := misc.CopyFolder(src, dst, false); err != nil {
|
||||
PrintError("[%d] failed to load folder from \"%s\" to \"%s\": %v", index, src, dst, err)
|
||||
PrintError("❌ failed to load folder from \"%s\" to \"%s\": %v", index, src, dst, err)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
if err := misc.CopyFile(src, dst); err != nil {
|
||||
PrintError("[%d] failed to load file from \"%s\" to \"%s\": %v", index, src, dst, err)
|
||||
PrintError("❌ failed to load file from \"%s\" to \"%s\": %v", index, src, dst, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
112
internal/pkgs.go
112
internal/pkgs.go
|
@ -8,7 +8,6 @@ import (
|
|||
"strings"
|
||||
|
||||
"github.com/datahearth/config-mapper/internal/configuration"
|
||||
"github.com/fatih/color"
|
||||
"github.com/gernest/wow"
|
||||
"github.com/gernest/wow/spin"
|
||||
"github.com/spf13/viper"
|
||||
|
@ -16,16 +15,16 @@ import (
|
|||
|
||||
// InstallPackages install all packages from the configuration file by installation order
|
||||
func InstallPackages(c configuration.PkgManagers) error {
|
||||
color.Blue("\n# Installing packages")
|
||||
pkgManagers := map[string]bool{}
|
||||
for _, pkgManager := range viper.GetStringSlice("exclude-pkg-managers") {
|
||||
pkgManagers[pkgManager] = true
|
||||
}
|
||||
|
||||
fmt.Println()
|
||||
for _, pkgManager := range c.InstallationOrder {
|
||||
color.Blue("## Installing %s packages", pkgManager)
|
||||
fmt.Printf("# Installing %s packages\n", pkgManager)
|
||||
if _, ok := pkgManagers[pkgManager]; ok {
|
||||
color.Blue("Skipping %s packages", pkgManager)
|
||||
fmt.Printf("⛔ Skipping %s packages\n", pkgManager)
|
||||
fmt.Println()
|
||||
continue
|
||||
}
|
||||
|
@ -71,50 +70,89 @@ func InstallPackages(c configuration.PkgManagers) error {
|
|||
}
|
||||
|
||||
if len(pkgs) == 0 {
|
||||
fmt.Printf("%s: nothing to do\n", pkgManager)
|
||||
fmt.Printf("✔️ nothing to do\n\n")
|
||||
continue
|
||||
}
|
||||
|
||||
var cmd *exec.Cmd
|
||||
v := viper.GetBool("verbose")
|
||||
commands := []*exec.Cmd{}
|
||||
// * package managers requiring sudo permission
|
||||
if pkgManager == "apt" || pkgManager == "nala" {
|
||||
cmd = exec.Command("sudo", pkgManager, "install", "-y")
|
||||
commands = append(commands, buildDefaultCommand([]string{"sudo", pkgManager, "install", "-y"}, pkgs, v))
|
||||
} else if pkgManager == "cargo" {
|
||||
commands = buildCargoCommand(pkgs, v)
|
||||
} else {
|
||||
cmd = exec.Command(pkgManager, "install")
|
||||
commands = append(commands, buildDefaultCommand([]string{pkgManager, "install"}, pkgs, v))
|
||||
}
|
||||
|
||||
for _, pkg := range pkgs {
|
||||
if strings.Contains(pkg, " ") {
|
||||
cmd.Args = append(cmd.Args, strings.Split(pkg, " ")...)
|
||||
} else {
|
||||
cmd.Args = append(cmd.Args, pkg)
|
||||
for i, cmd := range commands {
|
||||
spinner := wow.New(os.Stdout, spin.Get(spin.Dots3), " Installing...")
|
||||
if !v {
|
||||
spinner.Start()
|
||||
}
|
||||
if err := cmd.Run(); err != nil {
|
||||
if v {
|
||||
PrintError(err.Error())
|
||||
} else {
|
||||
msg := fmt.Sprintf(" %s", cmd.Args)
|
||||
if i == len(commands)-1 {
|
||||
msg = fmt.Sprintf("%s\n", msg)
|
||||
}
|
||||
spinner.PersistWith(spin.Spinner{Frames: []string{"❌"}}, msg)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
if !v {
|
||||
// msg := fmt.Sprintf(" %s %s", color.GreenString("Success\t"), cmd.Args)
|
||||
msg := fmt.Sprintf(" %s", cmd.Args)
|
||||
if i == len(commands)-1 {
|
||||
msg = fmt.Sprintf("%s\n", msg)
|
||||
}
|
||||
spinner.PersistWith(spin.Spinner{Frames: []string{"✔️"}}, msg)
|
||||
}
|
||||
}
|
||||
|
||||
spinner := wow.New(os.Stdout, spin.Get(spin.Dots3), " Running...")
|
||||
|
||||
v := viper.GetBool("verbose")
|
||||
if v {
|
||||
cmd.Stderr = os.Stderr
|
||||
cmd.Stdout = os.Stdout
|
||||
} else {
|
||||
spinner.Start()
|
||||
}
|
||||
|
||||
if err := cmd.Run(); err != nil {
|
||||
spinner.Stop()
|
||||
PrintError("\n%s command failed: %v", pkgManager, err)
|
||||
return err
|
||||
}
|
||||
|
||||
if v {
|
||||
// todo: find a way to clear spinner when done
|
||||
spinner.Stop()
|
||||
fmt.Println()
|
||||
}
|
||||
color.Green("%s packages intalled succesfully !", pkgManager)
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func buildCargoCommand(packages []string, verbose bool) []*exec.Cmd {
|
||||
commands := []*exec.Cmd{}
|
||||
|
||||
cmd := exec.Command("cargo", "install")
|
||||
for _, pkg := range packages {
|
||||
if strings.Contains(pkg, " ") {
|
||||
customCmd := exec.Command("cargo", "install")
|
||||
customCmd.Args = append(cmd.Args, strings.Split(pkg, " ")...)
|
||||
if verbose {
|
||||
customCmd.Stderr = os.Stderr
|
||||
customCmd.Stdout = os.Stdout
|
||||
}
|
||||
commands = append(commands, customCmd)
|
||||
} else {
|
||||
cmd.Args = append(cmd.Args, pkg)
|
||||
}
|
||||
}
|
||||
|
||||
if verbose {
|
||||
cmd.Stderr = os.Stderr
|
||||
cmd.Stdout = os.Stdout
|
||||
}
|
||||
if len(cmd.Args) > 2 {
|
||||
commands = append(commands, cmd)
|
||||
}
|
||||
|
||||
return commands
|
||||
}
|
||||
|
||||
func buildDefaultCommand(command, packages []string, verbose bool) *exec.Cmd {
|
||||
cmd := exec.Command(command[0], command[1:]...)
|
||||
cmd.Args = append(cmd.Args, packages...)
|
||||
if verbose {
|
||||
cmd.Stderr = os.Stderr
|
||||
cmd.Stdout = os.Stdout
|
||||
}
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
|
Reference in New Issue