14-1. CLI¶
CLI의 대상 해석과 두 실행 경로를 본다.
CLI 진입점과 대상 해석¶
코드 분석: main¶
파일: crates/omnigraph-cli/src/main.rs, main.rs L8-L26
바이너리가 라이브러리 함수에 일을 넘기는 방식을 본다.
fn main() -> Result<std::process::ExitCode> {
let runtime = Runtime::new()?;
// A refusal explains its fix without environment or source-location footers.
color_eyre::config::HookBuilder::default()
.display_env_section(false)
.display_location_section(false)
.install()?;
let code = match runtime.block_on(omnigraph_cli::app::run_from(std::env::args_os())) {
Ok(completion) => completion.code(),
Err(error) => {
// Reported as returning it from `main` would, but with the exit
// its outcome chose rather than 1, which says nothing changed.
let _ = writeln!(std::io::stderr(), "Error: {error:?}");
omnigraph_cli::app::exit_code(&error)
}
};
finish_runtime(runtime, code);
Ok(std::process::ExitCode::from(code as u8))
}
main 은 Tokio 런타임을 만들고 app::run_from 에 인자를 넘기는 것이 전부다. 실패해도 종료 코드는 1 로 고정하지 않고 오류가 고른 코드를 쓴다. 실패한 경우 런타임을 shutdown_background 로 닫아, 시간 초과된 DNS 조회 같은 블로킹 작업을 기다리지 않는다 (main.rs L28-L39). run_from 은 프로세스 훅 없이 호출할 수 있는 라이브러리 함수라 테스트와 임베딩에서도 같은 경로를 쓴다. 인자 파싱이 거절되면 사용법 오류 코드로 끝낸다 (app.rs L45-L79).
명령별 요구 사항¶
CLI 는 명령마다 어떤 종류의 대상이 필요한지를 자격 증명이나 I/O 전에 정한다. planes.rs 의 머리 주석이 이 match 가 백엔드 가용성을 소유한다고 적는다 (planes.rs L1-L2). Requirement 열거형은 열다섯 가지다 (planes.rs L8-L36).
| Requirement | 뜻 | 예시 명령 |
|---|---|---|
Local |
대상 없이 로컬에서 끝남 | catalogue, version, cluster validate |
Graph { direct_only: false } |
직접 그래프 또는 서버의 그래프 | query, mutate, changes, optimize |
Graph { direct_only: true } |
직접 저장소만 | cleanup, index, schema plan |
Standalone |
독립 그래프 루트만 | init, upgrade, schema apply |
ServedGraph |
서버의 한 그래프, HTTP 로만 | mcp |
Deployment |
클러스터 배포 대상 | cluster plan, cluster apply |
코드 분석: requirement 의 직접 전용 명령¶
파일: crates/omnigraph-cli/src/planes.rs, planes.rs L89-L104
서버를 거치지 않는 명령이 어떻게 표시되는지 본다.
Command::Init(_)
| Command::Upgrade(_)
| Command::Schema {
command: SchemaCommand::Apply { .. } | SchemaCommand::UpgradeSystemColumns { .. },
} => Standalone,
Command::Schema {
command: SchemaCommand::Plan { .. },
}
| Command::Index { .. }
| Command::RebuildFullTextIndexes(_)
| Command::Cleanup(_) => Graph {
direct_only: true,
root: true,
catalogue: false,
},
13장의 정리와 업그레이드가 여기서 서버 대상이 아닌 직접 저장소 명령으로 묶인다. 반면 optimize 는 서버 대상이면 서버의 기록된 작업(recorded operation)으로 실행된다 (planes.rs L82-L88). 서버가 내려간 클러스터를 다룰 수 있도록, 인라인 s3:// 클러스터 루트를 받는 명령은 배포 계열과 직접 유지보수 명령으로 제한된다 (planes.rs L184-L202).
코드 분석: scope::resolve¶
파일: crates/omnigraph-cli/src/scope.rs, scope.rs L12-L22
대상 하나를 확정하는 함수의 시작을 본다.
pub(crate) fn resolve(
cli: &Cli,
input: &InvocationConfig,
cwd: &Path,
explicit_config: bool,
stderr: &mut dyn Write,
) -> Result<Option<ResolvedTarget>> {
planes::guard_addressing(cli)
.map_err(|e| targets::refusal("argument_conflict", e.to_string()))?;
let need = planes::requirement(&cli.command);
if need == Requirement::Local {
머리 주석은 "Capture exactly one destination. Command inputs never participate in routing." 이다 (scope.rs L1). 대상은 init 의 위치 인자, --target, 별칭, defaults.target 순으로 고른다. 아무것도 없으면 target_required 로 거절하고 기본값으로 넘어가지 않는다 (scope.rs L93-L113). 확정된 대상은 TargetBinding 다섯 형태 중 하나다 (targets.rs L288-L305).
| TargetBinding | 뜻 |
|---|---|
DirectGraph |
그래프 하나의 저장소 URI |
DirectRoot |
클러스터 루트, --graph 나 기본 그래프로 하나를 고름 |
DirectCatalogue |
바이너리 자체 레지스트리를 읽는 카탈로그 범위 |
Server |
HTTP 엔드포인트와 토큰 |
Managed |
관리형 계정과 클러스터 |
사용자 문서는 질의 파일이나 폴더 맥락이 목적지를 바꾸지 못하고, 잘못된 명시 대상은 기본값으로 대체되지 않고 실패한다고 적는다 (cli/index.md).
CLI 의 두 실행 경로¶
대상이 정해지면 그래프 명령은 GraphClient 로 실행된다 (client.rs L204-L231). RFC 0009 가 임베디드 경로와 원격 경로를 이 열거형 하나로 합쳤다. 명령 계층을 평면(plane)으로 나누자던 RFC 0010 은 RFC 0011 로 대체되었다.
| 변형 | 연결 | 행위자 결정 |
|---|---|---|
Embedded |
프로세스 안에서 엔진을 직접 엶 | 쓰기에서 해석한 --as 행위자, 클러스터 그래프면 적용된 정책 설치 |
Remote |
HTTP 서버에 요청 | 서버가 토큰에서 결정, 클라이언트는 신원을 정하지 않음 |
Catalogue |
저장소 없이 카탈로그 범위만 | 읽기 질의와 schema show 만 허용 |
코드 분석: poll_changes_page 의 임베디드 가지¶
파일: crates/omnigraph-cli/src/client.rs, client.rs L1179-L1198
같은 명령이 직접 대상일 때 엔진 API 를 그대로 부르는 모습을 본다.
let position = if let Some(token) = page_token {
omnigraph::changes::ChangeFeedPosition::PageToken(token.to_string())
} else if let Some(cursor) = cursor {
omnigraph::changes::ChangeFeedPosition::Cursor(cursor.to_string())
} else {
omnigraph::changes::ChangeFeedPosition::Start(parse_change_feed_start(
start.unwrap_or("now"),
)?)
};
let page = session
.poll_change_feed(omnigraph::changes::ChangeFeedRequest {
branch: branch.map(str::to_string),
position,
scope: change_scope(filter.kinds, filter.types, filter.ops),
max_changes: limit,
max_bytes: None,
max_commits: None,
})
.await?;
Ok(change_feed_output(&page))
임베디드 가지는 읽기 세션을 열어 13장의 poll_change_feed 를 부르고, 결과를 서버와 같은 출력 DTO 로 바꾼다. Remote 가지는 같은 인자를 쿼리 문자열로 만들어 GET .../changes 를 보낸다 (client.rs L1131-L1173). 두 가지가 같은 출력 타입을 돌려주므로 렌더링 코드는 대상 종류를 모른다. 시작 위치가 없으면 둘 다 now 로 시작하고, 피드를 처음부터 읽으려면 beginning 을 명시해야 한다.