Skip to content

Commit

Permalink
refactor: improve command structure
Browse files Browse the repository at this point in the history
  • Loading branch information
pablobfonseca committed Dec 31, 2023
1 parent d0a41da commit 5c36c4f
Show file tree
Hide file tree
Showing 19 changed files with 236 additions and 322 deletions.
44 changes: 44 additions & 0 deletions cmd/emacs/install.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package emacs

import (
"os"
"path"

"github.com/pablobfonseca/dotfiles/src/config"
"github.com/pablobfonseca/dotfiles/src/utils"
"github.com/spf13/cobra"
"github.com/vbauerster/mpb/v7"
)

var InstallEmacsCmd = &cobra.Command{
Use: "emacs",
Short: "Install emacs files",
Run: func(cmd *cobra.Command, args []string) {
p := mpb.New()

verbose, _ := cmd.Flags().GetBool("verbose")

if emacsInstalled() {
utils.SkipMessage("Emacs is already installed")
} else {
installEmacsBar := utils.NewBar("Installing emacs", 1, p)

if err := utils.ExecuteCommand(verbose, "brew", "install", "--cask", "emacs"); err != nil {
utils.ErrorMessage("Error installing emacs symlink", err)
}
installEmacsBar.Increment()

}

utils.CloneRepoIfNotExists(verbose)

symlinkBar := utils.NewBar("Symlinking files", 1, p)

src := path.Join(config.DotfilesConfigDir(), "emacs.d")
dest := path.Join(config.EmacsConfigDir())
if err := os.Symlink(src, dest); err != nil {
utils.ErrorMessage("Error creating symlink", err)
}
symlinkBar.Increment()
},
}
39 changes: 39 additions & 0 deletions cmd/emacs/uninstall.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package emacs

import (
"github.com/pablobfonseca/dotfiles/src/config"
"github.com/pablobfonseca/dotfiles/src/utils"
"github.com/spf13/cobra"
"github.com/vbauerster/mpb/v7"
)

var UninstallEmacsCmd = &cobra.Command{
Use: "emacs",
Short: "Uninstall emacs files",
Run: func(cmd *cobra.Command, args []string) {
p := mpb.New()

verbose, _ := cmd.Flags().GetBool("verbose")
uninstallApp, _ := cmd.Flags().GetBool("uninstall-app")

if !emacsInstalled() {
utils.SkipMessage("Emacs is not installed")
return
}

uninstallBar := utils.NewBar("Uninstalling emacs", 1, p)

if uninstallApp {
if err := utils.ExecuteCommand(verbose, "brew", "uninstall", "emacs"); err != nil {
utils.ErrorMessage("Error uninstalling emacs", err)
}
uninstallBar.Increment()
}

removeFilesBar := utils.NewBar("Removing emacs files", 1, p)
if err := utils.ExecuteCommand(verbose, "rm", "-rf", config.EmacsConfigDir()); err != nil {
utils.ErrorMessage("Error removing emacs files", err)
}
removeFilesBar.Increment()
},
}
11 changes: 11 additions & 0 deletions cmd/emacs/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package emacs

import (
"path"

"github.com/pablobfonseca/dotfiles/src/utils"
)

func emacsInstalled() bool {
return utils.DirExists(path.Join("/Applications", "Emacs.app"))
}
25 changes: 25 additions & 0 deletions cmd/homebrew/install.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package homebrew

import (
"fmt"

"github.com/pablobfonseca/dotfiles/src/utils"
"github.com/spf13/cobra"
"github.com/vbauerster/mpb/v7"
)

