# Profil CLI, pembaruan, dan menunggu

Tiga hal yang membantu saat Anda memakai [alat baris perintah](/docs/cli) setiap hari atau di skrip. Masing-masing juga menunjukkan cara melakukan hal yang sama di kode dengan [SDK](/docs/sdks) atau API.

## Beberapa akun: profil

`apidly login --profile NAME` menyimpan kunci dengan sebuah nama, di samping kunci default Anda. Pilih profil untuk satu perintah dengan `--profile NAME` atau variabel lingkungan `APIDLY_PROFILE`, atau jadikan default dengan `apidly config use NAME`. `apidly config profiles` menampilkan profil dan `apidly config profiles rm NAME` menghapus satu. Jika `APIDLY_API_KEY` diatur, ia mengalahkan setiap profil tersimpan.

```bash
apidly login --profile work
apidly config profiles
apidly servers --profile work
APIDLY_PROFILE=work apidly balance
apidly config use work
apidly config profiles rm work
```

Kode tidak punya profil: setiap kunci adalah klien tersendiri. Simpan setiap kunci di variabel lingkungannya sendiri, jangan pernah di kode.

```bash
curl "https://apidly.com/api/v1/me" \
  -H "Authorization: Bearer $APIDLY_WORK_KEY"
```

```js
import { Apidly } from "@apidly/sdk";

const personal = new Apidly({ apiKey: process.env.APIDLY_API_KEY });
const work = new Apidly({ apiKey: process.env.APIDLY_WORK_KEY });
console.log((await work.billing.balance()).balance);
console.log((await personal.billing.balance()).balance);
```

```python
import os
from apidly import Client

personal = Client(api_key=os.environ["APIDLY_API_KEY"])
work = Client(api_key=os.environ["APIDLY_WORK_KEY"])
print(work.billing.balance()["balance"])
print(personal.billing.balance()["balance"])
```

## Perbarui CLI

`apidly update` mencari versi yang lebih baru, mengunduhnya dari situs ini, memeriksa checksum-nya, dan memasangnya dengan npm setelah bertanya kepada Anda. `--check` hanya melapor, dengan kode keluar `3` jika ada versi yang lebih baru, cocok untuk langkah CI; `--print` mencetak perintah alih-alih menjalankannya. Versi saat ini dan checksum setiap file juga ada di `latest.json`.

```bash
apidly --version
apidly update --check
apidly update --print
apidly update --yes

curl -fsS "https://apidly.com/downloads/latest.json" | jq -r '.version'
```

```js
const res = await fetch("https://apidly.com/downloads/latest.json");
const latest = await res.json();
const cli = latest.files.find((f) => f.kind === "cli");
console.log(latest.version, cli.name, cli.sha256);
```

```python
import json
import urllib.request

with urllib.request.urlopen("https://apidly.com/downloads/latest.json") as res:
    latest = json.load(res)
cli = next(f for f in latest["files"] if f["kind"] == "cli")
print(latest["version"], cli["name"], cli["sha256"])
```

## Tunggu server di skrip

`apidly servers wait ID` selesai dengan kode keluar `0` begitu server berjalan, atau `1` jika server tidak bisa mencapai keadaan itu, misalnya saat peluncuran gagal. `--for stopped` atau `--for destroyed` menunggu keadaan tersebut, dan `--timeout` menyerah setelah sekian detik (default 600). `apidly deploy --wait` dan `apidly stop --wait` menunggu di perintah yang sama. Ctrl-C mengakhiri penantian dengan kode keluar `130` dan membiarkan server apa adanya. Dengan API, baca `status` server setiap beberapa detik.

```bash
apidly servers wait ins_ID
apidly servers wait ins_ID --for stopped --timeout 300
apidly deploy --plan rtx-4090-1x --template pytorch --yes --wait
apidly stop ins_ID --wait

until [ "$(curl -fsS "https://apidly.com/api/v1/servers/ins_ID" \
  -H "Authorization: Bearer $APIDLY_API_KEY" | jq -r '.server.status')" = running ]; do
  sleep 5
done
```

```js
import { Apidly } from "@apidly/sdk";

const apidly = new Apidly({ apiKey: process.env.APIDLY_API_KEY });
for (;;) {
  const { server } = await apidly.servers.get("ins_ID");
  if (server.status === "running") break;
  if (server.status === "error" || server.status === "destroyed") {
    throw new Error(server.statusMessage ?? server.status);
  }
  await new Promise((r) => setTimeout(r, 5000));
}
```

```python
import os
import time
from apidly import Client

client = Client(api_key=os.environ["APIDLY_API_KEY"])
while True:
    server = client.servers.get("ins_ID")["server"]
    if server["status"] == "running":
        break
    if server["status"] in ("error", "destroyed"):
        raise SystemExit(server.get("statusMessage") or server["status"])
    time.sleep(5)
```
