| 610 |
800
|
|
std::path::PathBuf::from(std::env::var("HOME").unwrap_or_else(|_| ".".to_string()))
|
| 611 |
801
|
|
}
|
| 612 |
802
|
|
|
|
803
|
+ |
// ---------------------------------------------------------------------------
|
|
804
|
+ |
// tracker: issues, projects, milestones
|
|
805
|
+ |
// ---------------------------------------------------------------------------
|
|
806
|
+ |
|
|
807
|
+ |
const API_BASE: &str = "https://openagents.com/api/v1";
|
|
808
|
+ |
|
|
809
|
+ |
/// Unwrap a client result, or print the server's own refusal and exit non-zero.
|
|
810
|
+ |
///
|
|
811
|
+ |
/// Every tracker, box, and memory command ends here rather than in an
|
|
812
|
+ |
/// `unwrap_or_default`. That is the whole difference between reporting what the
|
|
813
|
+ |
/// server said and printing an empty list that reads as "there is nothing".
|
|
814
|
+ |
fn or_fail<T>(result: Result<T, crate::tracker::ApiError>) -> T {
|
|
815
|
+ |
match result {
|
|
816
|
+ |
Ok(value) => value,
|
|
817
|
+ |
Err(error) => fail(&error.to_string()),
|
|
818
|
+ |
}
|
|
819
|
+ |
}
|
|
820
|
+ |
|
|
821
|
+ |
/// Print the server's body verbatim under `--json`, or the human lines.
|
|
822
|
+ |
fn emit(json: bool, value: &serde_json::Value, human: &[String]) {
|
|
823
|
+ |
if json {
|
|
824
|
+ |
match serde_json::to_string_pretty(value) {
|
|
825
|
+ |
Ok(text) => println!("{}", text),
|
|
826
|
+ |
Err(error) => fail(&format!("Could not render JSON: {}", error)),
|
|
827
|
+ |
}
|
|
828
|
+ |
} else {
|
|
829
|
+ |
for line in human {
|
|
830
|
+ |
println!("{}", line);
|
|
831
|
+ |
}
|
|
832
|
+ |
}
|
|
833
|
+ |
}
|
|
834
|
+ |
|
|
835
|
+ |
fn field(value: &serde_json::Value, key: &str) -> String {
|
|
836
|
+ |
match value.get(key) {
|
|
837
|
+ |
Some(serde_json::Value::String(text)) => text.clone(),
|
|
838
|
+ |
Some(serde_json::Value::Null) | None => String::new(),
|
|
839
|
+ |
Some(other) => other.to_string(),
|
|
840
|
+ |
}
|
|
841
|
+ |
}
|
|
842
|
+ |
|
|
843
|
+ |
/// The names inside an array of objects, or of strings.
|
|
844
|
+ |
fn names(value: Option<&serde_json::Value>, key: &str) -> Vec<String> {
|
|
845
|
+ |
value
|
|
846
|
+ |
.and_then(serde_json::Value::as_array)
|
|
847
|
+ |
.map(|items| {
|
|
848
|
+ |
items
|
|
849
|
+ |
.iter()
|
|
850
|
+ |
.map(|item| match item {
|
|
851
|
+ |
serde_json::Value::String(text) => text.clone(),
|
|
852
|
+ |
other => field(other, key),
|
|
853
|
+ |
})
|
|
854
|
+ |
.collect()
|
|
855
|
+ |
})
|
|
856
|
+ |
.unwrap_or_default()
|
|
857
|
+ |
}
|
|
858
|
+ |
|
|
859
|
+ |
fn or_none(values: &[String]) -> String {
|
|
860
|
+ |
if values.is_empty() {
|
|
861
|
+ |
"none".to_string()
|
|
862
|
+ |
} else {
|
|
863
|
+ |
values.join(", ")
|
|
864
|
+ |
}
|
|
865
|
+ |
}
|
|
866
|
+ |
|
|
867
|
+ |
fn issue_references(value: Option<&serde_json::Value>) -> Vec<String> {
|
|
868
|
+ |
value
|
|
869
|
+ |
.and_then(serde_json::Value::as_array)
|
|
870
|
+ |
.map(|items| {
|
|
871
|
+ |
items
|
|
872
|
+ |
.iter()
|
|
873
|
+ |
.map(|item| {
|
|
874
|
+ |
format!(
|
|
875
|
+ |
"#{}",
|
|
876
|
+ |
item.get("number")
|
|
877
|
+ |
.map(|n| n.to_string())
|
|
878
|
+ |
.unwrap_or_else(|| "?".to_string())
|
|
879
|
+ |
)
|
|
880
|
+ |
})
|
|
881
|
+ |
.collect()
|
|
882
|
+ |
})
|
|
883
|
+ |
.unwrap_or_default()
|
|
884
|
+ |
}
|
|
885
|
+ |
|
|
886
|
+ |
fn number_or_question(value: &serde_json::Value, key: &str) -> String {
|
|
887
|
+ |
value
|
|
888
|
+ |
.get(key)
|
|
889
|
+ |
.filter(|v| !v.is_null())
|
|
890
|
+ |
.map(|v| v.to_string())
|
|
891
|
+ |
.unwrap_or_else(|| "?".to_string())
|
|
892
|
+ |
}
|
|
893
|
+ |
|
|
894
|
+ |
fn pad(text: &str, width: usize) -> String {
|
|
895
|
+ |
let mut out = text.to_string();
|
|
896
|
+ |
while out.chars().count() < width {
|
|
897
|
+ |
out.push(' ');
|
|
898
|
+ |
}
|
|
899
|
+ |
out
|
|
900
|
+ |
}
|
|
901
|
+ |
|
|
902
|
+ |
fn issue_row(issue: &serde_json::Value) -> String {
|
|
903
|
+ |
let extension = issue.get("openagents").cloned().unwrap_or(serde_json::Value::Null);
|
|
904
|
+ |
let labels = names(issue.get("labels"), "name");
|
|
905
|
+ |
format!(
|
|
906
|
+ |
"{}{}{}{}{}",
|
|
907
|
+ |
pad(&format!("#{}", number_or_question(issue, "number")), 7),
|
|
908
|
+ |
pad(&field(issue, "state"), 8),
|
|
909
|
+ |
field(issue, "title"),
|
|
910
|
+ |
if labels.is_empty() {
|
|
911
|
+ |
String::new()
|
|
912
|
+ |
} else {
|
|
913
|
+ |
format!(" ({})", labels.join(", "))
|
|
914
|
+ |
},
|
|
915
|
+ |
if extension.get("blocked") == Some(&serde_json::Value::Bool(true)) {
|
|
916
|
+ |
" [blocked]"
|
|
917
|
+ |
} else {
|
|
918
|
+ |
""
|
|
919
|
+ |
}
|
|
920
|
+ |
)
|
|
921
|
+ |
}
|
|
922
|
+ |
|
|
923
|
+ |
fn issue_view_human(issue: &serde_json::Value) -> Vec<String> {
|
|
924
|
+ |
let extension = issue.get("openagents").cloned().unwrap_or(serde_json::Value::Null);
|
|
925
|
+ |
let milestone = issue.get("milestone").cloned().unwrap_or(serde_json::Value::Null);
|
|
926
|
+ |
let author = issue
|
|
927
|
+ |
.get("user")
|
|
928
|
+ |
.map(|u| field(u, "login"))
|
|
929
|
+ |
.filter(|s| !s.is_empty())
|
|
930
|
+ |
.unwrap_or_else(|| "unknown".to_string());
|
|
931
|
+ |
let milestone_title = field(&milestone, "title");
|
|
932
|
+ |
vec![
|
|
933
|
+ |
format!(
|
|
934
|
+ |
"#{} {}",
|
|
935
|
+ |
number_or_question(issue, "number"),
|
|
936
|
+ |
field(issue, "title")
|
|
937
|
+ |
),
|
|
938
|
+ |
format!("State: {}", field(issue, "state")),
|
|
939
|
+ |
format!("Author: {}", author),
|
|
940
|
+ |
format!("Labels: {}", or_none(&names(issue.get("labels"), "name"))),
|
|
941
|
+ |
format!(
|
|
942
|
+ |
"Assignees: {}",
|
|
943
|
+ |
or_none(&names(issue.get("assignees"), "login"))
|
|
944
|
+ |
),
|
|
945
|
+ |
format!(
|
|
946
|
+ |
"Milestone: {}",
|
|
947
|
+ |
if milestone_title.is_empty() {
|
|
948
|
+ |
"none".to_string()
|
|
949
|
+ |
} else {
|
|
950
|
+ |
milestone_title
|
|
951
|
+ |
}
|
|
952
|
+ |
),
|
|
953
|
+ |
format!(
|
|
954
|
+ |
"Progress: {}",
|
|
955
|
+ |
{
|
|
956
|
+ |
let progress = field(&extension, "progress");
|
|
957
|
+ |
if progress.is_empty() { "unknown".to_string() } else { progress }
|
|
958
|
+ |
}
|
|
959
|
+ |
),
|
|
960
|
+ |
format!(
|
|
961
|
+ |
"Blocked: {}",
|
|
962
|
+ |
if extension.get("blocked") == Some(&serde_json::Value::Bool(true)) {
|
|
963
|
+ |
"yes"
|
|
964
|
+ |
} else {
|
|
965
|
+ |
"no"
|
|
966
|
+ |
}
|
|
967
|
+ |
),
|
|
968
|
+ |
format!(
|
|
969
|
+ |
"Blocked by: {}",
|
|
970
|
+ |
or_none(&issue_references(extension.get("blocked_by")))
|
|
971
|
+ |
),
|
|
972
|
+ |
format!("Blocks: {}", or_none(&issue_references(extension.get("blocks")))),
|
|
973
|
+ |
String::new(),
|
|
974
|
+ |
field(issue, "body"),
|
|
975
|
+ |
]
|
|
976
|
+ |
}
|
|
977
|
+ |
|
|
978
|
+ |
fn comment_thread_human(value: &serde_json::Value) -> Vec<String> {
|
|
979
|
+ |
let comments = value
|
|
980
|
+ |
.get("comments")
|
|
981
|
+ |
.and_then(serde_json::Value::as_array)
|
|
982
|
+ |
.cloned()
|
|
983
|
+ |
.unwrap_or_default();
|
|
984
|
+ |
if comments.is_empty() {
|
|
985
|
+ |
return vec![String::new(), "No comments.".to_string()];
|
|
986
|
+ |
}
|
|
987
|
+ |
let mut lines = vec![String::new(), format!("Comments ({}):", comments.len())];
|
|
988
|
+ |
for comment in &comments {
|
|
989
|
+ |
let author = comment
|
|
990
|
+ |
.get("user")
|
|
991
|
+ |
.map(|u| field(u, "login"))
|
|
992
|
+ |
.filter(|s| !s.is_empty())
|
|
993
|
+ |
.unwrap_or_else(|| "unknown".to_string());
|
|
994
|
+ |
lines.push(format!("- {}: {}", author, field(comment, "body")));
|
|
995
|
+ |
}
|
|
996
|
+ |
lines
|
|
997
|
+ |
}
|
|
998
|
+ |
|
|
999
|
+ |
fn dependency_human(graph: &serde_json::Value) -> Vec<String> {
|
|
1000
|
+ |
let edges = |key: &str| -> Vec<String> {
|
|
1001
|
+ |
graph
|
|
1002
|
+ |
.get(key)
|
|
1003
|
+ |
.and_then(serde_json::Value::as_array)
|
|
1004
|
+ |
.map(|items| {
|
|
1005
|
+ |
items
|
|
1006
|
+ |
.iter()
|
|
1007
|
+ |
.map(|entry| {
|
|
1008
|
+ |
format!(
|
|
1009
|
+ |
" #{} {} {}",
|
|
1010
|
+ |
number_or_question(entry, "number"),
|
|
1011
|
+ |
field(entry, "state"),
|
|
1012
|
+ |
field(entry, "title")
|
|
1013
|
+ |
)
|
|
1014
|
+ |
})
|
|
1015
|
+ |
.collect()
|
|
1016
|
+ |
})
|
|
1017
|
+ |
.unwrap_or_default()
|
|
1018
|
+ |
};
|
|
1019
|
+ |
let blocked_by = edges("blocked_by");
|
|
1020
|
+ |
let blocks = edges("blocks");
|
|
1021
|
+ |
let mut lines = vec![format!(
|
|
1022
|
+ |
"Blocked: {}",
|
|
1023
|
+ |
if graph.get("blocked") == Some(&serde_json::Value::Bool(true)) {
|
|
1024
|
+ |
"yes"
|
|
1025
|
+ |
} else {
|
|
1026
|
+ |
"no"
|
|
1027
|
+ |
}
|
|
1028
|
+ |
)];
|
|
1029
|
+ |
lines.push("Blocked by:".to_string());
|
|
1030
|
+ |
if blocked_by.is_empty() {
|
|
1031
|
+ |
lines.push(" none".to_string());
|
|
1032
|
+ |
} else {
|
|
1033
|
+ |
lines.extend(blocked_by);
|
|
1034
|
+ |
}
|
|
1035
|
+ |
lines.push("Blocks:".to_string());
|
|
1036
|
+ |
if blocks.is_empty() {
|
|
1037
|
+ |
lines.push(" none".to_string());
|
|
1038
|
+ |
} else {
|
|
1039
|
+ |
lines.extend(blocks);
|
|
1040
|
+ |
}
|
|
1041
|
+ |
lines
|
|
1042
|
+ |
}
|
|
1043
|
+ |
|
|
1044
|
+ |
/// Reads `--body` or `--body-file`, where `-` is standard input.
|
|
1045
|
+ |
fn resolve_body(body: Option<String>, body_file: Option<String>) -> Option<String> {
|
|
1046
|
+ |
match (body, body_file) {
|
|
1047
|
+ |
(Some(_), Some(_)) => fail("Use either --body or --body-file, not both."),
|
|
1048
|
+ |
(Some(text), None) => Some(text),
|
|
1049
|
+ |
(None, Some(path)) => {
|
|
1050
|
+ |
if path == "-" {
|
|
1051
|
+ |
use std::io::Read;
|
|
1052
|
+ |
let mut buffer = String::new();
|
|
1053
|
+ |
if let Err(error) = std::io::stdin().read_to_string(&mut buffer) {
|
|
1054
|
+ |
fail(&format!("Could not read the body from standard input: {}", error));
|
|
1055
|
+ |
}
|
|
1056
|
+ |
Some(buffer)
|
|
1057
|
+ |
} else {
|
|
1058
|
+ |
match std::fs::read_to_string(&path) {
|
|
1059
|
+ |
Ok(text) => Some(text),
|
|
1060
|
+ |
Err(error) => fail(&format!("Could not read {}: {}", path, error)),
|
|
1061
|
+ |
}
|
|
1062
|
+ |
}
|
|
1063
|
+ |
}
|
|
1064
|
+ |
(None, None) => None,
|
|
1065
|
+ |
}
|
|
1066
|
+ |
}
|
|
1067
|
+ |
|
|
1068
|
+ |
fn target_or_fail(repo: Option<String>) -> crate::tracker::RepoTarget {
|
|
1069
|
+ |
or_fail(crate::tracker::resolve_repo_target(repo.as_deref()))
|
|
1070
|
+ |
}
|
|
1071
|
+ |
|
|
1072
|
+ |
/// `FIELD=VALUE` pairs into the object the project routes take.
|
|
1073
|
+ |
fn parse_field_values(pairs: &[String]) -> serde_json::Value {
|
|
1074
|
+ |
let mut map = serde_json::Map::new();
|
|
1075
|
+ |
for pair in pairs {
|
|
1076
|
+ |
match pair.split_once('=') {
|
|
1077
|
+ |
Some((field, value)) if !field.trim().is_empty() => {
|
|
1078
|
+ |
map.insert(field.trim().to_string(), serde_json::json!(value));
|
|
1079
|
+ |
}
|
|
1080
|
+ |
_ => fail(&format!(
|
|
1081
|
+ |
"`{}` is not a field assignment. Pass --set FIELD=VALUE.",
|
|
1082
|
+ |
pair
|
|
1083
|
+ |
)),
|
|
1084
|
+ |
}
|
|
1085
|
+ |
}
|
|
1086
|
+ |
serde_json::Value::Object(map)
|
|
1087
|
+ |
}
|
|
1088
|
+ |
|
|
1089
|
+ |
async fn run_issue(action: IssueAction, token: Option<String>, json: bool) {
|
|
1090
|
+ |
let tracker = crate::tracker::TrackerClient::new(API_BASE, token);
|
|
1091
|
+ |
match action {
|
|
1092
|
+ |
IssueAction::List {
|
|
1093
|
+ |
repo,
|
|
1094
|
+ |
state,
|
|
1095
|
+ |
label,
|
|
1096
|
+ |
assignee,
|
|
1097
|
+ |
milestone,
|
|
1098
|
+ |
search,
|
|
1099
|
+ |
blocked,
|
|
1100
|
+ |
limit,
|
|
1101
|
+ |
} => {
|
|
1102
|
+ |
let target = target_or_fail(repo);
|
|
1103
|
+ |
let options = crate::tracker::IssueListOptions {
|
|
1104
|
+ |
limit,
|
|
1105
|
+ |
state: Some(state),
|
|
1106
|
+ |
label,
|
|
1107
|
+ |
assignee,
|
|
1108
|
+ |
milestone,
|
|
1109
|
+ |
search,
|
|
1110
|
+ |
blocked,
|
|
1111
|
+ |
};
|
|
1112
|
+ |
let result = or_fail(tracker.list_issues(&target, &options).await);
|
|
1113
|
+ |
let value = serde_json::json!({
|
|
1114
|
+ |
"pagination": result.pagination,
|
|
1115
|
+ |
"issues": result.issues,
|
|
1116
|
+ |
});
|
|
1117
|
+ |
let mut human: Vec<String> = if result.issues.is_empty() {
|
|
1118
|
+ |
vec!["No issues found.".to_string()]
|
|
1119
|
+ |
} else {
|
|
1120
|
+ |
result.issues.iter().map(issue_row).collect()
|
|
1121
|
+ |
};
|
|
1122
|
+ |
if !result.issues.is_empty() {
|
|
1123
|
+ |
human.push(String::new());
|
|
1124
|
+ |
human.push(match result.pagination.get("total").and_then(|t| t.as_u64()) {
|
|
1125
|
+ |
Some(total) => format!("Showing {} of {} issues.", result.issues.len(), total),
|
|
1126
|
+ |
None => format!("Showing {} issues.", result.issues.len()),
|
|
1127
|
+ |
});
|
|
1128
|
+ |
}
|
|
1129
|
+ |
emit(json, &value, &human);
|
|
1130
|
+ |
}
|
|
1131
|
+ |
IssueAction::View {
|
|
1132
|
+ |
number,
|
|
1133
|
+ |
repo,
|
|
1134
|
+ |
comments,
|
|
1135
|
+ |
} => {
|
|
1136
|
+ |
let target = target_or_fail(repo);
|
|
1137
|
+ |
let issue = or_fail(tracker.view_issue(&target, number).await);
|
|
1138
|
+ |
if !comments {
|
|
1139
|
+ |
emit(json, &issue, &issue_view_human(&issue));
|
|
1140
|
+ |
} else {
|
|
1141
|
+ |
let thread = or_fail(tracker.list_comments(&target, number).await);
|
|
1142
|
+ |
let mut human = issue_view_human(&issue);
|
|
1143
|
+ |
human.extend(comment_thread_human(&thread));
|
|
1144
|
+ |
let value = serde_json::json!({ "issue": issue, "comments": thread });
|
|
1145
|
+ |
emit(json, &value, &human);
|
|
1146
|
+ |
}
|
|
1147
|
+ |
}
|
|
1148
|
+ |
IssueAction::Create {
|
|
1149
|
+ |
title,
|
|
1150
|
+ |
body,
|
|
1151
|
+ |
body_file,
|
|
1152
|
+ |
label,
|
|
1153
|
+ |
assignee,
|
|
1154
|
+ |
milestone,
|
|
1155
|
+ |
repo,
|
|
1156
|
+ |
} => {
|
|
1157
|
+ |
let target = target_or_fail(repo);
|
|
1158
|
+ |
let text = resolve_body(body, body_file);
|
|
1159
|
+ |
let created = or_fail(
|
|
1160
|
+ |
tracker
|
|
1161
|
+ |
.create_issue(
|
|
1162
|
+ |
&target,
|
|
1163
|
+ |
&title,
|
|
1164
|
+ |
text.as_deref(),
|
|
1165
|
+ |
&label,
|
|
1166
|
+ |
&assignee,
|
|
1167
|
+ |
milestone,
|
|
1168
|
+ |
)
|
|
1169
|
+ |
.await,
|
|
1170
|
+ |
);
|
|
1171
|
+ |
emit(
|
|
1172
|
+ |
json,
|
|
1173
|
+ |
&created,
|
|
1174
|
+ |
&[format!(
|
|
1175
|
+ |
"Created issue #{} {}",
|
|
1176
|
+ |
number_or_question(&created, "number"),
|
|
1177
|
+ |
field(&created, "title")
|
|
1178
|
+ |
)],
|
|
1179
|
+ |
);
|
|
1180
|
+ |
}
|
|
1181
|
+ |
IssueAction::Close {
|
|
1182
|
+ |
number,
|
|
1183
|
+ |
repo,
|
|
1184
|
+ |
comment,
|
|
1185
|
+ |
} => {
|
|
1186
|
+ |
let target = target_or_fail(repo);
|
|
1187
|
+ |
if let Some(text) = comment {
|
|
1188
|
+ |
or_fail(tracker.comment_issue(&target, number, &text).await);
|
|
1189
|
+ |
}
|
|
1190
|
+ |
let issue = or_fail(tracker.set_issue_state(&target, number, "closed").await);
|
|
1191
|
+ |
emit(
|
|
1192
|
+ |
json,
|
|
1193
|
+ |
&issue,
|
|
1194
|
+ |
&[format!(
|
|
1195
|
+ |
"Closed issue #{} ({}).",
|
|
1196
|
+ |
number_or_question(&issue, "number"),
|
|
1197
|
+ |
field(&issue, "state")
|
|
1198
|
+ |
)],
|
|
1199
|
+ |
);
|
|
1200
|
+ |
}
|
|
1201
|
+ |
IssueAction::Reopen {
|
|
1202
|
+ |
number,
|
|
1203
|
+ |
repo,
|
|
1204
|
+ |
comment,
|
|
1205
|
+ |
} => {
|
|
1206
|
+ |
let target = target_or_fail(repo);
|
|
1207
|
+ |
if let Some(text) = comment {
|
|
1208
|
+ |
or_fail(tracker.comment_issue(&target, number, &text).await);
|
|
1209
|
+ |
}
|
|
1210
|
+ |
let issue = or_fail(tracker.set_issue_state(&target, number, "open").await);
|
|
1211
|
+ |
emit(
|
|
1212
|
+ |
json,
|
|
1213
|
+ |
&issue,
|
|
1214
|
+ |
&[format!(
|
|
1215
|
+ |
"Reopened issue #{} ({}).",
|
|
1216
|
+ |
number_or_question(&issue, "number"),
|
|
1217
|
+ |
field(&issue, "state")
|
|
1218
|
+ |
)],
|
|
1219
|
+ |
);
|
|
1220
|
+ |
}
|
|
1221
|
+ |
IssueAction::Comment {
|
|
1222
|
+ |
number,
|
|
1223
|
+ |
body,
|
|
1224
|
+ |
body_file,
|
|
1225
|
+ |
repo,
|
|
1226
|
+ |
} => {
|
|
1227
|
+ |
let target = target_or_fail(repo);
|
|
1228
|
+ |
match resolve_body(body, body_file) {
|
|
1229
|
+ |
Some(text) => {
|
|
1230
|
+ |
let comment = or_fail(tracker.comment_issue(&target, number, &text).await);
|
|
1231
|
+ |
emit(
|
|
1232
|
+ |
json,
|
|
1233
|
+ |
&comment,
|
|
1234
|
+ |
&[format!("Commented on #{}.", number)],
|
|
1235
|
+ |
);
|
|
1236
|
+ |
}
|
|
1237
|
+ |
// No body is a read of the thread, which is what the
|
|
1238
|
+ |
// TypeScript CLI does with `issue view --comments`.
|
|
1239
|
+ |
None => {
|
|
1240
|
+ |
let thread = or_fail(tracker.list_comments(&target, number).await);
|
|
1241
|
+ |
emit(json, &thread, &comment_thread_human(&thread));
|
|
1242
|
+ |
}
|
|
1243
|
+ |
}
|
|
1244
|
+ |
}
|
|
1245
|
+ |
IssueAction::Label {
|
|
1246
|
+ |
number,
|
|
1247
|
+ |
add,
|
|
1248
|
+ |
remove,
|
|
1249
|
+ |
repo,
|
|
1250
|
+ |
} => {
|
|
1251
|
+ |
let target = target_or_fail(repo);
|
|
1252
|
+ |
let mut value: Option<serde_json::Value> = None;
|
|
1253
|
+ |
if !add.is_empty() {
|
|
1254
|
+ |
value = Some(or_fail(tracker.add_labels(&target, number, &add).await));
|
|
1255
|
+ |
}
|
|
1256
|
+ |
for name in &remove {
|
|
1257
|
+ |
value = Some(or_fail(tracker.remove_label(&target, number, name).await));
|
|
1258
|
+ |
}
|
|
1259
|
+ |
let applied = match value {
|
|
1260
|
+ |
Some(value) => value,
|
|
1261
|
+ |
None => or_fail(tracker.list_labels(&target, number).await),
|
|
1262
|
+ |
};
|
|
1263
|
+ |
emit(
|
|
1264
|
+ |
json,
|
|
1265
|
+ |
&applied,
|
|
1266
|
+ |
&[format!(
|
|
1267
|
+ |
"Labels: {}",
|
|
1268
|
+ |
or_none(&names(applied.get("labels"), "name"))
|
|
1269
|
+ |
)],
|
|
1270
|
+ |
);
|
|
1271
|
+ |
}
|
|
1272
|
+ |
IssueAction::Assign {
|
|
1273
|
+ |
number,
|
|
1274
|
+ |
logins,
|
|
1275
|
+ |
repo,
|
|
1276
|
+ |
} => {
|
|
1277
|
+ |
let target = target_or_fail(repo);
|
|
1278
|
+ |
let value = or_fail(tracker.add_assignees(&target, number, &logins).await);
|
|
1279
|
+ |
emit(
|
|
1280
|
+ |
json,
|
|
1281
|
+ |
&value,
|
|
1282
|
+ |
&[format!(
|
|
1283
|
+ |
"Assignees: {}",
|
|
1284
|
+ |
or_none(&names(value.get("assignees"), "login"))
|
|
1285
|
+ |
)],
|
|
1286
|
+ |
);
|
|
1287
|
+ |
}
|
|
1288
|
+ |
IssueAction::Unassign {
|
|
1289
|
+ |
number,
|
|
1290
|
+ |
logins,
|
|
1291
|
+ |
repo,
|
|
1292
|
+ |
} => {
|
|
1293
|
+ |
let target = target_or_fail(repo);
|
|
1294
|
+ |
let value = or_fail(tracker.remove_assignees(&target, number, &logins).await);
|
|
1295
|
+ |
emit(
|
|
1296
|
+ |
json,
|
|
1297
|
+ |
&value,
|
|
1298
|
+ |
&[format!(
|
|
1299
|
+ |
"Assignees: {}",
|
|
1300
|
+ |
or_none(&names(value.get("assignees"), "login"))
|
|
1301
|
+ |
)],
|
|
1302
|
+ |
);
|
|
1303
|
+ |
}
|
|
1304
|
+ |
IssueAction::Deps {
|
|
1305
|
+ |
number,
|
|
1306
|
+ |
add,
|
|
1307
|
+ |
remove,
|
|
1308
|
+ |
repo,
|
|
1309
|
+ |
} => {
|
|
1310
|
+ |
let target = target_or_fail(repo);
|
|
1311
|
+ |
let mut value: Option<serde_json::Value> = None;
|
|
1312
|
+ |
if !add.is_empty() {
|
|
1313
|
+ |
value = Some(or_fail(tracker.add_dependencies(&target, number, &add).await));
|
|
1314
|
+ |
}
|
|
1315
|
+ |
for blocked_by in &remove {
|
|
1316
|
+ |
value = Some(or_fail(
|
|
1317
|
+ |
tracker.remove_dependency(&target, number, *blocked_by).await,
|
|
1318
|
+ |
));
|
|
1319
|
+ |
}
|
|
1320
|
+ |
let graph = match value {
|
|
1321
|
+ |
Some(value) => value,
|
|
1322
|
+ |
None => or_fail(tracker.dependencies(&target, number).await),
|
|
1323
|
+ |
};
|
|
1324
|
+ |
emit(json, &graph, &dependency_human(&graph));
|
|
1325
|
+ |
}
|
|
1326
|
+ |
IssueAction::Milestones { repo } => {
|
|
1327
|
+ |
let target = target_or_fail(repo);
|
|
1328
|
+ |
let value = or_fail(tracker.list_milestones(&target).await);
|
|
1329
|
+ |
let rows = value
|
|
1330
|
+ |
.get("milestones")
|
|
1331
|
+ |
.and_then(serde_json::Value::as_array)
|
|
1332
|
+ |
.cloned()
|
|
1333
|
+ |
.unwrap_or_default();
|
|
1334
|
+ |
let human: Vec<String> = if rows.is_empty() {
|
|
1335
|
+ |
vec!["No milestones found.".to_string()]
|
|
1336
|
+ |
} else {
|
|
1337
|
+ |
rows.iter()
|
|
1338
|
+ |
.map(|row| {
|
|
1339
|
+ |
format!(
|
|
1340
|
+ |
"{}{}{}",
|
|
1341
|
+ |
pad(&format!("#{}", number_or_question(row, "number")), 7),
|
|
1342
|
+ |
pad(&field(row, "state"), 8),
|
|
1343
|
+ |
field(row, "title")
|
|
1344
|
+ |
)
|
|
1345
|
+ |
})
|
|
1346
|
+ |
.collect()
|
|
1347
|
+ |
};
|
|
1348
|
+ |
emit(json, &value, &human);
|
|
1349
|
+ |
}
|
|
1350
|
+ |
}
|
|
1351
|
+ |
}
|
|
1352
|
+ |
|
|
1353
|
+ |
fn project_row(project: &serde_json::Value) -> String {
|
|
1354
|
+ |
format!(
|
|
1355
|
+ |
"{}{}{}{}",
|
|
1356
|
+ |
pad(&format!("#{}", number_or_question(project, "number")), 6),
|
|
1357
|
+ |
pad(&field(project, "state"), 8),
|
|
1358
|
+ |
field(project, "title"),
|
|
1359
|
+ |
if project.get("archived") == Some(&serde_json::Value::Bool(true)) {
|
|
1360
|
+ |
" [archived]"
|
|
1361
|
+ |
} else {
|
|
1362
|
+ |
""
|
|
1363
|
+ |
}
|
|
1364
|
+ |
)
|
|
1365
|
+ |
}
|
|
1366
|
+ |
|
|
1367
|
+ |
fn project_items_human(value: &serde_json::Value) -> Vec<String> {
|
|
1368
|
+ |
let items = value
|
|
1369
|
+ |
.get("items")
|
|
1370
|
+ |
.and_then(serde_json::Value::as_array)
|
|
1371
|
+ |
.cloned()
|
|
1372
|
+ |
.unwrap_or_default();
|
|
1373
|
+ |
if items.is_empty() {
|
|
1374
|
+ |
return vec!["No items on this board.".to_string()];
|
|
1375
|
+ |
}
|
|
1376
|
+ |
items
|
|
1377
|
+ |
.iter()
|
|
1378
|
+ |
.map(|item| {
|
|
1379
|
+ |
let issue = item.get("issue").cloned().unwrap_or(serde_json::Value::Null);
|
|
1380
|
+ |
let pairs: Vec<String> = item
|
|
1381
|
+ |
.get("values")
|
|
1382
|
+ |
.and_then(serde_json::Value::as_object)
|
|
1383
|
+ |
.map(|map| {
|
|
1384
|
+ |
map.iter()
|
|
1385
|
+ |
.map(|(field, value)| match value {
|
|
1386
|
+ |
serde_json::Value::String(text) => format!("{}={}", field, text),
|
|
1387
|
+ |
other => format!("{}={}", field, other),
|
|
1388
|
+ |
})
|
|
1389
|
+ |
.collect()
|
|
1390
|
+ |
})
|
|
1391
|
+ |
.unwrap_or_default();
|
|
1392
|
+ |
format!(
|
|
1393
|
+ |
"{} #{} {}",
|
|
1394
|
+ |
pad(&number_or_question(item, "id"), 6),
|
|
1395
|
+ |
number_or_question(&issue, "number"),
|
|
1396
|
+ |
pairs.join(" ")
|
|
1397
|
+ |
)
|
|
1398
|
+ |
})
|
|
1399
|
+ |
.collect()
|
|
1400
|
+ |
}
|
|
1401
|
+ |
|
|
1402
|
+ |
async fn run_project(action: ProjectAction, token: Option<String>, json: bool) {
|
|
1403
|
+ |
let tracker = crate::tracker::TrackerClient::new(API_BASE, token);
|
|
1404
|
+ |
match action {
|
|
1405
|
+ |
ProjectAction::List { repo, archived } => {
|
|
1406
|
+ |
let target = target_or_fail(repo);
|
|
1407
|
+ |
let value = or_fail(tracker.list_projects(&target, archived).await);
|
|
1408
|
+ |
let boards = value
|
|
1409
|
+ |
.get("projects")
|
|
1410
|
+ |
.and_then(serde_json::Value::as_array)
|
|
1411
|
+ |
.cloned()
|
|
1412
|
+ |
.unwrap_or_default();
|
|
1413
|
+ |
let human: Vec<String> = if boards.is_empty() {
|
|
1414
|
+ |
vec!["No projects found.".to_string()]
|
|
1415
|
+ |
} else {
|
|
1416
|
+ |
boards.iter().map(project_row).collect()
|
|
1417
|
+ |
};
|
|
1418
|
+ |
emit(json, &value, &human);
|
|
1419
|
+ |
}
|
|
1420
|
+ |
ProjectAction::View { number, repo } => {
|
|
1421
|
+ |
let target = target_or_fail(repo);
|
|
1422
|
+ |
let project = or_fail(tracker.view_project(&target, number).await);
|
|
1423
|
+ |
let human = vec![
|
|
1424
|
+ |
format!(
|
|
1425
|
+ |
"#{} {}",
|
|
1426
|
+ |
number_or_question(&project, "number"),
|
|
1427
|
+ |
field(&project, "title")
|
|
1428
|
+ |
),
|
|
1429
|
+ |
format!("State: {}", field(&project, "state")),
|
|
1430
|
+ |
format!(
|
|
1431
|
+ |
"Archived: {}",
|
|
1432
|
+ |
if project.get("archived") == Some(&serde_json::Value::Bool(true)) {
|
|
1433
|
+ |
"yes"
|
|
1434
|
+ |
} else {
|
|
1435
|
+ |
"no"
|
|
1436
|
+ |
}
|
|
1437
|
+ |
),
|
|
1438
|
+ |
format!("Owner: {}", {
|
|
1439
|
+ |
let owner = field(&project, "owner");
|
|
1440
|
+ |
if owner.is_empty() { "unknown".to_string() } else { owner }
|
|
1441
|
+ |
}),
|
|
1442
|
+ |
String::new(),
|
|
1443
|
+ |
field(&project, "description"),
|
|
1444
|
+ |
];
|
|
1445
|
+ |
emit(json, &project, &human);
|
|
1446
|
+ |
}
|
|
1447
|
+ |
ProjectAction::Create {
|
|
1448
|
+ |
title,
|
|
1449
|
+ |
description,
|
|
1450
|
+ |
repo,
|
|
1451
|
+ |
} => {
|
|
1452
|
+ |
if title.trim().is_empty() {
|
|
1453
|
+ |
fail("Pass --title with the project title.");
|
|
1454
|
+ |
}
|
|
1455
|
+ |
let target = target_or_fail(repo);
|
|
1456
|
+ |
let project = or_fail(
|
|
1457
|
+ |
tracker
|
|
1458
|
+ |
.create_project(&target, &title, description.as_deref())
|
|
1459
|
+ |
.await,
|
|
1460
|
+ |
);
|
|
1461
|
+ |
emit(
|
|
1462
|
+ |
json,
|
|
1463
|
+ |
&project,
|
|
1464
|
+ |
&[format!(
|
|
1465
|
+ |
"Created project #{} {}",
|
|
1466
|
+ |
number_or_question(&project, "number"),
|
|
1467
|
+ |
field(&project, "title")
|
|
1468
|
+ |
)],
|
|
1469
|
+ |
);
|
|
1470
|
+ |
}
|
|
1471
|
+ |
ProjectAction::Fields { number, repo } => {
|
|
1472
|
+ |
let target = target_or_fail(repo);
|
|
1473
|
+ |
let value = or_fail(tracker.project_fields(&target, number).await);
|
|
1474
|
+ |
let fields = value
|
|
1475
|
+ |
.get("fields")
|
|
1476
|
+ |
.and_then(serde_json::Value::as_array)
|
|
1477
|
+ |
.cloned()
|
|
1478
|
+ |
.unwrap_or_default();
|
|
1479
|
+ |
let human: Vec<String> = if fields.is_empty() {
|
|
1480
|
+ |
vec!["No fields on this board.".to_string()]
|
|
1481
|
+ |
} else {
|
|
1482
|
+ |
fields
|
|
1483
|
+ |
.iter()
|
|
1484
|
+ |
.map(|f| {
|
|
1485
|
+ |
let options = f
|
|
1486
|
+ |
.get("options")
|
|
1487
|
+ |
.and_then(|o| o.get("values"))
|
|
1488
|
+ |
.cloned()
|
|
1489
|
+ |
.unwrap_or(serde_json::Value::Null);
|
|
1490
|
+ |
format!(
|
|
1491
|
+ |
"{} ({}) {}",
|
|
1492
|
+ |
field(f, "name"),
|
|
1493
|
+ |
field(f, "data_type"),
|
|
1494
|
+ |
or_none(&names(Some(&options), "name"))
|
|
1495
|
+ |
)
|
|
1496
|
+ |
})
|
|
1497
|
+ |
.collect()
|
|
1498
|
+ |
};
|
|
1499
|
+ |
emit(json, &value, &human);
|
|
1500
|
+ |
}
|
|
1501
|
+ |
ProjectAction::Items { number, repo } => {
|
|
1502
|
+ |
let target = target_or_fail(repo);
|
|
1503
|
+ |
let value = or_fail(tracker.project_items(&target, number).await);
|
|
1504
|
+ |
emit(json, &value, &project_items_human(&value));
|
|
1505
|
+ |
}
|
|
1506
|
+ |
ProjectAction::ItemAdd {
|
|
1507
|
+ |
number,
|
|
1508
|
+ |
issue,
|
|
1509
|
+ |
repo,
|
|
1510
|
+ |
} => {
|
|
1511
|
+ |
let target = target_or_fail(repo);
|
|
1512
|
+ |
let value = or_fail(tracker.project_add_item(&target, number, issue).await);
|
|
1513
|
+ |
emit(json, &value, &project_items_human(&value));
|
|
1514
|
+ |
}
|
|
1515
|
+ |
ProjectAction::ItemSet {
|
|
1516
|
+ |
number,
|
|
1517
|
+ |
item,
|
|
1518
|
+ |
set,
|
|
1519
|
+ |
repo,
|
|
1520
|
+ |
} => {
|
|
1521
|
+ |
let target = target_or_fail(repo);
|
|
1522
|
+ |
let values = parse_field_values(&set);
|
|
1523
|
+ |
let value = or_fail(
|
|
1524
|
+ |
tracker
|
|
1525
|
+ |
.project_set_item_values(&target, number, &item, &values)
|
|
1526
|
+ |
.await,
|
|
1527
|
+ |
);
|
|
1528
|
+ |
emit(json, &value, &project_items_human(&value));
|
|
1529
|
+ |
}
|
|
1530
|
+ |
ProjectAction::ItemMove {
|
|
1531
|
+ |
number,
|
|
1532
|
+ |
item,
|
|
1533
|
+ |
set,
|
|
1534
|
+ |
position,
|
|
1535
|
+ |
repo,
|
|
1536
|
+ |
} => {
|
|
1537
|
+ |
if set.is_empty() && position.is_none() {
|
|
1538
|
+ |
fail("Pass --set FIELD=VALUE, --position, or both.");
|
|
1539
|
+ |
}
|
|
1540
|
+ |
let target = target_or_fail(repo);
|
|
1541
|
+ |
let values = parse_field_values(&set);
|
|
1542
|
+ |
let value = or_fail(
|
|
1543
|
+ |
tracker
|
|
1544
|
+ |
.project_move_item(&target, number, &item, &values, position)
|
|
1545
|
+ |
.await,
|
|
1546
|
+ |
);
|
|
1547
|
+ |
emit(json, &value, &project_items_human(&value));
|
|
1548
|
+ |
}
|
|
1549
|
+ |
ProjectAction::ItemRemove {
|
|
1550
|
+ |
number,
|
|
1551
|
+ |
item,
|
|
1552
|
+ |
repo,
|
|
1553
|
+ |
} => {
|
|
1554
|
+ |
let value = {
|
|
1555
|
+ |
let target = target_or_fail(repo);
|
|
1556
|
+ |
or_fail(tracker.project_remove_item(&target, number, &item).await)
|
|
1557
|
+ |
};
|
|
1558
|
+ |
emit(
|
|
1559
|
+ |
json,
|
|
1560
|
+ |
&value,
|
|
1561
|
+ |
&[format!("Removed item {} from project #{}.", item, number)],
|
|
1562
|
+ |
);
|
|
1563
|
+ |
}
|
|
1564
|
+ |
}
|
|
1565
|
+ |
}
|
|
1566
|
+ |
|
|
1567
|
+ |
// ---------------------------------------------------------------------------
|
|
1568
|
+ |
// box
|
|
1569
|
+ |
// ---------------------------------------------------------------------------
|
|
1570
|
+ |
|
|
1571
|
+ |
fn box_list_human(boxes: &[crate::box_client::BoxRecord]) -> Vec<String> {
|
|
1572
|
+ |
if boxes.is_empty() {
|
|
1573
|
+ |
return vec!["No boxes provisioned for this conversation.".to_string()];
|
|
1574
|
+ |
}
|
|
1575
|
+ |
let mut lines = vec!["BOX ID STATE SETUP LABEL CREATED".to_string()];
|
|
1576
|
+ |
for b in boxes {
|
|
1577
|
+ |
lines.push(format!(
|
|
1578
|
+ |
"{} {} {} {} {}",
|
|
1579
|
+ |
pad(&b.box_id, 13),
|
|
1580
|
+ |
pad(&b.state, 11),
|
|
1581
|
+ |
pad(&b.setup_status, 9),
|
|
1582
|
+ |
pad(b.label.as_deref().unwrap_or("-"), 12),
|
|
1583
|
+ |
b.created_at
|
|
1584
|
+ |
));
|
|
1585
|
+ |
}
|
|
1586
|
+ |
lines
|
|
1587
|
+ |
}
|
|
1588
|
+ |
|
|
1589
|
+ |
fn box_view_human(b: &crate::box_client::BoxRecord) -> Vec<String> {
|
|
1590
|
+ |
let mut lines = vec![
|
|
1591
|
+ |
format!("Box ID: {}", b.box_id),
|
|
1592
|
+ |
format!("State: {}", b.state),
|
|
1593
|
+ |
format!("Setup Status: {}", b.setup_status),
|
|
1594
|
+ |
format!("Label: {}", b.label.as_deref().unwrap_or("-")),
|
|
1595
|
+ |
format!("Created: {}", b.created_at),
|
|
1596
|
+ |
];
|
|
1597
|
+ |
if let Some(stopped) = &b.stopped_at {
|
|
1598
|
+ |
lines.push(format!("Stopped: {}", stopped));
|
|
1599
|
+ |
}
|
|
1600
|
+ |
lines
|
|
1601
|
+ |
}
|
|
1602
|
+ |
|
|
1603
|
+ |
fn run_list_human(runs: &[crate::box_client::BoxRunRecord]) -> Vec<String> {
|
|
1604
|
+ |
if runs.is_empty() {
|
|
1605
|
+ |
return vec!["No runs recorded for this box.".to_string()];
|
|
1606
|
+ |
}
|
|
1607
|
+ |
let mut lines =
|
|
1608
|
+ |
vec!["RUN ID STATE EXIT COMMAND".to_string()];
|
|
1609
|
+ |
for r in runs {
|
|
1610
|
+ |
let command = if r.command.chars().count() > 40 {
|
|
1611
|
+ |
format!("{}...", r.command.chars().take(37).collect::<String>())
|
|
1612
|
+ |
} else {
|
|
1613
|
+ |
r.command.clone()
|
|
1614
|
+ |
};
|
|
1615
|
+ |
lines.push(format!(
|
|
1616
|
+ |
"{} {} {} {}",
|
|
1617
|
+ |
pad(&r.id, 36),
|
|
1618
|
+ |
pad(&r.state, 10),
|
|
1619
|
+ |
pad(
|
|
1620
|
+ |
&r.exit_status
|
|
1621
|
+ |
.map(|c| c.to_string())
|
|
1622
|
+ |
.unwrap_or_else(|| "-".to_string()),
|
|
1623
|
+ |
5
|
|
1624
|
+ |
),
|
|
1625
|
+ |
command
|
|
1626
|
+ |
));
|
|
1627
|
+ |
}
|
|
1628
|
+ |
lines
|
|
1629
|
+ |
}
|
|
1630
|
+ |
|
|
1631
|
+ |
fn run_view_human(r: &crate::box_client::BoxRunRecord) -> Vec<String> {
|
|
1632
|
+ |
let mut lines = vec![
|
|
1633
|
+ |
format!("Run ID: {}", r.id),
|
|
1634
|
+ |
format!("Box ID: {}", r.box_id),
|
|
1635
|
+ |
format!("State: {}", r.state),
|
|
1636
|
+ |
format!("Command: {}", r.command),
|
|
1637
|
+ |
format!(
|
|
1638
|
+ |
"Exit Status: {}",
|
|
1639
|
+ |
r.exit_status
|
|
1640
|
+ |
.map(|c| c.to_string())
|
|
1641
|
+ |
.unwrap_or_else(|| "-".to_string())
|
|
1642
|
+ |
),
|
|
1643
|
+ |
format!(
|
|
1644
|
+ |
"Timed Out: {}",
|
|
1645
|
+ |
if r.timed_out == Some(true) { "yes" } else { "no" }
|
|
1646
|
+ |
),
|
|
1647
|
+ |
];
|
|
1648
|
+ |
if let Some(reason) = &r.failure_reason {
|
|
1649
|
+ |
lines.push(format!("Failure: {}", reason));
|
|
1650
|
+ |
}
|
|
1651
|
+ |
lines.push(format!(
|
|
1652
|
+ |
"Admitted: {}",
|
|
1653
|
+ |
r.admitted_at.as_deref().unwrap_or("-")
|
|
1654
|
+ |
));
|
|
1655
|
+ |
lines.push(format!(
|
|
1656
|
+ |
"Dispatched: {}",
|
|
1657
|
+ |
r.dispatched_at.as_deref().unwrap_or("-")
|
|
1658
|
+ |
));
|
|
1659
|
+ |
lines.push(format!(
|
|
1660
|
+ |
"Started: {}",
|
|
1661
|
+ |
r.started_at.as_deref().unwrap_or("-")
|
|
1662
|
+ |
));
|
|
1663
|
+ |
lines.push(format!(
|
|
1664
|
+ |
"Finished: {}",
|
|
1665
|
+ |
r.finished_at.as_deref().unwrap_or("-")
|
|
1666
|
+ |
));
|
|
1667
|
+ |
lines
|
|
1668
|
+ |
}
|
|
1669
|
+ |
|
|
1670
|
+ |
fn fanout_human(plan: &crate::box_client::BoxFanoutPlan) -> Vec<String> {
|
|
1671
|
+ |
let mut lines = vec![
|
|
1672
|
+ |
format!("Fanout Plan: {}", plan.id),
|
|
1673
|
+ |
format!(
|
|
1674
|
+ |
"Requested: {} boxes (Budgeted: {})",
|
|
1675
|
+ |
plan.requested_count,
|
|
1676
|
+ |
if plan.budgeted { "yes" } else { "no" }
|
|
1677
|
+ |
),
|
|
1678
|
+ |
format!("Admitted: {}", plan.admitted.len()),
|
|
1679
|
+ |
];
|
|
1680
|
+ |
for item in &plan.admitted {
|
|
1681
|
+ |
lines.push(format!(
|
|
1682
|
+ |
" [#{}] {} -> {} ({})",
|
|
1683
|
+ |
item.position,
|
|
1684
|
+ |
item.label,
|
|
1685
|
+ |
item.box_id.as_deref().unwrap_or("allocating"),
|
|
1686
|
+ |
item.state
|
|
1687
|
+ |
));
|
|
1688
|
+ |
}
|
|
1689
|
+ |
lines.push(format!("Queued: {}", plan.queued.len()));
|
|
1690
|
+ |
for item in &plan.queued {
|
|
1691
|
+ |
lines.push(format!(
|
|
1692
|
+ |
" [#{}] {} (Reason: {})",
|
|
1693
|
+ |
item.position,
|
|
1694
|
+ |
item.label,
|
|
1695
|
+ |
item.queue_reason
|
|
1696
|
+ |
.as_deref()
|
|
1697
|
+ |
.unwrap_or("waiting for capacity")
|
|
1698
|
+ |
));
|
|
1699
|
+ |
}
|
|
1700
|
+ |
lines
|
|
1701
|
+ |
}
|
|
1702
|
+ |
|
|
1703
|
+ |
fn to_value<T: serde::Serialize>(value: &T) -> serde_json::Value {
|
|
1704
|
+ |
serde_json::to_value(value).unwrap_or(serde_json::Value::Null)
|
|
1705
|
+ |
}
|
|
1706
|
+ |
|
|
1707
|
+ |
async fn run_box(action: BoxAction, token: Option<String>, json: bool) {
|
|
1708
|
+ |
let client = crate::box_client::BoxClient::new(API_BASE, token);
|
|
1709
|
+ |
match action {
|
|
1710
|
+ |
BoxAction::List { conversation } => {
|
|
1711
|
+ |
let id = or_fail(client.conversation_id(conversation.as_deref()).await);
|
|
1712
|
+ |
let boxes = or_fail(client.list_boxes(&id).await);
|
|
1713
|
+ |
emit(
|
|
1714
|
+ |
json,
|
|
1715
|
+ |
&serde_json::json!({ "boxes": to_value(&boxes) }),
|
|
1716
|
+ |
&box_list_human(&boxes),
|
|
1717
|
+ |
);
|
|
1718
|
+ |
}
|
|
1719
|
+ |
BoxAction::Create {
|
|
1720
|
+ |
conversation,
|
|
1721
|
+ |
label,
|
|
1722
|
+ |
} => {
|
|
1723
|
+ |
let id = or_fail(client.conversation_id(conversation.as_deref()).await);
|
|
1724
|
+ |
let record = or_fail(client.create_box(&id, label.as_deref()).await);
|
|
1725
|
+ |
let mut human = vec![format!(
|
|
1726
|
+ |
"Provisioned Box {} (state: {}, setup: {}).",
|
|
1727
|
+ |
record.box_id, record.state, record.setup_status
|
|
1728
|
+ |
)];
|
|
1729
|
+ |
if let Some(name) = &record.label {
|
|
1730
|
+ |
human.push(format!("Label: {}", name));
|
|
1731
|
+ |
}
|
|
1732
|
+ |
emit(json, &serde_json::json!({ "box": to_value(&record) }), &human);
|
|
1733
|
+ |
}
|
|
1734
|
+ |
BoxAction::View {
|
|
1735
|
+ |
box_id,
|
|
1736
|
+ |
conversation,
|
|
1737
|
+ |
} => {
|
|
1738
|
+ |
let id = or_fail(client.conversation_id(conversation.as_deref()).await);
|
|
1739
|
+ |
let record = or_fail(client.view_box(&id, &box_id).await);
|
|
1740
|
+ |
emit(
|
|
1741
|
+ |
json,
|
|
1742
|
+ |
&serde_json::json!({ "box": to_value(&record) }),
|
|
1743
|
+ |
&box_view_human(&record),
|
|
1744
|
+ |
);
|
|
1745
|
+ |
}
|
|
1746
|
+ |
BoxAction::Exec {
|
|
1747
|
+ |
box_id,
|
|
1748
|
+ |
command,
|
|
1749
|
+ |
conversation,
|
|
1750
|
+ |
timeout,
|
|
1751
|
+ |
} => {
|
|
1752
|
+ |
let id = or_fail(client.conversation_id(conversation.as_deref()).await);
|
|
1753
|
+ |
let joined = command.join(" ");
|
|
1754
|
+ |
let result = or_fail(client.execute_command(&id, &box_id, &joined, timeout).await);
|
|
1755
|
+ |
let mut human = Vec::new();
|
|
1756
|
+ |
if !result.stdout.is_empty() {
|
|
1757
|
+ |
human.push(result.stdout.trim_end().to_string());
|
|
1758
|
+ |
}
|
|
1759
|
+ |
if !result.stderr.is_empty() {
|
|
1760
|
+ |
human.push(format!("[STDERR] {}", result.stderr.trim_end()));
|
|
1761
|
+ |
}
|
|
1762
|
+ |
if result.timed_out {
|
|
1763
|
+ |
human.push("[TIMED OUT]".to_string());
|
|
1764
|
+ |
}
|
|
1765
|
+ |
emit(
|
|
1766
|
+ |
json,
|
|
1767
|
+ |
&serde_json::json!({ "result": to_value(&result) }),
|
|
1768
|
+ |
&human,
|
|
1769
|
+ |
);
|
|
1770
|
+ |
// The box's exit status is this process's exit status, so a script
|
|
1771
|
+ |
// that runs a command in a box can branch on it.
|
|
1772
|
+ |
if result.exit_code != 0 {
|
|
1773
|
+ |
std::process::exit(result.exit_code.clamp(1, 255) as i32);
|
|
1774
|
+ |
}
|
|
1775
|
+ |
}
|
|
1776
|
+ |
BoxAction::Stop {
|
|
1777
|
+ |
box_id,
|
|
1778
|
+ |
conversation,
|
|
1779
|
+ |
} => {
|
|
1780
|
+ |
let id = or_fail(client.conversation_id(conversation.as_deref()).await);
|
|
1781
|
+ |
let record = or_fail(client.stop_box(&id, &box_id).await);
|
|
1782
|
+ |
emit(
|
|
1783
|
+ |
json,
|
|
1784
|
+ |
&serde_json::json!({ "box": to_value(&record) }),
|
|
1785
|
+ |
&[format!(
|
|
1786
|
+ |
"Stopped Box {} (state: {}). Slot released.",
|
|
1787
|
+ |
record.box_id, record.state
|
|
1788
|
+ |
)],
|
|
1789
|
+ |
);
|
|
1790
|
+ |
}
|
|
1791
|
+ |
BoxAction::Run {
|
|
1792
|
+ |
box_id,
|
|
1793
|
+ |
command,
|
|
1794
|
+ |
conversation,
|
|
1795
|
+ |
} => {
|
|
1796
|
+ |
let id = or_fail(client.conversation_id(conversation.as_deref()).await);
|
|
1797
|
+ |
let joined = command.join(" ");
|
|
1798
|
+ |
let run = or_fail(client.start_run(&id, &box_id, &joined, None).await);
|
|
1799
|
+ |
emit(
|
|
1800
|
+ |
json,
|
|
1801
|
+ |
&serde_json::json!({ "run": to_value(&run) }),
|
|
1802
|
+ |
&[
|
|
1803
|
+ |
format!("Started background run {} on Box {}.", run.id, run.box_id),
|
|
1804
|
+ |
format!("State: {}", run.state),
|
|
1805
|
+ |
format!("Inspect with: oa box runs view {} {}", run.box_id, run.id),
|
|
1806
|
+ |
],
|
|
1807
|
+ |
);
|
|
1808
|
+ |
}
|
|
1809
|
+ |
BoxAction::Runs { action } => run_box_runs(action, &client, json).await,
|
|
1810
|
+ |
BoxAction::Fanout {
|
|
1811
|
+ |
count,
|
|
1812
|
+ |
labels,
|
|
1813
|
+ |
budgeted,
|
|
1814
|
+ |
conversation,
|
|
1815
|
+ |
request_id,
|
|
1816
|
+ |
} => {
|
|
1817
|
+ |
let id = or_fail(client.conversation_id(conversation.as_deref()).await);
|
|
1818
|
+ |
let plan = match request_id {
|
|
1819
|
+ |
Some(request) => or_fail(client.view_fanout(&id, &request).await),
|
|
1820
|
+ |
None => {
|
|
1821
|
+ |
let parsed: Vec<String> = labels
|
|
1822
|
+ |
.as_deref()
|
|
1823
|
+ |
.map(|raw| {
|
|
1824
|
+ |
raw.split(',')
|
|
1825
|
+ |
.map(|s| s.trim().to_string())
|
|
1826
|
+ |
.filter(|s| !s.is_empty())
|
|
1827
|
+ |
.collect()
|
|
1828
|
+ |
})
|
|
1829
|
+ |
.unwrap_or_default();
|
|
1830
|
+ |
or_fail(client.fanout(&id, count, &parsed, budgeted).await)
|
|
1831
|
+ |
}
|
|
1832
|
+ |
};
|
|
1833
|
+ |
emit(
|
|
1834
|
+ |
json,
|
|
1835
|
+ |
&serde_json::json!({ "plan": to_value(&plan) }),
|
|
1836
|
+ |
&fanout_human(&plan),
|
|
1837
|
+ |
);
|
|
1838
|
+ |
}
|
|
1839
|
+ |
}
|
|
1840
|
+ |
}
|
|
1841
|
+ |
|
|
1842
|
+ |
async fn run_box_runs(
|
|
1843
|
+ |
action: BoxRunAction,
|
|
1844
|
+ |
client: &crate::box_client::BoxClient,
|
|
1845
|
+ |
json: bool,
|
|
1846
|
+ |
) {
|
|
1847
|
+ |
match action {
|
|
1848
|
+ |
BoxRunAction::List {
|
|
1849
|
+ |
box_id,
|
|
1850
|
+ |
conversation,
|
|
1851
|
+ |
} => {
|
|
1852
|
+ |
let id = or_fail(client.conversation_id(conversation.as_deref()).await);
|
|
1853
|
+ |
let runs = or_fail(client.list_runs(&id, &box_id).await);
|
|
1854
|
+ |
emit(
|
|
1855
|
+ |
json,
|
|
1856
|
+ |
&serde_json::json!({ "runs": to_value(&runs) }),
|
|
1857
|
+ |
&run_list_human(&runs),
|
|
1858
|
+ |
);
|
|
1859
|
+ |
}
|
|
1860
|
+ |
BoxRunAction::View {
|
|
1861
|
+ |
box_id,
|
|
1862
|
+ |
run_id,
|
|
1863
|
+ |
conversation,
|
|
1864
|
+ |
} => {
|
|
1865
|
+ |
let id = or_fail(client.conversation_id(conversation.as_deref()).await);
|
|
1866
|
+ |
let run = or_fail(client.view_run(&id, &box_id, &run_id).await);
|
|
1867
|
+ |
emit(
|
|
1868
|
+ |
json,
|
|
1869
|
+ |
&serde_json::json!({ "run": to_value(&run) }),
|
|
1870
|
+ |
&run_view_human(&run),
|
|
1871
|
+ |
);
|
|
1872
|
+ |
}
|
|
1873
|
+ |
BoxRunAction::Output {
|
|
1874
|
+ |
box_id,
|
|
1875
|
+ |
run_id,
|
|
1876
|
+ |
offset,
|
|
1877
|
+ |
follow,
|
|
1878
|
+ |
interval_ms,
|
|
1879
|
+ |
conversation,
|
|
1880
|
+ |
} => {
|
|
1881
|
+ |
let id = or_fail(client.conversation_id(conversation.as_deref()).await);
|
|
1882
|
+ |
if !follow {
|
|
1883
|
+ |
let result = or_fail(client.run_output(&id, &box_id, &run_id, offset).await);
|
|
1884
|
+ |
let mut human = Vec::new();
|
|
1885
|
+ |
if result.truncated {
|
|
1886
|
+ |
// The box keeps a bounded log, so a read that starts before
|
|
1887
|
+ |
// the retained window begins mid-stream. Say so rather than
|
|
1888
|
+ |
// letting the gap read as the run's first line.
|
|
1889
|
+ |
human.push("[EARLIER OUTPUT DROPPED BY THE BOX]".to_string());
|
|
1890
|
+ |
}
|
|
1891
|
+ |
human.push(result.output.trim_end().to_string());
|
|
1892
|
+ |
emit(json, &to_value(&result), &human);
|
|
1893
|
+ |
return;
|
|
1894
|
+ |
}
|
|
1895
|
+ |
follow_run_output(client, &id, &box_id, &run_id, offset, interval_ms, json).await;
|
|
1896
|
+ |
}
|
|
1897
|
+ |
BoxRunAction::Cancel {
|
|
1898
|
+ |
box_id,
|
|
1899
|
+ |
run_id,
|
|
1900
|
+ |
conversation,
|
|
1901
|
+ |
} => {
|
|
1902
|
+ |
let id = or_fail(client.conversation_id(conversation.as_deref()).await);
|
|
1903
|
+ |
let run = or_fail(client.cancel_run(&id, &box_id, &run_id).await);
|
|
1904
|
+ |
emit(
|
|
1905
|
+ |
json,
|
|
1906
|
+ |
&serde_json::json!({ "run": to_value(&run) }),
|
|
1907
|
+ |
&[format!(
|
|
1908
|
+ |
"Requested cancellation for run {} (state: {}).",
|
|
1909
|
+ |
run.id, run.state
|
|
1910
|
+ |
)],
|
|
1911
|
+ |
);
|
|
1912
|
+ |
}
|
|
1913
|
+ |
}
|
|
1914
|
+ |
}
|
|
1915
|
+ |
|
|
1916
|
+ |
/// Print a followed run's output as it arrives, then its final record.
|
|
1917
|
+ |
///
|
|
1918
|
+ |
/// The loop itself lives in [`crate::box_client::BoxClient::follow_run_output`],
|
|
1919
|
+ |
/// where it is tested; this is the rendering half.
|
|
1920
|
+ |
async fn follow_run_output(
|
|
1921
|
+ |
client: &crate::box_client::BoxClient,
|
|
1922
|
+ |
conversation: &str,
|
|
1923
|
+ |
box_id: &str,
|
|
1924
|
+ |
run_id: &str,
|
|
1925
|
+ |
offset: Option<u64>,
|
|
1926
|
+ |
interval_ms: u64,
|
|
1927
|
+ |
json: bool,
|
|
1928
|
+ |
) {
|
|
1929
|
+ |
use std::io::Write;
|
|
1930
|
+ |
|
|
1931
|
+ |
let collected = std::cell::RefCell::new(String::new());
|
|
1932
|
+ |
let announced = std::cell::Cell::new(false);
|
|
1933
|
+ |
let followed = client
|
|
1934
|
+ |
.follow_run_output(
|
|
1935
|
+ |
conversation,
|
|
1936
|
+ |
box_id,
|
|
1937
|
+ |
run_id,
|
|
1938
|
+ |
offset,
|
|
1939
|
+ |
std::time::Duration::from_millis(interval_ms.max(50)),
|
|
1940
|
+ |
|chunk| {
|
|
1941
|
+ |
if chunk.truncated && !announced.get() {
|
|
1942
|
+ |
announced.set(true);
|
|
1943
|
+ |
if !json {
|
|
1944
|
+ |
println!("[EARLIER OUTPUT DROPPED BY THE BOX]");
|
|
1945
|
+ |
}
|
|
1946
|
+ |
}
|
|
1947
|
+ |
if chunk.output.is_empty() {
|
|
1948
|
+ |
return;
|
|
1949
|
+ |
}
|
|
1950
|
+ |
if json {
|
|
1951
|
+ |
collected.borrow_mut().push_str(&chunk.output);
|
|
1952
|
+ |
} else {
|
|
1953
|
+ |
print!("{}", chunk.output);
|
|
1954
|
+ |
let _ = std::io::stdout().flush();
|
|
1955
|
+ |
}
|
|
1956
|
+ |
},
|
|
1957
|
+ |
)
|
|
1958
|
+ |
.await;
|
|
1959
|
+ |
let (run, next_offset) = or_fail(followed);
|
|
1960
|
+ |
|
|
1961
|
+ |
if json {
|
|
1962
|
+ |
emit(
|
|
1963
|
+ |
true,
|
|
1964
|
+ |
&serde_json::json!({
|
|
1965
|
+ |
"run": to_value(&run),
|
|
1966
|
+ |
"output": collected.into_inner(),
|
|
1967
|
+ |
"next_offset": next_offset,
|
|
1968
|
+ |
"truncated": announced.get(),
|
|
1969
|
+ |
}),
|
|
1970
|
+ |
&[],
|
|
1971
|
+ |
);
|
|
1972
|
+ |
} else {
|
|
1973
|
+ |
println!();
|
|
1974
|
+ |
for line in run_view_human(&run) {
|
|
1975
|
+ |
println!("{}", line);
|
|
1976
|
+ |
}
|
|
1977
|
+ |
}
|
|
1978
|
+ |
}
|
|
1979
|
+ |
|
|
1980
|
+ |
// ---------------------------------------------------------------------------
|
|
1981
|
+ |
// memory
|
|
1982
|
+ |
// ---------------------------------------------------------------------------
|
|
1983
|
+ |
|
|
1984
|
+ |
fn memory_list_human(memories: &[crate::memory_client::MemoryRecord]) -> Vec<String> {
|
|
1985
|
+ |
if memories.is_empty() {
|
|
1986
|
+ |
return vec!["No memories stored for this account.".to_string()];
|
|
1987
|
+ |
}
|
|
1988
|
+ |
// One memory per block rather than one per row: a memory is a sentence a
|
|
1989
|
+ |
// person wrote, and a column would cut most of them off.
|
|
1990
|
+ |
let mut lines = Vec::new();
|
|
1991
|
+ |
for memory in memories {
|
|
1992
|
+ |
lines.push(format!(
|
|
1993
|
+ |
"{} [{}] {}",
|
|
1994
|
+ |
memory.id, memory.bucket, memory.created_at
|
|
1995
|
+ |
));
|
|
1996
|
+ |
lines.push(format!(" {}", memory.body));
|
|
1997
|
+ |
if let Some(source) = &memory.source_ref {
|
|
1998
|
+ |
lines.push(format!(" source: {}", source));
|
|
1999
|
+ |
}
|
|
2000
|
+ |
if let Some(replacement) = &memory.superseded_by {
|
|
2001
|
+ |
lines.push(format!(" superseded by: {}", replacement));
|
|
2002
|
+ |
}
|
|
2003
|
+ |
}
|
|
2004
|
+ |
lines
|
|
2005
|
+ |
}
|
|
2006
|
+ |
|
|
2007
|
+ |
async fn run_memory(action: MemoryAction, token: Option<String>, json: bool) {
|
|
2008
|
+ |
let client = crate::memory_client::MemoryClient::new(API_BASE, token);
|
|
2009
|
+ |
match action {
|
|
2010
|
+ |
MemoryAction::List {
|
|
2011
|
+ |
bucket,
|
|
2012
|
+ |
limit,
|
|
2013
|
+ |
include_superseded,
|
|
2014
|
+ |
} => {
|
|
2015
|
+ |
let memories = or_fail(
|
|
2016
|
+ |
client
|
|
2017
|
+ |
.list_memories(bucket.as_deref(), limit, include_superseded)
|
|
2018
|
+ |
.await,
|
|
2019
|
+ |
);
|
|
2020
|
+ |
emit(
|
|
2021
|
+ |
json,
|
|
2022
|
+ |
&serde_json::json!({ "memories": to_value(&memories) }),
|
|
2023
|
+ |
&memory_list_human(&memories),
|
|
2024
|
+ |
);
|
|
2025
|
+ |
}
|
|
2026
|
+ |
MemoryAction::Add {
|
|
2027
|
+ |
body,
|
|
2028
|
+ |
bucket,
|
|
2029
|
+ |
supersedes,
|
|
2030
|
+ |
source_ref,
|
|
2031
|
+ |
} => {
|
|
2032
|
+ |
let text = body.join(" ");
|
|
2033
|
+ |
let memory = or_fail(
|
|
2034
|
+ |
client
|
|
2035
|
+ |
.add_memory(
|
|
2036
|
+ |
&text,
|
|
2037
|
+ |
bucket.as_deref(),
|
|
2038
|
+ |
supersedes.as_deref(),
|
|
2039
|
+ |
source_ref.as_deref(),
|
|
2040
|
+ |
)
|
|
2041
|
+ |
.await,
|
|
2042
|
+ |
);
|
|
2043
|
+ |
let mut human = vec![
|
|
2044
|
+ |
format!(
|
|
2045
|
+ |
"Stored memory {} in the {} bucket.",
|
|
2046
|
+ |
memory.id, memory.bucket
|
|
2047
|
+ |
),
|
|
2048
|
+ |
format!(" {}", memory.body),
|
|
2049
|
+ |
];
|
|
2050
|
+ |
if let Some(replaced) = &supersedes {
|
|
2051
|
+ |
human.push(format!("Supersedes {}.", replaced));
|
|
2052
|
+ |
}
|
|
2053
|
+ |
emit(
|
|
2054
|
+ |
json,
|
|
2055
|
+ |
&serde_json::json!({ "memory": to_value(&memory) }),
|
|
2056
|
+ |
&human,
|
|
2057
|
+ |
);
|
|
2058
|
+ |
}
|
|
2059
|
+ |
MemoryAction::Delete { memory_id } => {
|
|
2060
|
+ |
let memory = or_fail(client.delete_memory(&memory_id).await);
|
|
2061
|
+ |
emit(
|
|
2062
|
+ |
json,
|
|
2063
|
+ |
&serde_json::json!({ "memory": to_value(&memory) }),
|
|
2064
|
+ |
&[
|
|
2065
|
+ |
format!("Removed memory {}.", memory.id),
|
|
2066
|
+ |
format!(" {}", memory.body),
|
|
2067
|
+ |
],
|
|
2068
|
+ |
);
|
|
2069
|
+ |
}
|
|
2070
|
+ |
}
|
|
2071
|
+ |
}
|
|
2072
|
+ |
|
| 613 |
2073
|
|
// ---------------------------------------------------------------------------
|
| 614 |
2074
|
|
// identity
|
| 615 |
2075
|
|
// ---------------------------------------------------------------------------
|