2个版本

使用旧的Rust 2015

0.1.1 2016年5月19日
0.1.0 2016年5月18日

#37 in #ignore

MIT 协议

125KB
3K SLoC

Rust 2.5K SLoC // 0.0% comments Ruby 456 SLoC Shell 12 SLoC

KIC (Keep It Clean)

Build Status

摘要

  • "kic" 防止有人把目录弄得乱七八糟。
  • 对于已注册的目录,"kic" 会自动将 "dust" 文件移动到 "dust box"。
  • "dust" 是 "kic" 中未指定为需求的文件。

注意

  • "kic" 在 UNIX 平台上使用您的 cron
  • "kic" 在 Windows 平台上不完整。
    • 自主删除
    • 隐藏文件的判断
    • 瞬间移动 "dust" 文件
    • 禁止运行的目录

安装

  1. 请安装 稳定 版本的 rust。请参阅 此处
  2. $cargo install kic
  3. 将 "~/.cargo/bin" 目录添加到 $PATH

MacOS 例子

$ curl -sSf https://static.rust-lang.org/rustup.sh | sh

$ cargo install kic

$ echo 'PATH=$PATH:$HOME/.cargo/bin' >> $HOME/.bash_profile && source $HOME/.bash_profile

基本用法

预备

  • 我们使用 tree 命令来方便地解释。

你好,"kic"!

  1. 更改您想注册的目录。
  2. $kic init
  3. 确认 .kic 目录和必要文件已在本目录中创建。
  4. 确认最初存在的文件已写入 "ignore" 文件。
$ pwd
/Users/tokutake/tmp

$ tree -a
.
├── dir1
   └── file2
└── file1

1 directory, 2 files

$ kic init
INFO: Create ".kic" directory
INFO: Create "warehouse" directory
INFO: Create "config.toml" file
INFO: Create "ignore" file

$ tree -a
.
├── .kic
   ├── config.toml
   ├── ignore
   └── warehouse
├── dir1
   └── file2
└── file1

3 directories, 4 files

$ cat .kic/ignore
./dir1/file2
./file1

手动尝试移动 "dust" 文件

  1. 制作 "dust" 文件。
  2. $ kic sweep all (dry-run)
  3. 检查 "dust" 文件未移动到 "dust box"。
  4. $kic sweep all indeed
  5. 检查 "dust" 文件已移动到 "dust box"。
$ touch file3 dir1/file4
.
├── .kic
   ├── config.toml
   ├── ignore
   └── warehouse
├── dir1
   ├── file2
   └── file4
├── file1
└── file3

3 directories, 6 files

$ kic sweep all
INFO: Create "2016-05-16" directory in ".kic/warehouse"
INFO: Create "sweep.log" file in ".kic/warehouse/2016-05-16"
INFO: Read "config.toml" file
INFO: Get the parameter for "sweep.moratorium"
INFO: Read "ignore" file
INFO: Move dusts to ".kic/warehouse/2016-05-16/dusts" (dry-run mode)
INFO:   => "./dir1/file4"
INFO:   => "./file3"
INFO: Move empty dirs to ".kic/warehouse/2016-05-16/dusts" (dry-run mode)

$ tree -a
.
├── .kic
   ├── config.toml
   ├── ignore
   └── warehouse
       └── 2016-05-16
           ├── dusts
           └── sweep.log
├── dir1
   ├── file2
   └── file4
├── file1
└── file3

5 directories, 7 files

$ kic sweep all indeed
INFO: Create "2016-05-16" directory in ".kic/warehouse"
INFO: Create "sweep.log" file in ".kic/warehouse/2016-05-16"
INFO: Read "config.toml" file
INFO: Get the parameter for "sweep.moratorium"
INFO: Read "ignore" file
INFO: Move dusts to ".kic/warehouse/2016-05-16/dusts"
INFO:   => "./dir1/file4"
INFO:   => "./file3"
INFO: Move empty dirs to ".kic/warehouse/2016-05-16/dusts"

$ tree -a
.
├── .kic
   ├── config.toml
   ├── ignore
   └── warehouse
       └── 2016-05-16
           ├── dusts
           │   ├── dir1
           │   │   └── file4
           │   └── file3
           └── sweep.log
├── dir1
   └── file2
└── file1

6 directories, 7 files

为什么在上面的例子中使用了 all 选项?

  1. 制作 "dust" 文件。
  2. $ kic sweep (dry-run)
  3. 确认 "dust" 文件未出现在列表中。
  4. 确认在将 "dust" 文件移动到 "dust box" 之前有一个暂停期(默认:10分钟)。
