-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathscript.rs
More file actions
289 lines (251 loc) · 9.3 KB
/
script.rs
File metadata and controls
289 lines (251 loc) · 9.3 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use super::Helper;
use crate::auth;
use crate::error::GwsError;
use crate::executor;
use anyhow::Context;
use clap::{Arg, ArgMatches, Command};
use serde_json::json;
use std::fs;
use std::future::Future;
use std::path::Path;
use std::pin::Pin;
pub struct ScriptHelper;
impl Helper for ScriptHelper {
fn inject_commands(
&self,
mut cmd: Command,
_doc: &crate::discovery::RestDescription,
) -> Command {
cmd = cmd.subcommand(
Command::new("+push")
.about("[Helper] Upload local files to an Apps Script project")
.arg(
Arg::new("script")
.long("script")
.help("Script Project ID")
.required(true)
.value_name("ID"),
)
.arg(
Arg::new("dir")
.long("dir")
.help("Directory containing script files (defaults to current dir)")
.value_name("DIR"),
)
.after_help(
"\
EXAMPLES:
gws script +push --script SCRIPT_ID
gws script +push --script SCRIPT_ID --dir ./src
TIPS:
Supports .gs, .js, .html, and appsscript.json files.
Skips hidden files and node_modules automatically.
This replaces ALL files in the project.",
),
);
cmd
}
fn handle<'a>(
&'a self,
doc: &'a crate::discovery::RestDescription,
matches: &'a ArgMatches,
_sanitize_config: &'a crate::helpers::modelarmor::SanitizeConfig,
) -> Pin<Box<dyn Future<Output = Result<bool, GwsError>> + Send + 'a>> {
Box::pin(async move {
if let Some(matches) = matches.subcommand_matches("+push") {
let script_id = matches.get_one::<String>("script").unwrap();
let dir_path = matches
.get_one::<String>("dir")
.map(|s| s.as_str())
.unwrap_or(".");
let safe_dir = crate::validate::validate_safe_dir_path(dir_path)?;
let mut files = Vec::new();
visit_dirs(&safe_dir, &mut files)?;
if files.is_empty() {
return Err(GwsError::Validation(format!(
"No eligible files found in '{}'",
dir_path
)));
}
// Find method: projects.updateContent
let projects_res = doc.resources.get("projects").ok_or_else(|| {
GwsError::Discovery("Resource 'projects' not found".to_string())
})?;
let update_method = projects_res.methods.get("updateContent").ok_or_else(|| {
GwsError::Discovery("Method 'projects.updateContent' not found".to_string())
})?;
// Build body
let body = json!({
"files": files
});
let body_str = body.to_string();
let scopes: Vec<&str> = update_method.scopes.iter().map(|s| s.as_str()).collect();
let (token, auth_method) = match auth::get_token(&scopes).await {
Ok(t) => (Some(t), executor::AuthMethod::OAuth),
Err(_) => (None, executor::AuthMethod::None),
};
let params = json!({
"scriptId": script_id
});
let params_str = params.to_string();
executor::execute_method(
doc,
update_method,
Some(¶ms_str),
Some(&body_str),
token.as_deref(),
auth_method,
None,
None,
matches.get_flag("dry-run"),
&executor::PaginationConfig::default(),
None,
&crate::helpers::modelarmor::SanitizeMode::Warn,
&crate::formatter::OutputFormat::default(),
false
)
.await?;
return Ok(true);
}
Ok(false)
})
}
}
fn visit_dirs(dir: &Path, files: &mut Vec<serde_json::Value>) -> Result<(), GwsError> {
if dir.is_dir() {
for entry in fs::read_dir(dir).context("Failed to read dir")? {
let entry = entry.context("Failed to read entry")?;
let path = entry.path();
if path.is_dir() {
visit_dirs(&path, files)?;
} else if let Some(file_obj) = process_file(&path)? {
files.push(file_obj);
}
}
}
Ok(())
}
fn process_file(path: &Path) -> Result<Option<serde_json::Value>, GwsError> {
let filename = path.file_name().and_then(|s| s.to_str()).unwrap_or("");
let extension = path.extension().and_then(|s| s.to_str()).unwrap_or("");
// Skip hidden files, node_modules, .git, etc. (basic filtering)
if filename.starts_with('.') || path.components().any(|c| c.as_os_str() == "node_modules") {
return Ok(None);
}
let (type_val, name_val) = match extension {
"gs" | "js" => (
"SERVER_JS",
filename.trim_end_matches(".js").trim_end_matches(".gs"),
),
"html" => ("HTML", filename.trim_end_matches(".html")),
"json" => {
if filename == "appsscript.json" {
("JSON", "appsscript")
} else {
return Ok(None);
}
}
_ => return Ok(None),
};
let content = fs::read_to_string(path).map_err(|e| {
GwsError::Validation(format!("Failed to read file '{}': {}", path.display(), e))
})?;
Ok(Some(json!({
"name": name_val,
"type": type_val,
"source": content
})))
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs::File;
use std::io::Write;
use tempfile::tempdir;
#[test]
fn test_process_file_server_js() {
let dir = tempdir().unwrap();
let file_path = dir.path().join("code.gs");
let mut file = File::create(&file_path).unwrap();
writeln!(file, "function foo() {{}}").unwrap();
let result = process_file(&file_path).unwrap().unwrap();
assert_eq!(result["name"], "code");
assert_eq!(result["type"], "SERVER_JS");
assert_eq!(
result["source"].as_str().unwrap().trim(),
"function foo() {}"
);
}
#[test]
fn test_process_file_html() {
let dir = tempdir().unwrap();
let file_path = dir.path().join("index.html");
let mut file = File::create(&file_path).unwrap();
writeln!(file, "<html></html>").unwrap();
let result = process_file(&file_path).unwrap().unwrap();
assert_eq!(result["name"], "index");
assert_eq!(result["type"], "HTML");
}
#[test]
fn test_process_file_appsscript_json() {
let dir = tempdir().unwrap();
let file_path = dir.path().join("appsscript.json");
let mut file = File::create(&file_path).unwrap();
writeln!(file, "{{}}").unwrap();
let result = process_file(&file_path).unwrap().unwrap();
assert_eq!(result["name"], "appsscript");
assert_eq!(result["type"], "JSON");
}
#[test]
fn test_process_file_ignored() {
let dir = tempdir().unwrap();
// Random JSON
let p1 = dir.path().join("other.json");
File::create(&p1).unwrap();
assert!(process_file(&p1).unwrap().is_none());
// Hidden file
let p2 = dir.path().join(".hidden.gs");
File::create(&p2).unwrap();
assert!(process_file(&p2).unwrap().is_none());
// node_modules
let node_modules = dir.path().join("node_modules");
fs::create_dir(&node_modules).unwrap();
let p3 = node_modules.join("dep.gs");
File::create(&p3).unwrap();
assert!(process_file(&p3).unwrap().is_none());
}
#[test]
fn test_visit_dirs() {
let dir = tempdir().unwrap();
// Root file
let f1 = dir.path().join("root.gs");
File::create(&f1).unwrap();
// Subdir file
let sub = dir.path().join("src");
fs::create_dir(&sub).unwrap();
let f2 = sub.join("utils.js");
File::create(&f2).unwrap();
// Ignored file
let f3 = dir.path().join("ignore.txt");
File::create(&f3).unwrap();
let mut files = Vec::new();
visit_dirs(dir.path(), &mut files).unwrap();
assert_eq!(files.len(), 2);
let names: Vec<&str> = files.iter().map(|f| f["name"].as_str().unwrap()).collect();
assert!(names.contains(&"root"));
assert!(names.contains(&"utils"));
}
}