1
0
Fork 0
mirror of synced 2024-12-22 14:41:07 -05:00
yadm/_docs/060_alternates.md

201 lines
7.9 KiB
Markdown
Raw Normal View History

2016-09-06 23:19:16 -04:00
---
title: "Alternate Files"
permalink: /docs/alternates
---
When possible, it is best to use the same files across all systems. However,
there are occasions when you need different files in some places. Below are
features and strategies for dealing with those occasions.
## Symlink alternates
It can be useful to have an automated way of choosing an alternate version of a
2019-10-20 16:07:13 -04:00
file for a different operating system, host, user, etc.
2016-09-06 23:19:16 -04:00
2019-10-20 16:07:13 -04:00
yadm will automatically create a symbolic link to the appropriate version of a
file, when a valid suffix is appended to the filename. The suffix contains the
conditions that must be met for that file to be used.
2016-09-06 23:19:16 -04:00
2019-10-20 16:07:13 -04:00
The suffix begins with `##`, followed by any number of conditions separated by
commas.
2016-09-06 23:19:16 -04:00
2019-10-20 16:07:13 -04:00
##<condition>[,<condition>,…]
2016-09-06 23:19:16 -04:00
2019-10-20 16:07:13 -04:00
Each condition is an attribute/value pair, separated by a period. Some
conditions do not require a "value", and in that case, the period and value can
be omitted. Most attributes can be abbreviated as a single letter. Values are
compared case-insensitive.
2016-09-06 23:19:16 -04:00
2019-10-20 16:07:13 -04:00
| Attribute | Meaning |
| - | - |
| `arch`, `a` | Valid if the value matches the architecture. Architecture is calculated by running <code>uname&nbsp;&#8209;m</code>. |
2019-10-20 16:07:13 -04:00
| `class`, `c` | Valid if the value matches the local.class configuration. Class must be manually set using <code>yadm&nbsp;config&nbsp;local.class&nbsp;&lt;class&gt;</code>. |
| `default` | Valid when no other alternate is valid. |
| `distro`, `d` | Valid if the value matches the distro. Distro is calculated by running <code>lsb_release&nbsp;&#8209;si</code> or inspecting `ID` from <code>/etc/os-release</code> |
| `distro_family`, `f` | Valid if the value matches the distro family. Distro family is calculated by inspecting `ID_LIKE` from <code>/etc/os-release</code> (or `ID` if `ID_LIKE` isn't found) |
| `extension`, `e` | A special "condition" that doesn't affect the selection process. Its purpose is instead to allow the alternate file to end with a certain extension to e.g. make editors highlight the content properly. |
2022-02-21 14:17:16 -05:00
| `hostname`, `h` | Valid if the value matches the short hostname. Hostname is calculated by running <code>uname&nbsp;&#8209;n</code>, and trimming off any domain. |
| `os`, `o` | Valid if the value matches the OS. OS is calculated by running <code>uname&nbsp;&#8209;s</code>. <sup>*</sup> |
| `template`, `t` | Valid when the value matches a supported template processor. See the [Templates](/docs/templates) section for more details. |
| `user`, `u` | Valid if the value matches the current user. Current user is calculated by running <code>id&nbsp;&#8209;u&nbsp;&#8209;n</code>. |
2016-09-06 23:19:16 -04:00
2019-11-14 09:10:06 -05:00
<sub><sup>*
The OS for "Windows Subsystem for Linux" is reported as "WSL", even though uname identifies as "Linux".
<br/>
*
2021-01-12 18:06:08 -05:00
The OS for Linux-like runtimes for Windows (e.g. MinGW, Cygwin) is obtained by running `uname -o`.
2019-11-14 09:10:06 -05:00
</sup></sub>
2019-10-20 16:07:13 -04:00
You may use any number of conditions, in any order. An alternate will only be
used if _ALL_ conditions are valid. For all files managed by yadm's repository
or listed in `$HOME/.config/yadm/encrypt`, if they match this naming convention,
symbolic links will be created for the most appropriate version.
2016-09-06 23:19:16 -04:00
2019-10-20 16:07:13 -04:00
The "most appropriate" version is determined by calculating a score for each
version of a file. A [template](/docs/templates) is always scored higher than
any symlink condition. The number of conditions is the next largest factor in
scoring. Files with more conditions will always be favored. Any invalid
condition will disqualify that file completely.
2016-09-06 23:19:16 -04:00
2019-10-20 16:07:13 -04:00
If you don't care to have all versions of alternates stored in the same
directory as the generated symlink, you can place them in the
`$HOME/.config/yadm/alt` directory. The generated symlink or processed template
will be created using the same relative path.
2016-09-06 23:19:16 -04:00
2019-10-20 16:07:13 -04:00
Alternate linking may best be demonstrated by example. Assume the following
files are managed by yadm's repository:
2016-09-06 23:19:16 -04:00
2019-10-20 16:07:13 -04:00
$HOME/path/example.txt##default
$HOME/path/example.txt##class.Work
$HOME/path/example.txt##os.Darwin
$HOME/path/example.txt##os.Darwin,hostname.host1
$HOME/path/example.txt##os.Darwin,hostname.host2
$HOME/path/example.txt##os.Linux
$HOME/path/example.txt##os.Linux,hostname.host1
$HOME/path/example.txt##os.Linux,hostname.host2
2016-09-06 23:19:16 -04:00
If running on a MacBook named `host2`, yadm will create a symbolic link which looks like this:
2016-09-06 23:19:16 -04:00
2019-10-20 16:07:13 -04:00
`$HOME/path/example.txt``$HOME/path/example.txt##os.Darwin,hostname.host2`
2016-09-06 23:19:16 -04:00
However, on another MacBook named `host3`, yadm will create a symbolic link which looks like this:
2019-10-20 16:07:13 -04:00
`$HOME/path/example.txt``$HOME/path/example.txt##os.Darwin`
2019-10-20 16:07:13 -04:00
Since the hostname doesn't match any of the managed files, the more generic
version is chosen. If running on a Linux server named `host4`, the link will be:
2016-09-06 23:19:16 -04:00
2019-10-20 16:07:13 -04:00
`$HOME/path/example.txt``$HOME/path/example.txt##os.Linux`
2019-10-20 16:07:13 -04:00
If running on a Solaris server, the link will use the default version:
2019-10-20 16:07:13 -04:00
`$HOME/path/example.txt``$HOME/path/example.txt##default`
2019-10-20 16:07:13 -04:00
If running on a system, with class set to `Work`, the link will be:
2019-10-20 16:07:13 -04:00
`$HOME/path/example.txt``$HOME/path/example.txt##class.Work`
2017-03-31 18:20:57 -04:00
2019-10-20 16:07:13 -04:00
If no `##default` version exists and no files have valid conditions, then no
link will be created.
2017-03-31 18:20:57 -04:00
2019-10-20 16:07:13 -04:00
Links are also created for directories named this way, as long as they have at
least one yadm managed file within them (at the top level).
2017-07-07 09:31:11 -04:00
2019-10-20 16:07:13 -04:00
yadm will automatically create these links by default. This can be disabled
using the `yadm.auto-alt` configuration. Even if disabled, links can be manually
created by running `yadm alt`.
2017-03-31 18:20:57 -04:00
2019-10-20 16:07:13 -04:00
## Class and Overrides
2017-03-31 18:20:57 -04:00
2019-10-20 16:07:13 -04:00
Class is a special value which is stored locally on each host (inside the local
repository). To use alternate symlinks using `##class.<CLASS>`, you must set the
value of class using the configuration `local.class`. This is set like any other
yadm configuration—with the `yadm config` command. The following sets the
`local.class` to be "Work".
2017-03-31 18:20:57 -04:00
2019-10-20 16:07:13 -04:00
yadm config local.class Work
2017-03-31 18:20:57 -04:00
Similarly, the values of `arch`, `os`, `hostname`, `user`, `distro`, and
`distro_family` can be manually overridden using the configuration options
`local.arch`, `local.os`, `local.hostname`, `local.user`, `local.distro`, and
`local.distro-family`.
2017-03-31 18:20:57 -04:00
Additional local classes can be defined using the `--add` switch.
yadm config --add local.class <additional-class>
You can display all of the classes using `--get-all`.
yadm config --get-all local.class
2019-10-20 16:07:13 -04:00
## Templates
2017-03-31 18:20:57 -04:00
2019-10-20 16:07:13 -04:00
Templates are another powerful tool for creating alternate content on each host.
See the [Templates](/docs/templates) documentation for full details.
2017-03-31 18:20:57 -04:00
2016-09-06 23:19:16 -04:00
## Strategies for alternate files on different systems
Where possible, you should try to use the same file on every system. Here are a few examples:
### .vimrc
```vim
let OS=substitute(system('uname -s'),"\n","","")
if (OS == "Darwin")
" do something that only makes sense on a Mac
endif
```
### .tmux.conf
# use reattach-to-user-namespace as the default command on OSX
if-shell "test -f /usr/local/bin/reattach-to-user-namespace" 'set -g default-command "reattach-to-user-namespace -l bash"'
### .bash_profile
```bash
system_type=$(uname -s)
if [ "$system_type" = "Darwin" ]; then
eval $(gdircolors $HOME/.dir_colors)
else
eval $(dircolors -b $HOME/.dir_colors)
fi
```
### .gitconfig
However, sometimes the type of file you are using doesn't allow for this type of
logic. If a configuration can do an "_include_", you can include a specific
2019-10-19 14:59:03 -04:00
alternate version using yadm. Consider these three files:
2016-09-06 23:19:16 -04:00
`.gitconfig`
```ini
[log]
decorate = short
abbrevCommit = true
[include]
path = .gitconfig.local
```
2019-12-02 06:25:40 -05:00
`.gitconfig.local##os.Darwin`
2016-09-06 23:19:16 -04:00
```ini
[user]
name = Tim Byrne
email = tim@personal.email.org
```
2019-12-02 06:25:40 -05:00
`.gitconfig.local##os.Linux`
2016-09-06 23:19:16 -04:00
```ini
[user]
name = Dr. Tim Byrne
email = dr.byrne@work.email.com
```
Configuring Git this way includes `.gitconfig.local` in the standard
2019-10-19 14:59:03 -04:00
`.gitconfig`. yadm will automatically link the correct version based on the
2017-01-10 08:50:59 -05:00
operating system. The bulk of your configurations can go in a single file, and
2016-09-06 23:19:16 -04:00
you just put the exceptions in OS-specific files.