$ touch file3 dir1/file4

$ kic sweep
INFO: Create "2016-05-16" directory in ".kic/warehouse"
INFO: Create "sweep.log" file in ".kic/warehouse/2016-05-16"
INFO: Read "config.toml" file
INFO: Get the parameter for "sweep.moratorium"
INFO: Read "ignore" file
INFO: Move dusts to ".kic/warehouse/2016-05-16/dusts" (dry-run mode)
INFO: Move empty dirs to ".kic/warehouse/2016-05-16/dusts" (dry-run mode)

$ cat .kic/config.toml

[burn]
moratorium = "2 weeks"

[sweep]
moratorium = "10 minutes"
period = "daily"
time = "00:00"

### ... (10 minutes later)
$ kic sweep
INFO: Create "2016-05-16" directory in ".kic/warehouse"
INFO: Create "sweep.log" file in ".kic/warehouse/2016-05-16"
INFO: Read "config.toml" file
INFO: Get the parameter for "sweep.moratorium"
INFO: Read "ignore" file
INFO: Move dusts to ".kic/warehouse/2016-05-16/dusts" (dry-run mode)
INFO:   => "./dir1/file4"
INFO:   => "./file3"
INFO: Move empty dirs to ".kic/warehouse/2016-05-16/dusts" (dry-run mode)

$ kic sweep indeed
INFO: Create "2016-05-16" directory in ".kic/warehouse"
INFO: Create "sweep.log" file in ".kic/warehouse/2016-05-16"
INFO: Read "config.toml" file
INFO: Get the parameter for "sweep.moratorium"
INFO: Read "ignore" file
INFO: Move dusts to ".kic/warehouse/2016-05-16/dusts"
INFO:   => "./dir1/file4"
INFO:   => "./file3"
INFO: Move empty dirs to ".kic/warehouse/2016-05-16/dusts"

### Enable to change "moratorium"
$ kic config set sweep.moratorium 0minute
INFO: Read "config.toml" file
INFO: Set the parameter for "sweep.moratorium"
INFO: Create "config.toml" file

$ touch file3 dir1/file4

$ kic sweep
INFO: Create "2016-05-16" directory in ".kic/warehouse"
INFO: Create "sweep.log" file in ".kic/warehouse/2016-05-16"
INFO: Read "config.toml" file
INFO: Get the parameter for "sweep.moratorium"
INFO: Read "ignore" file
INFO: Move dusts to ".kic/warehouse/2016-05-16/dusts" (dry-run mode)
INFO:   => "./dir1/file4"
INFO:   => "./file3"
INFO: Move empty dirs to ".kic/warehouse/2016-05-16/dusts" (dry-run mode)

手动尝试删除 "dust box"。

  1. 确认在删除 "dust box" 之前有一个暂停期(默认:2周)。
  2. 更改确认的暂停期。
  3. 更改 "box" 名称以进行确认。
  4. $ kic burn (dry-run)
  5. 检查 "box" 未从 "warehouse" 中删除。
  6. $kic burn indeed
  7. 检查 "box" 已从 "warehouse" 中删除。
$ cat .kic/config.toml 

[burn]
moratorium = "2 weeks"

[sweep]
moratorium = "0 minute"
period = "daily"
time = "00:00"

$ kic config set burn.moratorium 1day
INFO: Read "config.toml" file
INFO: Set the parameter for "burn.moratorium"
INFO: Create "config.toml" file

### Rename the directory which has been named as today to yesterday
$ mv .kic/warehouse/2016-05-16/ .kic/warehouse/2016-05-15/

$ kic burn
INFO: Read "config.toml" file
INFO: Get the parameter for "burn.moratorium"
INFO: Create "2016-05-16" directory in ".kic/warehouse"
INFO: Create "burn.log" file in ".kic/warehouse/2016-05-16"
INFO: Delete expired dusts (dry-run mode)
INFO:   => ".kic/warehouse/2016-05-15"

$ tree -a
.
├── .kic
│   ├── config.toml
│   ├── ignore
│   └── warehouse
│       ├── 2016-05-15
│       │   ├── dusts
│       │   │   ├── dir1
│       │   │   │   └── file4
│       │   │   └── file3
│       │   └── sweep.log
│       └── 2016-05-16
│           ├── burn.log
│           └── dusts
├── dir1
│   └── file2
└── file1

8 directories, 8 files

$ kic burn indeed
INFO: Read "config.toml" file
INFO: Get the parameter for "burn.moratorium"
INFO: Create "2016-05-16" directory in ".kic/warehouse"
INFO: Create "burn.log" file in ".kic/warehouse/2016-05-16"
INFO: Delete expired dusts
INFO:   => ".kic/warehouse/2016-05-15"

$ tree -a
.
├── .kic
│   ├── config.toml
│   ├── ignore
│   └── warehouse
│       └── 2016-05-16
│           ├── burn.log
│           └── dusts
├── dir1
│   └── file2
└── file1

5 directories, 5 files

### Action like below is also OK!
$ rm -rf .kic/warehouse/2016-05-16/

通过/从 cron 注册/注销

  1. $kic start
  2. 检查几个 "kic" 命令已添加到您的 cron。
  3. 通过 "config.toml" 修改 cron 中命令的运行时间。
  4. $kic end
  5. 检查与当前目录相关的 "kic" 命令已从您的 cron 中删除。
$ kic start
INFO: Read cron
INFO: Read "config.toml" file
INFO: Get the parameter for "sweep.period"
INFO: Get the parameter for "sweep.time"
INFO: Set new cron

$ crontab -l
###################################
# "kic" uses the lines from this.
# Please don't touch them and me!
###################################
0 12 * * *	/Users/tokutake/codes/kic/target/debug/kic patrol
0 0 * * *	cd /Users/tokutake/tmp && /Users/tokutake/codes/kic/target/debug/kic burn indeed
0 0 * * *	cd /Users/tokutake/tmp && /Users/tokutake/codes/kic/target/debug/kic sweep indeed
###################################
# "kic" uses the lines up to here.
# Please don't touch them and me!
###################################

$ kic config set sweep.period weekly
INFO: Read "config.toml" file
INFO: Set the parameter for "sweep.period"
INFO: Create "config.toml" file

$ kic config set sweep.time 14:00
INFO: Read "config.toml" file
INFO: Set the parameter for "sweep.time"
INFO: Create "config.toml" file

$ kic start
INFO: Read cron
INFO: Read "config.toml" file
INFO: Get the parameter for "sweep.period"
INFO: Get the parameter for "sweep.time"
INFO: Set new cron

$ crontab -l
###################################
# "kic" uses the lines from this.
# Please don't touch them and me!
###################################
0 12 * * *	/Users/tokutake/codes/kic/target/debug/kic patrol
0 0 * * *	cd /Users/tokutake/tmp && /Users/tokutake/codes/kic/target/debug/kic burn indeed
0 14 * * 0	cd /Users/tokutake/tmp && /Users/tokutake/codes/kic/target/debug/kic sweep indeed
###################################
# "kic" uses the lines up to here.
# Please don't touch them and me!
###################################

$ kic end
INFO: Read cron
INFO: Set new cron

$ crontab -l
### There is no contents

将文件或目录添加/删除到“忽略”列表中

  1. 创建非“灰尘”文件。
  2. 将文件添加到“忽略”列表。
  3. 检查文件是否不在列表中。
  4. 创建目录并在其中创建非“灰尘”文件。
  5. 将目录添加到“忽略”列表。
  6. 检查文件是否不在列表中。
  7. 删除目录中的文件。
  8. 检查空目录是否在列表中,无论“忽略”设置如何。
  9. 如果您想保留空目录,则添加一个空隐藏文件。
$ touch file3 file4

$ kic ignore add file3 file4
INFO: Read "ignore" file
INFO: Create "ignore" file

$ kic sweep all
INFO: Create "2016-05-17" directory in ".kic/warehouse"
INFO: Create "sweep.log" file in ".kic/warehouse/2016-05-17"
INFO: Read "config.toml" file
INFO: Get the parameter for "sweep.moratorium"
INFO: Read "ignore" file
INFO: Move dusts to ".kic/warehouse/2016-05-17/dusts" (dry-run mode)
INFO: Move empty dirs to ".kic/warehouse/2016-05-17/dusts" (dry-run mode)

$ mkdir dir2 && touch dir2/file{5,6}

### If you are leaving "dir1", specifying "dir1" is also OK!
$ kic ignore add dir2
INFO: Read "ignore" file
INFO: Create "ignore" file

$ kic sweep all
INFO: Create "2016-05-17" directory in ".kic/warehouse"
INFO: Create "sweep.log" file in ".kic/warehouse/2016-05-17"
INFO: Read "config.toml" file
INFO: Get the parameter for "sweep.moratorium"
INFO: Read "ignore" file
INFO: Move dusts to ".kic/warehouse/2016-05-17/dusts" (dry-run mode)
INFO: Move empty dirs to ".kic/warehouse/2016-05-17/dusts" (dry-run mode)

$ rm dir2/*

$ kic sweep all
INFO: Create "2016-05-17" directory in ".kic/warehouse"
INFO: Create "sweep.log" file in ".kic/warehouse/2016-05-17"
INFO: Read "config.toml" file
INFO: Get the parameter for "sweep.moratorium"
INFO: Read "ignore" file
INFO: Move dusts to ".kic/warehouse/2016-05-17/dusts" (dry-run mode)
INFO: Move empty dirs to ".kic/warehouse/2016-05-17/dusts" (dry-run mode)
INFO:   => "./dir2"

$ touch dir2/.kickeep

$ kic sweep all
INFO: Create "2016-05-17" directory in ".kic/warehouse"
INFO: Create "sweep.log" file in ".kic/warehouse/2016-05-17"
INFO: Read "config.toml" file
INFO: Get the parameter for "sweep.moratorium"
INFO: Read "ignore" file
INFO: Move dusts to ".kic/warehouse/2016-05-17/dusts" (dry-run mode)
INFO: Move empty dirs to ".kic/warehouse/2016-05-17/dusts" (dry-run mode)

"ignore"的有用子命令

  1. 创建非“灰尘”目录和文件。
  2. $kic ignore current
  3. 检查文件是否不在列表中。
  4. 删除非“灰尘”文件。
  5. $kic ignore refresh
  6. 检查不存在(和目录)的文件是否已经在“忽略”列表中不存在。
$ touch file{5,6,7}

$ mkdir dir3 && dir3/file{8,9,10}

### NOTICE: If you have registered some directories in "ignore", They will be deleted from "ignore" and Files in them will be added to it.
$ kic ignore current
CAUTION: Do you want to preserve current state? [yes/no]: y
INFO: Create "ignore" file

$ kic sweep all
INFO: Create "2016-05-18" directory in ".kic/warehouse"
INFO: Create "sweep.log" file in ".kic/warehouse/2016-05-18"
INFO: Read "config.toml" file
INFO: Get the parameter for "sweep.moratorium"
INFO: Read "ignore" file
INFO: Move dusts to ".kic/warehouse/2016-05-18/dusts" (dry-run mode)
INFO: Move empty dirs to ".kic/warehouse/2016-05-18/dusts" (dry-run mode)

$ cat .kic/ignore 
./dir1/file2
./dir3/file10
./dir3/file8
./dir3/file9
./file1
./file3
./file4
./file5
./file6
./file7

$ rm file{5,6,7}

$ kic ignore refresh

$ cat .kic/ignore
./dir1/file2
./dir3/file10
./dir3/file8
./dir3/file9
./file1
./file3
./file4

帮助我!

  1. 检查通用帮助信息。
  2. 检查每个命令的帮助信息。
$ kic
Usage:
    kic <Command>

Description:
    Keep your directories clean

Command:
    help    # Display usage for each command
    version # Display the version of this software
    init    # Register current directory, i.e. create ".kic" directory
    config  # Change "config.toml" file's contents
    ignore  # Change "ignore" file's contents
    sweep   # Move dust files and empty directories into "warehouse" directory
    burn    # Delete expired directories in "warehouse" directory
    start   # Start automatic "sweep" and "burn" (UNIX-like: cron, Windows: ?)
    end     # End automatic "sweep" and "burn" (UNIX-like: cron, Windows: ?)
    destroy # Unregister current directory, i.e. delete ".kic" directory
    patrol  # Keep your "cron" file clean (UNIX-like only)

$ kic help config
Usage:
    kic config set <Key> <Value>
    kic config init

Description:
    Change "config.toml" file's contents

Command:
    set  # Set parameters related to "sweep" and "burn" commands
    init # Initialize "config.toml" file

Keys:
    burn.moratorium  # Moratorium to delete directories in "warehouse"
    sweep.moratorium # Moratorium to Move "dust"s into "warehouse"
    sweep.period     # Period to Move "dust"s by automatic "sweep"
    sweep.time       # Time to Move "dust"s by automatic "sweep"

再见,"kic"!

  1. $kic destroy
  2. 如果您已留下,检查cron内容是否已删除。
$ kic start
INFO: Read cron
INFO: Read "config.toml" file
INFO: Get the parameter for "sweep.period"
INFO: Get the parameter for "sweep.time"
INFO: Set new cron

$ kic destroy
CAUTION: Do you want to clear all files related to "kic"? [yes/no]: y
INFO: Delete ".kic" directory
INFO: Read cron
INFO: Set new cron

$ crontab -l
### There is no contents

依赖关系

~6.5MB
~121K SLoC