#symlink #linux #content

app mksls

在文件中创建指定的符号链接

2 个稳定版本

1.0.1 2024年7月7日
1.0.0 2024年4月17日

#709命令行工具

GPL-3.0 许可证

51KB
994

mksls

mksls 是一个命令行程序,用于创建用户定义文件中指定的符号链接(请参阅 用例)。支持 Unix 操作系统(例如 Linux 发行版,MacOS),但不支持其他操作系统(例如 Windows)。

内容

用例

我对这个程序的使用场景是管理我的 dotfiles。我把所有想纳入版本控制的配置文件放在一个自定义文件夹中(在我的案例中是 ~/.dotfiles)。因此,我可以自由地组织文件,而无需在多个位置创建 Git 目录,或者创建一个像 ~/.config 那样大且大部分内容被 Git 忽略的大目录。

然而现在,配置文件的位置并不正确,因此使用了符号链接。所有符号链接都可以手动创建,但自动化的好处至少有两点

  1. 您可能希望对创建符号链接的位置进行版本控制,这样您在为另一台计算机设置配置时就不必查看每个工具的文档。
  2. 可能的情况是,您很久以前创建的一个符号链接不小心被删除或覆盖了。当您发现工具的行为与您预期的配置不一致时,您会想知道问题的原因。也许问题出在符号链接上!您不需要在工具的文档中检查配置文件的预期位置,只需运行 mksls 就可以完成,无论符号链接是否仍然存在、被删除或覆盖。

您可能会想

为什么还要安装另一个程序来完成这样一个简单的任务,而我可以使用一点脚本就能完成?

您是对的。实际上,我目前 dotfiles 设置的灵感来自 Andrew Burgess 的这篇文章 https://shaky.sh/simple-dotfiles/,其中包含一个 Bash 脚本,几乎完成了 mksls 所做的所有工作(同时做了一点更多的事情)。如果您不想安装另一个程序,并且有一个您可以轻松理解和修改以满足您需求的脚本,那么请选择它。

安装

从发布页面下载可执行文件

先决条件

请参阅 https://github.com/yanns1/mksls/releases

从 crates.io 下载 mksls

先决条件

  • 您需要安装 Rust(更具体地说 cargo)。

在您的终端中运行 cargo install mksls

从源代码构建

先决条件

  • 您需要安装 Rust(更具体地说 cargo)。
  • 您需要安装 Git。

克隆此仓库并从仓库根目录运行 ./install.sh (可能需要给予脚本可执行权限)。install.sh 使用 cargo 构建项目,然后在 ~/.local/bin 中创建指向生成的可执行文件的符号链接。

用法

所有内容都在 mksls --help[^1] 中解释。

Make symlinks specified in files.
--------
This program makes the symlinks specified in files within DIR having the base FILE.
A given file contains zero or more symlink specifications, where a symlink specification is a line with the following format:
<TARGET_PATH> <SYMLINK_PATH>
Notice the space in between.

There can be multiple spaces, but there needs to be at least one.
If a path contains a space, wrap it in double quotes.
For example, if <TARGET_PATH> contains a space, write this instead:
"<TARGET_PATH>" <SYMLINK_PATH>
If you have a double quote in one of the paths... Change it!

By default, the program is interactive.
If no file is found where a given symlink is about to be made, the symlink will be made.
However, if a file is found, you will be asked to choose between:
    [s]kip : Don't create the symlink and move on to the next one.
    [S]kip all : [s]kip for the current symlink and all further symlink conflicting with an existing file.
    [b]ackup : Move the existing file in BACKUP_DIR, then make the current symlink.
    [B]ackup all : [b]ackup for the current symlink and all further symlink conflicting with an existing file.
    [o]verwrite : Overwrite the existing file with the symlink (beware data loss!)
    [O]verwrite all : [o]verwrite for the current symlink and all further symlink conflicting with an existing file.
However it can be made uninteractive by using one (and only one) of these options:
    --always-skip (equivalent to always selecting 's')
    --always-backup (equivalent to always selecting 'b')
There is no --always-overwrite for you to not regret it.

The output of the command will always be a sequence of lines where each line has the format:
    (<action>) <link> -> <target>
One such line is printed for each symlink specification encountered, with <action> being one character
representing what has been done for that symlink:
    . : Already existed, so has been skipped.
    d : Done. The symlink was successfully created.
    s : There was a conflict between the link and an existing file, and choose to [s]kip.
    b : There was a conflict between the link and an existing file, and choose to [b]ackup.
    o : There was a conflict between the link and an existing file, and choose to [o]verwrite.
and <link> and <target> are respectively the link and target of the symlink specification.


Usage: mksls [OPTIONS] <DIR>

Arguments:
  <DIR>
          The directory in which to scan for files specifying symlinks.

Options:
  -f, --filename <FILENAME>
          The base (name + extension) of the file(s) specifying symlinks to make.

          By default, the name is "sls".
          If one is specified in the config file, it will be used instead.

  -b, --backup-dir <BACKUP_DIR>
          The backup directory in which to store the backed up files during execution.

          By default, it is set to:
              (Linux) $XDG_CONFIG_HOME/mksls/backups/ or .config/mksls/backups/ if $XDG_CONFIG_HOME is not set
              (Mac) $HOME/Library/Application Support/mksls/backups/

      --always-skip
          Always skip the symlinks conflicting with an existing file.

          This makes the program uninteractive.
          Of course, it can't be combined with --always-backup.

      --always-backup
          Always backup the conflicting file before replacing it by the symlink.

          This makes the program uninteractive.
          Of course, it can't be combined with --always-skip.

  -h, --help
          Print help (see a summary with '-h')

  -V, --version
          Print version

Configuration file:
You can provide other default values for the options:
    --filename
    --backup-dir
    --always-skip
    --always-backup
in a TOML configuration file located at:
    (Linux) $XDG_CONFIG_HOME/_project_path_ or .config/_project_path_ if $XDG_CONFIG_HOME is not set
    (Mac) $HOME/Library/Application Support/_project_path_
where _project_path_ is 'mksls/mksls.toml'.

Note:
    - If you didn't write a config file yourself, one with the default values will automatically be written.
    - Paths in the config file should be absolute.

待办事项

  • 制作shell补全(但目前来说过于繁琐)。
  • 集成测试

[^1]:实际上,帮助信息非常详细,一旦你明白了大致内容,你可能会更喜欢 mksls -h 中的简短版本。

依赖项

约 6–15MB
约 175K SLoC