diff --git a/cmd/emacs/install.go b/cmd/emacs/install.go new file mode 100644 index 0000000..f77fe89 --- /dev/null +++ b/cmd/emacs/install.go @@ -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() + }, +} diff --git a/cmd/emacs/uninstall.go b/cmd/emacs/uninstall.go new file mode 100644 index 0000000..1d7b578 --- /dev/null +++ b/cmd/emacs/uninstall.go @@ -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() + }, +} diff --git a/cmd/emacs/utils.go b/cmd/emacs/utils.go new file mode 100644 index 0000000..d9a4f30 --- /dev/null +++ b/cmd/emacs/utils.go @@ -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")) +} diff --git a/cmd/homebrew/install.go b/cmd/homebrew/install.go new file mode 100644 index 0000000..70b20f6 --- /dev/null +++ b/cmd/homebrew/install.go @@ -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() + }, +} diff --git a/cmd/install.go b/cmd/install.go index 40092d5..ff805aa 100644 --- a/cmd/install.go +++ b/cmd/install.go @@ -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) } diff --git a/cmd/root.go b/cmd/root.go index fefa537..4d8b334 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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" @@ -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() { diff --git a/cmd/uninstall.go b/cmd/uninstall.go index d0c67d7..25cf194 100644 --- a/cmd/uninstall.go +++ b/cmd/uninstall.go @@ -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) } diff --git a/cmd/update.go b/cmd/update.go deleted file mode 100644 index 083ec18..0000000 --- a/cmd/update.go +++ /dev/null @@ -1,40 +0,0 @@ -package cmd - -import ( - updaters "github.com/pablobfonseca/dotfiles/src/updaters" - "github.com/spf13/cobra" - "github.com/vbauerster/mpb/v7" -) - -var updateCmd = &cobra.Command{ - Use: "update", - Short: "Update the dotfiles", - Long: `Update the dotfiles. You can update all the dotfiles or just some of them. - Example: dotfiles update --all - dotfiles update --nvim - dotfiles update --brew - `, - Run: func(cmd *cobra.Command, args []string) { - p := mpb.New() - - nvim, _ := cmd.Flags().GetBool("nvim") - brew, _ := cmd.Flags().GetBool("brew") - - if nvim { - updaters.UpdateNvim(p, verbose) - } - - if brew { - updaters.UpdateBrew(p, verbose) - } - - p.Wait() - }, -} - -func init() { - rootCmd.AddCommand(updateCmd) - - updateCmd.Flags().BoolP("nvim", "n", false, "Update nvim files") - updateCmd.Flags().BoolP("brew", "b", false, "Update homebrew") -} diff --git a/cmd/vim/install.go b/cmd/vim/install.go index 378b7ad..eeb2a85 100644 --- a/cmd/vim/install.go +++ b/cmd/vim/install.go @@ -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() diff --git a/cmd/vim/uninstall.go b/cmd/vim/uninstall.go new file mode 100644 index 0000000..5dea4e3 --- /dev/null +++ b/cmd/vim/uninstall.go @@ -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() + }, +} diff --git a/cmd/vim/vim.go b/cmd/vim/vim.go deleted file mode 100644 index 982a3c3..0000000 --- a/cmd/vim/vim.go +++ /dev/null @@ -1,19 +0,0 @@ -package vim - -import ( - "fmt" - - "github.com/spf13/cobra" -) - -var VimRootCmd = &cobra.Command{ - Use: "vim", - Short: "vim is a text editor", - Run: func(cmd *cobra.Command, args []string) { - fmt.Println("vim called") - }, -} - -func init() { - VimRootCmd.AddCommand(vimInstallCmd) -} diff --git a/cmd/zsh/install.go b/cmd/zsh/install.go new file mode 100644 index 0000000..90da9c1 --- /dev/null +++ b/cmd/zsh/install.go @@ -0,0 +1,42 @@ +package zsh + +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 InstallZshCmd = &cobra.Command{ + Use: "zsh", + Short: "Install zsh files", + Run: func(cmd *cobra.Command, args []string) { + p := mpb.New() + + bar := utils.NewBar("Symlinking zsh files", 1, p) + + for _, file := range []string{"zshrc", "zshenv"} { + src := path.Join(config.DotfilesConfigDir(), "zsh", file) + dest := path.Join(os.Getenv("HOME"), "."+file) + + utils.InfoMessage("Syncing " + src + " to " + dest) + if _, err := os.Stat(dest); err == nil { + utils.SkipMessage("File already exists: " + dest) + continue + } + + if err := os.Symlink(src, dest); err != nil { + utils.ErrorMessage("Error creating symlink", err) + return + } + } + bar.Increment() + }, +} + +func emacsExists() bool { + return utils.DirExists(path.Join("/Applications", "Emacs.app")) +} diff --git a/src/installers/emacs.go b/src/installers/emacs.go deleted file mode 100644 index 2b51bfc..0000000 --- a/src/installers/emacs.go +++ /dev/null @@ -1,62 +0,0 @@ -package dotfiles - -import ( - "os" - "path" - - "github.com/pablobfonseca/dotfiles/src/config" - "github.com/pablobfonseca/dotfiles/src/utils" - "github.com/vbauerster/mpb/v7" -) - -func InstallEmacs(p *mpb.Progress, verbose bool) { - 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() -} - -func UninstallEmacs(uninstallApp bool, p *mpb.Progress, verbose bool) { - 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() -} - -func emacsInstalled() bool { - return utils.DirExists(path.Join("/Applications", "Emacs.app")) -} diff --git a/src/installers/homebrew.go b/src/installers/homebrew.go deleted file mode 100644 index cf31474..0000000 --- a/src/installers/homebrew.go +++ /dev/null @@ -1,18 +0,0 @@ -package dotfiles - -import ( - "fmt" - - "github.com/pablobfonseca/dotfiles/src/utils" - "github.com/vbauerster/mpb/v7" -) - -func InstallHomebrew(p *mpb.Progress, verbose bool) { - 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() -} diff --git a/src/installers/nvim.go b/src/installers/nvim.go deleted file mode 100644 index 53d4300..0000000 --- a/src/installers/nvim.go +++ /dev/null @@ -1,29 +0,0 @@ -package dotfiles - -import ( - "github.com/pablobfonseca/dotfiles/src/config" - "github.com/pablobfonseca/dotfiles/src/utils" - "github.com/vbauerster/mpb/v7" -) - -func UninstallNvim(uninstallApp bool, p *mpb.Progress, verbose bool) { - 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() -} diff --git a/src/installers/repo.go b/src/installers/repo.go deleted file mode 100644 index 7abdab4..0000000 --- a/src/installers/repo.go +++ /dev/null @@ -1,25 +0,0 @@ -package dotfiles - -import ( - "github.com/pablobfonseca/dotfiles/src/config" - "github.com/pablobfonseca/dotfiles/src/utils" - "github.com/vbauerster/mpb/v7" -) - -func CloneRepo(p *mpb.Progress, verbose bool) { - 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() -} - -func DeleteRepo(p *mpb.Progress, verbose bool) { - bar := utils.NewBar("Deleting dotfiles repo", 1, p) - - if err := utils.ExecuteCommand(verbose, "rm", "-rf", config.DotfilesConfigDir()); err != nil { - utils.ErrorMessage("Error deleting the repository", err) - } - bar.Increment() -} diff --git a/src/installers/zsh.go b/src/installers/zsh.go deleted file mode 100644 index abe6b5c..0000000 --- a/src/installers/zsh.go +++ /dev/null @@ -1,33 +0,0 @@ -package dotfiles - -import ( - "os" - "path" - - "github.com/pablobfonseca/dotfiles/src/config" - "github.com/pablobfonseca/dotfiles/src/utils" - "github.com/vbauerster/mpb/v7" -) - -var zshConfigPath = path.Join(os.Getenv("HOME"), "zsh") - -func InstallZsh(p *mpb.Progress) { - bar := utils.NewBar("Symlinking zsh files", 1, p) - - for _, file := range []string{"zshrc", "zshenv"} { - src := path.Join(config.DotfilesConfigDir(), "zsh", file) - dest := path.Join(os.Getenv("HOME"), "."+file) - - utils.InfoMessage("Syncing " + src + " to " + dest) - if _, err := os.Stat(dest); err == nil { - utils.SkipMessage("File already exists: " + dest) - continue - } - - if err := os.Symlink(src, dest); err != nil { - utils.ErrorMessage("Error creating symlink", err) - return - } - } - bar.Increment() -} diff --git a/src/updaters/brew.go b/src/updaters/brew.go deleted file mode 100644 index 8d2a106..0000000 --- a/src/updaters/brew.go +++ /dev/null @@ -1,21 +0,0 @@ -package dotfiles - -import ( - "github.com/pablobfonseca/dotfiles/src/utils" - "github.com/vbauerster/mpb/v7" -) - -func UpdateBrew(p *mpb.Progress, verbose bool) { - bar := utils.NewBar("Updating and upgrading brew packages", 2, p) - - if err := utils.ExecuteCommand(verbose, "brew", "update"); err != nil { - utils.ErrorMessage("Error updating brew packages", err) - } - bar.Increment() - - if err := utils.ExecuteCommand(verbose, "brew", "upgrade"); err != nil { - utils.ErrorMessage("Error upgrading brew packages", err) - } - - bar.Increment() -} diff --git a/src/updaters/nvim.go b/src/updaters/nvim.go deleted file mode 100644 index 9bd7f43..0000000 --- a/src/updaters/nvim.go +++ /dev/null @@ -1,15 +0,0 @@ -package dotfiles - -import ( - "github.com/pablobfonseca/dotfiles/src/utils" - "github.com/vbauerster/mpb/v7" -) - -func UpdateNvim(p *mpb.Progress, verbose bool) { - updateBar := utils.NewBar("Updating nvim packages", 1, p) - - if err := utils.ExecuteCommand(verbose, "nvim", "+NvChadUpdate", "+qall"); err != nil { - utils.ErrorMessage("Error updating nvim", err) - } - updateBar.Increment() -}