Skip to content

Commit 648a486

Browse files
authored
feat(cli): devbase shell-rc サブコマンドを追加 (#11)
* feat(cli): devbase shell-rc サブコマンドを追加 `devbase init` が書き込んだ rc ファイルのパスを stdout に 1 行出力する ユーティリティサブコマンド。`source "$(devbase shell-rc)"` のように コマンド置換と組み合わせて使い、ユーザーに環境(zsh / bash on Linux / bash on macOS)の判定を意識させずに rc を再読み込みできるようにする。 ロジックは既存の `get_shell_rc_file()` を再利用。DEVBASE_ROOT は不要。 - lib/devbase/commands/shell.py: 新規(cmd_shell_rc) - lib/devbase/cli.py: subparser 登録、prefix 解決リスト、dispatch (DEVBASE_ROOT 不要グループに配置) - bin/devbase: bash dispatcher の commands 一覧と case 分岐 - etc/devbase-completion.bash: bash 補完 - etc/_devbase: zsh 補完 * docs(shell-rc): cli-reference / README / getting-started を更新し commands/shell.py をリネーム PR #11 のレビュー指摘 (Codex) への対応。 - docs/user/cli-reference.md: `devbase shell-rc` セクションを追加。 mermaid 図と環境別出力テーブル、`source "$(...)"` の引用符必須注記を含める。 - README.md: クイックスタートを `source "$(./bin/devbase shell-rc)"` ワンライナーに変更し、`devbase build` 行を削除(`devbase up` が 自動でビルド/pull するため)。トップレベルコマンド一覧に shell-rc 追記。 - docs/user/getting-started.md: Step 3 の bash/zsh ケース分岐を `source "$(./bin/devbase shell-rc)"` に統一。引用符必須の注記を追加。 - lib/devbase/commands/shell.py を shell_rc.py にリネーム。 utils/shell.py との名前衝突による検索・補完時の混乱を回避(Codex N-2)。
1 parent e31b82d commit 648a486

8 files changed

Lines changed: 67 additions & 14 deletions

File tree

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ devbaseは、Docker Composeを使った再現性の高い開発環境を提供
2323
git clone https://github.com/devbasex/devbase.git
2424
cd devbase
2525
./bin/devbase init
26-
source ~/.bashrc # または ~/.zshrc
26+
source "$(./bin/devbase shell-rc)" # rc ファイル(zsh/bash on Linux/macOS)を自動判定して再読み込み
2727

2828
# 2. Pluginのインストール
2929
devbase plugin repo add user/repo # リポジトリ登録(init でサンプルレジストリ devbasex/devbase-samples は自動登録済み)
@@ -32,8 +32,7 @@ devbase plugin install <name> # Plugin名でインストール
3232
# 3. プロジェクトの起動
3333
cd projects/your-project
3434
devbase env init # 環境変数の設定(初回のみ)
35-
devbase build # コンテナイメージのビルド(初回のみ)
36-
devbase up # コンテナを起動
35+
devbase up # コンテナを起動(初回はイメージビルドを含むため時間がかかります)
3736
devbase login # コンテナにログイン
3837
```
3938

@@ -83,7 +82,7 @@ devbaseのコマンドは4つのグループにまとめられています。
8382

8483
- **ショートカット**: `up`, `down`, `login`, `build`, `ps` はトップレベルから直接使用可能
8584
- **プレフィックス略記**: `devbase p l``devbase plugin list`
86-
- **トップレベルコマンド**: `init`, `status`
85+
- **トップレベルコマンド**: `init`, `status`, `shell-rc`
8786

8887
全コマンドの構文・オプション・使用例は [CLIリファレンス](docs/user/cli-reference.md) を参照してください。
8988

bin/devbase

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ run_python() {
163163
# Resolve abbreviated command to full command name via unique prefix matching
164164
resolve_command() {
165165
local input="$1"
166-
local commands="init status container ct env plugin pl snapshot ss up down login build ps scale help"
166+
local commands="init status shell-rc container ct env plugin pl snapshot ss up down login build ps scale help"
167167
local matches=()
168168
for cmd in $commands; do
169169
[[ "$cmd" == "$input"* ]] && matches+=("$cmd")
@@ -186,7 +186,7 @@ case "$_resolved_cmd" in
186186
# Python-implemented commands
187187
--version|-V)
188188
run_python "$@" ;;
189-
init|status|container|ct|env|plugin|pl|snapshot|ss|up|down|login|ps|scale)
189+
init|status|shell-rc|container|ct|env|plugin|pl|snapshot|ss|up|down|login|ps|scale)
190190
run_python "${_resolved_cmd}" "${@:2}" ;;
191191
# Shell-implemented commands
192192
build) shift; cmd_build "$@" ;;

docs/user/cli-reference.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ devbase のコマンドは 4 つのグループとトップレベルコマンド
1010
graph TD
1111
A[devbase] --> B[init]
1212
A --> C[status]
13+
A --> H[shell-rc]
1314
A --> D[container / ct]
1415
A --> E[env]
1516
A --> F[plugin / pl]
@@ -86,6 +87,32 @@ devbase status
8687
- 環境変数の設定状況
8788
- スナップショットの状態
8889

90+
### `devbase shell-rc`
91+
92+
`devbase init` が書き込んだシェル設定ファイル(rc ファイル)のフルパスを stdout に 1 行だけ出力します。`source` のコマンド置換と組み合わせ、ユーザーが zsh / bash on Linux / bash on macOS のどれを使っているかを意識せずに rc ファイルを再読み込みするためのユーティリティです。
93+
94+
```
95+
devbase shell-rc
96+
```
97+
98+
判定ロジックは `devbase init` と同一なので、書き込み先と完全に一致します。
99+
100+
| 環境 | 出力例 |
101+
|------|--------|
102+
| zsh | `/Users/<user>/.zshrc` |
103+
| bash on macOS | `/Users/<user>/.bash_profile` |
104+
| bash on Linux | `/home/<user>/.bashrc` |
105+
106+
使用例:
107+
108+
```bash
109+
# 初期化直後に rc を再読み込み(環境差を吸収)
110+
./bin/devbase init
111+
source "$(./bin/devbase shell-rc)"
112+
```
113+
114+
> **⚠ 引用符は必須**: `source $(devbase shell-rc)` のように引用符を省くと、ホームディレクトリ名に空白を含む環境(例: `/Users/foo bar/.zshrc`)で word splitting が起き `source` が失敗します。必ず `source "$(devbase shell-rc)"` の形で書いてください。
115+
89116
## container (ct) グループ
90117

91118
コンテナのライフサイクル管理を行うコマンド群です。

docs/user/getting-started.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,22 @@ cd devbase
3434

3535
`init` コマンドは以下を自動実行します。
3636

37-
- `bin/devbase` を PATH に追加(`~/.bashrc` / `~/.zshrc` に追記
37+
- `bin/devbase` を PATH に追加(環境に応じて `~/.zshrc` / `~/.bashrc` / `~/.bash_profile` のいずれかに追記
3838
- シェル補完スクリプトの登録(Tab 補完が有効になる)
3939
- プラグイン設定ファイル `plugins.yml` の作成
4040

41+
書き込み先は現在のシェル種別と OS から自動判定されます(zsh → `~/.zshrc`、bash on macOS → `~/.bash_profile`、bash on Linux → `~/.bashrc`)。
42+
4143
### 3. シェルの再読み込み
4244

4345
```bash
44-
# Bash の場合
45-
source ~/.bashrc
46-
47-
# Zsh の場合
48-
source ~/.zshrc
46+
source "$(./bin/devbase shell-rc)"
4947
```
5048

49+
`devbase shell-rc``init` が書き込んだ rc ファイルのパスを 1 行で出力します。コマンド置換と組み合わせることで、環境差を意識せずに 1 行で再読み込みできます。
50+
51+
> **⚠ 引用符は必須**: `source $(./bin/devbase shell-rc)` のように引用符を省くと、ホームディレクトリ名に空白を含む環境(例: `/Users/foo bar/.zshrc`)で word splitting が起き `source` が失敗します。必ず `source "$(...)"` の形で書いてください。
52+
5153
> **Note:** 新しいターミナルを開いても同様に反映されます。
5254
5355
### 4. プラグインリポジトリの登録

etc/_devbase

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ _devbase() {
3838
commands=(
3939
'init:Initialize devbase environment'
4040
'status:Show overall status'
41+
'shell-rc:Print shell RC file path (for source ...)'
4142
'container:Manage containers'
4243
'ct:Manage containers (alias)'
4344
'env:Manage environment variables'

etc/devbase-completion.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ _devbase_completions() {
1010
cword=$COMP_CWORD
1111
}
1212

13-
local commands="init status container ct env plugin pl snapshot ss up down login build ps help"
13+
local commands="init status shell-rc container ct env plugin pl snapshot ss up down login build ps help"
1414
local container_subcommands="up down ps login logs scale build"
1515
local env_subcommands="init sync list set get delete edit project"
1616
local plugin_subcommands="list install uninstall update info sync repo"

lib/devbase/cli.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,10 @@ def _create_parser():
236236
# --- Top-level commands ---
237237
subparsers.add_parser('init', help='Initialize devbase environment')
238238
subparsers.add_parser('status', help='Show overall status')
239+
subparsers.add_parser(
240+
'shell-rc',
241+
help='Print shell RC file path (e.g. source "$(devbase shell-rc)")'
242+
)
239243

240244
_add_container_parser(subparsers)
241245
_add_env_parser(subparsers)
@@ -258,7 +262,7 @@ def _resolve_prefix(input_cmd, candidates):
258262

259263
def _expand_argv():
260264
"""Expand abbreviated command/subcommand names in sys.argv in-place."""
261-
commands = ['init', 'status', 'container', 'ct', 'env', 'plugin', 'pl',
265+
commands = ['init', 'status', 'shell-rc', 'container', 'ct', 'env', 'plugin', 'pl',
262266
'snapshot', 'ss', 'up', 'down', 'login', 'build', 'ps', 'scale', 'help']
263267
repo_subcmds = ['add', 'remove', 'list', 'refresh']
264268

@@ -315,6 +319,11 @@ def _dispatch(cmd, args):
315319
from devbase.commands.container import cmd_container
316320
return cmd_container(args)
317321

322+
# --- Commands not requiring DEVBASE_ROOT ---
323+
if cmd == 'shell-rc':
324+
from devbase.commands.shell_rc import cmd_shell_rc
325+
return cmd_shell_rc()
326+
318327
# --- Commands requiring DEVBASE_ROOT ---
319328
devbase_root = _require_devbase_root()
320329

lib/devbase/commands/shell_rc.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""Shell-related utility commands"""
2+
3+
from devbase.utils.shell import get_shell_rc_file
4+
5+
6+
def cmd_shell_rc() -> int:
7+
"""Print the appropriate shell RC file path to stdout (single line).
8+
9+
Intended for `source "$(devbase shell-rc)"` so users can reload the
10+
file `devbase init` wrote to without needing to know which one
11+
(zsh -> ~/.zshrc, bash on macOS -> ~/.bash_profile,
12+
bash on Linux -> ~/.bashrc).
13+
"""
14+
print(get_shell_rc_file())
15+
return 0

0 commit comments

Comments
 (0)