Co-authored-by: Brendan Allan <14191578+Brendonovich@users.noreply.github.com> Co-authored-by: Kit Langton <kit.langton@gmail.com> Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com> Co-authored-by: Affan Ali <93028901+affanali2k3@users.noreply.github.com> Co-authored-by: affanali2k3 <affanalikhanxx@gmail.com> Co-authored-by: Frank <frank@anoma.ly> Co-authored-by: opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com> Co-authored-by: 𝓛𝓲𝓽𝓽𝓵𝓮 𝓕𝓻𝓪𝓷𝓴 <little-frank@opencord.local> Co-authored-by: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Co-authored-by: Jay V <air@live.ca> Co-authored-by: Dax Raad <d@ironbay.co> Co-authored-by: Aarav Sareen <96787824+arvsrn@users.noreply.github.com> Co-authored-by: OpeOginni <107570612+OpeOginni@users.noreply.github.com> Co-authored-by: Luke Parker <10430890+Hona@users.noreply.github.com> Co-authored-by: Ben Guthrie <benjee.012@gmail.com> Co-authored-by: Dax <mail@thdxr.com> Co-authored-by: Filip <34747899+neriousy@users.noreply.github.com> Co-authored-by: Max Anderson <max.a.anderson95@gmail.com> Co-authored-by: Brendan Allan <git@brendonovich.dev> Co-authored-by: Jack <jack@anoma.ly> Co-authored-by: Shoubhit Dash <shoubhit2005@gmail.com> Co-authored-by: Dustin Deus <deusdustin@gmail.com> Co-authored-by: starptech <starptech@starptechs-MBP.fritz.box> Co-authored-by: Aiden Cline <aidenpcline@gmail.com> Co-authored-by: usrnk1 <7547651+usrnk1@users.noreply.github.com> Co-authored-by: Jay <53023+jayair@users.noreply.github.com> Co-authored-by: runvip <164729189+runvip@users.noreply.github.com> Co-authored-by: opencode <opencode@sst.dev> Co-authored-by: Julian Coy <julian@ex-machina.co> Co-authored-by: Vladimir Glafirov <vglafirov@gitlab.com>
80 lines
2.7 KiB
TypeScript
80 lines
2.7 KiB
TypeScript
import type { Argv, InferredOptionTypes } from "yargs"
|
|
import { ConfigV1 } from "@opencode-ai/core/v1/config/config"
|
|
import type { Config } from "@/config/config"
|
|
import { Effect } from "effect"
|
|
|
|
const options = {
|
|
port: {
|
|
type: "number" as const,
|
|
describe: "port to listen on",
|
|
default: 0,
|
|
},
|
|
hostname: {
|
|
type: "string" as const,
|
|
describe: "hostname to listen on",
|
|
default: "127.0.0.1",
|
|
},
|
|
mdns: {
|
|
type: "boolean" as const,
|
|
describe: "enable mDNS service discovery (defaults hostname to 0.0.0.0)",
|
|
default: false,
|
|
},
|
|
"mdns-domain": {
|
|
type: "string" as const,
|
|
describe: "custom domain name for mDNS service (default: opencode.local)",
|
|
default: "opencode.local",
|
|
},
|
|
cors: {
|
|
type: "string" as const,
|
|
array: true,
|
|
describe: "additional domains to allow for CORS",
|
|
default: [] as string[],
|
|
},
|
|
}
|
|
|
|
export type NetworkOptions = InferredOptionTypes<typeof options>
|
|
|
|
export function withNetworkOptions<T>(yargs: Argv<T>) {
|
|
return yargs.options(options)
|
|
}
|
|
|
|
export function hasArg(name: string) {
|
|
return networkArgs().some((arg) => arg === name || arg.startsWith(name + "="))
|
|
}
|
|
|
|
function hasBooleanArg(name: string) {
|
|
return networkArgs().some(
|
|
(arg) => arg === name || arg === name + "=true" || arg === name + "=false" || arg === "--no-" + name.slice(2),
|
|
)
|
|
}
|
|
|
|
function networkArgs() {
|
|
const separator = process.argv.indexOf("--")
|
|
return process.argv.slice(2, separator === -1 ? undefined : separator)
|
|
}
|
|
|
|
export const resolveNetworkOptions = Effect.fn("Cli.resolveNetworkOptions")(function* (args: NetworkOptions) {
|
|
const { Config } = yield* Effect.promise(() => import("@/config/config"))
|
|
const config = yield* Config.Service.use((cfg) => cfg.getGlobal())
|
|
return resolveNetworkOptionsNoConfig(args, config)
|
|
})
|
|
|
|
export function resolveNetworkOptionsNoConfig(args: NetworkOptions, config?: ConfigV1.Info) {
|
|
const portExplicitlySet = hasArg("--port")
|
|
const hostnameExplicitlySet = hasArg("--hostname")
|
|
const mdnsExplicitlySet = hasBooleanArg("--mdns")
|
|
const mdnsDomainExplicitlySet = hasArg("--mdns-domain")
|
|
const mdns = mdnsExplicitlySet ? args.mdns : (config?.server?.mdns ?? args.mdns)
|
|
const mdnsDomain = mdnsDomainExplicitlySet ? args["mdns-domain"] : (config?.server?.mdnsDomain ?? args["mdns-domain"])
|
|
const port = portExplicitlySet ? args.port : (config?.server?.port ?? args.port)
|
|
const hostname = hostnameExplicitlySet
|
|
? args.hostname
|
|
: mdns && !config?.server?.hostname
|
|
? "0.0.0.0"
|
|
: (config?.server?.hostname ?? args.hostname)
|
|
const configCors = config?.server?.cors ?? []
|
|
const argsCors = Array.isArray(args.cors) ? args.cors : args.cors ? [args.cors] : []
|
|
const cors = [...configCors, ...argsCors]
|
|
|
|
return { hostname, port, mdns, mdnsDomain, cors }
|
|
}
|