refactor(config): unmarshal configuration instead of raw read
This commit is contained in:
parent
29a9a03103
commit
b48a15ef3d
33
cmd/cli.go
33
cmd/cli.go
|
@ -21,6 +21,7 @@ var rootCmd = &cobra.Command{
|
|||
Short: "Manage your systems configuration",
|
||||
Long: `config-mapper aims to help you manage your configurations between systems
|
||||
with a single configuration file.`,
|
||||
Version: "v0.1.0.beta0",
|
||||
}
|
||||
var initCmd = &cobra.Command{
|
||||
Use: "init",
|
||||
|
@ -42,11 +43,27 @@ var loadCmd = &cobra.Command{
|
|||
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 {
|
||||
var config mapper.Configuration
|
||||
|
||||
if err := viper.Unmarshal(&config); err != nil {
|
||||
errLogger.Printf(pterm.Red(fmt.Sprintf("failed to decode configuration: %v\n", err)))
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if !viper.GetBool("disable-files") {
|
||||
if err := mapper.LoadFiles(config.Files); err != nil {
|
||||
errLogger.Printf(pterm.Red(fmt.Sprintf("error while loading files: %v\n", err)))
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
if !viper.GetBool("disable-folders") {
|
||||
if err := mapper.LoadFolders(config.Folders); err != nil {
|
||||
errLogger.Printf(pterm.Red(fmt.Sprintf("error while loading folders: %v\n", err)))
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
if !viper.GetBool("disable-pkgs") {
|
||||
if err := mapper.LoadPkgs(config.PackageManagers); err != nil {
|
||||
errLogger.Printf(pterm.Red(fmt.Sprintf("error while installing packages: %v\n", err)))
|
||||
os.Exit(1)
|
||||
}
|
||||
|
@ -64,9 +81,9 @@ func init() {
|
|||
loadCmd.PersistentFlags().Bool("disable-folders", false, "folders will be ignored")
|
||||
loadCmd.PersistentFlags().Bool("disable-pkgs", false, "package managers will be ignored")
|
||||
|
||||
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"))
|
||||
viper.BindPFlag("disable-files", loadCmd.PersistentFlags().Lookup("disable-files"))
|
||||
viper.BindPFlag("disable-folders", loadCmd.PersistentFlags().Lookup("disable-folders"))
|
||||
viper.BindPFlag("disable-pkgs", loadCmd.PersistentFlags().Lookup("disable-pkgs"))
|
||||
}
|
||||
|
||||
func Execute() {
|
||||
|
|
|
@ -9,27 +9,32 @@ import (
|
|||
)
|
||||
|
||||
type Configuration struct {
|
||||
Storage Storage `yaml:"storage"`
|
||||
Files []string `yaml:"files"`
|
||||
Folders []string `yaml:"folders"`
|
||||
PackageManagers PkgManagers `yaml:"package-managers"`
|
||||
Storage Storage `mapstructure:"storage"`
|
||||
Files []ItemLocation `mapstructure:"files"`
|
||||
Folders []ItemLocation `mapstructure:"folders"`
|
||||
PackageManagers PkgManagers `mapstructure:"package-managers"`
|
||||
}
|
||||
|
||||
type ItemLocation struct {
|
||||
Darwin string `mapstructure:"darwin"`
|
||||
Linux string `mapstructure:"linux"`
|
||||
}
|
||||
|
||||
type Storage struct {
|
||||
Location string `yaml:"location"`
|
||||
Git Git `yaml:"git"`
|
||||
Location string `mapstructure:"location"`
|
||||
Git Git `mapstructure:"git"`
|
||||
}
|
||||
|
||||
type Git struct {
|
||||
Username string `yaml:"username"`
|
||||
Password string `yaml:"password"`
|
||||
Repository string `yaml:"repository"`
|
||||
Username string `mapstructure:"username"`
|
||||
Password string `mapstructure:"password"`
|
||||
Repository string `mapstructure:"repository"`
|
||||
}
|
||||
|
||||
type PkgManagers struct {
|
||||
InstallationOrder []string `yaml:"installation-order"`
|
||||
Homebrew []string `yaml:"homebrew"`
|
||||
AptGet interface{} `yaml:"apt-get"`
|
||||
InstallationOrder []string `mapstructure:"installation-order"`
|
||||
Homebrew []string `mapstructure:"homebrew"`
|
||||
Aptitude []string `mapstructure:"apt-get"`
|
||||
}
|
||||
|
||||
func InitConfig() {
|
||||
|
|
|
@ -7,7 +7,6 @@ import (
|
|||
"os/exec"
|
||||
|
||||
"github.com/pterm/pterm"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -15,17 +14,15 @@ var (
|
|||
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 {
|
||||
func LoadPkgs(c PkgManagers) error {
|
||||
for _, pkg := range c.InstallationOrder {
|
||||
switch pkg {
|
||||
case "homebrew":
|
||||
if err := installBrewPkgs(); err != nil {
|
||||
if err := installBrewPkgs(c.Homebrew); err != nil {
|
||||
return ErrFailedInstallation
|
||||
}
|
||||
case "apt":
|
||||
if err := installAptPkgs(); err != nil {
|
||||
if err := installAptPkgs(c.Aptitude); err != nil {
|
||||
return ErrFailedInstallation
|
||||
}
|
||||
}
|
||||
|
@ -34,12 +31,12 @@ func LoadPkgs() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func installBrewPkgs() error {
|
||||
func installBrewPkgs(pkgs []string) error {
|
||||
if _, err := exec.LookPath("brew"); err != nil {
|
||||
errLogger.Println(pterm.Red("Homebrew is not installed on your system"))
|
||||
return nil
|
||||
}
|
||||
|
||||
pkgs := viper.GetStringSlice("package-managers.homebrew")
|
||||
if len(pkgs) == 0 {
|
||||
pterm.Println(pterm.Blue("homebrew: nothing to do"))
|
||||
return nil
|
||||
|
@ -67,15 +64,17 @@ func installBrewPkgs() error {
|
|||
return err
|
||||
}
|
||||
|
||||
introSpinner.Success("Packages intalled succesfully")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func installAptPkgs() error {
|
||||
func installAptPkgs(pkgs []string) error {
|
||||
if _, err := exec.LookPath("apt-get"); err != nil {
|
||||
errLogger.Println(pterm.Red("aptitude is not available on your system"))
|
||||
return nil
|
||||
}
|
||||
|
||||
pkgs := viper.GetStringSlice("package-managers.apt")
|
||||
if len(pkgs) == 0 {
|
||||
pterm.Println(pterm.Blue("aptitude: nothing to do"))
|
||||
return nil
|
||||
|
@ -104,5 +103,7 @@ func installAptPkgs() error {
|
|||
return err
|
||||
}
|
||||
|
||||
introSpinner.Success("Packages intalled succesfully")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
Reference in New Issue