Change code to make it more extandable in future

This commit is contained in:
Ivan Zosimov (Akvelon INC) 2022-02-22 14:55:30 +03:00
parent d450effe85
commit f2efd31d13
7 changed files with 75 additions and 44 deletions

View file

@ -15,15 +15,15 @@ export const restoreCache = async (
packageManager: string,
cacheDependencyPath?: string
) => {
const packageManagerInfo = await getPackageManagerInfo();
const packageManagerInfo = await getPackageManagerInfo(packageManager);
const platform = process.env.RUNNER_OS;
const cachePath = await getCacheDirectoryPath(packageManagerInfo);
const goSumFilePath = cacheDependencyPath
const dependencyFilePath = cacheDependencyPath
? cacheDependencyPath
: findGoSumFile(packageManagerInfo);
const fileHash = await glob.hashFiles(goSumFilePath);
: findDependencyFile(packageManagerInfo);
const fileHash = await glob.hashFiles(dependencyFilePath);
if (!fileHash) {
throw new Error(
@ -48,17 +48,17 @@ export const restoreCache = async (
core.info(`Cache restored from key: ${cacheKey}`);
};
const findGoSumFile = (packageManager: PackageManagerInfo) => {
let goSumFile = packageManager.goSumFilePattern;
const findDependencyFile = (packageManager: PackageManagerInfo) => {
let dependencyFile = packageManager.dependencyFilePattern;
const workspace = process.env.GITHUB_WORKSPACE!;
const rootContent = fs.readdirSync(workspace);
const goSumFileExists = rootContent.includes(goSumFile);
const goSumFileExists = rootContent.includes(dependencyFile);
if (!goSumFileExists) {
throw new Error(
`Dependencies file go.sum is not found in ${workspace}. Supported file pattern: ${goSumFile}`
`Dependencies file is not found in ${workspace}. Supported file pattern: ${dependencyFile}`
);
}
return path.join(workspace, goSumFile);
return path.join(workspace, dependencyFile);
};

View file

@ -21,13 +21,15 @@ export async function run() {
}
const cachePackages = async () => {
const cachingFlag = core.getInput('cache');
const cachingFlag = core.getBooleanInput('cache');
if (!cachingFlag) return;
const packageManager = core.getInput('package-manager');
const state = core.getState(State.CacheMatchedKey);
const primaryKey = core.getState(State.CachePrimaryKey);
const packageManagerInfo = await getPackageManagerInfo();
const packageManagerInfo = await getPackageManagerInfo(packageManager);
const cachePath = await getCacheDirectoryPath(packageManagerInfo);

View file

@ -1,13 +1,19 @@
import * as exec from '@actions/exec';
type SupportedPackageManagers = {
[prop: string]: PackageManagerInfo;
};
export interface PackageManagerInfo {
goSumFilePattern: string;
dependencyFilePattern: string;
getCacheFolderCommand: string;
}
export const defaultPackageManager: PackageManagerInfo = {
goSumFilePattern: 'go.sum',
getCacheFolderCommand: 'go env GOMODCACHE'
export const supportedPackageManagers: SupportedPackageManagers = {
default: {
dependencyFilePattern: 'go.sum',
getCacheFolderCommand: 'go env GOMODCACHE'
}
};
export const getCommandOutput = async (toolCommand: string) => {
@ -27,8 +33,14 @@ export const getCommandOutput = async (toolCommand: string) => {
return stdout.trim();
};
export const getPackageManagerInfo = async () => {
return defaultPackageManager;
export const getPackageManagerInfo = async (packageManager: string) => {
if (!supportedPackageManagers.packageManager) {
throw new Error(
`It's not possible to use ${packageManager}, please, check correctness of the package manager name spelling.`
);
}
return supportedPackageManagers.packageManager;
};
export const getCacheDirectoryPath = async (

View file

@ -18,7 +18,7 @@ export async function run() {
// stable will be true unless false is the exact input
// since getting unstable versions should be explicit
let stable = (core.getInput('stable') || 'true').toUpperCase() === 'TRUE';
const cache = core.getInput('cache');
const cache = core.getBooleanInput('cache');
core.info(`Setup go ${stable ? 'stable' : ''} version spec ${versionSpec}`);
@ -47,8 +47,9 @@ export async function run() {
if (isGhes()) {
throw new Error('Caching is not supported on GHES');
}
const packageManager = core.getInput('package-manager');
const cacheDependencyPath = core.getInput('cache-dependency-path');
await restoreCache(cache, cacheDependencyPath);
await restoreCache(packageManager, cacheDependencyPath);
}
// add problem matchers