Skip to content

Commit

Permalink
feat: add more installers and uninstallers
Browse files Browse the repository at this point in the history
  • Loading branch information
pablobfonseca committed Dec 12, 2023
1 parent a1e5ee0 commit 30cd7bb
Show file tree
Hide file tree
Showing 15 changed files with 182 additions and 145 deletions.
29 changes: 29 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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)
Binary file renamed dotfiles-cli → bin/dotfiles-cli
Binary file not shown.
38 changes: 16 additions & 22 deletions cmd/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
75 changes: 45 additions & 30 deletions src/installers/emacs.go
Original file line number Diff line number Diff line change
@@ -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"))
}
12 changes: 2 additions & 10 deletions src/installers/homebrew.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
61 changes: 34 additions & 27 deletions src/installers/nvim.go
Original file line number Diff line number Diff line change
@@ -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()
}
Expand Down
29 changes: 14 additions & 15 deletions src/installers/repo.go
Original file line number Diff line number Diff line change
@@ -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()
}
Loading

0 comments on commit 30cd7bb

Please sign in to comment.