mirror of
https://github.com/openziti/zrok.git
synced 2025-06-20 17:58:50 +02:00
minimal environment (#893)
This commit is contained in:
parent
d26c33c602
commit
83f39ff4ed
69
sdk/nodejs1/sdk/src/environment.ts
Normal file
69
sdk/nodejs1/sdk/src/environment.ts
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
import {environmentFile, metadataFile} from "./paths";
|
||||||
|
import * as fs from "node:fs";
|
||||||
|
|
||||||
|
const ENVIRONMENT_V = "v0.4";
|
||||||
|
|
||||||
|
export class Root {
|
||||||
|
metadata: Metadata;
|
||||||
|
environment: Environment|undefined;
|
||||||
|
|
||||||
|
constructor(metadata: Metadata, environment: Environment|undefined) {
|
||||||
|
this.metadata = metadata;
|
||||||
|
this.environment = environment;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class Metadata {
|
||||||
|
v: string;
|
||||||
|
rootPath: string;
|
||||||
|
|
||||||
|
constructor(v: string, rootPath: string = "") {
|
||||||
|
this.v = v;
|
||||||
|
this.rootPath = rootPath;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class Environment {
|
||||||
|
accountToken: string;
|
||||||
|
zId: string;
|
||||||
|
apiEndpoint: string;
|
||||||
|
|
||||||
|
constructor(accountToken: string, zId: string, apiEndpoint: string) {
|
||||||
|
this.accountToken = accountToken;
|
||||||
|
this.zId = zId;
|
||||||
|
this.apiEndpoint = apiEndpoint;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const loadRoot = (): Root => {
|
||||||
|
if(rootExists()) {
|
||||||
|
let metadata = loadMetadata();
|
||||||
|
let environment = loadEnvironment();
|
||||||
|
return new Root(metadata, environment);
|
||||||
|
}
|
||||||
|
throw new Error("unable to load root; did you 'zrok enable'?");
|
||||||
|
};
|
||||||
|
|
||||||
|
export const rootExists = (): boolean => {
|
||||||
|
return fs.existsSync(metadataFile());
|
||||||
|
}
|
||||||
|
|
||||||
|
const loadMetadata = (): Metadata => {
|
||||||
|
let f = metadataFile();
|
||||||
|
let data = fs.readFileSync(f);
|
||||||
|
let obj = JSON.parse(data.toString());
|
||||||
|
if(obj.v != ENVIRONMENT_V) {
|
||||||
|
throw new Error("invalid environment version! got version '" + obj.v + "' expected '" + ENVIRONMENT_V + "'");
|
||||||
|
}
|
||||||
|
return new Metadata(obj.v, f);
|
||||||
|
}
|
||||||
|
|
||||||
|
const loadEnvironment = (): Environment|undefined => {
|
||||||
|
let f = environmentFile();
|
||||||
|
if(!fs.existsSync(f)) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
let data = fs.readFileSync(f);
|
||||||
|
let obj = JSON.parse(data.toString());
|
||||||
|
return new Environment(obj.zrok_token, obj.ziti_identity, obj.api_endpoint);
|
||||||
|
}
|
@ -5,3 +5,10 @@ export const rootDir = (): string => {
|
|||||||
return join(homedir(), ".zrok");
|
return join(homedir(), ".zrok");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const metadataFile = (): string => {
|
||||||
|
return join(rootDir(), "metadata.json");
|
||||||
|
}
|
||||||
|
|
||||||
|
export const environmentFile = (): string => {
|
||||||
|
return join(rootDir(), "environment.json");
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user