-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
106 lines (99 loc) · 3.91 KB
/
Copy pathmain.rs
File metadata and controls
106 lines (99 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
mod auth;
mod command;
mod config;
mod connections;
mod init;
mod query;
mod results;
mod skill;
mod tables;
mod util;
mod workspace;
use anstyle::AnsiColor;
use clap::{Parser, builder::Styles};
use command::{AuthCommands, Commands, ConnectionsCommands, SkillCommands, TablesCommands, WorkspaceCommands};
#[derive(Parser)]
#[command(name = "hotdata", version, about = concat!("HotData CLI - Command line interface for HotData (v", env!("CARGO_PKG_VERSION"), ")"), long_about = None, disable_version_flag = true)]
#[command(styles=get_styles())]
struct Cli {
/// Print version
#[arg(short = 'v', short_aliases = ['V'], long, action = clap::ArgAction::Version)]
version: Option<bool>,
#[command(subcommand)]
command: Option<Commands>,
}
fn resolve_workspace(provided: Option<String>) -> String {
match config::load("default") {
Ok(profile) => match config::resolve_workspace_id(provided, &profile) {
Ok(id) => id,
Err(e) => {
eprintln!("error: {e}");
std::process::exit(1);
}
},
Err(e) => {
eprintln!("{e}");
std::process::exit(1);
}
}
}
fn main() {
dotenvy::dotenv().ok();
let cli = Cli::parse();
match cli.command {
None => {
use clap::CommandFactory;
Cli::command().print_help().unwrap();
println!();
}
Some(cmd) => match cmd {
Commands::Init => init::run(),
Commands::Info => eprintln!("not yet implemented"),
Commands::Auth { command } => match command {
AuthCommands::Login => auth::login(),
AuthCommands::Status { profile } => auth::status(&profile),
_ => eprintln!("not yet implemented"),
},
Commands::Datasets { .. } => eprintln!("not yet implemented"),
Commands::Query { sql, workspace_id, connection, format } => {
let workspace_id = resolve_workspace(workspace_id);
query::execute(&sql, &workspace_id, connection.as_deref(), &format)
}
Commands::Profile { .. } => eprintln!("not yet implemented"),
Commands::Workspaces { command } => match command {
WorkspaceCommands::List { format } => workspace::list(&format),
_ => eprintln!("not yet implemented"),
},
Commands::Connections { command } => match command {
ConnectionsCommands::List { workspace_id, format } => {
let workspace_id = resolve_workspace(workspace_id);
connections::list(&workspace_id, &format)
}
_ => eprintln!("not yet implemented"),
},
Commands::Tables { command } => match command {
TablesCommands::List { workspace_id, connection_id, schema, table, limit, cursor, format } => {
let workspace_id = resolve_workspace(workspace_id);
tables::list(&workspace_id, connection_id.as_deref(), schema.as_deref(), table.as_deref(), limit, cursor.as_deref(), &format)
}
},
Commands::Skill { command } => match command {
SkillCommands::Install { project } => {
if project { skill::install_project() } else { skill::install() }
}
SkillCommands::Status => skill::status(),
},
Commands::Results { result_id, workspace_id, format } => {
let workspace_id = resolve_workspace(workspace_id);
results::get(&result_id, &workspace_id, &format)
}
},
}
}
pub fn get_styles() -> clap::builder::Styles {
Styles::styled()
.header(AnsiColor::Yellow.on_default())
.usage(AnsiColor::Green.on_default())
.literal(AnsiColor::Green.on_default())
.placeholder(AnsiColor::Green.on_default())
}