phala
Version:
CLI for Managing Phala Cloud Services
108 lines (105 loc) • 82.5 kB
JavaScript
#!/usr/bin/env bun
var gr=Object.defineProperty;var hr=(t,e,o)=>e in t?gr(t,e,{enumerable:!0,configurable:!0,writable:!0,value:o}):t[e]=o;var qe=(t,e,o)=>(hr(t,typeof e!="symbol"?e+"":e,o),o);import{Command as Ro}from"commander";import Ze from"chalk";var Ye=Ze.hex("#cdfa50"),he=Ye(`
\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u28C0\u28E0\u28E4\u28E4\u28E4\u28C0\u2840\u2800\u2880\u28C0\u28E4\u28E4\u28C0\u2840\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800
\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2880\u28F4\u287F\u281B\u2809\u2801\u2800\u2808\u2809\u281B\u283F\u281B\u2809\u2809\u2809\u2809\u2819\u2837\u28C4\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800
\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u28A0\u28FF\u280B\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2818\u28E7\u2800\u2800\u2800\u2800\u2800\u2800\u2800
\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u28C0\u28FF\u2847\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u28B8\u28C6\u2800\u2800\u2800\u2800\u2800\u2800
\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2880\u28F4\u281E\u281B\u2809\u2819\u2803\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2808\u2809\u281B\u28B6\u28C4\u2800\u2800\u2800
\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u28A0\u28FF\u2803\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2808\u28FF\u28E6\u2800\u2800
\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u28B8\u28FF\u2840\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u28A0\u28FF\u28FF\u2800\u2800
\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2838\u28FF\u28F7\u28C4\u2840\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u28C0\u28F4\u28FF\u28FF\u287F\u2800\u2800
\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2819\u28BF\u28FF\u28FF\u28FF\u28FF\u28FF\u28FF\u28FF\u28FF\u281F\u2800\u2800\u28C0\u28F4\u28F6\u28FF\u28FF\u28FF\u28FF\u28FF\u28FF\u28FF\u28FF\u28FF\u28FF\u28FF\u28FF\u28FF\u281F\u2801\u2800\u2800
\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2809\u281B\u283B\u283F\u283F\u283F\u283F\u2803\u2800\u2880\u28FE\u28FF\u28FF\u28FF\u283F\u283F\u283F\u283F\u283F\u283F\u283F\u283F\u283F\u283F\u281B\u280B\u2801\u2800\u2800\u2800\u2800
\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u28FE\u28FF\u287F\u280B\u2801\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800
\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2880\u28C0\u2864\u2824\u2824\u2834\u2812\u2812\u2812\u2812\u2812\u28BF\u287F\u2813\u2812\u2812\u2822\u2824\u2824\u2824\u28C0\u28C0\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800
\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u28FE\u28EF\u28C0\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2801\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u28C0\u28E8\u28FF\u28E6\u28E4\u28C0\u2840\u2800\u2800\u2800\u2800\u2800\u2800
\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u28BB\u28FF\u28FF\u28FF\u28FF\u28FF\u28F6\u28F6\u28F6\u28F6\u28F6\u28F6\u28F6\u28F6\u28F6\u28F6\u28F6\u28F6\u28FE\u28FF\u28FF\u28FF\u28FF\u28FF\u284F\u2809\u281B\u28FF\u28C6\u2800\u2800\u2800\u2800\u2800
\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2818\u28FF\u28FF\u28FF\u28FF\u28FF\u28FF\u28FF\u28FF\u2800\u2800\u28C0\u28C0\u28C0\u28C0\u2878\u283F\u28FF\u28FF\u28FF\u28FF\u28FF\u28FF\u28FF\u2800\u2800\u2800\u28F8\u287F\u2800\u2800\u2800\u2800\u2800
\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2839\u28FF\u28FF\u28FF\u28FF\u28FF\u28FF\u28FF\u2800\u28B8\u28FF\u28FF\u28FF\u28FF\u2847\u2800\u28FF\u28FF\u28FF\u28FF\u28FF\u28FF\u2803\u2880\u28E0\u28F4\u287F\u2803\u2800\u2800\u2800\u2800\u2800
\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2819\u28FF\u28FF\u28FF\u28FF\u28FF\u28FF\u283F\u2887\u28C0\u28C0\u28C0\u28C0\u28F8\u28FF\u28FF\u28FF\u28FF\u28FF\u287F\u283F\u283F\u281B\u281B\u2801\u2800\u2800\u2800\u2800\u2800\u2800\u2800
\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2860\u2800\u2800\u2800\u2800\u2808\u283B\u28BF\u28FF\u28FF\u28FF\u2800\u28B8\u28FF\u28FF\u28FF\u28FF\u28FF\u28FF\u28FF\u28FF\u287F\u280B\u2800\u2800\u2800\u2800\u2820\u28C4\u2800\u2800\u2800\u2800\u2800\u2800\u2800
\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u28B8\u28C6\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2809\u281B\u283F\u283F\u28FF\u28FF\u28FF\u28FF\u28FF\u283F\u281F\u280B\u2801\u2800\u2800\u2800\u2800\u2800\u2800\u28C0\u28FE\u2800\u2800\u2800\u2800\u2800\u2800\u2800
\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2819\u283B\u28B6\u28E6\u28E4\u28C4\u28C0\u28C0\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u28C0\u28C0\u28C0\u28E4\u28F4\u28F6\u283F\u281B\u2801\u2800\u2800\u2800\u2800\u2800\u2800\u2800
\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2808\u2809\u2819\u281B\u283B\u283F\u283F\u283F\u283F\u28BF\u28FF\u28FF\u28FF\u283F\u283F\u283F\u283F\u281F\u281B\u281B\u2809\u2801\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800
`),Lo=Ye(`
\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u28C0\u28E0\u28E4\u28E4\u28E4\u28C0\u2840\u2800\u2880\u28C0\u28E4\u28E4\u28C0\u2840\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800
\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2880\u28F4\u287F\u281B\u2809\u2801\u2800\u2808\u2809\u281B\u283F\u281B\u2809\u2809\u2809\u2809\u2819\u2837\u28C4\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800
\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u28A0\u28FF\u280B\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2818\u28E7\u2800\u2800\u2800\u2800\u2800\u2800\u2800
\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u28C0\u28FF\u2847\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u28B8\u28C6\u2800\u2800\u2800\u2800\u2800\u2800
\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2880\u28F4\u281E\u281B\u2809\u2819\u2803\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2808\u2809\u281B\u28B6\u28C4\u2800\u2800\u2800
\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u28A0\u28FF\u2803\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2808\u28FF\u28E6\u2800\u2800
\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u28B8\u28FF\u2840\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u28A0\u28FF\u28FF\u2800\u2800
\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2838\u28FF\u28F7\u28C4\u2840\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u28C0\u28F4\u28FF\u28FF\u287F\u2800\u2800
\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2819\u28BF\u28FF\u28FF\u28FF\u28FF\u28FF\u28FF\u28FF\u28FF\u281F\u2800\u2800\u28C0\u28F4\u28F6\u28FF\u28FF\u28FF\u28FF\u28FF\u28FF\u28FF\u28FF\u28FF\u28FF\u28FF\u28FF\u28FF\u281F\u2801\u2800\u2800
\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2809\u281B\u283B\u283F\u283F\u283F\u283F\u2803\u2800\u2880\u28FE\u28FF\u28FF\u28FF\u283F\u283F\u283F\u283F\u283F\u283F\u283F\u283F\u283F\u283F\u281B\u280B\u2801\u2800\u2800\u2800\u2800
\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u28FE\u28FF\u287F\u280B\u2801\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800
\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2880\u28C0\u2864\u2824\u2824\u2834\u2812\u2812\u2812\u2812\u2812\u28BF\u287F\u2813\u2812\u2812\u2822\u2824\u2824\u2824\u28C0\u28C0\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800
\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u28FE\u28EF\u28C0\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2801\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u28C0\u28E8\u28FF\u28E6\u28E4\u28C0\u2840\u2800\u2800\u2800\u2800\u2800\u2800
\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u28BB\u28FF\u28FF\u28FF\u28FF\u28FF\u28F6\u28F6\u28F6\u28F6\u28F6\u28F6\u28F6\u28F6\u28F6\u28F6\u28F6\u28F6\u28FE\u28FF\u28FF\u28FF\u28FF\u28FF\u284F\u2809\u281B\u28FF\u28C6\u2800\u2800\u2800\u2800\u2800
\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2818\u28FF\u28FF\u28FF\u28FF\u28FF\u28FF\u28FF\u28FF\u2800\u2800\u28C0\u28C0\u28C0\u28C0\u2878\u283F\u28FF\u28FF\u28FF\u28FF\u28FF\u28FF\u28FF\u2800\u2800\u2800\u28F8\u287F\u2800\u2800\u2800\u2800\u2800
\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2839\u28FF\u28FF\u28FF\u28FF\u28FF\u28FF\u28FF\u2800\u28B8\u28FF\u28FF\u28FF\u28FF\u2847\u2800\u28FF\u28FF\u28FF\u28FF\u28FF\u28FF\u2803\u2880\u28E0\u28F4\u287F\u2803\u2800\u2800\u2800\u2800\u2800
\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2819\u28FF\u28FF\u28FF\u28FF\u28FF\u28FF\u283F\u2887\u28C0\u28C0\u28C0\u28C0\u28F8\u28FF\u28FF\u28FF\u28FF\u28FF\u287F\u283F\u283F\u281B\u281B\u2801\u2800\u2800\u2800\u2800\u2800\u2800\u2800
\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2860\u2800\u2800\u2800\u2800\u2808\u283B\u28BF\u28FF\u28FF\u28FF\u2800\u28B8\u28FF\u28FF\u28FF\u28FF\u28FF\u28FF\u28FF\u28FF\u287F\u280B\u2800\u2800\u2800\u2800\u2820\u28C4\u2800\u2800\u2800\u2800\u2800\u2800\u2800
\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u28B8\u28C6\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2809\u281B\u283F\u283F\u28FF\u28FF\u28FF\u28FF\u28FF\u283F\u281F\u280B\u2801\u2800\u2800\u2800\u2800\u2800\u2800\u28C0\u28FE\u2800\u2800\u2800\u2800\u2800\u2800\u2800
\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2819\u283B\u28B6\u28E6\u28E4\u28C4\u28C0\u28C0\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u28C0\u28C0\u28C0\u28E4\u28F4\u28F6\u283F\u281B\u2801\u2800\u2800\u2800\u2800\u2800\u2800\u2800
\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2808\u2809\u2819\u281B\u283B\u283F\u283F\u283F\u283F\u28BF\u28FF\u28FF\u28FF\u283F\u283F\u283F\u283F\u281F\u281B\u281B\u2809\u2801\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800\u2800
`),No=Ze.cyan("PHALA TEE CLOUD CLI");import{Command as zr}from"commander";import{Command as Lr}from"commander";import V from"node:fs";import Ie from"node:path";import Z from"node:os";import ye from"node:crypto";import _ from"chalk";function yr(t){let e=["[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)","(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))"].join("|");return t.replace(new RegExp(e,"g"),"")}function q(t){return yr(t).length}function re(t,e){if(!t)return[""];if(q(t)<=e)return[t];let o=[],n="",s=0,a=t.split(/(\s+)/).filter(c=>c.trim().length>0);for(let c of a){let l=q(c);if(l>e){n&&(o.push(n),n="",s=0),o.push(c);continue}s+l+(s>0?1:0)>e?(o.push(n),n=c,s=l):n?(n=`${n} ${c}`,s+=l+1):(n=c,s=l)}return n&&o.push(n),o}var r={error:(t,...e)=>{console.error(_.red("\u2717"),_.red(t),...e)},warn:(t,...e)=>{console.log(_.yellow("\u26A0"),_.yellow(t),...e)},info:(t,...e)=>{console.log(_.blue("\u2139"),_.blue(t),...e)},success:(t,...e)=>{console.log(_.green("\u2713"),_.green(t),...e)},debug:(t,...e)=>{process.env.DEBUG&&console.log(_.gray("\u{1F50D}"),_.gray(t),...e)},table:(t,e)=>{if(t.length===0){console.log(_.yellow("No data to display"));return}if(e)if(typeof e[0]=="string"){let o=e.map(n=>({key:n,header:n.charAt(0).toUpperCase()+n.slice(1)}));Te(t,{columns:o,borderStyle:"rounded",headerStyle:n=>_.cyan.bold(n)})}else Te(t,{columns:e,borderStyle:"rounded",headerStyle:o=>_.cyan.bold(o)});else Te(t,{borderStyle:"rounded",headerStyle:o=>_.cyan.bold(o)})},keyValueTable:(t,e)=>{let n={...{borderStyle:"rounded",enableTextWrapping:!0,maxDepth:2,formatKeys:!0,keyFormatter:w=>{let E=["URL","ID","API","UI","URI","CPU","GPU","RAM","JSON","XML","HTML","HTTP","HTTPS","SSH","FTP","IP","TCP","UDP","DNS","SSL","TLS","SQL","VCPU","CVM","TEE","IO"],I={teepod:"TEEPod",dstack:"Dstack"},M;w.includes("_")?M=w.split("_").map(F=>F.charAt(0).toUpperCase()+F.slice(1).toLowerCase()).join(" "):w.includes("-")?M=w.split("-").map(F=>F.charAt(0).toUpperCase()+F.slice(1).toLowerCase()).join(" "):M=w.charAt(0).toUpperCase()+w.slice(1).replace(/([a-z])([A-Z])/g,"$1 $2");for(let F of E){let O=new RegExp(`\\b${F.toLowerCase()}\\b`,"gi");M=M.replace(O,F)}for(let[F,O]of Object.entries(I)){let ge=new RegExp(`\\b${F}\\b`,"gi");M=M.replace(ge,O)}return M},valueFormatter:w=>String(w??"")},...e},{include:s,exclude:a,keyFormatter:c,valueFormatter:l,formatKeys:h,keyWidth:f,valueWidth:C,borderStyle:k,enableTextWrapping:m,maxDepth:p}=n,u=Object.keys(t);if(s&&(u=u.filter(w=>s.includes(w))),a&&(u=u.filter(w=>!a.includes(w))),u.length===0){console.log(_.yellow("No properties to display"));return}let g=u.map(w=>{let E=t[w],I;return E==null?I="":typeof E=="object"&&!Array.isArray(E)?I=Je(E,0,p):Array.isArray(E)?E.length===0?I="[]":typeof E[0]=="object"?I=`[${E.length} items]`:I=`[${E.join(", ")}]`:I=String(E),l&&(I=l(E,w)),{key:h&&c?_.cyan.bold(c(w)):_.cyan.bold(w),value:I}}),b=Xe(),d=f,v=C;if(d||(d=Math.max(...g.map(w=>q(w.key)),10),d=Math.min(d,Math.floor(b/3))),!v){v=Math.max(...g.map(I=>q(I.value)),10),v+=3;let w=7,E=b-d-w;v=Math.min(v,E)}let y=br(k),x=`${y.topLeft}${y.horizontal.repeat(d+2)}${y.topT}${y.horizontal.repeat(v+2)}${y.topRight}`,L=`${y.leftT}${y.horizontal.repeat(d+2)}${y.cross}${y.horizontal.repeat(v+2)}${y.rightT}`;console.log(x),console.log(L),g.forEach((w,E)=>{let I=m?re(w.key,d):[w.key],M=m?re(w.value,v):[w.value],F=Math.max(I.length,M.length);for(let O=0;O<F;O++){let ge=I[O]||"",He=M[O]||"",ur=ge+" ".repeat(Math.max(0,d-q(ge))),fr=He+" ".repeat(Math.max(0,v-q(He)));console.log(`${y.vertical} ${ur} ${y.vertical} ${fr} ${y.vertical}`)}E<g.length-1&&console.log(`${y.leftT}${y.horizontal.repeat(d+2)}${y.cross}${y.horizontal.repeat(v+2)}${y.rightT}`)});let $e=`${y.bottomLeft}${y.horizontal.repeat(d+2)}${y.bottomT}${y.horizontal.repeat(v+2)}${y.bottomRight}`;console.log($e)},startSpinner:t=>(process.stdout.write(`${_.blue("\u27F3")} ${t}... `),{stop:(e=!0,o)=>{let n=e?_.green("\u2713"):_.red("\u2717"),s=o?`: ${o}`:"";console.log(`${n}${s}`)}}),break(){console.log("")}};function Je(t,e,o){if(e>=o)return"[Nested Object]";if(t==null)return"";if(Array.isArray(t))return t.length===0?"[]":`[${t.length} items]`;let n=[];for(let[s,a]of Object.entries(t))a==null?n.push(`${s}: `):typeof a=="object"?n.push(`${s}: ${Je(a,e+1,o)}`):n.push(`${s}: ${a}`);return n.join(", ")}function br(t="single"){return{single:{topLeft:"\u250C",topRight:"\u2510",bottomLeft:"\u2514",bottomRight:"\u2518",horizontal:"\u2500",vertical:"\u2502",leftT:"\u251C",rightT:"\u2524",topT:"\u252C",bottomT:"\u2534",cross:"\u253C"},double:{topLeft:"\u2554",topRight:"\u2557",bottomLeft:"\u255A",bottomRight:"\u255D",horizontal:"\u2550",vertical:"\u2551",leftT:"\u2560",rightT:"\u2563",topT:"\u2566",bottomT:"\u2569",cross:"\u256C"},rounded:{topLeft:"\u256D",topRight:"\u256E",bottomLeft:"\u2570",bottomRight:"\u256F",horizontal:"\u2500",vertical:"\u2502",leftT:"\u251C",rightT:"\u2524",topT:"\u252C",bottomT:"\u2534",cross:"\u253C"}}[t]}function Xe(){return process.stdout.columns||80}function vr(t,e,o={}){let n=Xe(),s=o.borderChars??3,c=(o.additionalBorderWidth??1)+e.length*s,l=n-c,h={},f=0,C=0;for(let p of e){let u=p.key;if(p.fixedWidth!==void 0){h[u]=p.fixedWidth,f+=p.fixedWidth;continue}let g=p.minWidth??p.header.length,b;p.getWidth?b=Math.max(g,p.header.length,...t.map(d=>p.getWidth(d))):p.getValue?b=Math.max(g,p.header.length,...t.map(d=>String(p.getValue(d)||"").length)):b=Math.max(g,p.header.length,...t.map(d=>String(d[p.key]||"").length)),h[u]=b,f+=b,C+=p.weight??1}let k=Math.max(0,l-f);if(k>0&&C>0)for(let p of e){let u=p.key;if(p.fixedWidth===void 0&&p.weight){let g=Math.floor(k*(p.weight/C));h[u]+=g}}let m=Object.values(h).reduce((p,u)=>p+u,0)+c;if(m>n){let p=l/(m-c),u={};for(let g of e){let b=g.key,d=g.minWidth??g.header.length;u[b]=Math.max(d,Math.floor(h[b]*p))}return u}return h}function Te(t,e={}){if(e.keyValueMode&&t.length===1){let m=t[0],p=[];e.columns||(e.columns=Object.keys(m).map(u=>({key:u,header:u.charAt(0).toUpperCase()+u.slice(1).replace(/([A-Z])/g," $1")})));for(let u of e.columns){let g=String(u.key),b;if(u.accessor)b=u.accessor(m);else if(typeof u.key=="string"&&u.key.includes(".")){let d=u.key.split("."),v=m;for(let y of d){if(v==null){b="";break}v=v[y]}b=v}else b=m[u.key];u.formatter&&(b=u.formatter(b)),p.push({key:u.header||g,value:b})}t=p,e.columns=[{key:"key",minWidth:15},{key:"value",minWidth:20}]}if(t.length===0){console.log(_.yellow("No data to display"));return}let n={...{includeHeaders:!0,border:!0,borderStyle:"single",headerStyle:m=>_.bold(m),cellStyle:m=>m,enableTextWrapping:!0},...e},s=n.columns;if(s)s=s.map(m=>({...m,enableTextWrapping:m.enableTextWrapping!==void 0?m.enableTextWrapping:n.enableTextWrapping}));else{let m=t[0];s=Object.keys(m).map(p=>({key:p,header:p.charAt(0).toUpperCase()+p.slice(1),enableTextWrapping:n.enableTextWrapping}))}let c={single:{topLeft:"\u250C",topRight:"\u2510",bottomLeft:"\u2514",bottomRight:"\u2518",horizontal:"\u2500",vertical:"\u2502",leftT:"\u251C",rightT:"\u2524",topT:"\u252C",bottomT:"\u2534",cross:"\u253C"},double:{topLeft:"\u2554",topRight:"\u2557",bottomLeft:"\u255A",bottomRight:"\u255D",horizontal:"\u2550",vertical:"\u2551",leftT:"\u2560",rightT:"\u2563",topT:"\u2566",bottomT:"\u2569",cross:"\u256C"},rounded:{topLeft:"\u256D",topRight:"\u256E",bottomLeft:"\u2570",bottomRight:"\u256F",horizontal:"\u2500",vertical:"\u2502",leftT:"\u251C",rightT:"\u2524",topT:"\u252C",bottomT:"\u2534",cross:"\u253C"}}[n.borderStyle],l=s.map(m=>({key:m.key,header:m.header||String(m.key),minWidth:m.minWidth||3,weight:m.weight,enableTextWrapping:m.enableTextWrapping,getValue:m.accessor?p=>m.accessor(p):p=>{if(typeof m.key=="string"&&m.key.includes(".")){let u=m.key.split("."),g=p;for(let b of u){if(g==null)return"";g=g[b]}return g??""}return p[m.key]??""}})),h=vr(t,l),f=s.map((m,p)=>({...m,width:h[l[p].key]})),C=f.map(m=>m.header||String(m.key)),k=(m,p)=>{let u=f.map(d=>{let v;if(d.accessor)v=d.accessor(m);else if(typeof d.key=="string"&&d.key.includes(".")){let x=d.key.split("."),L=m;for(let $e of x){if(L==null){v="";break}L=L[$e]}v=L??""}else v=m[d.key]??"";let y=d.formatter?d.formatter(v):String(v||"");return d.enableTextWrapping?{lines:re(y,d.width),key:String(d.key)}:{lines:[y.length>d.width?y.substring(0,d.width-1)+"\u2026":y],key:String(d.key)}}),g=Math.max(...u.map(d=>d.lines.length)),b=[];for(let d=0;d<g;d++){let v=u.map((y,x)=>{let L=y.lines[d]||"";return n.cellStyle(L.padEnd(f[x].width),p,y.key)});b.push(v)}return b};if(n.border){let m=c.topLeft+f.map(u=>c.horizontal.repeat(u.width+2)).join(c.topT)+c.topRight;if(console.log(m),n.includeHeaders){let u=f.map((d,v)=>({lines:d.enableTextWrapping?re(C[v],d.width):[C[v]],width:d.width})),g=Math.max(...u.map(d=>d.lines.length));for(let d=0;d<g;d++){let v=c.vertical+u.map(y=>{let x=y.lines[d]||"";return" "+n.headerStyle(x.padEnd(y.width))+" "}).join(c.vertical)+c.vertical;console.log(v)}let b=c.leftT+f.map(d=>c.horizontal.repeat(d.width+2)).join(c.cross)+c.rightT;console.log(b)}t.forEach((u,g)=>{let b=k(u,g);if(b.forEach(d=>{console.log(c.vertical+d.map(v=>` ${v} `).join(c.vertical)+c.vertical)}),g<t.length-1&&b.length>1){let d=c.leftT+f.map(v=>c.horizontal.repeat(v.width+2)).join(c.cross)+c.rightT;console.log(d)}});let p=c.bottomLeft+f.map(u=>c.horizontal.repeat(u.width+2)).join(c.bottomT)+c.bottomRight;console.log(p)}else{if(n.includeHeaders){let m=f.map((g,b)=>({lines:g.enableTextWrapping?re(C[b],g.width):[C[b]],width:g.width})),p=Math.max(...m.map(g=>g.lines.length));for(let g=0;g<p;g++){let b=m.map(d=>{let v=d.lines[g]||"";return n.headerStyle(v.padEnd(d.width))}).join(" ");console.log(b)}let u=f.map(g=>"\u2500".repeat(g.width)).join(" ");console.log(u)}t.forEach((m,p)=>{let u=k(m,p);u.forEach(g=>{console.log(g.join(" "))}),p<t.length-1&&u.length>1&&console.log("")})}console.log(`Total: ${t.length} rows`)}var oe=Ie.join(Z.homedir(),".phala-cloud"),ne=Ie.join(oe,"api-key"),Pe=Ie.join(oe,"docker-credentials.json");function Qe(){if(!V.existsSync(oe))try{V.mkdirSync(oe,{recursive:!0})}catch(t){throw r.error(`Failed to create directory ${oe}:`,t),t}}function et(){let t=[Z.hostname(),Z.platform(),Z.arch(),Z.cpus()[0]?.model||"",Z.userInfo().username],e=ye.createHash("sha256");return e.update(t.join("|")),e.digest()}function Cr(t){try{let e=et(),o=ye.randomBytes(16),n=ye.createCipheriv("aes-256-cbc",e.slice(0,32),o),s=n.update(t,"utf8","hex");return s+=n.final("hex"),o.toString("hex")+":"+s}catch(e){throw r.error("Encryption failed:",e),new Error("Failed to encrypt data")}}function kr(t){try{let e=et(),o=t.split(":");if(o.length!==2)throw new Error("Invalid encrypted format");let n=Buffer.from(o[0],"hex"),s=o[1],a=ye.createDecipheriv("aes-256-cbc",e.slice(0,32),n),c=a.update(s,"hex","utf8");return c+=a.final("utf8"),c}catch(e){throw r.error("Decryption failed:",e),new Error("Failed to decrypt data")}}async function De(t){Qe();try{let e=Cr(t);V.writeFileSync(ne,e,{mode:384})}catch(e){throw r.error("Failed to save API key:",e),e}}async function Y(){try{if(V.existsSync(ne)){let t=V.readFileSync(ne,"utf8").trim();return kr(t)}return null}catch(t){return r.error("Failed to read API key:",t),null}}async function se(){try{V.existsSync(ne)?(V.unlinkSync(ne),r.success("API key removed successfully.")):r.warn("No API key found to remove.")}catch(t){throw r.error("Failed to remove API key:",t),t}}async function Ae(t){Qe();try{V.writeFileSync(Pe,JSON.stringify(t,null,2),{mode:384}),r.success("Docker information saved successfully.")}catch(e){throw r.error("Failed to save Docker information:",e),e}}async function N(){try{if(V.existsSync(Pe)){let t=V.readFileSync(Pe,"utf8");return JSON.parse(t)}return null}catch(t){return r.error("Failed to read Docker credentials:",t),null}}import Nr from"prompts";import wr from"axios";var Re=process.env.CLOUD_API_URL||"https://cloud-api.phala.network",S=process.env.CLOUD_URL||"https://cloud.phala.network",tt="0.0.1";var rt="phalanetwork/tappd-simulator:latest",Fe=2,Me=4096,Ve=40;var ot="dstack-0.3.5",$={USER_INFO:"/api/v1/auth/me",TEEPODS:"/api/v1/teepods/available",TEEPOD_IMAGES:t=>`/api/v1/teepods/${t}/images`,CVMS:t=>`/api/v1/cvms?user_id=${t}`,CVM_BY_APP_ID:t=>`/api/v1/cvms/app_${t}`,CVM_NETWORK:t=>`/api/v1/cvms/app_${t}/network`,CVM_START:t=>`/api/v1/cvms/app_${t}/start`,CVM_STOP:t=>`/api/v1/cvms/app_${t}/stop`,CVM_RESTART:t=>`/api/v1/cvms/app_${t}/restart`,CVM_LOGS:t=>`/api/v1/cvms/app_${t}/logs`,CVM_FROM_CONFIGURATION:"/api/v1/cvms/from_cvm_configuration",CVM_PUBKEY:"/api/v1/cvms/pubkey/from_cvm_configuration",CVM_UPGRADE:t=>`/api/v1/cvms/app_${t}/compose`,CVM_ATTESTATION:t=>`/api/v1/cvms/app_${t}/attestation`,CVM_RESIZE:t=>`/api/v1/cvms/app_${t}/resources`},nt=`version: '3.8'
services:
postgres:
image: postgres:15
environment:
- POSTGRES_PASSWORD=postgres
- POSTGRES_USER=postgres
- POSTGRES_DB=eliza
volumes:
- postgres-data:/var/lib/postgresql/data
ports:
- "127.0.0.1:5432:5432"
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
timeout: 5s
retries: 5
restart: always
eliza:
image: {{imageName}}
container_name: elizav2
command: bun run start
stdin_open: true
tty: true
volumes:
- /var/run/tappd.sock:/var/run/tappd.sock
environment:
{{#each envVars}} - {{{this}}}
{{/each}}
ports:
- "3000:3000"
- "5173:5173"
depends_on:
postgres:
condition: service_healthy
restart: always
networks:
- eliza-network
networks:
eliza-network:
driver: bridge
volumes:
postgres-data:`,st=`version: '3.8'
services:
app:
image: {{imageName}}
container_name: app
volumes:
- /var/run/tappd.sock:/var/run/tappd.sock
environment:
{{#each envVars}} - {{{this}}}
{{/each}}
restart: always
`;function Le(t){try{return JSON.stringify(t)}catch(e){return e instanceof Error&&e.message.includes("cyclic")?"[Cyclic Object]":String(t)}}var Ne=class{client;apiKey=null;constructor(e){r.debug(`Creating API client with base URL: ${e}`),this.client=wr.create({baseURL:e,headers:{"Content-Type":"application/json","User-Agent":`tee-cloud-cli/${tt}`}}),this.client.interceptors.request.use(async o=>{if(!this.apiKey){if(this.apiKey=await Y(),!this.apiKey)throw new Error('API key not found. Please set an API key first with "phala auth login"');r.debug(`API key loaded: ${this.apiKey.substring(0,5)}...`)}return o.headers["X-API-Key"]=this.apiKey,r.debug(`Making request to: ${o.baseURL}${o.url}`),o}),this.client.interceptors.response.use(o=>(r.debug(`Received successful response from: ${o.config.url}`),o),o=>{if(o.response){let{status:n,data:s}=o.response;r.debug(`Received error response: ${n} - ${Le(s)}`),n===401?r.error("Authentication failed. Please check your API key."):n===403?r.error("You do not have permission to perform this action."):n===404?r.error("Resource not found."):r.error(`API Error (${n}): ${s.message||Le(s)}`)}else o.request?(r.error("No response received from the server. Please check your internet connection."),r.debug(`Request details: ${Le(o.request).substring(0,200)}...`)):r.error(`Error: ${o.message}`);return Promise.reject(o)})}async get(e,o){try{return r.debug(`GET request to: ${e}`),(await this.client.get(e,o)).data}catch(n){throw r.debug(`GET request failed: ${n instanceof Error?n.message:String(n)}`),n}}async post(e,o,n){try{return r.debug(`POST request to: ${e}`),(await this.client.post(e,o,n)).data}catch(s){throw r.debug(`POST request failed: ${s instanceof Error?s.message:String(s)}`),s}}async put(e,o,n){try{return r.debug(`PUT request to: ${e}`),(await this.client.put(e,o,n)).data}catch(s){throw r.debug(`PUT request failed: ${s instanceof Error?s.message:String(s)}`),s}}async delete(e,o){try{return r.debug(`DELETE request to: ${e}`),(await this.client.delete(e,o)).data}catch(n){throw r.debug(`DELETE request failed: ${n instanceof Error?n.message:String(n)}`),n}}async patch(e,o,n){try{return r.debug(`PATCH request to: ${e}`),(await this.client.patch(e,o,n)).data}catch(s){throw r.debug(`PATCH request failed: ${s instanceof Error?s.message:String(s)}`),s}}};r.debug(`Initializing API client with URL: ${Re}`);var T=new Ne(Re);import{z as i}from"zod";import{INVALID as _r,ParseStatus as Sr,ZodIssueCode as J,ZodParsedType as it,ZodType as xr,addIssueToContext as X,z as at}from"zod";var Er="ZodDecimal",$r=/(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/,ae=class extends xr{_parse(e){if(e.data!==null&&typeof e.data=="object"&&"toNumber"in e.data&&(e.data=e.data.toNumber()),this._def.coerce&&(e.data=Number(e.data)),this._getType(e)!==it.number){let a=this._getOrReturnCtx(e);return X(a,{code:J.invalid_type,expected:it.number,received:a.parsedType}),_r}let n,s=new Sr;for(let a of this._def.checks)if(a.kind==="precision"){let c=e.data.toString().match($r);Math.max((c[1]?c[1].length:0)-(c[2]?parseInt(c[2],10):0),0)>a.value&&(n=this._getOrReturnCtx(e,n),X(n,{code:J.custom,message:a.message,params:{precision:a.value}}),s.dirty())}else a.kind==="wholeNumber"?e.data.toString().split(".")[0].length>a.value&&(n=this._getOrReturnCtx(e,n),X(n,{code:J.custom,message:a.message,params:{wholeNumber:a.value}}),s.dirty()):a.kind==="min"?(a.inclusive?e.data<a.value:e.data<=a.value)&&(n=this._getOrReturnCtx(e,n),X(n,{code:J.too_small,minimum:a.value,type:"number",inclusive:a.inclusive,exact:!1,message:a.message}),s.dirty()):a.kind==="max"?(a.inclusive?e.data>a.value:e.data>=a.value)&&(n=this._getOrReturnCtx(e,n),X(n,{code:J.too_big,maximum:a.value,type:"number",inclusive:a.inclusive,exact:!1,message:a.message}),s.dirty()):a.kind==="finite"&&(Number.isFinite(e.data)||(n=this._getOrReturnCtx(e,n),X(n,{code:J.not_finite,message:a.message}),s.dirty()));return{status:s.value,value:e.data}}setLimit(e,o,n,s){return new ae({...this._def,checks:[...this._def.checks,{kind:e,value:o,inclusive:n,message:s}]})}_addCheck(e){return new ae({...this._def,checks:[...this._def.checks,e]})}lte(e,o){return this.setLimit("max",e,!0,o)}lt(e,o){return this.setLimit("max",e,!1,o)}max=this.lte;gt(e,o){return this.setLimit("min",e,!1,o)}gte(e,o){return this.setLimit("min",e,!0,o)}min=this.gte;precision(e,o){return this._addCheck({kind:"precision",value:e,message:o})}wholeNumber(e,o){return this._addCheck({kind:"wholeNumber",value:e,message:o})}get minValue(){let e=null;for(let o of this._def.checks)o.kind==="min"&&(e===null||o.value>e)&&(e=o.value);return e}get maxValue(){let e=null;for(let o of this._def.checks)o.kind==="max"&&(e===null||o.value<e)&&(e=o.value);return e}positive(e){return this._addCheck({kind:"min",value:0,inclusive:!1,message:e})}negative(e){return this._addCheck({kind:"max",value:0,inclusive:!1,message:e})}nonpositive(e){return this._addCheck({kind:"max",value:0,inclusive:!0,message:e})}nonnegative(e){return this._addCheck({kind:"min",value:0,inclusive:!0,message:e})}finite(e){return this._addCheck({kind:"finite",message:e})}safe(e){return this._addCheck({kind:"min",inclusive:!0,value:Number.MIN_SAFE_INTEGER,message:e})._addCheck({kind:"max",inclusive:!0,value:Number.MAX_SAFE_INTEGER,message:e})}get isFinite(){let e=null,o=null;for(let n of this._def.checks){if(n.kind==="finite")return!0;n.kind==="min"?(o===null||n.value>o)&&(o=n.value):n.kind==="max"&&(e===null||n.value<e)&&(e=n.value)}return Number.isFinite(o)&&Number.isFinite(e)}},ie=ae;qe(ie,"create",e=>new ae({checks:[],typeName:Er,coerce:e?.coerce??!1}));var ct=at.object({template:at.string().min(1,"Template cannot be empty")});var Tr=i.object({password:i.string(),registry:i.string().nullable(),username:i.string()}),Pr=i.object({docker_compose_file:i.string(),docker_config:Tr.optional(),features:i.array(i.string()),kms_enabled:i.boolean(),manifest_version:i.number(),name:i.string(),public_logs:i.boolean(),public_sysinfo:i.boolean(),runner:i.string().optional(),salt:i.string().nullable().optional(),tproxy_enabled:i.boolean(),version:i.string().optional()}),Ir=i.object({name:i.string(),image:i.string(),compose_file:Pr,vcpu:i.number(),memory:i.number(),disk_size:i.number(),ports:i.array(i.any())}),Dr=i.object({id:i.string(),name:i.string(),status:i.string(),uptime:i.string(),app_url:i.string(),app_id:i.string(),instance_id:i.string(),configuration:Ir,exited_at:i.string(),boot_progress:i.string(),boot_error:i.string(),shutdown_progress:i.string(),image_version:i.string()}),Ar=i.object({id:i.number(),username:i.string()}),Rr=i.object({id:i.number(),name:i.string()}),Oe=i.object({hosted:Dr,name:i.string(),managed_user:Ar,node:Rr,listed:i.boolean(),status:i.string(),in_progress:i.boolean(),dapp_dashboard_url:i.string().nullable(),syslog_endpoint:i.string(),allow_upgrade:i.boolean()}),ce=i.object({id:i.number(),name:i.string(),status:i.string(),teepod_id:i.number().nullable(),teepod:i.object({id:i.number(),name:i.string()}).nullable(),user_id:i.number(),app_id:i.string(),vm_uuid:i.string().nullable(),instance_id:i.string().nullable(),app_url:i.string().nullable(),base_image:i.string(),vcpu:i.number(),memory:i.number(),disk_size:i.number(),manifest_version:i.number(),version:i.string(),runner:i.string(),docker_compose_file:i.string(),features:i.array(i.string()).nullable(),created_at:i.string(),encrypted_env_pubkey:i.string()}),mt=i.object({app_env_encrypt_pubkey:i.string(),app_id_salt:i.string()}),pt=i.object({id:i.number(),teepod_id:i.number().nullable(),teepod:i.object({id:i.number(),name:i.string()}).nullable(),name:i.string(),status:i.string(),in_progress:i.boolean(),app_id:i.string(),vm_uuid:i.string(),instance_id:i.string(),vcpu:i.number(),memory:i.number(),disk_size:i.number(),base_image:i.string(),encrypted_env_pubkey:i.string(),listed:i.boolean(),project_id:i.string(),project_type:i.string().nullable()}),lt=i.object({username:i.string(),email:i.string(),credits:ie.create({coerce:!0}),role:i.string(),avatar:i.string(),flag_reset_password:i.boolean(),team_name:i.string(),team_tier:i.string(),trial_ended_at:i.string().nullable()}),un=i.array(Oe),dt=i.object({detail:i.string()}),fn=i.object({key:i.string(),value:i.string()}),ut=i.object({name:i.string(),description:i.string().optional(),version:i.array(i.number()).optional(),is_dev:i.boolean().optional(),rootfs_hash:i.string().optional(),shared_ro:i.boolean().optional(),cmdline:i.string().optional(),kernel:i.string().optional(),initrd:i.string().optional(),hda:i.string().nullable().optional(),rootfs:i.string().optional(),bios:i.string().optional()}),Fr=i.object({teepod_id:i.number().nullable(),name:i.string(),listed:i.boolean(),resource_score:i.number(),remaining_vcpu:i.number(),remaining_memory:i.number(),remaining_cvm_slots:i.number(),images:i.array(ut).optional()}),Mr=i.object({max_instances:i.number().nullable(),max_vcpu:i.number().nullable(),max_memory:i.number().nullable(),max_disk:i.number().nullable()}),ft=i.object({tier:i.string(),capacity:Mr,nodes:i.array(Fr)}),Vr=i.object({is_online:i.boolean(),is_public:i.boolean(),error:i.string().nullable(),internal_ip:i.string(),latest_handshake:i.string(),public_urls:i.array(i.object({app:i.string(),instance:i.string()}))}),gt=i.object({is_online:i.boolean(),is_public:i.boolean(),error:i.string().nullable(),app_certificates:i.array(i.object({subject:i.object({common_name:i.string().nullable(),organization:i.string().nullable(),country:i.string().nullable(),state:i.string().nullable().optional(),locality:i.string().nullable().optional()}),issuer:i.object({common_name:i.string().nullable(),organization:i.string().nullable(),country:i.string().nullable()}),serial_number:i.string(),not_before:i.string(),not_after:i.string(),version:i.string(),fingerprint:i.string(),signature_algorithm:i.string(),sans:i.string().nullable(),is_ca:i.boolean(),position_in_chain:i.number(),quote:i.string().nullable()})).nullable(),tcb_info:i.object({mrtd:i.string(),rootfs_hash:i.string(),rtmr0:i.string(),rtmr1:i.string(),rtmr2:i.string(),rtmr3:i.string(),event_log:i.array(i.object({imr:i.number(),event_type:i.number(),digest:i.string(),event:i.string(),event_payload:i.string()}))}).nullable(),compose_file:i.string().nullable()});function ht(t){try{return JSON.stringify(t)}catch(e){return e instanceof Error&&e.message.includes("cyclic")?"[Cyclic Object]":String(t)}}async function j(){try{r.debug(`Fetching user info from ${$.USER_INFO}`);let t=await T.get($.USER_INFO);r.debug(`Received response: ${ht(t)}`);try{return lt.parse(t)}catch(e){throw r.error(`Failed to parse user info response: ${e}`),r.debug(`Response structure: ${ht(t)}`),e}}catch(t){throw r.error(`Failed to get user info: ${t instanceof Error?t.message:String(t)}`),new Error(`Failed to get user info: ${t instanceof Error?t.message:String(t)}`)}}var yt=new Lr().name("login").description("Set the API key for authentication").argument("[api-key]","Phala Cloud API key to set").action(async t=>{try{let e;if(!t)t=(await Nr({type:"password",name:"apiKey",message:"Enter your API key:",validate:async n=>{if(n.length===0)return"API key cannot be empty";try{if(await De(n),e=await j(),!e.username)return await se(),"Invalid API key"}catch{return"Invalid API key"}return!0}})).apiKey;else if(await De(t),e=await j(),!e.username)return await se(),"Invalid API key";r.success(`Welcome ${e.username}! API key validated and saved successfully`),r.break(),r.info(`Open in Web UI at ${S}/dashboard/`)}catch(e){r.error(`Failed to set API key: ${e instanceof Error?e.message:String(e)}`),process.exit(1)}});import{Command as Or}from"commander";var bt=new Or().name("logout").description("Remove the stored API key").action(async()=>{try{await se(),r.success("API key removed successfully")}catch(t){r.error(`Failed to remove API key: ${t instanceof Error?t.message:String(t)}`),process.exit(1)}});import{Command as Ur}from"commander";var vt=new Ur().name("status").description("Check authentication status").option("-j, --json","Output in JSON format").option("-d, --debug","Enable debug output").action(async t=>{try{t.debug&&(process.env.DEBUG="true");let e=await Y();if(!e){r.warn('Not authenticated. Please set an API key with "phala auth login"');return}r.debug(`Using API key: ${e.substring(0,5)}...`);let o=r.startSpinner("Checking authentication status");try{let n=await j();if(o.stop(!0),t.json){console.log(JSON.stringify(n,null,2));return}r.break(),r.success(`Authenticated as ${n.username}`);let s={Username:n.username,Email:n.email,Role:n.role,Team:`${n.team_name} (${n.team_tier})`,Credits:`$${n.credits}`};n.trial_ended_at&&(s["Trial Ended At"]=n.trial_ended_at),r.keyValueTable(s,{borderStyle:"rounded"})}catch(n){o.stop(!1),r.error("Authentication failed. Your API key may be invalid or expired."),r.info('Please set a new API key with "phala auth login"'),t.debug&&r.debug(`Error details: ${n instanceof Error?n.message:String(n)}`)}}catch(e){r.error(`Failed to check authentication status: ${e instanceof Error?e.message:String(e)}`),process.exit(1)}});var Ct=new zr().name("auth").description("Authenticate with Phala Cloud").addCommand(yt).addCommand(bt).addCommand(vt);import{Command as oo}from"commander";import{Command as Jr}from"commander";import{execa as xt}from"execa";import K from"node:fs";import Be from"node:path";import Gr from"handlebars";import{exec as Kr,spawn as Hr}from"node:child_process";import{promisify as qr}from"node:util";import Zr from"node:os";import jr from"inquirer";import Ue from"node:fs";import ze from"node:path";function W(t,e=process.cwd()){let o=ze.resolve(e,t);if(!Ue.existsSync(o))throw new Error(`File not found at ${o}`);return!0}async function B(t,e,o="file",n=process.cwd()){return(await jr.prompt([{type:"input",name:o,message:t,default:e,validate:a=>{let c=ze.resolve(n,a);return Ue.existsSync(c)?!0:`File not found at ${c}`}}]))[o]}function be(t,e){for(let o of t){let n=ze.join(process.cwd(),o);if(Ue.existsSync(n))return e?r.info(e.replace("{path}",n)):r.info(`File detected: ${n}`),o}}import*as R from"node:fs";import*as U from"node:path";import*as Q from"node:os";import{execSync as ee,spawn as Wr}from"node:child_process";import*as je from"node:net";var D={version:"0.1.4",baseUrl:"https://github.com/Leechael/tappd-simulator/releases/download/v0.1.4",installDir:U.join(Q.homedir(),".phala-cloud","tappd-simulator"),defaultLogPath:U.join(Q.homedir(),".phala-cloud","logs","tappd-simulator.log"),platforms:{darwin:{filename:"tappd-simulator-0.1.4-aarch64-apple-darwin.tgz",extractedFolder:"tappd-simulator-0.1.4-aarch64-apple-darwin",socketArg:"unix:/tmp/tappd.sock"},linux:{filename:"tappd-simulator-0.1.4-x86_64-linux-musl.tgz",extractedFolder:"tappd-simulator-0.1.4-x86_64-linux-musl",socketArg:"unix:/tmp/tappd.sock"},win32:{filename:"tappd-simulator-0.1.4-x86_64-pc-windows-msvc.tgz",extractedFolder:"tappd-simulator-0.1.4-x86_64-pc-windows-msvc",socketArg:"127.0.0.1:8090"}}};function kt(){try{if(!R.existsSync(D.installDir))return!1;let t=Q.platform();if(!D.platforms[t])throw new Error(`Unsupported platform: ${t}`);let e=U.join(D.installDir,D.platforms[t].extractedFolder);if(!R.existsSync(e))return!1;let n=U.join(e,t==="win32"?"tappd-simulator.exe":"tappd-simulator");return R.existsSync(n)}catch(t){return r.error("Error checking if simulator is installed:",t),!1}}function me(){let t=Q.platform();if(!D.platforms[t])throw new Error(`Unsupported platform: ${t}. Only darwin, linux, and win32 are supported.`);return t}async function wt(t){let e=o=>{r.info(o),t&&t(o)};try{let o=me(),n=D.platforms[o];R.existsSync(D.installDir)||(r.info(`Creating installation directory at ${D.installDir}`),R.mkdirSync(D.installDir,{recursive:!0})),process.chdir(D.installDir);let s=`${D.baseUrl}/${n.filename}`;r.info(`Downloading simulator from ${s}`),ee(`wget ${s}`,{stdio:"inherit"}),r.info(`Extracting ${n.filename}`),ee(`tar -xvf ${n.filename}`,{stdio:"inherit"}),r.success("Simulator installation completed successfully")}catch(o){throw r.error("Error installing simulator:",o),new Error(`Failed to install simulator: ${o}`)}}async function _t(t={}){try{let e=me(),o=D.platforms[e],n=U.join(D.installDir,o.extractedFolder);process.chdir(n);let s=e==="win32"?"tappd-simulator.exe":"./tappd-simulator",a={background:t.background??!0,logToFile:t.logToFile??!0,logFilePath:t.logFilePath??D.defaultLogPath};if(a.logToFile){let f=U.dirname(a.logFilePath);R.existsSync(f)||R.mkdirSync(f,{recursive:!0}),r.info(`Simulator logs will be written to: ${a.logFilePath}`)}r.info(`Starting simulator with: ${s} -l ${o.socketArg}`);let c="inherit",l=null;a.logToFile&&(l=R.createWriteStream(a.logFilePath,{flags:"a"}),c=["ignore",l,l]);let h=Wr(s,["-l",o.socketArg],{stdio:c,shell:e==="win32",detached:a.background});if(l){let f=new Date().toISOString();l.write(`
[${f}] Simulator started
`)}return a.background&&(h.unref(),r.success("Simulator is running in the background")),await We(),h}catch(e){throw r.error("Error running simulator:",e),new Error(`Failed to run simulator: ${e}`)}}async function ve(){try{let t=me(),e=D.platforms[t];if(t==="darwin"||t==="linux"){let o="/tmp/tappd.sock";return R.existsSync(o)?new Promise(n=>{let s=je.createConnection({path:o}).on("connect",()=>{s.end(),n(!0)}).on("error",()=>{n(!1)});setTimeout(()=>{s.end(),n(!1)},1e3)}):!1}if(t==="win32"){let o="127.0.0.1";return new Promise(s=>{let a=je.createConnection({host:o,port:8090}).on("connect",()=>{a.end(),s(!0)}).on("error",()=>{s(!1)});setTimeout(()=>{a.end(),s(!1)},1e3)})}return!1}catch(t){return r.error("Error checking if simulator is running:",t),!1}}async function St(){try{let t=me();if(!await ve())return r.info("Simulator is not running"),!0;r.info("Stopping simulator..."),t==="win32"?ee(`for /f "tokens=5" %a in ('netstat -ano ^| findstr :8080') do taskkill /F /PID %a`,{stdio:"inherit"}):ee("pkill -f tappd-simulator",{stdio:"inherit"});let e=!await ve();return e?r.success("Simulator stopped successfully"):r.error("Failed to stop simulator"),await G(),e}catch(t){return r.error("Error stopping simulator:",t),!1}}function Br(){return me()==="win32"?"http://127.0.0.1:8090":"unix:///tmp/tappd.sock"}async function We(t){try{let e=Br(),o=t||e;return await ee(`export DSTACK_SIMULATOR_ENDPOINT=${o}`),r.success(`Setting DSTACK_SIMULATOR_ENDPOINT=${o} for current process`),t}catch(e){throw r.error("Error setting simulator endpoint environment variable:",e),new Error(`Failed to set simulator endpoint: ${e}`)}}async function G(){return await ee("unset DSTACK_SIMULATOR_ENDPOIN"),r.success("Deleted DSTACK_SIMULATOR_ENDPOINT from current process"),!0}var pe=qr(Kr),Et=".phala-cloud/logs",Yr=".phala-cloud/compose",$t=10,P=class{username;image;registry;constructor(e,o,n){this.image=e,this.username=o||"",this.registry=n||""}ensureLogsDir(){let e=Be.resolve(Et);K.existsSync(e)||K.mkdirSync(e,{recursive:!0})}getLogFilePath(e,o){let n=new Date().toISOString().replace(/[:.]/g,"-");return Be.resolve(Et,`${o||this.image}-${e}-${n}.log`)}getSystemArchitecture(){let e=Zr.arch();switch(e){case"arm":case"arm64":return"arm64";case"x64":return"amd64";default:return e}}spawnProcess(e,o,n,s){return new Promise((a,c)=>{let l=Hr(e,o);this.ensureLogsDir();let h=this.getLogFilePath(n,s),f=K.createWriteStream(h,{flags:"a"}),C=[],k=(m,p=!1)=>{let u=m.toString().split(`
`);f.write(m),u.forEach(g=>{g.trim()&&(C.push(g),C.length>$t&&C.shift(),console.clear(),console.log(`Latest ${$t} lines (full log at ${h}):`),console.log("-".repeat(50)),C.forEach(b=>{p?console.error(b):console.log(b)}))})};l.stdout.on("data",m=>k(m)),l.stderr.on("data",m=>k(m,!0)),l.on("close",m=>{f.end(),m===0?(console.log(`
Operation completed. Full log available at: ${h}`),a()):c(new Error(`Process exited with code ${m}. Check log file: ${h}`))}),l.on("error",m=>{f.end(),c(m)})})}setCredentials(e,o){this.username=e,o&&(this.registry=o)}async buildImage(e,o){try{let n=this.getSystemArchitecture(),s=`${this.username}/${this.image}:${o}`,a=r.startSpinner(`Building Docker image ${this.username}/${this.image}:${o}`);W(e);let c=["build","-t",s,"-f",e];return n==="arm64"&&(console.log("Detected arm64 architecture, using --platform linux/amd64"),c.push("--platform","linux/amd64")),c.push("."),await this.spawnProcess("docker",c,"build",this.image),a.stop(!0,`Docker image ${s} built successfully`),!0}catch(n){return r.error(`Failed to build Docker image: ${n instanceof Error?n.message:String(n)}`),!1}}async pushImage(e){try{let o=r.startSpinner(`Pushing Docker image ${e} to Docker Hub`);if(!await N())throw o.stop(!1),new Error('Docker credentials not found. Please log in first with "phala docker login"');let s=e;return console.log(`Pushing image ${s} to Docker Hub...`),await this.spawnProcess("docker",["push",s],"push",e.replace(/.*\/+(\w+):.*$/g,"$1")),o.stop(!0,`Docker image ${s} pushed successfully`),!0}catch(o){return r.error(`Failed to push Docker image: ${o instanceof Error?o.message:String(o)}`),!1}}async login(e,o,n){try{let s=r.startSpinner(`Logging in to Docker Hub as ${e}`);return await this.checkLogin()?(s.stop(!0,`Logged in as ${e}`),this.setCredentials(e,n),!0):(await xt("docker",["login",...n?[n]:[],"-u",e,"--password-stdin"],{input:o}),s.stop(!0,"Logged in to Docker Hub successfully"),this.setCredentials(e,n),!0)}catch(s){return r.error(`Failed to login to Docker Hub: ${s instanceof Error?s.message:String(s)}`),!1}}async checkLogin(){try{let e=new Promise((n,s)=>{setTimeout(()=>s(new Error("Docker login check timed out")),5e3)}),o=xt("docker",["login"]).then(n=>n.stdout.includes("Login Succeeded"));return await Promise.race([o,e])}catch(e){return r.debug(`Docker login check failed: ${e instanceof Error?e.message:String(e)}`),!1}}async buildComposeFile(e,o,n){if(!this.username)throw new Error("Docker Hub username is required for building compose file");let s=n==="eliza"?nt:st,a=ct.parse({template:s}),c=Yr;K.existsSync(c)||(r.info(`Creating directory: ${c}`),K.mkdirSync(c,{recursive:!0}));let l=[];o&&(l=K.readFileSync(o,"utf-8").split(`
`).filter(p=>p&&!p.startsWith("#")).map(p=>{let u=p.indexOf("#");return u>0&&(p=p.substring(0,u).trim()),p.trim()}).filter(p=>p.includes("=")).map(p=>{let[u,g]=p.split("=",2),b=u.trim();return(g?g.trim():"")===""?null:`${b}=${b}`}).filter(Boolean));let h=e,C=Gr.compile(a.template,{noEscape:!0})({imageName:h,envVars:l.map(m=>m.replace(/=.*/,"=${"+m.split("=")[0]+"}"))}),k=Be.join(c,`${e.replace(/.*\/+(\w+):.*$/g,"$1")}-tee-compose.yaml`);return K.writeFileSync(k,C),r.success(`Backup of docker compose file created at: ${k}`),k}async runComposeLocally(e,o){try{let n=r.startSpinner(`Running Docker Compose file at ${e}`);W(e);let s=["-f",e,"up","-d"];return o&&(W(o),s.splice(2,0,"--env-file",o)),await pe(`docker compose ${s.join(" ")}`),n.stop(!0,"Docker Compose file running successfully"),!0}catch(n){return r.error(`Failed to run Docker Compose file: ${n instanceof Error?n.message:String(n)}`),!1}}async runSimulator(e,o){try{r.info(`Running TEE simulator with image ${e}`),r.info("Pulling latest simulator image..."),await pe(`docker pull ${e}`),r.info("Starting simulator in background...");let{stdout:n}=await pe(`docker run -d --name tee-simulator --rm -p ${o}:${o} ${e}`),s=n.trim();return r.success(`TEE simulator running successfully. Container ID: ${s}`),r.break(),r.break(),r.info("Useful commands:"),r.info(`- View logs: docker logs -f ${s}`),r.info(`- Stop simulator: docker stop ${s}`),We(`http://localhost:${o}`),!0}catch(n){return r.error(`Failed to run TEE simulator: ${n instanceof Error?n.message:String(n)}`),!1}}async stopSimulator(){try{let e=r.startSpinner("Stopping TEE simulator...");return await pe("docker stop tee-simulator"),await G(),e.stop(!0,"TEE simulator stopped successfully"),!0}catch(e){return r.error(`Failed to stop TEE simulator: ${e instanceof Error?e.message:String(e)}`),!1}}static async listLocalImages(){try{let{stdout:e}=await pe('docker images --format "{{.Repository}}:{{.Tag}}"'),n=(await N())?.username;return e.split(`
`).filter(a=>a&&!a.includes("<none>")).filter(a=>a.includes(`${n}/`)).map(a=>({imageName:a}))}catch(e){return r.error(`Failed to list local Docker images: ${e instanceof Error?e.message:String(e)}`),[]}}};import Tt from"prompts";var Pt=new Jr().name("login").description("Login to Docker Hub").option("-u, --username <username>","Docker Hub username").option("-p, --password <password>","Docker Hub password").option("-r, --registry <registry>","Docker registry URL").action(async t=>{try{let e=t.username,o=t.password,n=t.registry;if(!e){r.info("First we need your Docker Hub username to check if you are already logged in.");let l=await Tt({type:"text",name:"username",message:"Enter your Docker Hub username:",validate:h=>h.length>0?!0