@tigrisdata/cli 2.1.0 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/README.md +381 -1
  2. package/dist/auth/client.js +1 -1
  3. package/dist/auth/config.js +1 -1
  4. package/dist/auth/s3-client.js +1 -1
  5. package/dist/auth/storage.js +1 -1
  6. package/dist/cli.js +11 -8
  7. package/dist/index.js +11 -8
  8. package/dist/lib/buckets/create.js +2 -2
  9. package/dist/lib/buckets/delete.js +2 -0
  10. package/dist/lib/buckets/get.js +7 -0
  11. package/dist/lib/buckets/list.js +6 -6
  12. package/dist/lib/configure/index.js +2 -2
  13. package/dist/lib/cp.js +1 -1
  14. package/dist/lib/forks/create.js +2 -0
  15. package/dist/lib/forks/list.js +7 -0
  16. package/dist/lib/login/credentials.js +2 -2
  17. package/dist/lib/login/oauth.js +5 -0
  18. package/dist/lib/login/select.js +5 -5
  19. package/dist/lib/logout.js +2 -2
  20. package/dist/lib/ls.js +6 -1
  21. package/dist/lib/mk.js +1 -1
  22. package/dist/lib/mv.js +1 -1
  23. package/dist/lib/objects/delete.js +2 -0
  24. package/dist/lib/objects/get.js +2 -0
  25. package/dist/lib/objects/list.js +7 -0
  26. package/dist/lib/objects/put.js +7 -0
  27. package/dist/lib/organizations/create.js +5 -0
  28. package/dist/lib/organizations/list.js +10 -0
  29. package/dist/lib/organizations/select.js +9 -0
  30. package/dist/lib/rm.js +1 -1
  31. package/dist/lib/snapshots/list.js +7 -0
  32. package/dist/lib/snapshots/take.js +2 -0
  33. package/dist/lib/touch.js +1 -0
  34. package/dist/lib/whoami.js +3 -3
  35. package/dist/specs.yaml +260 -259
  36. package/dist/utils/messages.js +2 -2
  37. package/dist/utils/path.js +1 -1
  38. package/package.json +13 -4
  39. package/dist/lib/login/ui.js +0 -5
  40. package/dist/lib/orgs/create.js +0 -2
  41. package/dist/lib/orgs/list.js +0 -7
  42. package/dist/lib/orgs/select.js +0 -6
  43. /package/dist/lib/{stat.js → _stat.js} +0 -0
@@ -1,2 +1,2 @@
1
- import{readFileSync as g,existsSync as f}from"fs";import{join as m,dirname as d}from"path";import{fileURLToPath as S}from"url";import*as l from"yaml";var C=S(import.meta.url),M=d(C),a=null;function x(){let n=M;for(let e=0;e<5;e++){let s=m(n,"specs.yaml");if(f(s))return s;n=m(n,"..")}throw new Error("Could not find specs.yaml")}function y(){if(!a){let n=x(),e=g(n,"utf8");a=l.parse(e)}return a}function u(n,e){let t=y().commands.find(o=>o.name===n);return t?e&&t.operations?t.operations.find(o=>o.name===e)||null:t:null}var c={success:"\u2714",failure:"\u2716",hint:"\u2192"};function r(n){let e=u(n.command,n.operation);if(e)return e.messages}function i(n,e){let s=n;return s=s.replace(/\\n/g,`
2
- `),e&&(s=s.replace(/\{\{(\w+)\}\}/g,(t,o)=>{let p=e[o];return p!==void 0?String(p):`{{${o}}}`})),s}function V(n,e){let s=r(n);s?.onStart&&console.log(i(s.onStart,e))}function O(n,e){let s=r(n);s?.onSuccess&&console.log(`${c.success} ${i(s.onSuccess,e)}`)}function P(n,e,s){let t=r(n);t?.onFailure&&console.error(`${c.failure} ${i(t.onFailure,s)}`),e&&console.error(` ${e}`)}function _(n,e){let s=r(n);s?.onEmpty&&console.log(i(s.onEmpty,e))}function j(n,e){let s=r(n);s?.onAlreadyDone&&console.log(i(s.onAlreadyDone,e))}function D(n,e){let s=r(n);s?.hint&&console.log(`${c.hint} ${i(s.hint,e)}`)}function E(n,e){return{command:n,operation:e}}export{E as msg,j as printAlreadyDone,_ as printEmpty,P as printFailure,D as printHint,V as printStart,O as printSuccess};
1
+ import{readFileSync as f,existsSync as d}from"fs";import{join as m,dirname as S}from"path";import{fileURLToPath as C}from"url";import*as l from"yaml";var M=C(import.meta.url),x=S(M),c=null;function y(){let n=x;for(let e=0;e<5;e++){let s=m(n,"specs.yaml");if(d(s))return s;n=m(n,"..")}throw new Error("Could not find specs.yaml")}function A(){if(!c){let n=y(),e=f(n,"utf8");c=l.parse(e)}return c}function g(n,e){let t=A().commands.find(r=>r.name===n);return t?e&&t.operations?t.operations.find(r=>r.name===e)||null:t:null}var p={success:"\u2714",failure:"\u2716",hint:"\u2192"};function a(){return process.stdout.isTTY===!0}function o(n){let e=g(n.command,n.operation);if(e)return e.messages}function i(n,e){let s=n;return s=s.replace(/\\n/g,`
2
+ `),e&&(s=s.replace(/\{\{(\w+)\}\}/g,(t,r)=>{let u=e[r];return u!==void 0?String(u):`{{${r}}}`})),s}function O(n,e){if(!a())return;let s=o(n);s?.onStart&&console.log(i(s.onStart,e))}function P(n,e){if(!a())return;let s=o(n);s?.onSuccess&&console.log(`${p.success} ${i(s.onSuccess,e)}`)}function T(n,e,s){let t=o(n);t?.onFailure&&console.error(`${p.failure} ${i(t.onFailure,s)}`),e&&console.error(` ${e}`)}function _(n,e){if(!a())return;let s=o(n);s?.onEmpty&&console.log(i(s.onEmpty,e))}function j(n,e){if(!a())return;let s=o(n);s?.onAlreadyDone&&console.log(i(s.onAlreadyDone,e))}function D(n,e){if(!a())return;let s=o(n);s?.hint&&console.log(`${p.hint} ${i(s.hint,e)}`)}function E(n,e){return{command:n,operation:e}}export{E as msg,j as printAlreadyDone,_ as printEmpty,T as printFailure,D as printHint,O as printStart,P as printSuccess};
@@ -1 +1 @@
1
- function r(s){let t=s.split("/");return{bucket:t[0],path:t.slice(1).join("/")}}function e(s,t){return{source:r(s),destination:r(t)}}export{r as parsePath,e as parsePaths};
1
+ import{list as g}from"@tigrisdata/storage";function a(i){let t=i.split("/");return{bucket:t[0],path:t.slice(1).join("/")}}async function p(i,t,s){let{data:e}=await g({prefix:`${t}/`,limit:1,config:{...s,bucket:i}});return!!(e?.items&&e.items.length>0)}function f(i,t){return{source:a(i),destination:a(t)}}async function d(i,t,s){let e=[],n;do{let{data:r,error:o}=await g({prefix:t,paginationToken:n,config:{...s,bucket:i}});if(o)return{items:e,error:o};r?.items&&e.push(...r.items),n=r?.hasMore?r.paginationToken:void 0}while(n);return{items:e}}export{p as isPathFolder,d as listAllItems,a as parsePath,f as parsePaths};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tigrisdata/cli",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "description": "Command line interface for Tigris object storage",
5
5
  "type": "module",
