diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..fd70577 --- /dev/null +++ b/Makefile @@ -0,0 +1,29 @@ +APP_NAME := dotfiles-cli + +GOBASE := $(shell pwd) +GOBIN := $(GOBASE)/bin +GOFILES := $(wildcard *.go) + +GOCMD := go +GOBUILD := $(GOCMD) build +GOCLEAN := $(GOCMD) clean +GOINSTALL := $(GOCMD) install + +BINARY_NAME := $(GOBIN)/$(APP_NAME) + +.PHONY: all build clean install + +all: build + +build: + $(GOBUILD) -o $(BINARY_NAME) -v + +clean: + $(GOCLEAN) + rm -f $(BINARY_NAME) + +install: + $(GOINSTALL) + +run: build + $(BINARY_NAME) diff --git a/dotfiles-cli b/bin/dotfiles-cli similarity index 55% rename from dotfiles-cli rename to bin/dotfiles-cli index d3134bd..52f172f 100755 Binary files a/dotfiles-cli and b/bin/dotfiles-cli differ diff --git a/cmd/uninstall.go b/cmd/uninstall.go index 2da6e3a..cb6f1d0 100644 --- a/cmd/uninstall.go +++ b/cmd/uninstall.go @@ -16,45 +16,39 @@ var uninstallCmd = &cobra.Command{ Long: `Uninstall the dotfiles. You can uninstall all the dotfiles or just some of them. Example: dotfiles uninstall --all dotfiles uninstall --nvim + dotfiles uninstall --emacs `, Run: func(cmd *cobra.Command, args []string) { p := mpb.New() - // all, _ := cmd.Flags().GetBool("all") + all, _ := cmd.Flags().GetBool("all") nvim, _ := cmd.Flags().GetBool("nvim") - // emacs, _ := cmd.Flags().GetBool("emacs") - // zsh, _ := cmd.Flags().GetBool("zsh") + emacs, _ := cmd.Flags().GetBool("emacs") - // if all { - // installAll(p) - // } + if all { + uninstallAll(p) + } if nvim { dotfiles.UninstallNvim(p) } - // if emacs { - // dotfiles.InstallEmacs(p) - // } - // if zsh { - // dotfiles.InstallZsh(p) - // } + if emacs { + dotfiles.UninstallEmacs(p) + } p.Wait() }, } -// func installAll(p *mpb.Progress) { -// dotfiles.CloneRepo(p) -// dotfiles.InstallHomebrew(p) -// dotfiles.InstallNvim(p) -// dotfiles.InstallZsh(p) -// dotfiles.InstallEmacs(p) -// } +func uninstallAll(p *mpb.Progress) { + dotfiles.DeleteRepo(p) + dotfiles.UninstallNvim(p) + dotfiles.UninstallEmacs(p) +} func init() { rootCmd.AddCommand(uninstallCmd) - // uninstallCmd.Flags().BoolP("all", "a", false, "Install all the dotfiles") + uninstallCmd.Flags().BoolP("all", "a", false, "Uninstall all the dotfiles") uninstallCmd.Flags().BoolP("nvim", "n", false, "Uninstall nvim files") - // uninstallCmd.Flags().BoolP("emacs", "e", false, "Install emacs files") - // uninstallCmd.Flags().BoolP("zsh", "z", false, "Install zsh files") + uninstallCmd.Flags().BoolP("emacs", "e", false, "Uninstall emacs files") } diff --git a/go.mod b/go.mod index 7df56a1..4bd8c55 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( require ( github.com/VividCortex/ewma v1.2.0 // indirect github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d // indirect + github.com/enescakir/emoji v1.0.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/mattn/go-runewidth v0.0.13 // indirect github.com/rivo/uniseg v0.2.0 // indirect diff --git a/go.sum b/go.sum index 093c8ff..18e1133 100644 --- a/go.sum +++ b/go.sum @@ -3,6 +3,8 @@ github.com/VividCortex/ewma v1.2.0/go.mod h1:nz4BbCtbLyFDeC9SUHbtcT5644juEuWfUAU github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d h1:licZJFw2RwpHMqeKTCYkitsPqHNxTmd4SNR5r94FGM8= github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d/go.mod h1:asat636LX7Bqt5lYEZ27JNDcqxfjdBQuJ/MM4CN/Lzo= github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/enescakir/emoji v1.0.0 h1:W+HsNql8swfCQFtioDGDHCHri8nudlK1n5p2rHCJoog= +github.com/enescakir/emoji v1.0.0/go.mod h1:Bt1EKuLnKDTYpLALApstIkAjdDrS/8IAgTkKp+WKFD0= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU= diff --git a/src/installers/emacs.go b/src/installers/emacs.go index f50b765..b2a2a9d 100644 --- a/src/installers/emacs.go +++ b/src/installers/emacs.go @@ -1,45 +1,60 @@ package dotfiles import ( - "fmt" "os" "path" - utils "github.com/pablobfonseca/dotfiles-cli/src" + "github.com/pablobfonseca/dotfiles-cli/src/utils" "github.com/vbauerster/mpb/v7" - "github.com/vbauerster/mpb/v7/decor" ) +var emacsConfigPath = path.Join(os.Getenv("HOME"), ".emacs.d") + func InstallEmacs(p *mpb.Progress) { - installEmacs := p.AddBar(100, - mpb.PrependDecorators( - decor.Name("Installing emacs", decor.WC{W: len("Installing emacs") + 1, C: decor.DidentRight}), - ), - mpb.AppendDecorators( - decor.OnComplete(decor.EwmaETA(decor.ET_STYLE_GO, 60), "done"), - ), - ) - - if err := utils.ExecuteCommand("brew", "--cask", "install", "emacs"); err != nil { - fmt.Println("Error installing emacs:", err) + if emacsInstalled() { + utils.SkipMessage("Emacs is already installed") + } else { + installEmacsBar := utils.NewBar("Installing emacs", 1, p) + + if err := utils.ExecuteCommand("brew", "install", "--cask", "emacs"); err != nil { + utils.ErrorMessage("Error installing emacs symlink", err) + } + installEmacsBar.Increment() + + } + + if utils.DirExists(emacsConfigPath) { + utils.SkipMessage("Emacs folder already exists") return } - installEmacs.Increment() - - bar := p.AddBar(100, - mpb.PrependDecorators( - decor.Name("Symlinking emacs folder", decor.WC{W: len("Symlinking emacs folder") + 1, C: decor.DidentRight}), - ), - mpb.AppendDecorators( - decor.OnComplete(decor.EwmaETA(decor.ET_STYLE_GO, 60), "done"), - ), - ) - - src := path.Join(os.Getenv("HOME"), ".dotfiles", "emacs.d") - dest := path.Join(os.Getenv("HOME"), ".emacs.d") - if err := os.Symlink(src, dest); err != nil { - fmt.Println("Error creating symlink:", err) + + cloningBar := utils.NewBar("Cloning emacs files", 1, p) + utils.InfoMessage("Emacs directory does not exists, cloning...") + if err := utils.ExecuteCommand("git", "clone", "/~https://github.com/pablobfonseca/emacs.d", emacsConfigPath); err != nil { + utils.ErrorMessage("Error cloning the repository", err) + } + cloningBar.Increment() +} + +func UninstallEmacs(p *mpb.Progress) { + if !emacsInstalled() { + utils.SkipMessage("Emacs is not installed") return } - bar.Increment() + + uninstallBar := utils.NewBar("Uninstalling emacs", 2, p) + + if err := utils.ExecuteCommand("brew", "uninstall", "emacs"); err != nil { + utils.ErrorMessage("Error uninstalling emacs", err) + } + uninstallBar.Increment() + + if err := utils.ExecuteCommand("rm", "-rf", emacsConfigPath); err != nil { + utils.ErrorMessage("Error removing emacs files", err) + } + uninstallBar.Increment() +} + +func emacsInstalled() bool { + return utils.DirExists(path.Join("/Applications", "Emacs.app")) } diff --git a/src/installers/homebrew.go b/src/installers/homebrew.go index 8007a97..a1231dd 100644 --- a/src/installers/homebrew.go +++ b/src/installers/homebrew.go @@ -3,20 +3,12 @@ package dotfiles import ( "fmt" - utils "github.com/pablobfonseca/dotfiles-cli/src" + "github.com/pablobfonseca/dotfiles-cli/src/utils" "github.com/vbauerster/mpb/v7" - "github.com/vbauerster/mpb/v7/decor" ) func InstallHomebrew(p *mpb.Progress) { - bar := p.AddBar(100, - mpb.PrependDecorators( - decor.Name("Installing homebrew", decor.WC{W: len("Installing homebre") + 1, C: decor.DidentRight}), - ), - mpb.AppendDecorators( - decor.OnComplete(decor.EwmaETA(decor.ET_STYLE_GO, 60), "done"), - ), - ) + bar := utils.NewBar("Installing homebrew", 1, p) if err := utils.ExecuteCommand("/bin/bash", "-c", "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"); err != nil { fmt.Println("Error installing homebrew:", err) diff --git a/src/installers/nvim.go b/src/installers/nvim.go index c286077..f450cb2 100644 --- a/src/installers/nvim.go +++ b/src/installers/nvim.go @@ -1,68 +1,75 @@ package dotfiles import ( - "fmt" - "log" "os" "path" - utils "github.com/pablobfonseca/dotfiles-cli/src" + "github.com/pablobfonseca/dotfiles-cli/src/utils" "github.com/vbauerster/mpb/v7" ) +var dotfilesPath = path.Join(os.Getenv("HOME"), ".dotfiles") var nvimConfigPath = path.Join(os.Getenv("HOME"), ".config", "nvim") var nvChadRepo = "/~https://github.com/NvChad/NvChad" func InstallNvim(p *mpb.Progress) { if nvimInstalled() { - fmt.Println("nvim already installed, skipping...") + utils.SkipMessage("nvim already installed") return - } else { - installNvimBar := NewBar("Installing nvim", 1, p) - if err := utils.ExecuteCommand("brew", "install", "neovim"); err != nil { - log.Fatal("Error installing nvim:", err) + } + + if !utils.DirExists(dotfilesPath) { + cloningBar := utils.NewBar("Cloning dotfiles", 1, p) + utils.InfoMessage("Dotfiles directory does not exists, cloning...") + if err := utils.ExecuteCommand("git", "clone", utils.DotfilesRepo, dotfilesPath); err != nil { + utils.ErrorMessage("Error cloning the repository", err) } - installNvimBar.Increment() + cloningBar.Increment() + } + + installNvimBar := utils.NewBar("Installing nvim", 1, p) + if err := utils.ExecuteCommand("brew", "install", "neovim"); err != nil { + utils.ErrorMessage("Error installing nvim", err) } + installNvimBar.Increment() if utils.DirExists(nvimConfigPath) { - fmt.Println("nvim files already exists, skipping...") + utils.SkipMessage("nvim files already exists") return - } else { - installNvChadBar := NewBar("Installing NvChad", 1, p) + } + installNvChadBar := utils.NewBar("Installing NvChad", 1, p) - if err := utils.ExecuteCommand("git", "clone", "--depth", "1", nvChadRepo, nvimConfigPath); err != nil { - log.Fatal("Error cloning the repository:", err) - } - installNvChadBar.Increment() + if err := utils.ExecuteCommand("git", "clone", "--depth", "1", nvChadRepo, nvimConfigPath); err != nil { + utils.ErrorMessage("Error cloning the repository", err) + } + installNvChadBar.Increment() - symlinkBar := NewBar("Symlinking files", 1, p) + symlinkBar := utils.NewBar("Symlinking files", 1, p) - src := path.Join(os.Getenv("HOME"), ".dotfiles", "nvim", "custom") - dest := path.Join(nvimConfigPath, "lua", "custom") - if err := os.Symlink(src, dest); err != nil { - log.Fatal("Error creating symlink:", err) - } - symlinkBar.Increment() + src := path.Join(dotfilesPath, "nvim", "custom") + dest := path.Join(nvimConfigPath, "lua", "custom") + if err := os.Symlink(src, dest); err != nil { + utils.ErrorMessage("Error creating symlink", err) } + symlinkBar.Increment() } func UninstallNvim(p *mpb.Progress) { if !nvimInstalled() { - fmt.Println("nvim is not installed, skipping...") + utils.SkipMessage("nvim is not installed") return } - uninstallBar := NewBar("Uninstalling nvim", 2, p) + uninstallBar := utils.NewBar("Uninstalling nvim", 2, p) if err := utils.ExecuteCommand("brew", "uninstall", "neovim"); err != nil { - log.Fatal("Error uninstalling nvim:", err) + utils.ErrorMessage("Error uninstalling nvim", err) } uninstallBar.Increment() if err := utils.ExecuteCommand("rm", "-rf", nvimConfigPath); err != nil { - log.Fatal("Error removing nvim files:", err) + utils.ErrorMessage("Error removing nvim files", err) } uninstallBar.Increment() } diff --git a/src/installers/repo.go b/src/installers/repo.go index ea9179f..3e1be17 100644 --- a/src/installers/repo.go +++ b/src/installers/repo.go @@ -1,28 +1,27 @@ package dotfiles import ( - "fmt" "os" "path" - utils "github.com/pablobfonseca/dotfiles-cli/src" + "github.com/pablobfonseca/dotfiles-cli/src/utils" "github.com/vbauerster/mpb/v7" - "github.com/vbauerster/mpb/v7/decor" ) func CloneRepo(p *mpb.Progress) { - bar := p.AddBar(100, - mpb.PrependDecorators( - decor.Name("Cloning repository", decor.WC{W: len("Cloning") + 1, C: decor.DidentRight}), - ), - mpb.AppendDecorators( - decor.OnComplete(decor.EwmaETA(decor.ET_STYLE_GO, 60), "done"), - ), - ) + bar := utils.NewBar("Cloning dotfiles repo", 1, p) - if err := utils.ExecuteCommand("git", "clone", "/~https://github.com/pablobfonseca/dotfiles.git", path.Join(os.Getenv("HOME"), ".dotfiles")); err != nil { - fmt.Println("Error cloning the repository:", err) - return + if err := utils.ExecuteCommand("git", "clone", utils.DotfilesRepo, path.Join(os.Getenv("HOME"), ".dotfiles")); err != nil { + utils.ErrorMessage("Error cloning the repository", err) } - bar.Increment() // 100 % since cloning is done + bar.Increment() +} + +func DeleteRepo(p *mpb.Progress) { + bar := utils.NewBar("Deleting dotfiles repo", 1, p) + + if err := utils.ExecuteCommand("rm", "-rf", path.Join(os.Getenv("HOME"), ".dotfiles")); err != nil { + utils.ErrorMessage("Error deleting the repository", err) + } + bar.Increment() } diff --git a/src/installers/zsh.go b/src/installers/zsh.go index 281454b..f7c0559 100644 --- a/src/installers/zsh.go +++ b/src/installers/zsh.go @@ -1,29 +1,21 @@ package dotfiles import ( - "fmt" "os" "path" + "github.com/pablobfonseca/dotfiles-cli/src/utils" "github.com/vbauerster/mpb/v7" - "github.com/vbauerster/mpb/v7/decor" ) func InstallZsh(p *mpb.Progress) { - bar := p.AddBar(100, - mpb.PrependDecorators( - decor.Name("Symlinking zsh files", decor.WC{W: len("Symlinking zsh files") + 1, C: decor.DidentRight}), - ), - mpb.AppendDecorators( - decor.OnComplete(decor.EwmaETA(decor.ET_STYLE_GO, 60), "done"), - ), - ) + bar := utils.NewBar("Symlinking zsh files", 1, p) for _, file := range []string{"zshrc", "zshenv"} { src := path.Join(os.Getenv("HOME"), ".dotfiles", "zsh", file) dest := path.Join(os.Getenv("HOME"), "."+file) if err := os.Symlink(src, dest); err != nil { - fmt.Println("Error creating symlink:", err) + utils.ErrorMessage("Error creating symlink", err) return } } diff --git a/src/updaters/brew.go b/src/updaters/brew.go index fa8defc..da1d958 100644 --- a/src/updaters/brew.go +++ b/src/updaters/brew.go @@ -1,26 +1,18 @@ package dotfiles import ( - "fmt" + "log" - utils "github.com/pablobfonseca/dotfiles-cli/src" + "github.com/pablobfonseca/dotfiles-cli/src/utils" "github.com/vbauerster/mpb/v7" - "github.com/vbauerster/mpb/v7/decor" ) func UpdateBrew(p *mpb.Progress) { - updateBar := p.AddBar(100, - mpb.PrependDecorators( - decor.Name("Updating homebrew", decor.WC{W: len("Updating homebrew") + 1, C: decor.DidentRight}), - ), - mpb.AppendDecorators( - decor.OnComplete(decor.EwmaETA(decor.ET_STYLE_GO, 60), "done"), - ), - ) + bar := utils.NewBar("Updating brew packages", 1, p) if err := utils.ExecuteCommand("brew", "update"); err != nil { - fmt.Println("Error updating homebrew:", err) + log.Fatal("Error updating homebrew:", err) return } - updateBar.Increment() + bar.Increment() } diff --git a/src/updaters/nvim.go b/src/updaters/nvim.go index 479f73c..d8a149c 100644 --- a/src/updaters/nvim.go +++ b/src/updaters/nvim.go @@ -1,26 +1,17 @@ package dotfiles import ( - "fmt" + "log" - utils "github.com/pablobfonseca/dotfiles-cli/src" + "github.com/pablobfonseca/dotfiles-cli/src/utils" "github.com/vbauerster/mpb/v7" - "github.com/vbauerster/mpb/v7/decor" ) func UpdateNvim(p *mpb.Progress) { - updateBar := p.AddBar(100, - mpb.PrependDecorators( - decor.Name("Updating nvim", decor.WC{W: len("Updating nvim") + 1, C: decor.DidentRight}), - ), - mpb.AppendDecorators( - decor.OnComplete(decor.EwmaETA(decor.ET_STYLE_GO, 60), "done"), - ), - ) + updateBar := utils.NewBar("Updating nvim packages", 1, p) if err := utils.ExecuteCommand("nvim", "+NvChadUpdate", "+qall"); err != nil { - fmt.Println("Error updating nvim:", err) - return + log.Fatal("Error updating nvim:", err) } updateBar.Increment() } diff --git a/src/installers/bar.go b/src/utils/bar.go similarity index 57% rename from src/installers/bar.go rename to src/utils/bar.go index 148adad..e30a5c7 100644 --- a/src/installers/bar.go +++ b/src/utils/bar.go @@ -1,8 +1,9 @@ -package dotfiles +package utils import ( "fmt" + "github.com/enescakir/emoji" "github.com/vbauerster/mpb/v7" "github.com/vbauerster/mpb/v7/decor" ) @@ -11,8 +12,8 @@ func NewBar(name string, total int64, p *mpb.Progress) *mpb.Bar { return p.New(total, mpb.BarStyle().Lbound("╢").Filler("▌").Tip("▌").Padding("░").Rbound("╟"), mpb.PrependDecorators( - decor.Name(fmt.Sprintf("%s: ", name), decor.WC{C: decor.DidentRight | decor.DextraSpace}), - decor.OnComplete(decor.AverageETA(decor.ET_STYLE_GO), "✅"), + decor.Name(fmt.Sprintf("%v %s: ", emoji.PlayButton, name), decor.WC{C: decor.DidentRight | decor.DextraSpace}), + decor.OnComplete(decor.AverageETA(decor.ET_STYLE_GO), emoji.CheckMarkButton.String()), ), mpb.AppendDecorators(decor.Percentage()), ) diff --git a/src/utils/log.go b/src/utils/log.go new file mode 100644 index 0000000..ec89556 --- /dev/null +++ b/src/utils/log.go @@ -0,0 +1,20 @@ +package utils + +import ( + "fmt" + "log" + + "github.com/enescakir/emoji" +) + +func ErrorMessage(message string, err error) { + log.Fatalf("%v %s: %v", emoji.CrossMark, message, err) +} + +func SkipMessage(message string) { + fmt.Printf("%v %s, skipping...\n", emoji.CheckMark, message) +} + +func InfoMessage(message string) { + fmt.Printf("%v %s\n", emoji.Information, message) +} diff --git a/src/utils.go b/src/utils/utils.go similarity index 88% rename from src/utils.go rename to src/utils/utils.go index 9ad44ff..e788c0e 100644 --- a/src/utils.go +++ b/src/utils/utils.go @@ -1,4 +1,4 @@ -package dotfiles +package utils import ( "bytes" @@ -7,6 +7,8 @@ import ( "os/exec" ) +var DotfilesRepo = "/~https://github.com/pablobfonseca/dotfiles.git" + func CommandExists(command string) bool { _, err := exec.LookPath(command) return err == nil