Repository navigation
Expand file tree
/
Copy pathremote-cli.ts
More file actions
137 lines (112 loc) · 4.86 KB
/
Copy pathremote-cli.ts
File metadata and controls
137 lines (112 loc) · 4.86 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
export * as RemoteCli from "./remote-cli"
import { Effect, Schema } from "effect"
import { HttpClient, HttpClientResponse } from "effect/http"
export class Failure extends Schema.TaggedError<Failure>()("RemoteCliFailure", {
code: Schema.Literals(["platform", "version", "install"]),
detail: Schema.String,
}) {
override get message() {
return this.detail
}
}
export function quote(value: string) {
return `'${value.replaceAll("'", "'\\''")}'`
}
export function requireVersion(version: string) {
if (version !== "local" && !/^[0-9][a-zA-Z0-9.+-]*$/.test(version))
throw new Failure({ code: "version", detail: version })
return version
}
export function discoverScript(options: { fromPath?: boolean; cache?: { directory: string; prefix: string } } = {}) {
return `cli=${options.fromPath ? "$(command -v opencode || true)" : '""'}
if [ -z "$cli" ] && [ -x "$HOME/.opencode/bin/opencode" ]; then cli="$HOME/.opencode/bin/opencode"; fi
${
options.cache
? `if [ -z "$cli" ]; then
for binary in "$HOME"/${quote(options.cache.directory)}/${quote(options.cache.prefix)}*/opencode; do
if [ -x "$binary" ]; then cli="$binary"; fi
done
fi
`
: ""
}if [ -n "$cli" ]; then printf '%s\\n' "$cli"; fi
`
}
// Adapters supply a quoted shell expression, including remote HOME or wslpath expansion.
export function versionScript(command: string) {
return `if [ -x ${command} ]; then ${command} --version 2>/dev/null || true; fi\n`
}
export function parseVersion(output: string) {
const line = output
.split(/\r?\n/)
.find((line) => line.trim())
?.trim()
if (!line) return null
const marker = line.lastIndexOf(" v")
const version = marker === -1 ? line : line.slice(marker + 2)
if (!version) throw new Error("V2 CLI did not provide a version")
return version
}
export const probeScript = `set -eu
os=$(uname -s | tr '[:upper:]' '[:lower:]')
arch=$(uname -m)
case "$os" in linux|darwin) ;; *) exit 2 ;; esac
case "$arch" in x86_64|amd64) arch=x64 ;; aarch64|arm64) arch=arm64 ;; *) exit 2 ;; esac
target="$os-$arch"
if [ "$arch" = x64 ]; then target="$target-baseline"; fi
if [ "$os" = linux ]; then
if [ -f /etc/alpine-release ] || (ldd --version 2>&1 | grep -qi musl); then target="$target-musl"; fi
fi
printf 'OPENCODE_REMOTE_TARGET=%s\\n' "$target"
`
export function archiveUrl(target: string, version: string) {
if (!/^(linux|darwin)-(x64-baseline|arm64)(-musl)?$/.test(target))
throw new Failure({ code: "platform", detail: target })
return `https://registry.npmjs.org/@opencode/cli-${target}/-/cli-${target}-${requireVersion(version)}.tgz`
}
type Source = { type: "download"; url: string } | { type: "archive" } | { type: "installer"; binary?: string }
export function installScript(input: { version: string; directory?: string; source: Source }) {
const version = requireVersion(input.version)
// The managed CLI installer also configures the user's shell PATH. Private
// installations use archives so their destination and shell setup stay isolated.
if (input.source.type === "installer")
return `set -eu
curl -fsSL https://raw.githubusercontent.com/anomalyco/opencode/v2/install | bash -s -- ${input.source.binary ? `--binary ${input.source.binary}` : `--version ${quote(version)}`}
${verifyScript('"$HOME/.opencode/bin/opencode"', version)}
`
return `set -eu
umask 077
destination="$HOME"/${quote(`${input.directory ?? ".opencode/bin"}/opencode`)}
mkdir -p "$(dirname "$destination")"
stage=$(mktemp -d "$(dirname "$destination")/.install-XXXXXX")
trap 'rm -rf "$stage"' EXIT
${stageBinary(input.source)}
chmod 755 "$stage/package/bin/opencode"
${verifyScript('"$stage/package/bin/opencode"', version)}
mv "$stage/package/bin/opencode" "$destination"
`
}
function stageBinary(source: Exclude<Source, { type: "installer" }>) {
if (source.type === "archive") return 'cat > "$stage/archive.tgz"\ntar -xzf "$stage/archive.tgz" -C "$stage"'
return `url=${quote(source.url)}
if command -v curl >/dev/null 2>&1; then
curl -fsSL --connect-timeout 15 --max-time 180 "$url" -o "$stage/archive.tgz"
else
wget -T 180 -O "$stage/archive.tgz" "$url"
fi
tar -xzf "$stage/archive.tgz" -C "$stage"`
}
function verifyScript(command: string, version: string) {
return `test "$(${command} --version | awk '{print $NF}' | sed 's/^v//')" = ${quote(version)}`
}
const Beta = Schema.Struct({ version: Schema.String.check(Schema.isPattern(/^0\.0\.0-beta-\d+(?:\.\d+)?$/)) })
export const latestBeta = Effect.fn("RemoteCli.latestBeta")(function* () {
const http = yield* HttpClient.HttpClient
const metadata = yield* http.get("https://registry.npmjs.org/@opencode%2fcli/beta").pipe(
Effect.flatMap(HttpClientResponse.filterStatusOk),
Effect.flatMap(HttpClientResponse.schemaBodyJson(Beta)),
Effect.timeout("30 seconds"),
Effect.mapError(() => new Failure({ code: "install", detail: "https://registry.npmjs.org/@opencode%2fcli/beta" })),
)
return metadata.version
})