forked from windmill-labs/windmill
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschedule.ts
More file actions
142 lines (127 loc) · 3.48 KB
/
Copy pathschedule.ts
File metadata and controls
142 lines (127 loc) · 3.48 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
// deno-lint-ignore-file no-explicit-any
import { colors, Command, log, SEP, Table } from "./deps.ts";
import { requireLogin, resolveWorkspace, validatePath } from "./context.ts";
import * as wmill from "./gen/services.gen.ts";
import {
GlobalOptions,
isSuperset,
parseFromFile,
removeType,
} from "./types.ts";
import { Schedule } from "./gen/types.gen.ts";
export interface ScheduleFile {
schedule: string;
on_failure: string;
script_path: string;
args: any;
timezone: string;
is_flow: boolean;
enabled: boolean;
}
async function list(opts: GlobalOptions) {
const workspace = await resolveWorkspace(opts);
await requireLogin(opts);
const schedules = await wmill.listSchedules({
workspace: workspace.workspaceId,
});
new Table()
.header(["Path", "Schedule"])
.padding(2)
.border(true)
.body(schedules.map((x) => [x.path, x.schedule]))
.render();
}
export async function pushSchedule(
workspace: string,
path: string,
schedule: Schedule | ScheduleFile | undefined,
localSchedule: ScheduleFile
): Promise<void> {
path = removeType(path, "schedule").replaceAll(SEP, "/");
log.debug(`Processing local schedule ${path}`);
// deleting old app if it exists in raw mode
try {
schedule = await wmill.getSchedule({ workspace, path });
log.debug(`Schedule ${path} exists on remote`);
} catch {
log.debug(`Schedule ${path} does not exist on remote`);
//ignore
}
if (schedule) {
if (isSuperset(localSchedule, schedule)) {
log.debug(`Schedule ${path} is up to date`);
return;
}
log.debug(`Updating schedule ${path}`);
try {
log.info(colors.bold.yellow(
`Updating schedule ${path}`
));
await wmill.updateSchedule({
workspace: workspace,
path,
requestBody: {
...localSchedule,
},
});
if (localSchedule.enabled != schedule.enabled) {
log.info(colors.bold.yellow(
`Schedule ${path} is ${localSchedule.enabled ? "enabled" : "disabled"} locally but not on remote, updating remote`
));
await wmill.setScheduleEnabled({
workspace: workspace,
path,
requestBody: {
enabled: localSchedule.enabled,
},
});
}
} catch (e) {
console.error(e.body);
throw e;
}
} else {
console.log(colors.bold.yellow("Creating new schedule " + path));
try {
await wmill.createSchedule({
workspace: workspace,
requestBody: {
path: path,
...localSchedule,
},
});
} catch (e) {
console.error(e.body);
throw e;
}
}
}
async function push(opts: GlobalOptions, filePath: string, remotePath: string) {
const workspace = await resolveWorkspace(opts);
await requireLogin(opts);
if (!validatePath(remotePath)) {
return;
}
const fstat = await Deno.stat(filePath);
if (!fstat.isFile) {
throw new Error("file path must refer to a file.");
}
console.log(colors.bold.yellow("Pushing schedule..."));
await pushSchedule(
workspace.workspaceId,
remotePath,
undefined,
parseFromFile(filePath)
);
console.log(colors.bold.underline.green("Schedule pushed"));
}
const command = new Command()
.description("schedule related commands")
.action(list as any)
.command(
"push",
"push a local schedule spec. This overrides any remote versions."
)
.arguments("<file_path:string> <remote_path:string>")
.action(push as any);
export default command;