Skip to content

Commit

Permalink
Added option to overwrite file
Browse files Browse the repository at this point in the history
  • Loading branch information
m-radmacher committed Oct 18, 2022
1 parent a0612c0 commit b415958
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ GH-GET allows you to easily retrieve the latest artifact of a workflow from GitH

## Arguments

- **-r**: The repository containing the workflow
- **-u**: The owner of the repository
- **-a**: The name of your artifact (can be configured in the actions .yml)
- **-w**: The id of the workflow or the name of the actions .yml file
- **-o**: The output path. The file name will always follow this schema: `${artifactName}-${runNumber}.zip`
- **-p**: A personal access token with the `repo` permission
- **-r**/**--repository**: The repository containing the workflow
- **-u**/**--user**: The owner of the repository
- **-a**/**--artifact**: The name of your artifact (can be configured in the actions .yml)
- **-w**/**--workflow**: The id of the workflow or the name of the actions .yml file
- **-o**/**--output**: The output path. The file name will always follow this schema: `${artifactName}-${runNumber}.zip`
- **-p**/**--pat**: A personal access token with the `repo` permission
- **-f**/**--overwrite**: Overwrite the old file instead of creating a new one? Changes the file schema to `${artifactName}.zip`

## Example

Expand Down
29 changes: 25 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Configuration = {
workflowId?: string;
outputPath?: string;
pat?: string;
overwrite?: boolean;
};

async function run() {
Expand All @@ -19,23 +20,36 @@ async function run() {
const argParts = arg.split('=');
switch (argParts[0]) {
case '-r':
case '--repository':
config.repo = argParts[1];
break;
case '-u':
case '--user':
config.owner = argParts[1];
break;
case '-a':
case '--artifact':
config.artifactName = argParts[1];
break;
case '-w':
case '--workflow':
config.workflowId = argParts[1];
break;
case '-o':
case '--output':
config.outputPath = argParts[1];
break;
case '-p':
case '--pat':
config.pat = argParts[1];
break;
case '-v':
case '--overwrite':
config.overwrite = Boolean(argParts[1]);
break;
default:
console.log('Unknown arguement: ' + argParts[0]);
break;
}
}

Expand Down Expand Up @@ -115,10 +129,17 @@ async function run() {
archive_format: 'zip',
});

fs.appendFileSync(
path.join(config.outputPath, `${config.artifactName}-${highestRunNumber}.zip`),
Buffer.from(file as any)
);
if (config.overwrite) {
// overwrite old file
fs.rmSync(path.join(config.outputPath, `${config.artifactName}.zip`));
fs.appendFileSync(path.join(config.outputPath, `${config.artifactName}.zip`), Buffer.from(file as any));
} else {
// create new file
fs.appendFileSync(
path.join(config.outputPath, `${config.artifactName}-${highestRunNumber}.zip`),
Buffer.from(file as any)
);
}
}

run();

0 comments on commit b415958

Please sign in to comment.