diff options
| -rw-r--r-- | locales/help_docs/en.yml | 7 | ||||
| -rw-r--r-- | locales/help_docs/zh-CN.yml | 9 | ||||
| -rw-r--r--[-rwxr-xr-x] | scripts/completion_jv.sh | 26 | ||||
| -rw-r--r-- | src/bin/jv.rs | 72 |
4 files changed, 110 insertions, 4 deletions
diff --git a/locales/help_docs/en.yml b/locales/help_docs/en.yml index a4dbaa6..820da3a 100644 --- a/locales/help_docs/en.yml +++ b/locales/help_docs/en.yml @@ -169,7 +169,8 @@ jv: no_account_set: | Current workspace account is `unknown`, meaning no account is set - Use `jv as <account_name>` to set account for workspace + Use `jv as <account_name>` to set account for workspace + or use `jv login <account_name> <upstream_address>` to directly login to the upstream vault account_not_exist: | The account `%{account}` set for the current workspace is not registered on your computer @@ -189,6 +190,7 @@ jv: jv [in|out] - Import or export files [REMOTE] **UPSTREAM VAULT**: + login <ACCOUNT> <UPSTREAM> - Login to upstream vault [REMOTE] direct <UPSTREAM> - Direct workspace to upstream vault [REMOTE] unstain - Unstain workspace, clear association update - Download latest information [REMOTE] @@ -411,6 +413,9 @@ jv: Built-in documentation includes JustEnoughVCS usage guides, collaboration paradigms, and best practices. confirm: + login: | + You are logging into upstream vault %{upstream} as %{account}, please confirm + unstain: | This operation will disconnect the current workspace from the upstream vault `%{upstream}` If you reconnect to a vault with a mismatched identifier, it will cause serious problems, please operate with caution! diff --git a/locales/help_docs/zh-CN.yml b/locales/help_docs/zh-CN.yml index 2bd91ee..922514b 100644 --- a/locales/help_docs/zh-CN.yml +++ b/locales/help_docs/zh-CN.yml @@ -161,7 +161,8 @@ jv: no_account_set: | 当前工作区的账户为 `unknown`,这意味着您并未设置账户 - 使用 `jv as <账户名称>` 来为工作区指定账户 + 使用 `jv as <账户名称>` 来为工作区指定账户 + 或使用 `jv login <账户名称> <上游地址>` 来关联至上游库 account_not_exist: | 当前工作区设置的账户 `%{account}` 并未注册至您的计算机 @@ -178,7 +179,8 @@ jv: jv u 下载最新信息,jv t 追踪文件,jv a 对齐文件结构到表,jv in/out 导入或导出文件 **上游库**: - direct <上游地址> - 定向到工作区到上游库 [远程] + login <账户> <地址> - 设置账户、定向并获得上游信息 [远程] + direct <地址> - 定向到工作区到上游库 [远程] unstain - 祛色工作区,清除关联 update - 同步最新的信息 [远程] @@ -414,6 +416,9 @@ jv: 内建文档包含 JustEnoughVCS 的使用指南、协作范式和最佳实践 confirm: + login: | + 您正在以 %{account} 身份登陆上游库 %{upstream},请确认 + unstain: | 此操作将会断开当前工作区与上游库 `%{upstream}` 的关联 若重新连接至标识不匹配的库,会导致严重的问题,请谨慎操作! diff --git a/scripts/completion_jv.sh b/scripts/completion_jv.sh index 0532bad..51bd242 100755..100644 --- a/scripts/completion_jv.sh +++ b/scripts/completion_jv.sh @@ -14,7 +14,7 @@ _jv_completion() { local subsubcmd="${words[2]}" # Subcommands - local base_commands="create init direct unstain account update sheet status here import export in out move mv docs exit use sheets accounts as make drop track hold throw" + local base_commands="create init direct unstain account update sheet status here import export in out move mv docs exit use sheets accounts as make drop track hold throw login" # Subcommands - Account local account_commands="list as add remove movekey mvkey mvk help" @@ -87,6 +87,30 @@ _jv_completion() { return 0 fi + # Completion login + if [[ "$subcmd" == "login" ]]; then + if [[ $cword -eq 2 ]]; then + local accounts + accounts=$($cmd account list --raw 2>/dev/null) + COMPREPLY=($(compgen -W "$accounts" -- "$cur")) + elif [[ $cword -eq 3 ]]; then + local ip_history + ip_history=$($cmd _ip_history 2>/dev/null) + COMPREPLY=($(compgen -W "$ip_history" -- "$cur")) + fi + return 0 + fi + + # Completion direct + if [[ "$subcmd" == "direct" ]]; then + if [[ $cword -eq 2 ]]; then + local ip_history + ip_history=$($cmd _ip_history 2>/dev/null) + COMPREPLY=($(compgen -W "$ip_history" -- "$cur")) + fi + return 0 + fi + # aliases case "$subcmd" in "as") diff --git a/src/bin/jv.rs b/src/bin/jv.rs index 0fcd7d5..e8e3c64 100644 --- a/src/bin/jv.rs +++ b/src/bin/jv.rs @@ -180,6 +180,14 @@ enum JustEnoughVcsWorkspaceCommand { /// Drop a sheet Drop(SheetDropArgs), + + /// As Member, Direct, and Update + #[command(alias = "signin")] + Login(LoginArgs), + + // Completion Helpers + #[command(name = "_ip_history")] + HistoryIpAddress, } #[derive(Parser, Debug)] @@ -302,6 +310,23 @@ struct SheetDropArgs { } #[derive(Parser, Debug)] +struct LoginArgs { + /// Show help information + #[arg(short, long)] + help: bool, + + /// Whether to skip confirmation + #[arg(short = 'C', long)] + confirm: bool, + + /// Member ID + login_member_id: MemberId, + + /// Upstream + upstream: String, +} + +#[derive(Parser, Debug)] struct CreateWorkspaceArgs { /// Show help information #[arg(short, long)] @@ -860,6 +885,53 @@ async fn main() { JustEnoughVcsWorkspaceCommand::Drop(args) => { jv_sheet_drop(args).await; } + JustEnoughVcsWorkspaceCommand::Login(args) => { + if !args.confirm { + println!( + "{}", + t!( + "jv.confirm.login", + account = args.login_member_id, + upstream = args.upstream + ) + .trim() + .yellow() + ); + confirm_hint_or(t!("common.confirm"), || exit(1)).await; + } + + let user_dir = match UserDirectory::current_doc_dir() { + Some(dir) => dir, + None => { + eprintln!("{}", t!("jv.fail.account.no_user_dir").red()); + return; + } + }; + + jv_account_as( + user_dir, + SetLocalWorkspaceAccountArgs { + help: false, + account_name: args.login_member_id, + }, + ) + .await; + + jv_direct(DirectArgs { + help: false, + upstream: Some(args.upstream.clone()), + confirm: true, + }) + .await; + + jv_update(UpdateArgs { help: false }).await; + } + JustEnoughVcsWorkspaceCommand::HistoryIpAddress => { + get_recent_ip_address() + .await + .iter() + .for_each(|ip| println!("{}", ip)); + } } } |