6
6
  "exports": {
@@ -21,12 +21,18 @@
21
21
  },
22
22
  "scripts": {
23
23
  "build": "tsup",
24
- "dev": "tsup --watch",
24
+ "dev": "export $(grep -v '^#' .env | xargs) && tsup --watch",
25
+ "cli": "node dist/cli.js",
25
26
  "lint": "eslint src --ext .ts",
26
27
  "lint:fix": "eslint src --ext .ts --fix",
27
28
  "format": "prettier --write \"src/**/*.ts\"",
28
29
  "format:check": "prettier --check \"src/**/*.ts\"",
30
+ "test": "vitest run",
31
+ "test:watch": "vitest",
32
+ "test:unit": "vitest run test/utils",
33
+ "test:integration": "vitest run test/cli.test.ts",
29
34
  "publint": "publint",
35
+ "updatedocs": "tsx scripts/update-docs.ts",
30
36
  "prepublishOnly": "npm run build",
31
37
  "clean": "rm -rf dist",
32
38
  "semantic-release": "semantic-release",
@@ -72,7 +78,7 @@
72
78
  },
73
79
  "dependencies": {
74
80
  "@aws-sdk/client-s3": "^3.908.0",
75
- "@tigrisdata/storage": "^2.10.0",
81
+ "@tigrisdata/storage": "^2.10.1",
76
82
  "axios": "^1.12.2",
77
83
  "commander": "^11.0.0",
78
84
  "enquirer": "^2.4.1",
@@ -87,12 +93,15 @@
87
93
  "@types/node": "^20.0.0",
88
94
  "@typescript-eslint/eslint-plugin": "^6.0.0",
89
95
  "@typescript-eslint/parser": "^6.0.0",
96
+ "dotenv": "^17.2.3",
90
97
  "eslint": "^8.0.0",
91
98
  "husky": "^8.0.0",
92
99
  "prettier": "^3.0.0",
93
100
  "publint": "^0.2.0",
94
101
  "semantic-release": "^25.0.2",
95
102
  "tsup": "^8.0.0",
96
- "typescript": "^5.0.0"
103
+ "tsx": "^4.21.0",
104
+ "typescript": "^5.0.0",
105
+ "vitest": "^4.0.15"
97
106
  }
98
107
  }
