Files
k8s-deploy/src/utilities/fileUtils.ts
T
github-actions[bot] e3c97bfc20 v3 new release (#182)
* Make pulling of images switchable (#178)

* Make namespace annotation switchable (#177)

* Bump tmpl from 1.0.4 to 1.0.5 (#152)

Bumps [tmpl](https://github.com/daaku/nodejs-tmpl) from 1.0.4 to 1.0.5.
- [Release notes](https://github.com/daaku/nodejs-tmpl/releases)
- [Commits](https://github.com/daaku/nodejs-tmpl/commits/v1.0.5)

---
updated-dependencies:
- dependency-name: tmpl
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump ansi-regex from 5.0.0 to 5.0.1 (#166)

Bumps [ansi-regex](https://github.com/chalk/ansi-regex) from 5.0.0 to 5.0.1.
- [Release notes](https://github.com/chalk/ansi-regex/releases)
- [Commits](https://github.com/chalk/ansi-regex/compare/v5.0.0...v5.0.1)

---
updated-dependencies:
- dependency-name: ansi-regex
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump minimist from 1.2.5 to 1.2.6 (#175)

Bumps [minimist](https://github.com/substack/minimist) from 1.2.5 to 1.2.6.
- [Release notes](https://github.com/substack/minimist/releases)
- [Commits](https://github.com/substack/minimist/compare/1.2.5...1.2.6)

---
updated-dependencies:
- dependency-name: minimist
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Add directory functionality (#181)

* Add node modules and compiled JavaScript from main

Co-authored-by: Jan Röhrich <roehrijn@users.noreply.github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Jaiveer Katariya <35347859+jaiveerk@users.noreply.github.com>
Co-authored-by: Oliver King <oking3@uncc.edu>
2022-04-12 13:22:09 -04:00

109 lines
3.2 KiB
TypeScript

import * as fs from "fs";
import * as path from "path";
import * as core from "@actions/core";
import * as os from "os";
import { getCurrentTime } from "./timeUtils";
export function getTempDirectory(): string {
return process.env["runner.tempDirectory"] || os.tmpdir();
}
export function writeObjectsToFile(inputObjects: any[]): string[] {
const newFilePaths = [];
inputObjects.forEach((inputObject: any) => {
try {
const inputObjectString = JSON.stringify(inputObject);
if (inputObject?.metadata?.name) {
const fileName = getManifestFileName(
inputObject.kind,
inputObject.metadata.name
);
fs.writeFileSync(path.join(fileName), inputObjectString);
newFilePaths.push(fileName);
} else {
core.debug(
"Input object is not proper K8s resource object. Object: " +
inputObjectString
);
}
} catch (ex) {
core.debug(
`Exception occurred while writing object to file ${inputObject}: ${ex}`
);
}
});
return newFilePaths;
}
export function writeManifestToFile(
inputObjectString: string,
kind: string,
name: string
): string {
if (inputObjectString) {
try {
const fileName = getManifestFileName(kind, name);
fs.writeFileSync(path.join(fileName), inputObjectString);
return fileName;
} catch (ex) {
throw Error(
`Exception occurred while writing object to file: ${inputObjectString}. Exception: ${ex}`
);
}
}
}
function getManifestFileName(kind: string, name: string) {
const filePath = `${kind}_${name}_ ${getCurrentTime().toString()}`;
const tempDirectory = getTempDirectory();
return path.join(tempDirectory, path.basename(filePath));
}
export function getFilesFromDirectories(
filePaths: string[]
): string[]{
const fullPathSet: Set<string> = new Set<string>()
filePaths.forEach((fileName => {
try {
if(fs.lstatSync(fileName).isDirectory()){
recurisveManifestGetter(fileName).forEach((file) => {fullPathSet.add(file)})
} else if(getFileExtension(fileName) === "yml" || getFileExtension(fileName) === "yaml"){
fullPathSet.add(fileName)
} else{
core.debug(`Detected non-manifest file, ${fileName}, continuing... ` )
}
} catch (ex) {
throw Error(
`Exception occurred while reading the file ${fileName}: ${ex}`
);
}
}))
return Array.from(fullPathSet)
}
function recurisveManifestGetter(dirName: string): string[]{
const toRet: string[] = []
fs.readdirSync(dirName).forEach((fileName) => {
const fnwd: string = path.join(dirName, fileName)
if(fs.lstatSync(fnwd).isDirectory()){
toRet.push(...recurisveManifestGetter(fnwd))
} else if(getFileExtension(fileName) === "yml" || getFileExtension(fileName) === "yaml"){
toRet.push(path.join(dirName, fileName))
} else{
core.debug(`Detected non-manifest file, ${fileName}, continuing... ` )
}
})
return toRet
}
function getFileExtension(fileName: string){
return fileName.slice((fileName.lastIndexOf(".") - 1 >>> 0) + 2)
}