Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deprecated trans_r/w for install/update #415

Merged
merged 5 commits into from Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 7 additions & 5 deletions docs/config/config-dotfiles.md
Expand Up @@ -14,11 +14,13 @@ Entry | Description
`ignoreempty` | If true, an empty template will not be deployed (defaults to the value of `ignoreempty`)
`instignore` | List of patterns to ignore when installing (enclose in quotes when using wildcards; see [ignore patterns](config-file.md#ignore-patterns))
`template` | If false, disable templating for this dotfile (defaults to the value of `template_dotfile_default`)
`trans_read` | Transformation key to apply when installing this dotfile (must be defined in the **trans_read** entry below; see [transformations](config-transformations.md))
`trans_write` | Transformation key to apply when updating this dotfile (must be defined in the **trans_write** entry below; see [transformations](config-transformations.md))
`trans_install` | Transformation key to apply when installing this dotfile (must be defined in the **trans_install** entry below; see [transformations](config-transformations.md))
`trans_update` | Transformation key to apply when updating this dotfile (must be defined in the **trans_update** entry below; see [transformations](config-transformations.md))
`upignore` | List of patterns to ignore when updating (enclose in quotes when using wildcards; see [ignore patterns](config-file.md#ignore-patterns))
<s>link_children</s> | Replaced by `link: link_children`
<s>trans</s> | Replaced by `trans_read`
<s>trans</s> | Replaced by `trans_install`
<s>trans_read</s> | Replaced by `trans_install`
<s>trans_write</s> | Replaced by `trans_update`

```yaml
<dotfile-key-name>:
Expand All @@ -37,8 +39,8 @@ Entry | Description
- <action-key>
template: (true|false)
chmod: '<file-permissions>'
trans_read: <transformation-key>
trans_write: <transformation-key>
trans_install: <transformation-key>
trans_update: <transformation-key>
```

## Dotfile actions
Expand Down
32 changes: 16 additions & 16 deletions docs/config/config-transformations.md
Expand Up @@ -14,14 +14,14 @@ For examples of transformation uses, see:

There are two types of transformations available:

* **Read transformations**: used to transform dotfiles before they are installed ([config](config-config.md) key `trans_read`)
* **Install transformations**: used to transform dotfiles before they are installed ([config](config-config.md) key `trans_install`)
* Used for commands `install` and `compare`
* They have two mandatory arguments:
* **{0}** will be replaced with the dotfile to process
* **{1}** will be replaced with a temporary file to store the result of the transformation
* This Happens **before** the dotfile is templated (see [templating](../template/templating.md))

* **Write transformations**: used to transform files before updating a dotfile ([config](config-config.md) key `trans_write`)
* **Update/Import transformations**: used to transform files before updating/importing a dotfile ([config](config-config.md) key `trans_update`)
* Used for command `update` and `import`
* They have two mandatory arguments:
* **{0}** will be replaced with the file path to update the dotfile with
Expand All @@ -36,13 +36,13 @@ Transformations also support additional positional arguments that must start fro

For example:
```yaml
trans_read:
trans_install:
targ: echo "$(basename {0}); {{@@ _dotfile_key @@}}; {2}; {3}" > {1}
dotfiles:
f_abc:
dst: /tmp/abc
src: abc
trans_read: targ "{{@@ profile @@}}" lastarg
trans_install: targ "{{@@ profile @@}}" lastarg
profiles:
p1:
dotfiles:
Expand All @@ -51,21 +51,21 @@ profiles:

will result in `abc; f_abc; p1; lastarg`.

## trans_read entry
## trans_install entry

The **trans_read** entry (optional) contains a transformations mapping (See [transformations](config-transformations.md)).
The **trans_install** entry (optional) contains a transformations mapping (See [transformations](config-transformations.md)).

```yaml
trans_read:
trans_install:
<trans-key>: <command-to-execute>
```

## trans_write entry
## trans_update entry

The **trans_write** entry (optional) contains a write transformations mapping (See [transformations](config-transformations.md)).
The **trans_update** entry (optional) contains a write transformations mapping (See [transformations](config-transformations.md)).

```yaml
trans_write:
trans_update:
<trans-key>: <command-to-execute>
```

Expand All @@ -77,10 +77,10 @@ and [template variables](../template/template-variables.md#template-variables)).

A very dumb example:
```yaml
trans_read:
trans_install:
r_echo_abs_src: echo "{0}: {{@@ _dotfile_abs_src @@}}" > {1}
r_echo_var: echo "{0}: {{@@ r_var @@}}" > {1}
trans_write:
trans_update:
w_echo_key: echo "{0}: {{@@ _dotfile_key @@}}" > {1}
w_echo_var: echo "{0}: {{@@ w_var @@}}" > {1}
variables:
Expand All @@ -90,11 +90,11 @@ dotfiles:
f_abc:
dst: ${tmpd}/abc
src: abc
trans_read: r_echo_abs_src
trans_write: w_echo_key
trans_install: r_echo_abs_src
trans_update: w_echo_key
f_def:
dst: ${tmpd}/def
src: def
trans_read: r_echo_var
trans_write: w_echo_var
trans_install: r_echo_var
trans_update: w_echo_var
```
24 changes: 12 additions & 12 deletions docs/howto/sensitive-dotfiles.md
Expand Up @@ -37,9 +37,9 @@ First you need to define the encryption/decryption methods, for example
```yaml
variables:
keyid: "11223344"
trans_read:
trans_install:
_decrypt: "gpg -q --for-your-eyes-only--no-tty -d {0} > {1}"
trans_write:
trans_update:
_encrypt: "gpg -q -r {{@@ keyid @@}} --armor --no-tty -o {1} -e {0}"
```

Expand All @@ -60,37 +60,37 @@ Using GPG keys:
```yaml
variables:
keyid: "11223344"
trans_read:
trans_install:
_decrypt: "gpg -q --for-your-eyes-only--no-tty -d {0} > {1}"
trans_write:
trans_update:
_encrypt: "gpg -q -r {{@@ keyid @@}} --armor --no-tty -o {1} -e {0}"
```

Passphrase is stored in an environment variable:
```yaml
trans_read:
trans_install:
_decrypt: "echo {{@@ env['THE_KEY'] @@}} | gpg -q --batch --yes --for-your-eyes-only --passphrase-fd 0 --no-tty -d {0} > {1}"
trans_write:
trans_update:
_encrypt: "echo {{@@ env['THE_KEY'] @@}} | gpg -q --batch --yes --passphrase-fd 0 --no-tty -o {1} -c {0}"
```

Passphrase is stored as a variable:
```yaml
variables:
gpg_password: "some password"
trans_read:
trans_install:
_decrypt: "echo {{@@ gpg_password @@}} | gpg -q --batch --yes --for-your-eyes-only --passphrase-fd 0 --no-tty -d {0} > {1}"
trans_write:
trans_update:
_encrypt: "echo {{@@ gpg_password @@}} | gpg -q --batch --yes --passphrase-fd 0 --no-tty -o {1} -c {0}"
```

Passphrase is retrieved using a script:
```yaml
dynvariables:
gpg_password: "./get-password.sh"
trans_read:
trans_install:
_decrypt: "echo {{@@ gpg_password @@}} | gpg -q --batch --yes --for-your-eyes-only --passphrase-fd 0 --no-tty -d {0} > {1}"
trans_write:
trans_update:
_encrypt: "echo {{@@ gpg_password @@}} | gpg -q --batch --yes --passphrase-fd 0 --no-tty -o {1} -c {0}"
```

Expand All @@ -100,9 +100,9 @@ variables:
gpg_password_file: "/tmp/the-password"
dynvariables:
gpg_password: "cat {{@@ gpg_password_file @@}}"
trans_read:
trans_install:
_decrypt: "echo {{@@ gpg_password @@}} | gpg -q --batch --yes --for-your-eyes-only --passphrase-fd 0 --no-tty -d {0} > {1}"
trans_write:
trans_update:
_encrypt: "echo {{@@ gpg_password @@}} | gpg -q --batch --yes --passphrase-fd 0 --no-tty -o {1} -c {0}"
```

Expand Down
6 changes: 3 additions & 3 deletions docs/howto/store-compressed-directories.md
@@ -1,13 +1,13 @@
# Handle compressed directories

This is an example of how to use transformations (`trans_read` and `trans_write`) to store
This is an example of how to use transformations (`trans_install` and `trans_update`) to store
compressed directories and deploy them with dotdrop.

Start by defining the transformations:
```yaml
trans_read:
trans_install:
uncompress: "mkdir -p {1} && tar -xf {0} -C {1}"
trans_write:
trans_update:
compress: "tar -cf {1} -C {0} ."
```

Expand Down
73 changes: 38 additions & 35 deletions dotdrop/cfg_aggregator.py
Expand Up @@ -69,23 +69,23 @@ def del_dotfile_from_profile(self, dotfile, profile):
return self.cfgyaml.del_dotfile_from_profile(dotfile.key, profile.key)

def new_dotfile(self, src, dst, link, chmod=None,
trans_read=None, trans_write=None):
trans_install=None, trans_update=None):
"""
import a new dotfile
@src: path in dotpath
@dst: path in FS
@link: LinkType
@chmod: file permission
@trans_read: read transformation
@trans_write: write transformation
@trans_install: read transformation
@trans_update: write transformation
"""
dst = self.path_to_dotfile_dst(dst)
dotfile = self.get_dotfile_by_src_dst(src, dst)
if not dotfile:
# add the dotfile
dotfile = self._create_new_dotfile(src, dst, link, chmod=chmod,
trans_read=trans_read,
trans_write=trans_write)
trans_install=trans_install,
trans_update=trans_update)

if not dotfile:
return False
Expand Down Expand Up @@ -237,25 +237,25 @@ def get_dotfile(self, key, profile_key=None):
########################################################

def _create_new_dotfile(self, src, dst, link, chmod=None,
trans_read=None, trans_write=None):
trans_install=None, trans_update=None):
"""create a new dotfile"""
# get a new dotfile with a unique key
key = self._get_new_dotfile_key(dst)
self.log.dbg(f'new dotfile key: {key}')
# add the dotfile
trans_r_key = trans_w_key = None
if trans_read:
trans_r_key = trans_read.key
if trans_write:
trans_w_key = trans_write.key
trans_install_key = trans_update_key = None
if trans_install:
trans_install_key = trans_install.key
if trans_update:
trans_update_key = trans_update.key
if not self.cfgyaml.add_dotfile(key, src, dst, link,
chmod=chmod,
trans_r_key=trans_r_key,
trans_w_key=trans_w_key):
trans_install_key=trans_install_key,
trans_update_key=trans_update_key):
return None
return Dotfile(key, dst, src,
trans_r=trans_read,
trans_w=trans_write)
trans_install=trans_install,
trans_update=trans_update)