var InstallHomebrewCmd = &cobra.Command{
Use: "homebrew",
Short: "Install homebrew",
Run: func(cmd *cobra.Command, args []string) {
p := mpb.New()
verbose, _ := cmd.Flags().GetBool("verbose")
bar := utils.NewBar("Installing homebrew", 1, p)

if err := utils.ExecuteCommand(verbose, "/bin/bash", "-c", "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"); err != nil {
fmt.Println("Error installing homebrew:", err)
return
}
bar.Increment()
},
}
48 changes: 21 additions & 27 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,43 @@ import (
"github.com/spf13/cobra"
"github.com/vbauerster/mpb/v7"

dotfiles "github.com/pablobfonseca/dotfiles/src/installers"
"github.com/pablobfonseca/dotfiles/cmd/emacs"
"github.com/pablobfonseca/dotfiles/cmd/homebrew"
"github.com/pablobfonseca/dotfiles/cmd/vim"
"github.com/pablobfonseca/dotfiles/cmd/zsh"
"github.com/pablobfonseca/dotfiles/src/config"
"github.com/pablobfonseca/dotfiles/src/utils"
)

var installCmd = &cobra.Command{
Use: "install",
Short: "Install the dotfiles",
Example: "dotfiles install",
Long: `Install the dotfiles. You can install all the dotfiles or just some of them.
Example: dotfiles install --all
dotfiles install --nvim
`,
Example: "dotfiles install vim",
Long: "Install the dotfiles. You can install all the dotfiles or just some of them.",
Run: func(cmd *cobra.Command, args []string) {
p := mpb.New()

all, _ := cmd.Flags().GetBool("all")
emacs, _ := cmd.Flags().GetBool("emacs")
zsh, _ := cmd.Flags().GetBool("zsh")

if all {
installAll(p)
}
if emacs {
dotfiles.InstallEmacs(p, verbose)
if utils.DirExists(config.DotfilesConfigDir()) {
utils.SkipMessage("Dotfiles repo already exists")
return
}
if zsh {
dotfiles.InstallZsh(p)

bar := utils.NewBar("Cloning dotfiles repo", 1, p)

if err := utils.ExecuteCommand(verbose, "git", "clone", config.RepositoryUrl(), config.DotfilesConfigDir()); err != nil {
utils.ErrorMessage("Error cloning the repository", err)
}
bar.Increment()

p.Wait()
},
}

func installAll(p *mpb.Progress) {
dotfiles.CloneRepo(p, verbose)
dotfiles.InstallHomebrew(p, verbose)
dotfiles.InstallZsh(p)
dotfiles.InstallEmacs(p, verbose)
}

func init() {
rootCmd.AddCommand(installCmd)

installCmd.Flags().BoolP("all", "a", false, "Install all the dotfiles")
installCmd.Flags().BoolP("emacs", "e", false, "Install emacs files")
installCmd.Flags().BoolP("zsh", "z", false, "Install zsh files")
installCmd.AddCommand(vim.InstallVimCmd)
installCmd.AddCommand(emacs.InstallEmacsCmd)
installCmd.AddCommand(zsh.InstallZshCmd)
installCmd.AddCommand(homebrew.InstallHomebrewCmd)
}
4 changes: 0 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"path"

tea "github.com/charmbracelet/bubbletea"
"github.com/pablobfonseca/dotfiles/cmd/vim"
"github.com/pablobfonseca/dotfiles/src/utils"
"github.com/pablobfonseca/dotfiles/src/utils/prompts"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -39,9 +38,6 @@ func init() {
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.dotfiles/config.toml)")
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose output")
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")

// commands
rootCmd.AddCommand(vim.VimRootCmd)
}

func initConfig() {
Expand Down
38 changes: 12 additions & 26 deletions cmd/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,37 @@ import (
"github.com/spf13/cobra"
"github.com/vbauerster/mpb/v7"

dotfiles "github.com/pablobfonseca/dotfiles/src/installers"
"github.com/pablobfonseca/dotfiles/cmd/emacs"
"github.com/pablobfonseca/dotfiles/cmd/vim"
"github.com/pablobfonseca/dotfiles/src/config"
"github.com/pablobfonseca/dotfiles/src/utils"
)

var uninstallCmd = &cobra.Command{
Use: "uninstall",
Short: "Uninstall the dotfiles",
Long: `Uninstall the dotfiles. You can uninstall all the dotfiles or just some of them.
Example: dotfiles uninstall --nvim
dotfiles uninstall --emacs
`,
Long: "Uninstall the dotfiles. You can uninstall all the dotfiles or just some of them.",
Run: func(cmd *cobra.Command, args []string) {
p := mpb.New()

all, _ := cmd.Flags().GetBool("all")
nvim, _ := cmd.Flags().GetBool("nvim")
emacs, _ := cmd.Flags().GetBool("emacs")
bar := utils.NewBar("Deleting dotfiles repo", 1, p)

if all {
uninstallAll(p)
}
if nvim {
dotfiles.UninstallNvim(uninstallApp, p, verbose)
}
if emacs {
dotfiles.UninstallEmacs(uninstallApp, p, verbose)
if err := utils.ExecuteCommand(verbose, "rm", "-rf", config.DotfilesConfigDir()); err != nil {
utils.ErrorMessage("Error deleting the repository", err)
}
bar.Increment()

p.Wait()
},
}

func uninstallAll(p *mpb.Progress) {
dotfiles.DeleteRepo(p, verbose)
dotfiles.UninstallNvim(uninstallApp, p, verbose)
dotfiles.UninstallEmacs(uninstallApp, p, verbose)
}

var uninstallApp bool

func init() {
rootCmd.AddCommand(uninstallCmd)

uninstallCmd.Flags().BoolVarP(&uninstallApp, "prune", "p", false, "Also uninstall the app")
uninstallCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Verbose output")

uninstallCmd.Flags().BoolP("all", "a", false, "Uninstall all dotfiles")
uninstallCmd.Flags().BoolP("nvim", "n", false, "Uninstall nvim files")
uninstallCmd.Flags().BoolP("emacs", "e", false, "Uninstall emacs files")
uninstallCmd.AddCommand(vim.UninstallVimCmd)
uninstallCmd.AddCommand(emacs.UninstallEmacsCmd)
}
40 changes: 0 additions & 40 deletions cmd/update.go

This file was deleted.

6 changes: 3 additions & 3 deletions cmd/vim/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (

const NVCHAD_REPO = "/~https://github.com/NvChad/NvChad"

var vimInstallCmd = &cobra.Command{
Use: "install",
Short: "Install the vim files",
var InstallVimCmd = &cobra.Command{
Use: "vim",
Short: "Install vim files",
Run: func(cmd *cobra.Command, args []string) {
p := mpb.New()

Expand Down
39 changes: 39 additions & 0 deletions cmd/vim/uninstall.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package vim

import (
"github.com/pablobfonseca/dotfiles/src/config"
"github.com/pablobfonseca/dotfiles/src/utils"
"github.com/spf13/cobra"
"github.com/vbauerster/mpb/v7"
)

var UninstallVimCmd = &cobra.Command{
Use: "vim",
Short: "Uninstall vim files",
Run: func(cmd *cobra.Command, args []string) {
p := mpb.New()

verbose, _ := cmd.Flags().GetBool("verbose")
uninstallApp, _ := cmd.Flags().GetBool("uninstall-app")

if !utils.CommandExists("nvim") {
utils.SkipMessage("nvim is not installed")
return
}

if uninstallApp {
uninstallBar := utils.NewBar("Uninstalling nvim", 1, p)

if err := utils.ExecuteCommand(verbose, "brew", "uninstall", "neovim"); err != nil {
utils.ErrorMessage("Error uninstalling nvim", err)
}
uninstallBar.Increment()
}

removeFilesBar := utils.NewBar("Removing nvim files", 1, p)
if err := utils.ExecuteCommand(verbose, "rm", "-rf", config.NvimConfigDir()); err != nil {
utils.ErrorMessage("Error removing nvim files", err)
}
removeFilesBar.Increment()
},
}
19 changes: 0 additions & 19 deletions cmd/vim/vim.go

This file was deleted.

Loading

0 comments on commit 5c36c4f

Please sign in to comment.