@@ -1,5 +0,0 @@
1
- import m from"axios";import G from"open";function v(){return{domain:process.env.AUTH0_DOMAIN||"auth-dev.tigris.dev",clientId:process.env.AUTH0_CLIENT_ID||"JdJVYIyw0O1uHi5L5OJH903qaWBgd3gF",audience:process.env.AUTH0_AUDIENCE||"https://tigris-api-dev"}}var A="https://tigris";import{homedir as j}from"os";import{join as _}from"path";import{readFileSync as V,writeFileSync as H,existsSync as O,mkdirSync as J}from"fs";import{chmod as W}from"fs/promises";var h=_(j(),".tigris"),u=_(h,"config.json");function B(){O(h)||J(h,{recursive:!0,mode:448})}function c(){if(O(u))try{let t=V(u,"utf8");return JSON.parse(t)}catch{return{}}return{}}async function g(t){B(),H(u,JSON.stringify(t,null,2),{mode:384});try{await W(u,384)}catch{}}async function y(t){let e=c();e.tokens=t,await g(e)}async function d(){return c().tokens||null}async function C(){let t=c();delete t.tokens,await g(t)}async function P(t){let e=c();e.organizations=t,await g(e)}function I(){return c().organizations||[]}async function M(t){let e=c();e.selectedOrganization=t,await g(e)}async function b(t){let e=c();e.loginMethod=t,await g(e)}var k=class{config;baseUrl;constructor(){this.config=v(),this.baseUrl=`https://${this.config.domain}`}async login(e){let o=(await m.post(`${this.baseUrl}/oauth/device/code`,{client_id:this.config.clientId,audience:this.config.audience,scope:"openid profile email offline_access"},{headers:{"Content-Type":"application/x-www-form-urlencoded"}})).data;e?.onDeviceCode?.(o.user_code,o.verification_uri),await this.sleep(2e3);try{await G(o.verification_uri_complete)}catch{}e?.onWaiting?.();let i=await this.pollForToken(o.device_code,o.interval||5);await y(i),b("oauth"),await this.extractAndStoreOrganizations(i.idToken)}async pollForToken(e,n){let i=0;for(;i<60;){i++;try{let s=(await m.post(`${this.baseUrl}/oauth/token`,{client_id:this.config.clientId,device_code:e,grant_type:"urn:ietf:params:oauth:grant-type:device_code"},{headers:{"Content-Type":"application/x-www-form-urlencoded"}})).data,a=Date.now()+(s.expires_in||3600)*1e3;return{accessToken:s.access_token,refreshToken:s.refresh_token,idToken:s.id_token,expiresAt:a}}catch(r){if(m.isAxiosError(r)&&r.response){let s=r.response.data?.error;if(s==="authorization_pending"){await this.sleep(n*1e3);continue}if(s==="slow_down"){n+=5,await this.sleep(n*1e3);continue}throw new Error(r.response.data?.error_description||"Authentication failed")}throw r}}throw new Error("Authentication timed out. Please try again.")}async getAccessToken(){let e=await d();if(!e)throw new Error('Not authenticated. Please run "tigris login" to authenticate.');let n=300*1e3;return Date.now()+n>=e.expiresAt&&(e=await this.refreshAccessToken(e)),e.accessToken}async refreshAccessToken(e){let n=null;if(e?.refreshToken?n=e:n=await d(),!n)throw new Error('No refresh token available. Please run "tigris login" to re-authenticate.');try{let i=(await m.post(`${this.baseUrl}/oauth/token`,{client_id:this.config.clientId,grant_type:"refresh_token",refresh_token:n.refreshToken,scope:"openid profile email offline_access"},{headers:{"Content-Type":"application/x-www-form-urlencoded"}})).data,r={accessToken:i.access_token,refreshToken:i.refresh_token||n.refreshToken,idToken:i.id_token||n.idToken,expiresAt:Date.now()+(i.expires_in||3600)*1e3};return await y(r),r}catch{throw await C(),new Error('Token refresh failed. Please run "tigris login" to re-authenticate.')}}async getIdTokenClaims(){let e=await d();if(!e||!e.idToken)throw new Error('Not authenticated. Please run "tigris login" to authenticate.');try{let n=e.idToken.split(".")[1],o=Buffer.from(n,"base64").toString("utf8");return JSON.parse(o)}catch{throw new Error("Failed to decode ID token")}}async extractAndStoreOrganizations(e){if(e)try{let n=e.split(".")[1],o=Buffer.from(n,"base64").toString("utf8"),r=JSON.parse(o)[A];if(!r)return;let s=r?.ns?.map(a=>typeof a=="object"&&a!==null?{id:a.id,name:a.name,displayName:a.name}:{id:a,name:a,displayName:a})||[];if(s.length===0)return;P(s)}catch{}}async getOrganizations(){return await this.getAccessToken(),I()}async logout(){await C()}async isAuthenticated(){return await d()!==null}sleep(e){return new Promise(n=>setTimeout(n,e))}},w=null;function z(){return w||(w=new k),w}import{readFileSync as Y,existsSync as K}from"fs";import{join as D,dirname as q}from"path";import{fileURLToPath as Q}from"url";import*as E from"yaml";var X=Q(import.meta.url),Z=q(X),T=null;function ee(){let t=Z;for(let e=0;e<5;e++){let n=D(t,"specs.yaml");if(K(n))return n;t=D(t,"..")}throw new Error("Could not find specs.yaml")}function ne(){if(!T){let t=ee(),e=Y(t,"utf8");T=E.parse(e)}return T}function N(t,e){let o=ne().commands.find(i=>i.name===t);return o?e&&o.operations?o.operations.find(i=>i.name===e)||null:o:null}var x={success:"\u2714",failure:"\u2716",hint:"\u2192"};function p(t){let e=N(t.command,t.operation);if(e)return e.messages}function f(t,e){let n=t;return n=n.replace(/\\n/g,`
2
- `),e&&(n=n.replace(/\{\{(\w+)\}\}/g,(o,i)=>{let r=e[i];return r!==void 0?String(r):`{{${i}}}`})),n}function $(t,e){let n=p(t);n?.onStart&&console.log(f(n.onStart,e))}function S(t,e){let n=p(t);n?.onSuccess&&console.log(`${x.success} ${f(n.onSuccess,e)}`)}function F(t,e,n){let o=p(t);o?.onFailure&&console.error(`${x.failure} ${f(o.onFailure,n)}`),e&&console.error(` ${e}`)}function L(t,e){let n=p(t);n?.onAlreadyDone&&console.log(f(n.onAlreadyDone,e))}function R(t,e){let n=p(t);n?.hint&&console.log(`${x.hint} ${f(n.hint,e)}`)}function U(t,e){return{command:t,operation:e}}var l=U("login","ui");async function te(){$(l);try{let t=z();if(await t.isAuthenticated()){L(l);return}await t.login({onDeviceCode:(o,i)=>{console.log(`
3
- Your confirmation code: ${o}
4
- `),console.log(`If browser doesn't open, visit: ${i}`)},onWaiting:()=>console.log(`
5
- Waiting for authentication...`)});let n=await t.getOrganizations();if(n.length>0){let o=n[0];M(o.id),S(l,{org:o.displayName||o.name}),n.length>1&&R(l,{count:n.length})}else S(l,{org:"none"})}catch{F(l),process.exit(1)}}var Se=te;export{Se as default,te as ui};
@@ -1,2 +0,0 @@
1
- import{createOrganization as se}from"@tigrisdata/storage";function _(t,e,n){for(let o of e)if(t[o]!==void 0)return t[o];return n}import{S3Client as ke}from"@aws-sdk/client-s3";import{homedir as V}from"os";import{join as I}from"path";import{readFileSync as G,writeFileSync as J,existsSync as O,mkdirSync as q}from"fs";import{chmod as B}from"fs/promises";var y=I(V(),".tigris"),l=I(y,"config.json");function W(){O(y)||q(y,{recursive:!0,mode:448})}function c(){if(O(l))try{let t=G(l,"utf8");return JSON.parse(t)}catch{return{}}return{}}async function p(t){W(),J(l,JSON.stringify(t,null,2),{mode:384});try{await B(l,384)}catch{}}async function w(t){let e=c();e.tokens=t,await p(e)}async function d(){return c().tokens||null}async function C(){let t=c();delete t.tokens,await p(t)}async function P(t){let e=c();e.organizations=t,await p(e)}function M(){return c().organizations||[]}function T(){return c().selectedOrganization||null}function N(){let t=c();return t.temporaryCredentials||t.credentials||null}async function E(t){let e=c();e.loginMethod=t,await p(e)}function z(){return c().loginMethod||null}import u from"axios";import Y from"open";function b(){return{domain:process.env.AUTH0_DOMAIN||"auth-dev.tigris.dev",clientId:process.env.AUTH0_CLIENT_ID||"JdJVYIyw0O1uHi5L5OJH903qaWBgd3gF",audience:process.env.AUTH0_AUDIENCE||"https://tigris-api-dev"}}var D="https://tigris";var S=class{config;baseUrl;constructor(){this.config=b(),this.baseUrl=`https://${this.config.domain}`}async login(e){let o=(await u.post(`${this.baseUrl}/oauth/device/code`,{client_id:this.config.clientId,audience:this.config.audience,scope:"openid profile email offline_access"},{headers:{"Content-Type":"application/x-www-form-urlencoded"}})).data;e?.onDeviceCode?.(o.user_code,o.verification_uri),await this.sleep(2e3);try{await Y(o.verification_uri_complete)}catch{}e?.onWaiting?.();let i=await this.pollForToken(o.device_code,o.interval||5);await w(i),E("oauth"),await this.extractAndStoreOrganizations(i.idToken)}async pollForToken(e,n){let i=0;for(;i<60;){i++;try{let s=(await u.post(`${this.baseUrl}/oauth/token`,{client_id:this.config.clientId,device_code:e,grant_type:"urn:ietf:params:oauth:grant-type:device_code"},{headers:{"Content-Type":"application/x-www-form-urlencoded"}})).data,a=Date.now()+(s.expires_in||3600)*1e3;return{accessToken:s.access_token,refreshToken:s.refresh_token,idToken:s.id_token,expiresAt:a}}catch(r){if(u.isAxiosError(r)&&r.response){let s=r.response.data?.error;if(s==="authorization_pending"){await this.sleep(n*1e3);continue}if(s==="slow_down"){n+=5,await this.sleep(n*1e3);continue}throw new Error(r.response.data?.error_description||"Authentication failed")}throw r}}throw new Error("Authentication timed out. Please try again.")}async getAccessToken(){let e=await d();if(!e)throw new Error('Not authenticated. Please run "tigris login" to authenticate.');let n=300*1e3;return Date.now()+n>=e.expiresAt&&(e=await this.refreshAccessToken(e)),e.accessToken}async refreshAccessToken(e){let n=null;if(e?.refreshToken?n=e:n=await d(),!n)throw new Error('No refresh token available. Please run "tigris login" to re-authenticate.');try{let i=(await u.post(`${this.baseUrl}/oauth/token`,{client_id:this.config.clientId,grant_type:"refresh_token",refresh_token:n.refreshToken,scope:"openid profile email offline_access"},{headers:{"Content-Type":"application/x-www-form-urlencoded"}})).data,r={accessToken:i.access_token,refreshToken:i.refresh_token||n.refreshToken,idToken:i.id_token||n.idToken,expiresAt:Date.now()+(i.expires_in||3600)*1e3};return await w(r),r}catch{throw await C(),new Error('Token refresh failed. Please run "tigris login" to re-authenticate.')}}async getIdTokenClaims(){let e=await d();if(!e||!e.idToken)throw new Error('Not authenticated. Please run "tigris login" to authenticate.');try{let n=e.idToken.split(".")[1],o=Buffer.from(n,"base64").toString("utf8");return JSON.parse(o)}catch{throw new Error("Failed to decode ID token")}}async extractAndStoreOrganizations(e){if(e)try{let n=e.split(".")[1],o=Buffer.from(n,"base64").toString("utf8"),r=JSON.parse(o)[D];if(!r)return;let s=r?.ns?.map(a=>typeof a=="object"&&a!==null?{id:a.id,name:a.name,displayName:a.name}:{id:a,name:a,displayName:a})||[];if(s.length===0)return;P(s)}catch{}}async getOrganizations(){return await this.getAccessToken(),M()}async logout(){await C()}async isAuthenticated(){return await d()!==null}sleep(e){return new Promise(n=>setTimeout(n,e))}},k=null;function f(){return k||(k=new S),k}async function Q(){return z()}async function R(){let t=await Q();if(!t)throw new Error('Not authenticated. Please run "tigris login" or "tigris configure" first.');if(t==="oauth"){let o=await f().getAccessToken();if(!T())throw new Error('No organization selected. Please run "tigris orgs select" first.');let r=process.env.TIGRIS_ENDPOINT??"https://t3.storage.dev",s=process.env.TIGRIS_STORAGE_IAM_ENDPOINT??"https://iam.storageapi.dev",a=process.env.AUTH0_DOMAIN??"https://auth.tigris.dev";return{sessionToken:o,accessKeyId:"",secretAccessKey:"",endpoint:r,organizationId:T()??void 0,iamEndpoint:s,authDomain:a}}let e=N();if(!e)throw new Error('No credentials found. Please run "tigris configure" first.');return{accessKeyId:e.accessKeyId,secretAccessKey:e.secretAccessKey,endpoint:e.endpoint}}import{readFileSync as X,existsSync as Z}from"fs";import{join as K,dirname as ee}from"path";import{fileURLToPath as te}from"url";import*as L from"yaml";var ne=te(import.meta.url),oe=ee(ne),x=null;function ie(){let t=oe;for(let e=0;e<5;e++){let n=K(t,"specs.yaml");if(Z(n))return n;t=K(t,"..")}throw new Error("Could not find specs.yaml")}function re(){if(!x){let t=ie(),e=X(t,"utf8");x=L.parse(e)}return x}function F(t,e){let o=re().commands.find(i=>i.name===t);return o?e&&o.operations?o.operations.find(i=>i.name===e)||null:o:null}var A={success:"\u2714",failure:"\u2716",hint:"\u2192"};function m(t){let e=F(t.command,t.operation);if(e)return e.messages}function h(t,e){let n=t;return n=n.replace(/\\n/g,`
2
- `),e&&(n=n.replace(/\{\{(\w+)\}\}/g,(o,i)=>{let r=e[i];return r!==void 0?String(r):`{{${i}}}`})),n}function $(t,e){let n=m(t);n?.onStart&&console.log(h(n.onStart,e))}function U(t,e){let n=m(t);n?.onSuccess&&console.log(`${A.success} ${h(n.onSuccess,e)}`)}function v(t,e,n){let o=m(t);o?.onFailure&&console.error(`${A.failure} ${h(o.onFailure,n)}`),e&&console.error(` ${e}`)}function H(t,e){let n=m(t);n?.hint&&console.log(`${A.hint} ${h(n.hint,e)}`)}function j(t,e){return{command:t,operation:e}}var g=j("orgs","create");async function ae(t){$(g);let e=_(t,["name","N"]);e||(v(g,"Organization name is required"),process.exit(1));let n=await R(),{data:o,error:i}=await se(e,{config:n});i&&(v(g,i.message),process.exit(1));let r=f(),s=await r.refreshAccessToken();s.idToken&&await r.extractAndStoreOrganizations(s.idToken);let a=o.id;U(g,{name:e,id:a}),H(g,{name:e})}export{ae as default};
@@ -1,7 +0,0 @@
1
- function M(n,e,t){for(let o of e)if(n[o]!==void 0)return n[o];return t}function X(n){return JSON.stringify(n,null,2)}function Q(n,e=" "){return Object.entries(n).map(([t,o])=>`${e}<${t}>${o}</${t}>`).join(`
2
- `)}function Z(n,e,t){let o=[`<${e}>`];return n.forEach(i=>{o.push(` <${t}>`),o.push(Q(i," ")),o.push(` </${t}>`)}),o.push(`</${e}>`),o.join(`
3
- `)}function z(n){if(n==null)return"";if(n instanceof Date)return P(n);if(typeof n=="string"){let e=new Date(n);if(!isNaN(e.getTime())&&n.includes("T"))return P(e)}return String(n)}function P(n){return new Intl.DateTimeFormat(void 0,{year:"numeric",month:"short",day:"numeric",hour:"2-digit",minute:"2-digit"}).format(n)}function ee(n,e){return e.map(t=>{if(t.width)return t.width;let o=t.header.length,i=n.reduce((r,c)=>{let s=z(c[t.key]);return Math.max(r,s.length)},0);return Math.max(o,i)})}function ne(n,e){let t=[],o=ee(n,e),i="\u250C"+o.map(a=>"\u2500".repeat(a+2)).join("\u252C")+"\u2510",r="\u251C"+o.map(a=>"\u2500".repeat(a+2)).join("\u253C")+"\u2524",c="\u2514"+o.map(a=>"\u2500".repeat(a+2)).join("\u2534")+"\u2518";t.push(`
4
- `+i);let s="\u2502 "+e.map((a,y)=>a.header.padEnd(o[y])).join(" \u2502 ")+" \u2502";return t.push(s),t.push(r),n.forEach(a=>{let y=e.map((u,p)=>{let g=z(a[u.key]);return u.align==="right"?g.padStart(o[p]):g.padEnd(o[p])});t.push("\u2502 "+y.join(" \u2502 ")+" \u2502")}),t.push(c+`
5
- `),t.join(`
6
- `)}function N(n,e,t,o,i){switch(e){case"json":return X(n);case"xml":return Z(n,t,o);default:return ne(n,i)}}import C from"axios";import ce from"open";function D(){return{domain:process.env.AUTH0_DOMAIN||"auth-dev.tigris.dev",clientId:process.env.AUTH0_CLIENT_ID||"JdJVYIyw0O1uHi5L5OJH903qaWBgd3gF",audience:process.env.AUTH0_AUDIENCE||"https://tigris-api-dev"}}var E="https://tigris";import{homedir as te}from"os";import{join as $}from"path";import{readFileSync as oe,writeFileSync as ie,existsSync as j,mkdirSync as re}from"fs";import{chmod as se}from"fs/promises";var S=$(te(),".tigris"),w=$(S,"config.json");function ae(){j(S)||re(S,{recursive:!0,mode:448})}function d(){if(j(w))try{let n=oe(w,"utf8");return JSON.parse(n)}catch{return{}}return{}}async function m(n){ae(),ie(w,JSON.stringify(n,null,2),{mode:384});try{await se(w,384)}catch{}}async function A(n){let e=d();e.tokens=n,await m(e)}async function h(){return d().tokens||null}async function _(){let n=d();delete n.tokens,await m(n)}async function R(n){let e=d();e.organizations=n,await m(e)}function F(){return d().organizations||[]}async function L(n){let e=d();e.selectedOrganization=n,await m(e)}function U(){return d().selectedOrganization||null}async function V(n){let e=d();e.loginMethod=n,await m(e)}var O=class{config;baseUrl;constructor(){this.config=D(),this.baseUrl=`https://${this.config.domain}`}async login(e){let o=(await C.post(`${this.baseUrl}/oauth/device/code`,{client_id:this.config.clientId,audience:this.config.audience,scope:"openid profile email offline_access"},{headers:{"Content-Type":"application/x-www-form-urlencoded"}})).data;e?.onDeviceCode?.(o.user_code,o.verification_uri),await this.sleep(2e3);try{await ce(o.verification_uri_complete)}catch{}e?.onWaiting?.();let i=await this.pollForToken(o.device_code,o.interval||5);await A(i),V("oauth"),await this.extractAndStoreOrganizations(i.idToken)}async pollForToken(e,t){let i=0;for(;i<60;){i++;try{let c=(await C.post(`${this.baseUrl}/oauth/token`,{client_id:this.config.clientId,device_code:e,grant_type:"urn:ietf:params:oauth:grant-type:device_code"},{headers:{"Content-Type":"application/x-www-form-urlencoded"}})).data,s=Date.now()+(c.expires_in||3600)*1e3;return{accessToken:c.access_token,refreshToken:c.refresh_token,idToken:c.id_token,expiresAt:s}}catch(r){if(C.isAxiosError(r)&&r.response){let c=r.response.data?.error;if(c==="authorization_pending"){await this.sleep(t*1e3);continue}if(c==="slow_down"){t+=5,await this.sleep(t*1e3);continue}throw new Error(r.response.data?.error_description||"Authentication failed")}throw r}}throw new Error("Authentication timed out. Please try again.")}async getAccessToken(){let e=await h();if(!e)throw new Error('Not authenticated. Please run "tigris login" to authenticate.');let t=300*1e3;return Date.now()+t>=e.expiresAt&&(e=await this.refreshAccessToken(e)),e.accessToken}async refreshAccessToken(e){let t=null;if(e?.refreshToken?t=e:t=await h(),!t)throw new Error('No refresh token available. Please run "tigris login" to re-authenticate.');try{let i=(await C.post(`${this.baseUrl}/oauth/token`,{client_id:this.config.clientId,grant_type:"refresh_token",refresh_token:t.refreshToken,scope:"openid profile email offline_access"},{headers:{"Content-Type":"application/x-www-form-urlencoded"}})).data,r={accessToken:i.access_token,refreshToken:i.refresh_token||t.refreshToken,idToken:i.id_token||t.idToken,expiresAt:Date.now()+(i.expires_in||3600)*1e3};return await A(r),r}catch{throw await _(),new Error('Token refresh failed. Please run "tigris login" to re-authenticate.')}}async getIdTokenClaims(){let e=await h();if(!e||!e.idToken)throw new Error('Not authenticated. Please run "tigris login" to authenticate.');try{let t=e.idToken.split(".")[1],o=Buffer.from(t,"base64").toString("utf8");return JSON.parse(o)}catch{throw new Error("Failed to decode ID token")}}async extractAndStoreOrganizations(e){if(e)try{let t=e.split(".")[1],o=Buffer.from(t,"base64").toString("utf8"),r=JSON.parse(o)[E];if(!r)return;let c=r?.ns?.map(s=>typeof s=="object"&&s!==null?{id:s.id,name:s.name,displayName:s.name}:{id:s,name:s,displayName:s})||[];if(c.length===0)return;R(c)}catch{}}async getOrganizations(){return await this.getAccessToken(),F()}async logout(){await _()}async isAuthenticated(){return await h()!==null}sleep(e){return new Promise(t=>setTimeout(t,e))}},v=null;function J(){return v||(v=new O),v}import ye from"enquirer";import{readFileSync as de,existsSync as le}from"fs";import{join as B,dirname as ue}from"path";import{fileURLToPath as pe}from"url";import*as H from"yaml";var ge=pe(import.meta.url),fe=ue(ge),I=null;function me(){let n=fe;for(let e=0;e<5;e++){let t=B(n,"specs.yaml");if(le(t))return t;n=B(n,"..")}throw new Error("Could not find specs.yaml")}function he(){if(!I){let n=me(),e=de(n,"utf8");I=H.parse(e)}return I}function W(n,e){let o=he().commands.find(i=>i.name===n);return o?e&&o.operations?o.operations.find(i=>i.name===e)||null:o:null}var G={success:"\u2714",failure:"\u2716",hint:"\u2192"};function T(n){let e=W(n.command,n.operation);if(e)return e.messages}function k(n,e){let t=n;return t=t.replace(/\\n/g,`
7
- `),e&&(t=t.replace(/\{\{(\w+)\}\}/g,(o,i)=>{let r=e[i];return r!==void 0?String(r):`{{${i}}}`})),t}function Y(n,e){let t=T(n);t?.onStart&&console.log(k(t.onStart,e))}function b(n,e){let t=T(n);t?.onSuccess&&console.log(`${G.success} ${k(t.onSuccess,e)}`)}function x(n,e,t){let o=T(n);o?.onFailure&&console.error(`${G.failure} ${k(o.onFailure,t)}`),e&&console.error(` ${e}`)}function q(n,e){let t=T(n);t?.onEmpty&&console.log(k(t.onEmpty,e))}function K(n,e){return{command:n,operation:e}}var l=K("orgs","list");async function we(n){Y(l);let e=M(n,["format","f","F"],"select");try{let t=J();await t.getAccessToken();let o=await t.getOrganizations();if(o.length===0){q(l);return}let i=U();if(e==="select"){let s=o.map(a=>({name:a.id,message:`${a.displayName||a.name} (${a.id})`,hint:a.id===i?"currently selected":void 0}));try{let u=(await ye.prompt({type:"select",name:"organization",message:"Select an organization:",choices:s.map(f=>f.message),initial:i?o.findIndex(f=>f.id===i):0})).organization.match(/\(([^)]+)\)$/),p=u?u[1]:o[0].id;L(p);let g=o.find(f=>f.id===p);b(l,{name:g?.displayName||g?.name});return}catch{x(l,"Selection cancelled"),process.exit(0)}}let r=o.map(s=>({id:s.id,name:s.name,displayName:s.displayName||s.name,selected:s.id===i?"*":""})),c=N(r,e,"organizations","organization",[{key:"selected",header:" ",width:1},{key:"id",header:"ID"},{key:"name",header:"Name"},{key:"displayName",header:"Display Name"}]);console.log(c),b(l,{count:r.length})}catch(t){t instanceof Error?x(l,t.message):x(l),process.exit(1)}}export{we as default};
@@ -1,6 +0,0 @@
1
- function S(t,e,n){for(let o of e)if(t[o]!==void 0)return t[o];return n}import u from"axios";import G from"open";function v(){return{domain:process.env.AUTH0_DOMAIN||"auth-dev.tigris.dev",clientId:process.env.AUTH0_CLIENT_ID||"JdJVYIyw0O1uHi5L5OJH903qaWBgd3gF",audience:process.env.AUTH0_AUDIENCE||"https://tigris-api-dev"}}var A="https://tigris";import{homedir as U}from"os";import{join as _}from"path";import{readFileSync as j,writeFileSync as V,existsSync as O,mkdirSync as J}from"fs";import{chmod as H}from"fs/promises";var m=_(U(),".tigris"),f=_(m,"config.json");function B(){O(m)||J(m,{recursive:!0,mode:448})}function c(){if(O(f))try{let t=j(f,"utf8");return JSON.parse(t)}catch{return{}}return{}}async function d(t){B(),V(f,JSON.stringify(t,null,2),{mode:384});try{await H(f,384)}catch{}}async function h(t){let e=c();e.tokens=t,await d(e)}async function g(){return c().tokens||null}async function y(){let t=c();delete t.tokens,await d(t)}async function I(t){let e=c();e.organizations=t,await d(e)}function P(){return c().organizations||[]}async function M(t){let e=c();e.selectedOrganization=t,await d(e)}async function b(t){let e=c();e.loginMethod=t,await d(e)}var w=class{config;baseUrl;constructor(){this.config=v(),this.baseUrl=`https://${this.config.domain}`}async login(e){let o=(await u.post(`${this.baseUrl}/oauth/device/code`,{client_id:this.config.clientId,audience:this.config.audience,scope:"openid profile email offline_access"},{headers:{"Content-Type":"application/x-www-form-urlencoded"}})).data;e?.onDeviceCode?.(o.user_code,o.verification_uri),await this.sleep(2e3);try{await G(o.verification_uri_complete)}catch{}e?.onWaiting?.();let i=await this.pollForToken(o.device_code,o.interval||5);await h(i),b("oauth"),await this.extractAndStoreOrganizations(i.idToken)}async pollForToken(e,n){let i=0;for(;i<60;){i++;try{let s=(await u.post(`${this.baseUrl}/oauth/token`,{client_id:this.config.clientId,device_code:e,grant_type:"urn:ietf:params:oauth:grant-type:device_code"},{headers:{"Content-Type":"application/x-www-form-urlencoded"}})).data,a=Date.now()+(s.expires_in||3600)*1e3;return{accessToken:s.access_token,refreshToken:s.refresh_token,idToken:s.id_token,expiresAt:a}}catch(r){if(u.isAxiosError(r)&&r.response){let s=r.response.data?.error;if(s==="authorization_pending"){await this.sleep(n*1e3);continue}if(s==="slow_down"){n+=5,await this.sleep(n*1e3);continue}throw new Error(r.response.data?.error_description||"Authentication failed")}throw r}}throw new Error("Authentication timed out. Please try again.")}async getAccessToken(){let e=await g();if(!e)throw new Error('Not authenticated. Please run "tigris login" to authenticate.');let n=300*1e3;return Date.now()+n>=e.expiresAt&&(e=await this.refreshAccessToken(e)),e.accessToken}async refreshAccessToken(e){let n=null;if(e?.refreshToken?n=e:n=await g(),!n)throw new Error('No refresh token available. Please run "tigris login" to re-authenticate.');try{let i=(await u.post(`${this.baseUrl}/oauth/token`,{client_id:this.config.clientId,grant_type:"refresh_token",refresh_token:n.refreshToken,scope:"openid profile email offline_access"},{headers:{"Content-Type":"application/x-www-form-urlencoded"}})).data,r={accessToken:i.access_token,refreshToken:i.refresh_token||n.refreshToken,idToken:i.id_token||n.idToken,expiresAt:Date.now()+(i.expires_in||3600)*1e3};return await h(r),r}catch{throw await y(),new Error('Token refresh failed. Please run "tigris login" to re-authenticate.')}}async getIdTokenClaims(){let e=await g();if(!e||!e.idToken)throw new Error('Not authenticated. Please run "tigris login" to authenticate.');try{let n=e.idToken.split(".")[1],o=Buffer.from(n,"base64").toString("utf8");return JSON.parse(o)}catch{throw new Error("Failed to decode ID token")}}async extractAndStoreOrganizations(e){if(e)try{let n=e.split(".")[1],o=Buffer.from(n,"base64").toString("utf8"),r=JSON.parse(o)[A];if(!r)return;let s=r?.ns?.map(a=>typeof a=="object"&&a!==null?{id:a.id,name:a.name,displayName:a.name}:{id:a,name:a,displayName:a})||[];if(s.length===0)return;I(s)}catch{}}async getOrganizations(){return await this.getAccessToken(),P()}async logout(){await y()}async isAuthenticated(){return await g()!==null}sleep(e){return new Promise(n=>setTimeout(n,e))}},C=null;function z(){return C||(C=new w),C}import{readFileSync as W,existsSync as Y}from"fs";import{join as E,dirname as q}from"path";import{fileURLToPath as K}from"url";import*as N from"yaml";var Q=K(import.meta.url),X=q(Q),k=null;function Z(){let t=X;for(let e=0;e<5;e++){let n=E(t,"specs.yaml");if(Y(n))return n;t=E(t,"..")}throw new Error("Could not find specs.yaml")}function ee(){if(!k){let t=Z(),e=W(t,"utf8");k=N.parse(e)}return k}function D(t,e){let o=ee().commands.find(i=>i.name===t);return o?e&&o.operations?o.operations.find(i=>i.name===e)||null:o:null}var $={success:"\u2714",failure:"\u2716",hint:"\u2192"};function T(t){let e=D(t.command,t.operation);if(e)return e.messages}function x(t,e){let n=t;return n=n.replace(/\\n/g,`
2
- `),e&&(n=n.replace(/\{\{(\w+)\}\}/g,(o,i)=>{let r=e[i];return r!==void 0?String(r):`{{${i}}}`})),n}function F(t,e){let n=T(t);n?.onStart&&console.log(x(n.onStart,e))}function R(t,e){let n=T(t);n?.onSuccess&&console.log(`${$.success} ${x(n.onSuccess,e)}`)}function p(t,e,n){let o=T(t);o?.onFailure&&console.error(`${$.failure} ${x(o.onFailure,n)}`),e&&console.error(` ${e}`)}function L(t,e){return{command:t,operation:e}}var l=L("orgs","select");async function ne(t){F(l);let e=S(t,["name","N"]);e||(p(l,"Organization name or ID is required"),process.exit(1));try{let n=z();await n.getAccessToken();let o=await n.getOrganizations(),i=o.find(r=>r.id===e||r.name===e);if(!i){let r=o.map(s=>` - ${s.name} (${s.id})`).join(`
3
- `);p(l,`Organization "${e}" not found
4
-
5
- Available organizations:
6
- ${r}`),process.exit(1)}M(i.id),R(l,{name:i.name})}catch(n){n instanceof Error?p(l,n.message):p(l),process.exit(1)}}export{ne as default};
File without changes