Repository navigation
Expand file tree
/
Copy pathstackql.rs
More file actions
127 lines (107 loc) · 3.84 KB
/
Copy pathstackql.rs
File metadata and controls
127 lines (107 loc) · 3.84 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// utils/stackql.rs
//! # StackQL Utility Module
//!
//! This module provides functionalities for interacting with the `stackql` binary,
//! such as retrieving version information, installed providers, and the binary path.
//! It serves as a bridge between your Rust application and the StackQL CLI tool.
//!
//! ## Features
//! - Retrieve `stackql` binary version and SHA information.
//! - List installed StackQL providers.
//! - Get the path to the `stackql` binary.
//!
//! ## Example Usage
//! ```rust
//! use crate::utils::stackql::{get_version, get_installed_providers, get_stackql_path};
//!
//! if let Ok(version_info) = get_version() {
//! println!("StackQL Version: {}, SHA: {}", version_info.version, version_info.sha);
//! }
//!
//! if let Ok(providers) = get_installed_providers() {
//! for provider in providers {
//! println!("Provider: {}, Version: {}", provider.name, provider.version);
//! }
//! }
//!
//! if let Some(path) = get_stackql_path() {
//! println!("StackQL Binary Path: {:?}", path);
//! }
//! ```
use std::path::PathBuf;
use std::process::Command as ProcessCommand;
use crate::utils::binary::get_binary_path;
/// Holds version information retrieved from the `stackql` binary.
pub struct VersionInfo {
pub version: String,
pub sha: String,
}
/// Represents a provider installed in the `stackql` environment.
pub struct Provider {
pub name: String,
pub version: String,
}
/// Retrieves the version and SHA information of the `stackql` binary.
pub fn get_version() -> Result<VersionInfo, String> {
let binary_path = match get_binary_path() {
Some(path) => path,
_none => return Err("StackQL binary not found".to_string()),
};
let output = match ProcessCommand::new(&binary_path).arg("--version").output() {
Ok(output) => output,
Err(e) => return Err(format!("Failed to execute stackql: {}", e)),
};
if !output.status.success() {
return Err("Failed to get version information".to_string());
}
let output_str = String::from_utf8_lossy(&output.stdout);
let version_line = match output_str.lines().next() {
Some(line) => line,
_none => return Err("Empty version output".to_string()),
};
let tokens: Vec<&str> = version_line.split_whitespace().collect();
if tokens.len() < 4 {
return Err("Unexpected version format".to_string());
}
let version = tokens[1].to_string();
let sha = tokens[3].replace("(", "").replace(")", "");
Ok(VersionInfo { version, sha })
}
/// Retrieves a list of installed StackQL providers.
pub fn get_installed_providers() -> Result<Vec<Provider>, String> {
let binary_path = match get_binary_path() {
Some(path) => path,
_none => return Err("StackQL binary not found".to_string()),
};
let output = match ProcessCommand::new(&binary_path)
.arg("exec")
.arg("SHOW PROVIDERS")
.output()
{
Ok(output) => output,
Err(e) => return Err(format!("Failed to execute stackql: {}", e)),
};
if !output.status.success() {
return Err("Failed to get providers information".to_string());
}
let output_str = String::from_utf8_lossy(&output.stdout);
let mut providers = Vec::new();
for line in output_str.lines() {
if line.contains("name") || line.contains("----") {
continue;
}
let fields: Vec<&str> = line.split('|').collect();
if fields.len() >= 3 {
let name = fields[1].trim().to_string();
let version = fields[2].trim().to_string();
if !name.is_empty() && name != "name" && !name.contains("----") {
providers.push(Provider { name, version });
}
}
}
Ok(providers)
}
/// Retrieves the path to the `stackql` binary.
pub fn get_stackql_path() -> Option<PathBuf> {
get_binary_path()
}