Skip to content

Commit

Permalink
fix(error messages): env parser, secrets/vars list
Browse files Browse the repository at this point in the history
  • Loading branch information
wilfreud committed Apr 23, 2024
1 parent 1020c21 commit 396c9ca
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 37 deletions.
71 changes: 36 additions & 35 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,7 @@ program
new Option("-j, --json <path>", "Path to a JSON file to parse").conflicts(
"env"
)
)
.action((opts) => {
const { env, token, repo, owner, json } = opts;
if (token) repoManager = new RepoManager(token);

if (repo) config.repositoryName = repo;

if (owner) config.repositoryOwner = owner;
});
);

// Add commands
const varsCommand = new Command("vars");
Expand Down Expand Up @@ -153,11 +145,20 @@ varsCommand.action(async () => {

switch (actionOption) {
case "list":
const variables = await repoManager?.listRepoVariables(config);
console.table(variables?.data.variables);
console.log(
chalk.bgGreen("Total: " + variables?.data.total_count + " ")
);
try {
const variables = await repoManager?.listRepoVariables(config);
console.table(variables?.data.variables);
console.log(
chalk.bgGreen("Total: " + variables?.data.total_count + " ")
);
} catch (err) {
if (config.verbose) console.error(err);
console.error(
chalk.red(
"An error occured; please check the repository infos (owner & name)"
)
);
}
break;

case "create":
Expand Down Expand Up @@ -194,7 +195,12 @@ secretsCommand
console.table(secs?.data.secrets);
console.log(chalk.bgGreen("Total:", secs?.data.total_count));
} catch (err) {
console.error(chalk.red(err));
if (config.verbose) console.error(err);
console.error(
chalk.red(
"An error occured; please check the repository infos (owner & name)"
)
);
}
});

Expand Down Expand Up @@ -286,23 +292,22 @@ program.on("option:owner", (owner) => {
});

program.on("option:env", (env) => {
if (!program.getOptionValue("json"))
try {
parsedKeyValues = envParser(env, { verbose: config.verbose || false });
} catch (err) {
console.error(chalk.red(err));
process.exit(-1);
}
const verbose: boolean = program.opts().verbose;
try {
parsedKeyValues = envParser(env, { verbose });
} catch (err) {
console.error(chalk.red(err));
process.exit(-1);
}
});

program.on("option:json", (json) => {
if (!program.getOptionValue("env"))
try {
parsedKeyValues = jsonParser(json, { verbose: config.verbose || false });
} catch (err) {
console.error(chalk.red(err));
process.exit(-1);
}
try {
parsedKeyValues = jsonParser(json, { verbose: config.verbose || false });
} catch (err) {
console.error(chalk.red(err));
process.exit(-1);
}
});

program.action(() => {
Expand Down Expand Up @@ -331,7 +336,8 @@ async function fn() {
// check auth and set default owner

try {
config.repositoryOwner = (await repoManager.getUserLogin()) || "";
if (!config.repositoryOwner)
config.repositoryOwner = (await repoManager.getUserLogin()) || "";

if (config.verbose) {
console.log(
Expand Down Expand Up @@ -369,8 +375,3 @@ async function fn() {
config.repositoryOwner = repoOwner;
}
}

// // If no command is provided, show help
// if (!process.argv.slice(2).length) {
// program.outputHelp();
// }
4 changes: 2 additions & 2 deletions src/lib/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ export function envParser(
const filecontent = readFileSync(filePath);
result = parse(filecontent);
} catch (error) {
if (config?.verbose) console.error(error);
if (true || config?.verbose) console.error(error);
throw new Error(
"Error reading JSON file. Please ensure the file exists and is valid."
"Error reading env file. Please ensure the file exists and is valid."
);
}
return result;
Expand Down
1 change: 1 addition & 0 deletions src/lib/repository-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export class RepoManager {
public async listRepoSecrets(
config: Configuration
): Promise<ReturnType<typeof this.app.rest.actions.listRepoSecrets>> {
console.log("Config:", config);
spinner.start("Fetching repository secrets...\n");
return await this.app.rest.actions.listRepoSecrets({
owner: config.repositoryOwner,
Expand Down

0 comments on commit 396c9ca

Please sign in to comment.