Documentation
¶
Overview ¶
Package dotfiles provides cobra commands for managing user dotfiles via a bare git repository.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var CopyChangesCmd = &cobra.Command{ Use: "copy-changes", Short: "copy modified dotfiles to local git repo", Long: `This command copies modified dotfiles from the worktree to the local git repository for committing.`, Run: func(cmd *cobra.Command, args []string) { log.Start("Copying modified dotfiles") isVerbose := utils.IsVerbose(cmd) repoPath := os.ExpandEnv(viper.GetString("dotfiles.repoPath")) if repoPath == "" { log.Error("dotfiles.repoPath is not set or resolves to an empty string in the configuration file") return } log.Verbose(isVerbose, "dotfiles.repoPath: %s", repoPath) worktreePath := os.ExpandEnv(viper.GetString("dotfiles.worktree")) if worktreePath == "" { log.Error("dotfiles.worktree is not set or resolves to an empty string in the configuration file") return } log.Verbose(isVerbose, "dotfiles.worktree: %s", worktreePath) devPath := os.ExpandEnv(viper.GetString("git.devPath")) if devPath == "" { log.Error("git.devPath is not set or resolves to an empty string in the configuration file") return } log.Verbose(isVerbose, "git.devPath: %s", devPath) engCfgPath := filepath.Join(devPath, "eng-cfg") log.Verbose(isVerbose, "eng-cfg path: %s", engCfgPath) modifiedFiles, err := getModifiedFilesFunc(repoPath, worktreePath) if err != nil { log.Error("Failed to get modified files: %s", err) return } if len(modifiedFiles) == 0 { log.Info("No modified files found") return } log.Info("Found %d modified files", len(modifiedFiles)) for _, file := range modifiedFiles { src := filepath.Join(worktreePath, file) dest := filepath.Join(engCfgPath, file) destDir := filepath.Dir(dest) if err := os.MkdirAll(destDir, 0755); err != nil { log.Error("Failed to create directory %s: %s", destDir, err) continue } if err := copyFile(src, dest, isVerbose); err != nil { log.Error("Failed to copy %s to %s: %s", src, dest, err) continue } log.Info("Copied %s to %s", file, dest) } log.Success("Copied modified dotfiles successfully") // Ask to reset var resetConfirm bool prompt := &survey.Confirm{ Message: "Do you want to reset the local copies in the worktree?", } prompt.Default = true err = survey.AskOne(prompt, &resetConfirm) if err != nil { log.Error("Failed to get user confirmation: %s", err) return } if resetConfirm { log.Start("Resetting local copies") for _, file := range modifiedFiles { if err := resetFile(repoPath, worktreePath, file); err != nil { log.Error("Failed to reset %s: %s", file, err) continue } log.Verbose(isVerbose, "Reset %s", file) } log.Success("Reset local copies successfully") } }, }
CopyChangesCmd defines the cobra command for copying modified dotfiles to the local git repository.
var DotfilesCmd = &cobra.Command{ Use: "dotfiles", Short: "Manage dotfiles", Long: `This command is used to facilitate the management of private hidden dot files.`, Run: func(cmd *cobra.Command, args []string) { showInfo, _ := cmd.Flags().GetBool("info") isVerbose := utils.IsVerbose(cmd) if showInfo { log.Info("Current dotfiles configuration:") repoPath := os.ExpandEnv(viper.GetString("dotfiles.repoPath")) worktreePath := os.ExpandEnv(viper.GetString("dotfiles.worktree")) if repoPath == "" { log.Warn(" Repository Path (dotfiles.repoPath): Not Set") } else { log.Info(" Repository Path (dotfiles.repoPath): %s", repoPath) } if worktreePath == "" { log.Warn(" Worktree Path (dotfiles.worktree): Not Set") } else { log.Info(" Worktree Path (dotfiles.worktree): %s", worktreePath) } return } if len(args) == 0 { log.Verbose(isVerbose, "No subcommand provided, showing help.") err := cmd.Help() cobra.CheckErr(err) } else { log.Verbose(isVerbose, "Subcommand '%s' provided.", args[0]) } }, }
DotfilesCmd serves as the base command for all dotfiles related operations. It doesn't perform any action itself but groups subcommands like sync and fetch.
var FetchCmd = &cobra.Command{ Use: "fetch", Short: "fetch your local bare repository", Long: `This command fetches remote changes to the local bare dot repository.`, Run: func(cmd *cobra.Command, args []string) { log.Start("Fetching dotfiles") repoPath := os.ExpandEnv(viper.GetString("dotfiles.repoPath")) if repoPath == "" { log.Error("dotfiles.repoPath is not set or resolves to an empty string in the configuration file") return } worktreePath := os.ExpandEnv(viper.GetString("dotfiles.worktree")) if worktreePath == "" { log.Error("dotfiles.worktree is not set or resolves to an empty string in the configuration file") return } err := fetchRepo(repoPath, worktreePath) if err != nil { log.Error("Failed to fetch dotfiles: %s", err) return } log.Success("Dotfiles fetched successfully") }, }
FetchCmd defines the cobra command for fetching the dotfiles repository. It only fetches remote changes without merging them.
var InstallCmd = &cobra.Command{ Use: "install", Short: "Install dotfiles from a bare git repository", Long: `Install dotfiles from a bare git repository. This command will: - Check and install prerequisites (Homebrew, Git, Bash, SSH keys) - Clone your dotfiles repository as a bare repository - Backup any conflicting files - Checkout dotfiles to your home directory - Initialize git submodules - Configure git to hide untracked files`, Run: func(cmd *cobra.Command, args []string) { if err := installDotfiles(); err != nil { log.Fatal("Dotfiles installation failed: %v", err) } }, }
var StatusCmd = &cobra.Command{ Use: "status", Short: "check the status of your dotfiles repository", Long: `This command checks the status of your local bare dotfiles repository to see if there are any local changes.`, Run: func(cmd *cobra.Command, args []string) { log.Start("Checking dotfiles status") isVerbose := utils.IsVerbose(cmd) repoPath := viper.GetString("dotfiles.repoPath") repoPath = os.ExpandEnv(repoPath) if repoPath == "" { log.Error("dotfiles.repoPath is not set or resolves to an empty string in the configuration file") return } log.Verbose(isVerbose, "dotfiles.repoPath: %s", repoPath) worktreePath := viper.GetString("dotfiles.worktree") worktreePath = os.ExpandEnv(worktreePath) if worktreePath == "" { log.Error("dotfiles.worktree is not set in the configuration file") return } log.Verbose(isVerbose, "dotfiles.worktree: %s", worktreePath) err := checkStatus(repoPath, worktreePath) if err != nil { log.Error("Failed to check status: %s", err) return } log.Success("Status check complete") }, }
StatusCmd defines the cobra command for checking the status of the dotfiles repository. It shows any local changes, untracked files, or uncommitted modifications.
var SyncCmd = &cobra.Command{ Use: "sync", Short: "sync your local bare repository", Long: `This command fetches and pulls in remote changes to the local bare dot repository.`, Run: func(cmd *cobra.Command, args []string) { log.Start("Syncing dotfiles") isVerbose := utils.IsVerbose(cmd) repoPath := viper.GetString("dotfiles.repoPath") repoPath = os.ExpandEnv(repoPath) if repoPath == "" { log.Error("dotfiles.repoPath is not set or resolves to an empty string in the configuration file") return } log.Verbose(isVerbose, "dotfiles.repoPath: %s", repoPath) worktreePath := viper.GetString("dotfiles.worktree") worktreePath = os.ExpandEnv(worktreePath) if worktreePath == "" { log.Error("dotfiles.worktree is not set in the configuration file") return } log.Verbose(isVerbose, "dotfiles.worktree: %s", worktreePath) log.Info("Fetching dotfiles") err := fetchRepo(repoPath, worktreePath) if err != nil { log.Error("Failed to fetch dotfiles: %s", err) return } log.Info("Pulling dotfiles with rebase") err = pullRebaseRepo(repoPath, worktreePath) if err != nil { log.Error("Failed to pull and rebase dotfiles: %s", err) return } log.Success("Dotfiles synced successfully") }, }
SyncCmd defines the cobra command for syncing the dotfiles repository. It fetches remote changes and then performs a pull with rebase.
Functions ¶
This section is empty.
Types ¶
This section is empty.