Skip to content

Commit

Permalink
Re-use SPI.Command in Analyze
Browse files Browse the repository at this point in the history
  • Loading branch information
finestructure committed Nov 21, 2023
1 parent d71bcd3 commit 09f852f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 39 deletions.
39 changes: 4 additions & 35 deletions Sources/App/Commands/Analyze.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,11 @@ import Vapor
enum Analyze {

struct Command: AsyncCommand {
static let defaultLimit = 1

struct Signature: CommandSignature {
@Option(name: "limit", short: "l")
var limit: Int?

@Option(name: "id")
var id: UUID?

@Option(name: "url")
var url: String?
}
typealias Signature = SPICommand.Signature

var help: String { "Run package analysis (fetching git repository and inspecting content)" }

enum Mode {
case id(Package.Id)
case limit(Int)
case url(String)

init(signature: Signature) {
if let id = signature.id {
self = .id(id)
} else if let url = signature.url {
self = .url(url)
} else if let limit = signature.limit {
self = .limit(limit)
} else {
self = .limit(Command.defaultLimit)
}
}
}

func run(using context: CommandContext, signature: Signature) async throws {
func run(using context: CommandContext, signature: SPICommand.Signature) async throws {
let client = context.application.client
let eventLoop = context.application.eventLoopGroup.any()
let db = context.application._db(.psql, on: eventLoop)
Expand All @@ -64,13 +35,11 @@ enum Analyze {

Analyze.resetMetrics()

let mode = Mode(signature: signature)

do {
try await analyze(client: client,
database: db,
logger: logger,
mode: mode)
mode: .init(signature: signature))
} catch {
logger.error("\(error.localizedDescription)")
}
Expand Down Expand Up @@ -140,7 +109,7 @@ extension Analyze {
static func analyze(client: Client,
database: Database,
logger: Logger,
mode: Analyze.Command.Mode) async throws {
mode: SPICommand.Mode) async throws {
let start = DispatchTime.now().uptimeNanoseconds
defer { AppMetrics.analyzeDurationSeconds?.time(since: start) }

Expand Down
6 changes: 2 additions & 4 deletions Sources/App/Commands/Ingest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Fluent

struct IngestCommand: AsyncCommand {
typealias Signature = SPICommand.Signature

var help: String { "Run package ingestion (fetching repository metadata)" }

func run(using context: CommandContext, signature: SPICommand.Signature) async throws {
Expand All @@ -28,10 +28,8 @@ struct IngestCommand: AsyncCommand {

Self.resetMetrics()

let mode = SPICommand.Mode(signature: signature)

do {
try await ingest(client: client, database: db, logger: logger, mode: mode)
try await ingest(client: client, database: db, logger: logger, mode: .init(signature: signature))
} catch {
logger.error("\(error.localizedDescription)")
}
Expand Down

0 comments on commit 09f852f

Please sign in to comment.