dotfiles

package
v1.11.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 18, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Overview

Package dotfiles provides cobra commands for managing user dotfiles via a bare git repository.

Index

Constants

This section is empty.

Variables

View Source
var CheckoutCmd = &cobra.Command{
	Use:   "checkout",
	Short: "checkout files in your local bare repository",
	Long:  `This command checks out files from the dotfiles repository, optionally discarding local changes with the force flag.`,
	Run: func(cmd *cobra.Command, args []string) {
		log.Start("Checking out dotfiles")

		isVerbose := utils.IsVerbose(cmd)
		all, _ := cmd.Flags().GetBool("all")
		force, _ := cmd.Flags().GetBool("force")

		repoPath, worktreePath, err := getDotfilesConfig()
		if err != nil || repoPath == "" {
			log.Error("Dotfiles repository path is not set in configuration")
			return
		}
		log.Verbose(isVerbose, "Repository path: %s", repoPath)
		log.Verbose(isVerbose, "Worktree path:   %s", worktreePath)

		operation := "Checking out"
		if force {
			operation += " (force)"
		}
		if all {
			operation += " all files"
		} else {
			operation += " files"
		}
		log.Info(operation)

		err = checkoutRepo(repoPath, worktreePath, force, all)
		if err != nil {
			log.Error("Failed to checkout dotfiles: %s", err)
			return
		}

		log.Success("Dotfiles checked out successfully")
	},
}

CheckoutCmd defines the cobra command for checking out files in the dotfiles repository. It can checkout all files or specific files, optionally with force to discard local changes.

View Source
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, worktreePath, err := getDotfilesConfig()
		if err != nil || repoPath == "" {
			log.Error("Dotfiles repository path is not set in configuration")
			return
		}
		log.Verbose(isVerbose, "Repository path: %s", repoPath)
		log.Verbose(isVerbose, "Worktree path:   %s", worktreePath)

		devPath := os.ExpandEnv(viper.GetString("git.dev_path"))
		if devPath == "" {
			log.Error("Development folder path is not set in configuration")
			return
		}
		log.Verbose(isVerbose, "Development path: %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, 0o755); 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.

View Source
var DotfilesCmd = &cobra.Command{
	Use:     "dotfiles",
	Short:   "Manage dotfiles",
	Long:    `This command is used to facilitate the management of private hidden dot files.`,
	Aliases: []string{"cfg"},
	Run: func(cmd *cobra.Command, args []string) {
		showInfo, _ := cmd.Flags().GetBool("info")
		isVerbose := utils.IsVerbose(cmd)

		if showInfo {
			log.Info("Current dotfiles configuration:")
			repoPath, worktreePath, _ := getDotfilesConfig()

			if repoPath == "" {
				log.Warn("  Repository Path: Not Set")
			} else {
				log.Info("  Repository Path: %s", repoPath)
			}
			if worktreePath == "" {
				log.Warn("  Worktree Path:   Not Set")
			} else {
				log.Info("  Worktree Path:   %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.

View Source
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, worktreePath, err := getDotfilesConfig()
		if err != nil || repoPath == "" {
			log.Error("Dotfiles repository path is not set in configuration")
			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.

View Source
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)
	- Setup SSH keys for GitHub when required by the repository URL
  - 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(utils.IsVerbose(cmd)); err != nil {
			log.Fatal("Dotfiles installation failed: %v", err)
		}
	},
}
View Source
var SecretsBackupCmd = &cobra.Command{
	Use:   "backup",
	Short: "Backup managed dotfiles env values into Bitwarden Secrets Manager",
	RunE: func(cmd *cobra.Command, _args []string) error {
		return utils.BackupDotfilesSecrets(dotfilesSecretsOptions(cmd))
	},
}

SecretsBackupCmd saves managed env keys from the dotfiles worktree into bws.

View Source
var SecretsCmd = &cobra.Command{
	Use:   "secrets",
	Short: "Backup and restore dotfiles env secrets via Bitwarden Secrets Manager",
	Long:  `Backup and restore managed dotfiles env files using a tracked manifest and Bitwarden Secrets Manager.`,
	RunE: func(cmd *cobra.Command, _args []string) error {
		return cmd.Help()
	},
}

SecretsCmd manages manifest-driven dotfiles env secrets with Bitwarden Secrets Manager.

View Source
var SecretsDoctorCmd = &cobra.Command{
	Use:   "doctor",
	Short: "Validate manifest templates and Bitwarden Secrets Manager values",
	RunE: func(cmd *cobra.Command, _args []string) error {
		return utils.DoctorDotfilesSecrets(dotfilesSecretsOptions(cmd))
	},
}

SecretsDoctorCmd validates manifest mappings, templates, and bws secrets.

View Source
var SecretsRestoreCmd = &cobra.Command{
	Use:   "restore",
	Short: "Restore managed dotfiles env files from Bitwarden Secrets Manager",
	RunE: func(cmd *cobra.Command, _args []string) error {
		return utils.RestoreDotfilesSecrets(dotfilesSecretsOptions(cmd))
	},
}

SecretsRestoreCmd restores managed env files from tracked templates and bws values.

View Source
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, worktreePath, err := getDotfilesConfig()
		if err != nil || repoPath == "" {
			log.Error("Dotfiles repository path is not set in configuration")
			return
		}
		log.Verbose(isVerbose, "Repository path: %s", repoPath)
		log.Verbose(isVerbose, "Worktree path:   %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.

View Source
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, worktreePath, err := getDotfilesConfig()
		if err != nil || repoPath == "" {
			log.Error("Dotfiles repository path is not set in configuration")
			return
		}
		log.Verbose(isVerbose, "Repository path: %s", repoPath)
		log.Verbose(isVerbose, "Worktree path:   %s", worktreePath)

		if err = SyncRepo(repoPath, worktreePath, isVerbose); err != nil {
			log.Error("Sync failed: %v", 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

func SyncRepo added in v0.29.0

func SyncRepo(repoPath, worktreePath string, isVerbose bool) error

SyncRepo performs the fetch and pull-rebase operations for a bare repository.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL