16 个版本 (3 个稳定版本)
1.1.0 | 2024 年 4 月 25 日 |
---|---|
1.0.1 | 2023 年 11 月 13 日 |
1.0.0 | 2023 年 8 月 8 日 |
0.6.0 | 2022 年 11 月 28 日 |
0.1.3 | 2018 年 3 月 8 日 |
#88 在 命令行工具
31KB
527 行
Projector,一个用于管理多个项目的 CLI
我参与了很多 FOSS 项目,这实际上是我的工作的一部分。不幸的是,这意味着移动我的项目目录(特别是我的 GOPATH)会非常繁琐。因此,我编写了这个 CLI 来帮助我查找和管理我的庞大的 Git 仓库文件夹。Projector 可以看作是无需设置的 mr (myrepos) 版本。但比那更灵活。
安装
您可以通过 cargo 从 crates.io 安装 projector
cargo install projector
或者您可以使用以下命令本地构建它
git clone https://github.com/chasinglogic/projector $PATH_TO_PROJECTOR
cd $PATH_TO_PROJECTOR
cargo install --path .
用法
projector 0.2.0
Mathew Robinson <chasinglogic@gmail.com>
USAGE:
projector [OPTIONS] [SUBCOMMAND]
FLAGS:
-h, --help Prints help information
-V, --version Prints version information
OPTIONS:
-c, --code-dir <CODE_DIR> The root of where to search for projects. Also can be
configured using the environment variable CODE_DIR.
Default: ~/Code
-e, --exclude <PATTERN> A regex which will be used to exclude directories from commands.
-i, --include <PATTERN> A regex which will be used to include directories from commands. Overrides
excludes so if a directory is matched by an exclude pattern and an include
pattern the directory will be included.
SUBCOMMANDS:
help Prints this message or the help of the given subcommand(s)
list
run
定义
Projector 将任何 Git 仓库视为“项目”。在未来的版本中,我将扩展这个定义,但对我目前来说,这就是它的用法。此外,projector 假设您所有的代码/项目都在一个目录下。对我而言,我使用 ~/Code
。
Projector 有两个功能: list
和 run
。
列表
list
将打印出您代码目录下的项目列表。
您可能会想知道,我会用这个做什么?答案是快速移动!
您可以在您的 bashrc 中创建一个 bash 函数,如下所示
function sp() {
cd $(projector list | grep -i $1)
}
现在,如果您source您的 bashrc 并输入 sp $name_of_a_project
,您将立即转到您的项目目录。例如
Users/chasinglogic λ . .bashrc
Users/chasinglogic λ sp projector
Code/projector master λ sp dfm
chasinglogic/dfm master λ pwd
/Users/chasinglogic/Code/go/src/github.com/chasinglogic/dfm
chasinglogic/dfm master λ
可选地,如果您还有另一个我喜欢的工具 FZF,您可以进一步将其变为模糊搜索您的项目列表
function sp() {
cd $(projector list | fzf)
}
现在您会得到以下内容
/Users/chasinglogic/Code/mongodb/mongo-tools
/Users/chasinglogic/Code/mongodb/kernel-tools
/Users/chasinglogic/Code/mongodb/evergreen-packer
/Users/chasinglogic/Code/mongodb/chef-repo
/Users/chasinglogic/Code/mongodb/mongo-release-tools
/Users/chasinglogic/Code/website
/Users/chasinglogic/Code/python/praelatus
/Users/chasinglogic/Code/python/praelatus/.venv/src/elasticutils
/Users/chasinglogic/Code/python/projector
/Users/chasinglogic/Code/archive/projector
/Users/chasinglogic/Code/projector
/Users/chasinglogic/Code/go/src/github.com/pkg/errors
/Users/chasinglogic/Code/go/src/github.com/praelatus/praelatus
/Users/chasinglogic/Code/go/src/github.com/praelatus/praelatus-poc
/Users/chasinglogic/Code/go/src/github.com/chasinglogic/dfm
/Users/chasinglogic/Code/go/src/github.com/chasinglogic/fireplace
/Users/chasinglogic/Code/go/src/github.com/mattn/go-zglob
/Users/chasinglogic/Code/go/src/github.com/josharian/impl
/Users/chasinglogic/Code/go/src/github.com/jstemmer/gotags
/Users/chasinglogic/Code/go/src/github.com/dgrijalva/jwt-go
/Users/chasinglogic/Code/go/src/github.com/fatih/motion
/Users/chasinglogic/Code/go/src/github.com/fatih/gomodifytags
/Users/chasinglogic/Code/go/src/github.com/dominikh/go-tools
/Users/chasinglogic/Code/go/src/github.com/goreleaser/goreleaser
/Users/chasinglogic/Code/go/src/github.com/goreleaser/archive
/Users/chasinglogic/Code/go/src/github.com/nsf/gocode
/Users/chasinglogic/Code/go/src/github.com/davidrjenni/reftools
/Users/chasinglogic/Code/go/src/github.com/alecthomas/gometalinter
/Users/chasinglogic/Code/go/src/github.com/kisielk/errcheck
/Users/chasinglogic/Code/go/src/github.com/kisielk/gotool
/Users/chasinglogic/Code/go/src/github.com/zmb3/gogetdoc
/Users/chasinglogic/Code/go/src/github.com/google/go-github
/Users/chasinglogic/Code/go/src/github.com/google/go-querystring
/Users/chasinglogic/Code/go/src/github.com/golang/lint
/Users/chasinglogic/Code/go/src/github.com/rogpeppe/godef
/Users/chasinglogic/Code/go/src/github.com/apex/log
/Users/chasinglogic/Code/go/src/github.com/urfave/cli
/Users/chasinglogic/Code/go/src/github.com/klauspost/asmfmt
/Users/chasinglogic/Code/go/src/github.com/gorilla/mux
/Users/chasinglogic/Code/go/src/golang.org/x/sync
/Users/chasinglogic/Code/go/src/golang.org/x/oauth2
/Users/chasinglogic/Code/go/src/golang.org/x/net
/Users/chasinglogic/Code/go/src/golang.org/x/tools
/Users/chasinglogic/Code/go/src/golang.org/x/crypto
/Users/chasinglogic/Code/go/src/honnef.co/go/tools
/Users/chasinglogic/Code/go/src/gopkg.in/mgo.v2
> /Users/chasinglogic/Code/go/src/gopkg.in/yaml.v2
56/56
>
然后您可以使用 FZF 的出色界面进行搜索和选择
> /Users/chasinglogic/Code/archive/projector
/Users/chasinglogic/Code/python/projector
/Users/chasinglogic/Code/projector
3/56
> projector
输入并 voila!
Users/chasinglogic λ sp
Code/projector master λ pwd
/Users/chasinglogic/Code/projector
Code/projector master λ
运行
运行允许您在所有项目中运行 shell 命令。例如,如果您想在每个项目中运行 git status
Code/projector master λ projector run git status
nothing to commit, working tree clean
On branch emacs-26
Your branch is up-to-date with 'origin/emacs-26'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
update.sh
nothing added to commit but untracked files present (use "git add" to track)
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: Cargo.lock
no changes added to commit (use "git add" and/or "git commit -a")
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working tree clean
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working tree clean
Code/projector master λ
您传递给程序的任何标志都会传递给程序,因此您可以像平常一样输入命令,无需奇怪的 shell 引号!
一些设置(可选)需要
投影仪无需任何设置即可使用,前提是您的代码位于 ~/Code
或已设置 $CODE_DIR
环境变量。然而,每个优秀的命令行界面都为高级用户提供了某些配置,投影仪也不例外。主要的配置集中在包含和排除项目目录。例如,我在我的 $GOPATH
/Users/mathewrobinson/Code/go/src/github.com/goreleaser/nfpm
/Users/mathewrobinson/Code/go/src/github.com/goreleaser/archive
/Users/mathewrobinson/Code/go/src/github.com/goreleaser/goreleaser
/Users/mathewrobinson/Code/go/src/github.com/dominikh/go-tools
/Users/mathewrobinson/Code/go/src/github.com/ramya-rao-a/go-outline
/Users/mathewrobinson/Code/go/src/github.com/fatih/gomodifytags
/Users/mathewrobinson/Code/go/src/github.com/fatih/color
/Users/mathewrobinson/Code/go/src/github.com/fatih/motion
/Users/mathewrobinson/Code/go/src/github.com/evergreen-ci/evergreen
/Users/mathewrobinson/Code/go/src/github.com/derekparker/delve
/Users/mathewrobinson/Code/go/src/github.com/mdempsky/unconvert
/Users/mathewrobinson/Code/go/src/github.com/jstemmer/gotags
/Users/mathewrobinson/Code/go/src/github.com/josharian/impl
/Users/mathewrobinson/Code/go/src/github.com/MichaelTJones/walk
/Users/mathewrobinson/Code/go/src/github.com/mattn/go-isatty
/Users/mathewrobinson/Code/go/src/github.com/mattn/go-zglob
/Users/mathewrobinson/Code/go/src/github.com/mattn/go-colorable
/Users/mathewrobinson/Code/go/src/github.com/uudashr/gopkgs
/Users/mathewrobinson/Code/go/src/github.com/chasinglogic/licensure
... (list truncated for brevity)
其中大多数都不是我的,所以我不希望它们出现在我的投影仪输出中或在运行 projector run
时被使用。我可以使用 --exclude
标志,它支持由 regex 包接受的正则表达式来排除 go 目录。例如
projector --exclude '.*go.*' list
但这也会排除我的 go 源目录。使用 --include
标志,我可以添加一个正则表达式,如果它与包含正则表达式匹配,并且与排除正则表达式匹配,则包含该目录。此功能存在是因为 regex 包不支持前瞻/后顾。因此,新命令是
projector --exclude '.*go.*' --include '.*github.com/chasinglogic.*' list
这是一个每次都要输入的繁琐命令,所以你可以创建一个别名,或者创建一个位于 ~/.projector.yml
的配置文件,如下所示
---
code_dir: ~/Code
includes:
- .*github.com/chasinglogic.*
- .*github.com/mongodb.*
excludes:
- .*go.*
这做的是同样的事情。在此配置文件中,包括和排除都是正则表达式的列表,它们将被进行或运算。任何匹配排除模式的内容都会被排除,除非它也匹配包含模式。
在配置文件内部需要 code_dir。
许可证
Copyright 2018 Mathew Robinson <[email protected]>. All rights reserved.
Use of this source code is governed by the GPLv3 license that can be found in
the LICENSE file.
依赖项
~5–17MB
~173K SLoC