########################################################
# parsing
Expand Down Expand Up @@ -297,15 +297,15 @@ def _load(self, reloading=False):
self.actions = Action.parse_dict(self.cfgyaml.actions)
debug_list('actions', self.actions, self.debug)

# trans_r
self.log.dbg('parsing trans_r')
self.trans_r = Transform.parse_dict(self.cfgyaml.trans_r)
debug_list('trans_r', self.trans_r, self.debug)
# trans_install
self.log.dbg('parsing trans_install')
self.trans_install = Transform.parse_dict(self.cfgyaml.trans_install)
debug_list('trans_install', self.trans_install, self.debug)

# trans_w
self.log.dbg('parsing trans_w')
self.trans_w = Transform.parse_dict(self.cfgyaml.trans_w)
debug_list('trans_w', self.trans_w, self.debug)
# trans_update
self.log.dbg('parsing trans_update')
self.trans_update = Transform.parse_dict(self.cfgyaml.trans_update)
debug_list('trans_update', self.trans_update, self.debug)

# variables
self.log.dbg('parsing variables')
Expand Down Expand Up @@ -334,14 +334,17 @@ def _load(self, reloading=False):
msg = f'default actions: {self.settings.default_actions}'
self.log.dbg(msg)

# patch trans_w/trans_r in dotfiles
# patch trans_install in dotfiles
trans_inst_args = self._get_trans_update_args(self.get_trans_install)
self._patch_keys_to_objs(self.dotfiles,
"trans_r",
self._get_trans_w_args(self.get_trans_r),
CfgYaml.key_trans_install,
trans_inst_args,
islist=False)
# patch trans_update in dotfiles
trans_update_args = self._get_trans_update_args(self.get_trans_update)
self._patch_keys_to_objs(self.dotfiles,
"trans_w",
self._get_trans_w_args(self.get_trans_w),
CfgYaml.key_trans_update,
trans_update_args,
islist=False)

self.log.dbg('done parsing cfgyaml into cfg_aggregator')
Expand Down Expand Up @@ -542,7 +545,7 @@ def _get_action_w_args(self, key):
action = self._get_action(key)
return action

def _get_trans_w_args(self, getter):
def _get_trans_update_args(self, getter):
"""return transformation by key with the arguments"""
def getit(key):
fields = shlex.split(key)
Expand All @@ -557,16 +560,16 @@ def getit(key):
return trans
return getit

def get_trans_r(self, key):
"""return the trans_r with this key"""
def get_trans_install(self, key):
"""return the trans_install with this key"""
try:
return next(x for x in self.trans_r if x.key == key)
return next(x for x in self.trans_install if x.key == key)
except StopIteration:
return None

def get_trans_w(self, key):
"""return the trans_w with this key"""
def get_trans_update(self, key):
"""return the trans_update with this key"""
try:
return next(x for x in self.trans_w if x.key == key)
return next(x for x in self.trans_update if x.key == key)
except StopIteration:
return None