3
0
Fork 0
mirror of https://github.com/Swatinem/rust-cache synced 2026-02-07 11:32:15 +00:00

chore: make config load from state explicit

Make the loading of config from state explicit by splitting into a
dedicated static method.
This commit is contained in:
Steven Hartland 2023-05-17 21:42:24 +01:00
parent 68377a4bda
commit c7502cd809
4 changed files with 62 additions and 27 deletions

View file

@ -46,15 +46,6 @@ export class CacheConfig {
*/
static async new(): Promise<CacheConfig> {
const self = new CacheConfig();
const source = core.getState(STATE_CONFIG);
if (source !== "") {
// Post action, use what we calculated in the main action ensuring consistency.
Object.assign(self, JSON.parse(source));
self.workspaces = self.workspaces
.map((w: any) => new Workspace(w.root, w.target));
return self;
}
// Construct key prefix:
// This uses either the `shared-key` input,
@ -177,6 +168,28 @@ export class CacheConfig {
return self;
}
/**
* Reads and returns the cache config from the action `state`.
*
* @throws {Error} if the state is not present.
* @returns {CacheConfig} the configuration.
* @see {@link CacheConfig#saveState}
* @see {@link CacheConfig#new}
*/
static fromState(): CacheConfig {
const source = core.getState(STATE_CONFIG);
if (!source) {
throw new Error("Cache configuration not found in state");
}
const self = new CacheConfig();
Object.assign(self, JSON.parse(source));
self.workspaces = self.workspaces
.map((w: any) => new Workspace(w.root, w.target));
return self;
}
/**
* Prints the configuration to the action log.
*/