|
| 1 | +// |
| 2 | +// File.swift |
| 3 | +// |
| 4 | +// |
| 5 | +// Created by Brandon Sneed on 7/7/22. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | +import SwiftCLI |
| 10 | +import Spinner |
| 11 | +import ColorizeSwift |
| 12 | +import Segment |
| 13 | + |
| 14 | +class EdgeFnGroup: CommandGroup { |
| 15 | + let name = "edgefn" |
| 16 | + let shortDescription = "Work with and develop edge functions" |
| 17 | + let children: [Routable] = [EdgeFnLatestCommand(), EdgeFnUpload(), EdgeFnDisable()] |
| 18 | + init() {} |
| 19 | +} |
| 20 | + |
| 21 | +class EdgeFnDisable: Command { |
| 22 | + let name = "disable" |
| 23 | + let shortDescription = "Disable edge functions for a given source ID" |
| 24 | + |
| 25 | + @Param var sourceId: String |
| 26 | + |
| 27 | + func execute() throws { |
| 28 | + guard let workspace = currentWorkspace else { exitWithError(code: .commandFailed, message: "No authentication tokens found."); return } |
| 29 | + executeAndWait { semaphore in |
| 30 | + let spinner = Spinner(.dots, "Uploading edge function ...") |
| 31 | + spinner.start() |
| 32 | + |
| 33 | + PAPI.shared.edgeFunctions.disable(token: workspace.token, sourceId: sourceId) { data, response, error in |
| 34 | + spinner.stop() |
| 35 | + |
| 36 | + if let error = error { |
| 37 | + exitWithError(error) |
| 38 | + } |
| 39 | + |
| 40 | + let statusCode = PAPI.shared.statusCode(response: response) |
| 41 | + |
| 42 | + switch statusCode { |
| 43 | + case .ok: |
| 44 | + // success! |
| 45 | + print("Edge functions disabled for \(self.sourceId.italic.bold).") |
| 46 | + |
| 47 | + case .unauthorized: |
| 48 | + fallthrough |
| 49 | + case .unauthorized2: |
| 50 | + exitWithError(code: .commandFailed, message: "Supplied token is not authorized.") |
| 51 | + case .notFound: |
| 52 | + exitWithError(code: .commandFailed, message: "No edge functions were found.") |
| 53 | + default: |
| 54 | + exitWithError("An unknown error occurred.") |
| 55 | + } |
| 56 | + semaphore.signal() |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | + |
| 63 | +class EdgeFnUpload: Command { |
| 64 | + let name = "upload" |
| 65 | + let shortDescription = "Upload an edge function" |
| 66 | + |
| 67 | + @Param var sourceId: String |
| 68 | + @Param var filePath: String |
| 69 | + |
| 70 | + func execute() throws { |
| 71 | + guard let workspace = currentWorkspace else { exitWithError(code: .commandFailed, message: "No authentication tokens found."); return } |
| 72 | + |
| 73 | + var uploadURL: URL? = nil |
| 74 | + |
| 75 | + let fileURL = URL(fileURLWithPath: filePath.expandingTildeInPath) |
| 76 | + |
| 77 | + // generate upload URL |
| 78 | + executeAndWait { semaphore in |
| 79 | + let spinner = Spinner(.dots, "Generating upload URL ...") |
| 80 | + spinner.start() |
| 81 | + |
| 82 | + PAPI.shared.edgeFunctions.generateUploadURL(token: workspace.token, sourceId: sourceId) { data, response, error in |
| 83 | + spinner.stop() |
| 84 | + |
| 85 | + if let error = error { |
| 86 | + exitWithError(error) |
| 87 | + } |
| 88 | + |
| 89 | + let statusCode = PAPI.shared.statusCode(response: response) |
| 90 | + |
| 91 | + switch statusCode { |
| 92 | + case .ok: |
| 93 | + // success! |
| 94 | + if let jsonData = data, let json = try? JSONSerialization.jsonObject(with: jsonData) as? [String: Any] { |
| 95 | + if let uploadString = json[keyPath: "data.uploadURL"] as? String { |
| 96 | + uploadURL = URL(string: uploadString) |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + case .unauthorized: |
| 101 | + fallthrough |
| 102 | + case .unauthorized2: |
| 103 | + exitWithError(code: .commandFailed, message: "Supplied token is not authorized.") |
| 104 | + case .notFound: |
| 105 | + exitWithError(code: .commandFailed, message: "No edge functions were found.") |
| 106 | + default: |
| 107 | + exitWithError("An unknown error occurred.") |
| 108 | + } |
| 109 | + semaphore.signal() |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + // upload it to the URL we were given. |
| 114 | + executeAndWait { semaphore in |
| 115 | + let spinner = Spinner(.dots, "Uploading \(fileURL.lastPathComponent) ...") |
| 116 | + spinner.start() |
| 117 | + |
| 118 | + PAPI.shared.edgeFunctions.uploadToGeneratedURL(token: workspace.token, url: uploadURL, fileURL: fileURL) { data, response, error in |
| 119 | + spinner.stop() |
| 120 | + |
| 121 | + if let error = error { |
| 122 | + exitWithError(error) |
| 123 | + } |
| 124 | + |
| 125 | + let statusCode = PAPI.shared.statusCode(response: response) |
| 126 | + |
| 127 | + switch statusCode { |
| 128 | + case .ok: |
| 129 | + // success! |
| 130 | + break |
| 131 | + |
| 132 | + case .unauthorized: |
| 133 | + fallthrough |
| 134 | + case .unauthorized2: |
| 135 | + exitWithError(code: .commandFailed, message: "Supplied token is not authorized.") |
| 136 | + case .notFound: |
| 137 | + exitWithError(code: .commandFailed, message: "No edge functions were found.") |
| 138 | + default: |
| 139 | + exitWithError("An unknown error occurred.") |
| 140 | + } |
| 141 | + semaphore.signal() |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + // call create to make a new connection to the version we just posted. |
| 146 | + executeAndWait { semaphore in |
| 147 | + let spinner = Spinner(.dots, "Creating new edge function version ...") |
| 148 | + spinner.start() |
| 149 | + |
| 150 | + PAPI.shared.edgeFunctions.createNewVersion(token: workspace.token, sourceId: sourceId, uploadURL: uploadURL) { data, response, error in |
| 151 | + spinner.stop() |
| 152 | + |
| 153 | + if let error = error { |
| 154 | + exitWithError(error) |
| 155 | + } |
| 156 | + |
| 157 | + let statusCode = PAPI.shared.statusCode(response: response) |
| 158 | + |
| 159 | + switch statusCode { |
| 160 | + case .ok: |
| 161 | + // success! |
| 162 | + if let jsonData = data, let jsonObject = try? JSONSerialization.jsonObject(with: jsonData) as? [String: Any] { |
| 163 | + if let json = try? JSON(jsonObject) { |
| 164 | + print(json.prettyPrint()) |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + case .unauthorized: |
| 169 | + fallthrough |
| 170 | + case .unauthorized2: |
| 171 | + exitWithError(code: .commandFailed, message: "Supplied token is not authorized.") |
| 172 | + case .notFound: |
| 173 | + exitWithError(code: .commandFailed, message: "No edge functions were found.") |
| 174 | + default: |
| 175 | + exitWithError("An unknown error occurred.") |
| 176 | + } |
| 177 | + semaphore.signal() |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +class EdgeFnLatestCommand: Command { |
| 185 | + let name = "latest" |
| 186 | + let shortDescription = "Get info about the latest Edge Function in use" |
| 187 | + |
| 188 | + @Param var sourceId: String |
| 189 | + |
| 190 | + func execute() throws { |
| 191 | + guard let workspace = currentWorkspace else { exitWithError(code: .commandFailed, message: "No authentication tokens found."); return } |
| 192 | + |
| 193 | + executeAndWait { semaphore in |
| 194 | + let spinner = Spinner(.dots, "Retrieving latest Edge Functino info ...") |
| 195 | + spinner.start() |
| 196 | + |
| 197 | + PAPI.shared.edgeFunctions.latest(token: workspace.token, sourceId: sourceId) { data, response, error in |
| 198 | + spinner.stop() |
| 199 | + |
| 200 | + if let error = error { |
| 201 | + exitWithError(error) |
| 202 | + } |
| 203 | + |
| 204 | + let statusCode = PAPI.shared.statusCode(response: response) |
| 205 | + |
| 206 | + switch statusCode { |
| 207 | + case .ok: |
| 208 | + // success! |
| 209 | + if let jsonData = data, let jsonObject = try? JSONSerialization.jsonObject(with: jsonData) as? [String: Any] { |
| 210 | + if let json = try? JSON(jsonObject) { |
| 211 | + print(json.prettyPrint()) |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + case .unauthorized: |
| 216 | + fallthrough |
| 217 | + case .unauthorized2: |
| 218 | + exitWithError(code: .commandFailed, message: "Supplied token is not authorized.") |
| 219 | + case .notFound: |
| 220 | + exitWithError(code: .commandFailed, message: "No edge functions were found.") |
| 221 | + default: |
| 222 | + exitWithError("An unknown error occurred.") |
| 223 | + } |
| 224 | + semaphore.signal() |
| 225 | + } |
| 226 | + } |
| 227 | + } |
| 228 | +} |
| 229 | + |
0 commit comments