dotm

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2019 License: MIT Imports: 17 Imported by: 0

README

dotm - Dotfile Manager

Build Status Godoc

dotm is a dotfile manager. It works by symlinking the dotfiles from multiple profiles to the $HOME directory. It expects the dotfile profile to be under source controll by git. This makes it easy to share dotfiles.

Installation

Using installer

The install.sh script will automatically install the latest binary from the github release artifacts.

$ sh <(curl -s https://raw.githubusercontent.com/relnod/dotm/master/install.sh)

By adding the --user flag the install directory will be $HOME/.local/bin instead of /usr/local/bin.

Using Go

If you have a working go environment, you can simply install it via go get.

$ go get github.com/relnod/dotm/cmd/dotm
Completion
# Bash completions can be generated with the following command
$ dotm --genCompletions > /etc/bash_completion.d/dotm

# A cd helper can be generated with the following command
$ dotm --genChangeDirectory >> ~/.bashrc

$ dcd myprofile

Usage

dotm works by symlinking the files from a dotfile folder to its corresponding place under the $HOME directory of the user.

$ dotm
Usage:
  dotm [flags]
  dotm [command]

Available Commands:
  add         Add a new/existing file to the profile
  config      Sets/Gets values from the configuration file.
  fix         Tries to fix the configuration file
  help        Help about any command
  init        Initialize a new dotfile profile from the given path.
  install     Install dotfiles from a remote git repository
  list        Lists all profiles
  new         Create a new dotfile profile
  uninstall   Uninstall the profile
  update      Updates the symlinks for a given profile.

Flags:
      --genChangeDirectory   generate bash change directory command
      --genCompletions       generate bash completions
  -h, --help                 help for dotm
      --version              version for dotm

Use "dotm [command] --help" for more information about a command.

Examples:

# New (empty) dotfile profile
$ dotm new myprofile

# New profile from an existing dotfile folder
$ dotm init <path-to-existing-dotfile-folder>
$ dotm init --profile=myprofile <path-to-existing-dotfile-folder>

# New profile from a remote git repository
$ dotm install <url-to-remote-repository>
$ dotm install --profile=myprofile <url-to-remote-repository>

# Updating a profile
$ dotm update myprofile
$ dotm update myprofile --fromRemote
$ dotm update myprofile --no-hooks

# Get/Set a configuration value
$ dotm config ignore_prefix "_"
$ dotm config profile.default.path
$ dotm config profile.default.path "/my/path"
Configuration file

The configuration file is located at $HOME/.config/dotm/config.toml. It can hold multiple profiles. Each profile consists of a path to the local dotfile location and an optional path to a remote git repository. It is also possible to specify top level directories, that get included/excluded or add pre/post update hooks.

Example:

# $HOME/.config/dotm/config.toml

# You can define multiple profiles
[profiles.default]

# Upstream git repository
remote = "github.com/relnod/dotm"

# Path to the local dotfile folder
path = "$HOME/.config/dotm/profiles/default"

# Configs to be included
includes = [
    "bash",
    "nvim",
    "tmux"
]

# Configs to be excluded
excludes = [
    "bash",
    "nvim",
    "tmux"
]

# Pre update hooks
pre_update = [
    "echo 'pre update'"
]

# Post update hooks
post_update = [
    "echo 'post update'"
]

# Map of variables used for template processing
[profiles.default.vars]
foo = "bar"
bar = "foo"
Dotfiles folder

A Dotfile folder consists of multiple top level directories to group similar configuration files (e.g. "vim" or "tmux"). The file structure below those top level directories are directly mapped to the $HOME directory.

Example:

tmux/.tmux.conf             -> $HOME/.tmux.conf
bash/.bashrc                -> $HOME/.bashrc
nvim/.config/nvim/init.vim  -> $HOME/.config/nvim/init.vim
Template files

Template files can be used to dynamically add user identifying information inside configuration files. All files with a .tpl file ending are treated as template files. Templates support the syntax from the go text/template package. Variables can be configured per profile. When a template gets processed, a temporary file with the same name plus a .out ending gets generated. This file will be symlinked to the destination file without the .tpl suffix. Make sure to add *.tpl.out to your .gitignore when using templates to prevent adding those to git.

Example:

# $HOME/.config/dotm/profiles/myprofile/git/.gitconfig.tpl

[user]
    name = {{ .GitUser }}
    email = {{ .GitEmail }}
Hooks

Update hooks can be applied via global config, at profile root or per top level directory. For hooks at profile root and top level directory you can create a hooks.toml. Note: This file won't be symlinked.

Example:

# $HOME/.config/dotm/profiles/myprofile/nvim/hooks.toml
pre_update = [
    "nvim +PlugInstall +qall"
]

Breaking Changes

Although dotm is considered somewhat stable, some breaking changes are expected until a 1.0 release. When a breaking change is introduced try to run the fix command. This tries to restore the original behaviour by modifying the configuration file.

# Restore old behaviour by modifying the configuration file
$ dotm fix

Development

There is a Makefile at the repository root to help with development. Start by running make to execute tests and check for lint failures.

Tests are mostly wrtten using the go internal testscript package. The testscripts are located at cmd/dotm/testdata. For more information on how to use testscript see this README located in the go repository.

License

dotm is licensed under the MIT License. See the LICENSE details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add

func Add(profile, dir, path string) error

Add adds the given file to the profile under the given top level dir. If the file already exists under $HOME/path, A backup is created and then copied to the profile.

func Fix added in v0.4.0

func Fix() error

Fix tries to fix the configuration file, after breaking changes were introduced.

List of things that get fixed:

  • [0.3.0] move config from olf location at $HOME/.dotfiles/dotm.toml
  • [0.3.0] set ignore_prefix to "_", when not set
  • [0.4.0] set hooks_enabled to true, when not set

func Init

func Init(p *Profile, opts *InitOptions) error

Init initializes a new dotfile profile from an existing local path.

func Install

func Install(p *Profile, opts *InstallOptions) error

Install calls InstallWithContext with the background context.

func InstallWithContext

func InstallWithContext(ctx context.Context, p *Profile, opts *InstallOptions) error

InstallWithContext installs a new dotfile profile by cloning the remote repository to the local path. The clone operation can be canceled with the passed context.

func New

func New(p *Profile) error

New creates a new dotfile profile.

func Uninstall

func Uninstall(profile string, opts *UninstallOptions) error

Uninstall unlinks a dotfile profile. If any backup files exist, they get restored.

func Update

func Update(profile string, opts *UpdateOptions) error

Update calls UpdateWithContext with the background context.

func UpdateWithContext

func UpdateWithContext(ctx context.Context, profile string, opts *UpdateOptions) error

UpdateWithContext updates the symlinks for the given profile.

When the given profile is empty all profiles get updated.

Types

type Config

type Config struct {
	// Ingoreprefix is a prefix to be ignored when traversing the dotfiles.
	// Both directories and files get ignored if they have the prefix.
	// For directories with the prefix, all sub directories get ignored aswell.
	//
	// The default value is "_".
	IgnorePrefix string `toml:"ignore_prefix" clic:"ignore_prefix"`

	// Profiles is the list of profiles.
	Profiles map[string]*Profile `toml:"profiles" clic:"profile"`
}

Config represents the configuration file for dotm. A configuration file consists of multiple profiles.

func LoadConfig

func LoadConfig() (*Config, error)

LoadConfig loads the config file.

func LoadOrCreateConfig

func LoadOrCreateConfig() (*Config, error)

LoadOrCreateConfig tries to load the config file. If it doesn't not exist, it returns a new config.

func (*Config) AddProfile

func (c *Config) AddProfile(p *Profile) (*Profile, error)

AddProfile adds a new profile. If a profile with the same name exists, it returns an error.

It sanitizes and expands vars.

If the profile path already exists, it returns an error. If a profile with the same name exists, it returns an error.

func (*Config) AddProfileFromExistingPath

func (c *Config) AddProfileFromExistingPath(p *Profile) (*Profile, error)

AddProfileFromExistingPath adds a new profile.

It sanitizes and expands vars.

If a profile with the same name exists, it returns an error.

func (*Config) Profile

func (c *Config) Profile(name string) (*Profile, error)

Profile returns the profile with the corresponding name. The returned profile has expanded vars.

If no profile with the given name exists an error get returned.

func (*Config) Write

func (c *Config) Write() error

Write writes the config file.

type Hook

type Hook []string

Hook represents one type of hook.

func (Hook) Exec

func (h Hook) Exec(ctx context.Context) error

Exec executes a hook.

type Hooks

type Hooks struct {
	PreUpdate  Hook `toml:"pre_update" clic:"pre_update"`
	PostUpdate Hook `toml:"post_update" clic:"post_update"`
}

Hooks represents all hooks.

func LoadHooksFromFile

func LoadHooksFromFile(path string) (*Hooks, error)

LoadHooksFromFile reads a hook file from a given file path.

type InitOptions

type InitOptions struct {
	LinkOptions
}

InitOptions are the options used to initialize a dotfile profile.

type InstallOptions

type InstallOptions struct {
	LinkOptions
}

InstallOptions are the options used to install a dotfile profile.

type LinkOptions

type LinkOptions struct {
	Force bool
	Dry   bool
	TraversalOptions
}

LinkOptions are the options used during the symlink creation.

type Profile

type Profile struct {
	Name         string            `toml:"-" clic:"name"`
	Path         string            `toml:"path" clic:"path"`
	Remote       string            `toml:"remote" clic:"remote"`
	HooksEnabled bool              `toml:"hooks_enabled" clic:"hooks_enabled"`
	Includes     []string          `toml:"includes" clic:"includes"`
	Excludes     []string          `toml:"excludes" clic:"excludes"`
	Vars         map[string]string `toml:"vars" clic:"vars"`
	Hooks
	// contains filtered or unexported fields
}

Profile defines the data of a dotfile profile.

type TraversalOptions

type TraversalOptions struct {
	Includes     []string
	Excludes     []string
	IgnorePrefix string
}

TraversalOptions are used during the dotfile traversal.

type UninstallOptions

type UninstallOptions struct {
	Dry   bool // Dry performes a dry run
	Clean bool // Clean also removes the local dotfile path and the config entry
}

UninstallOptions are the options used to uninstall a dotfile profile.

type UpdateOptions

type UpdateOptions struct {
	FromRemote bool
	ExecHooks  bool

	LinkOptions
}

UpdateOptions are the options used to update a dotfile profile.

Directories

Path Synopsis
cmd
internal
clic
Package clic implements a command line configuration parser.
Package clic implements a command line configuration parser.

Jump to

Keyboard shortcuts

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