feat(cli): implement pkgs installation

This commit is contained in:
DataHearth 2022-02-25 00:17:02 +01:00
parent 17ea7c89de
commit 29a9a03103
4 changed files with 177 additions and 32 deletions

View File

@ -5,7 +5,8 @@ import (
"log"
"os"
"github.com/datahearth/config-mapper/internal/git"
mapper "github.com/datahearth/config-mapper/internal"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
@ -28,46 +29,44 @@ var initCmd = &cobra.Command{
copy it into the destination field`,
Run: func(cmd *cobra.Command, args []string) {
logger.Println("initializing config-mapper folder from configuration...")
if _, err := git.OpenGitRepo(); err != nil {
errLogger.Printf("failed to initialize folder: %v\n", err)
if _, err := mapper.OpenGitRepo(); err != nil {
errLogger.Printf(pterm.Red(fmt.Sprintf("failed to initialize folder: %v\n", err)))
os.Exit(1)
}
logger.Printf("repository initialized at \"%v\"\n", viper.GetString("storage.location"))
},
}
func init() {
cobra.OnInitialize(initConfig)
rootCmd.AddCommand(initCmd)
var loadCmd = &cobra.Command{
Use: "load",
Short: "Load your configurations onto your system",
Long: `Load your files, folders and package managers deps configurations onto your new
onto your new system based on your configuration file`,
Run: func(cmd *cobra.Command, args []string) {
viper.GetStringSlice("files")
viper.GetStringSlice("folder")
packageManagers := viper.GetStringMap("package-managers")
if packageManagers != nil {
if err := mapper.LoadPkgs(); err != nil {
errLogger.Printf(pterm.Red(fmt.Sprintf("error while installing packages: %v\n", err)))
os.Exit(1)
}
}
},
}
func initConfig() {
h, err := os.UserHomeDir()
if err != nil {
errLogger.Printf("can't get home directory through $HOME variable: %v\n", err)
os.Exit(1)
}
func init() {
cobra.OnInitialize(mapper.InitConfig)
if c := os.Getenv("CONFIG_MAPPER_CFG"); c != "" {
viper.AddConfigPath(c)
} else {
viper.AddConfigPath(h)
}
rootCmd.AddCommand(initCmd)
rootCmd.AddCommand(loadCmd)
viper.SetConfigType("yml")
viper.SetConfigName("config-mapper")
loadCmd.PersistentFlags().Bool("disable-files", false, "files will be ignored")
loadCmd.PersistentFlags().Bool("disable-folders", false, "folders will be ignored")
loadCmd.PersistentFlags().Bool("disable-pkgs", false, "package managers will be ignored")
viper.SetDefault("storage.location", fmt.Sprintf("%s/config-mapper", os.TempDir()))
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
errLogger.Println("configuration file not found", err)
} else {
errLogger.Printf("failed to read config: %v\n", err)
}
os.Exit(1)
}
viper.BindPFlag("disable-files", loadCmd.Flags().Lookup("disable-files"))
viper.BindPFlag("disable-folders", loadCmd.Flags().Lookup("disable-folders"))
viper.BindPFlag("disable-pkgs", loadCmd.Flags().Lookup("disable-pkgs"))
}
func Execute() {

View File

@ -1,5 +1,13 @@
package mapper
import (
"fmt"
"os"
"github.com/pterm/pterm"
"github.com/spf13/viper"
)
type Configuration struct {
Storage Storage `yaml:"storage"`
Files []string `yaml:"files"`
@ -23,3 +31,33 @@ type PkgManagers struct {
Homebrew []string `yaml:"homebrew"`
AptGet interface{} `yaml:"apt-get"`
}
func InitConfig() {
h, err := os.UserHomeDir()
if err != nil {
errLogger.Printf("can't get home directory through $HOME variable: %v\n", err)
os.Exit(1)
}
if c := os.Getenv("CONFIG_MAPPER_CFG"); c != "" {
viper.AddConfigPath(c)
} else {
viper.AddConfigPath(h)
}
viper.SetConfigType("yml")
viper.SetConfigName(".config-mapper")
viper.SetDefault("storage.location", fmt.Sprintf("%s/config-mapper", os.TempDir()))
viper.SetDefault("package-managers.installation-order", []string{"apt", "homebrew"})
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
errLogger.Println(pterm.Red(err))
} else {
errLogger.Printf(pterm.Red(fmt.Sprintf("failed to read config: %v\n", err)))
}
os.Exit(1)
}
}

View File

@ -1,4 +1,4 @@
package git
package mapper
import (
"errors"

108
internal/pkgs.go Normal file
View File

@ -0,0 +1,108 @@
package mapper
import (
"errors"
"log"
"os"
"os/exec"
"github.com/pterm/pterm"
"github.com/spf13/viper"
)
var (
errLogger = log.New(os.Stderr, "", 0)
ErrFailedInstallation = errors.New("failed to installed some packages. Please, checkout STDERR for more information")
)
func LoadPkgs() error {
order := viper.GetStringSlice("package-managers.installation-order")
for _, pkg := range order {
switch pkg {
case "homebrew":
if err := installBrewPkgs(); err != nil {
return ErrFailedInstallation
}
case "apt":
if err := installAptPkgs(); err != nil {
return ErrFailedInstallation
}
}
}
return nil
}
func installBrewPkgs() error {
if _, err := exec.LookPath("brew"); err != nil {
errLogger.Println(pterm.Red("Homebrew is not installed on your system"))
}
pkgs := viper.GetStringSlice("package-managers.homebrew")
if len(pkgs) == 0 {
pterm.Println(pterm.Blue("homebrew: nothing to do"))
return nil
}
cmd := exec.Command("brew", "install")
cmd.Args = append(cmd.Args, pkgs...)
introSpinner, _ := pterm.DefaultSpinner.WithShowTimer(true).WithRemoveWhenDone(false).Start("Installing homebrew packages")
chErr := make(chan error)
defer close(chErr)
go func(chErr chan error) {
if err := cmd.Run(); err != nil {
chErr <- err
return
}
chErr <- nil
}(chErr)
err := <-chErr
introSpinner.Stop()
if err != nil {
return err
}
return nil
}
func installAptPkgs() error {
if _, err := exec.LookPath("apt-get"); err != nil {
errLogger.Println(pterm.Red("aptitude is not available on your system"))
}
pkgs := viper.GetStringSlice("package-managers.apt")
if len(pkgs) == 0 {
pterm.Println(pterm.Blue("aptitude: nothing to do"))
return nil
}
cmd := exec.Command("sudo", "apt-get", "install")
cmd.Args = append(cmd.Args, pkgs...)
introSpinner, _ := pterm.DefaultSpinner.WithShowTimer(true).WithRemoveWhenDone(false).Start("Installing aptitude packages")
chErr := make(chan error)
defer close(chErr)
go func(chErr chan error) {
if err := cmd.Run(); err != nil {
chErr <- err
return
}
chErr <- nil
}(chErr)
err := <-chErr
introSpinner.Stop()
if err != nil {
return err
}
return nil
}