feat(cli): add verbose flag and a spinner for pkgs

This commit is contained in:
DataHearth 2022-06-06 15:32:01 +02:00
parent 588e3c1e66
commit d3bab447e0
No known key found for this signature in database
GPG Key ID: E88FD356ACC5F3C4
5 changed files with 33 additions and 12 deletions

View File

@ -54,10 +54,12 @@ func init() {
rootCmd.AddCommand(initCmd)
rootCmd.AddCommand(loadCmd)
rootCmd.AddCommand(saveCmd)
rootCmd.PersistentFlags().BoolP("verbose", "v", false, "STDOUT will be more verbose")
rootCmd.PersistentFlags().StringP("configuration-file", "c", "", "location of configuration file")
rootCmd.PersistentFlags().String("ssh-user", "", "SSH username to retrieve configuration file")
rootCmd.PersistentFlags().String("ssh-password", "", "SSH password to retrieve configuration file")
rootCmd.PersistentFlags().String("ssh-key", "", "SSH key to retrieve configuration file (if a passphrase is needed, use the \"CONFIG_MAPPER_PASS\" env variable")
viper.BindPFlag("verbose", rootCmd.PersistentFlags().Lookup("verbose"))
viper.BindPFlag("configuration-file", rootCmd.PersistentFlags().Lookup("configuration-file"))
viper.BindPFlag("ssh-user", rootCmd.PersistentFlags().Lookup("ssh-user"))
viper.BindPFlag("ssh-password", rootCmd.PersistentFlags().Lookup("ssh-password"))

5
go.mod
View File

@ -3,11 +3,13 @@ module github.com/datahearth/config-mapper
go 1.17
require (
github.com/fatih/color v1.13.0
github.com/gernest/wow v0.1.0
github.com/go-git/go-git/v5 v5.4.2
github.com/spf13/cobra v1.3.0
github.com/spf13/viper v1.10.1
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
github.com/fatih/color v1.13.0
)
require (
@ -36,7 +38,6 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/xanzy/ssh-agent v0.3.0 // indirect
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d // indirect
golang.org/x/sys v0.0.0-20211210111614-af8b64212486 // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect

4
go.sum
View File

@ -123,6 +123,8 @@ github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYF
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
github.com/fsnotify/fsnotify v1.5.1 h1:mZcQUHVQUQWoPXXtuf9yuEXKudkV2sx1E06UadKWpgI=
github.com/fsnotify/fsnotify v1.5.1/go.mod h1:T3375wBYaZdLLcVNkcVbzGHY7f1l/uK5T5Ai1i3InKU=
github.com/gernest/wow v0.1.0 h1:g9xdwCwP0+xgVYlA2sopI0gZHqXe7HjI/7/LykG4fks=
github.com/gernest/wow v0.1.0/go.mod h1:dEPabJRi5BneI1Nev1VWo0ZlcTWibHWp43qxKms4elY=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gliderlabs/ssh v0.2.2 h1:6zsha5zo/TWhRhwqCD3+EarCAgZ2yN28ipRnGPnwkI0=
github.com/gliderlabs/ssh v0.2.2/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0=
@ -407,6 +409,7 @@ go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9i
go.uber.org/zap v1.17.0/go.mod h1:MXVU+bhUf/A7Xi2HNOnopQOrmycQ5Ih87HtOu4q5SSo=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190103213133-ff983b9c42bc/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190219172222-a4c6cb3142f2/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
@ -531,6 +534,7 @@ golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181026203630-95b1ffbd15a5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190116161447-11f53e031339/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

View File

@ -41,7 +41,7 @@ func NewItemsActions(items []configuration.OSLocation, storage string, repositor
}
func (e *Items) Action(action string) {
color.Blue("# %s", action)
color.Blue("# %s files and folders\n", action)
newLines := []string{}
for i, l := range e.locations {

View File

@ -3,19 +3,19 @@ package mapper
import (
"errors"
"fmt"
"log"
"os"
"os/exec"
"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"
"gopkg.in/yaml.v3"
)
var (
errLogger = log.New(os.Stderr, "", 0)
ErrFailedInstallation = errors.New("failed to install some packages. Please, checkout STDERR for more information")
ErrFailedSaving = errors.New("failed to save some packages. Please, checkout STDERR for more information")
ErrBrewNotAvailable = errors.New("homebrew is not available on your system")
@ -23,7 +23,7 @@ var (
)
func LoadPkgs(c configuration.PkgManagers) error {
color.Blue("# Load folders into saved location")
color.Blue("\n# Installing packages")
for _, pkg := range c.InstallationOrder {
switch pkg {
@ -44,7 +44,7 @@ func LoadPkgs(c configuration.PkgManagers) error {
}
func SavePkgs(cfg configuration.Configuration) error {
color.Blue("# Save user installed packages")
color.Blue("# Saving user installed packages")
for _, pkg := range cfg.PackageManagers.InstallationOrder {
switch pkg {
@ -66,7 +66,7 @@ func SaveBrewPkgs(cfg configuration.Configuration) error {
return err
}
color.Blue("## Saving Homebrew packages")
color.Blue("\n## Saving Homebrew packages")
o, err := exec.Command("brew", "leaves", "--installed-on-request").Output()
if err != nil {
@ -101,15 +101,29 @@ func installBrewPkgs(pkgs []string) error {
cmd := exec.Command("brew", "install")
cmd.Args = append(cmd.Args, pkgs...)
color.Blue("## Installing Homebrew packages")
color.Blue("\n## Installing Homebrew packages")
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()
}
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
spinner.Stop()
PrintError("brew command failed: %v", err)
return err
}
color.Green("Packages intalled succesfully !")
if v {
// todo: find a way to clear spinner when done
spinner.Stop()
}
color.Green("\nPackages intalled succesfully !")
return nil
}
@ -127,7 +141,7 @@ func installAptPkgs(pkgs []string) error {
cmd := exec.Command("sudo", "apt-get", "install")
cmd.Args = append(cmd.Args, pkgs...)
color.Blue("## Installing aptitude packages")
color.Blue("\n## Installing aptitude packages")
if err := cmd.Run(); err != nil {
PrintError("aptitude command failed: %v", err)