Skip to content

Commit

Permalink
Validate inputs in add and remove
Browse files Browse the repository at this point in the history
  • Loading branch information
chinarjoshi committed Sep 18, 2023
1 parent fbd77e1 commit 9b135ec
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/subcmds/add.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
#include "../file_io.h"

bool add_cmd(Cli *cli) {
// Initialize structures and validate inputs
struct Add a = cli->cmd.add;
if (!a.command) {
printf("Error invalid args: must provide command to alias.\n");
return false;
}
HashTable *ht;
create_hash_table(&ht, INITIAL_CAPACITY, LOAD_FACTOR);
Entry *entry;
Expand Down
8 changes: 5 additions & 3 deletions src/subcmds/remove.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@
#include <unistd.h>

bool remove_cmd(Cli *cli) {
// Initialize structures and variables
// Initialize structures and validate inputs
struct Remove r = cli->cmd.remove;
if (!r.aliases) {
printf("No %s provided.\n", r.recursive ? "section" : "alias");
}
HashTable *ht;
create_hash_table(&ht, INITIAL_CAPACITY, LOAD_FACTOR);
Entry *entry;

// Pick the alias file name ('.env' if 'a.local', else '~/.aliases')
const char *alias_fname = (r.local) ? AUTOENV_FNAME : ALIAS_FNAME;
// Make sure the alias file exists
if (access(alias_fname, F_OK) == -1) {
FILE *f = fopen(alias_fname, "w");
fclose(f);
}
// Open the correct alias file ('.env' if 'a.local', else '~/.aliases')
FILE *alias_f = fopen(alias_fname, "r");
if (!alias_f)
return cleanup("Error: aliases file not found: %s\n", alias_fname, ht, 0, 0);
Expand Down Expand Up @@ -70,6 +73,5 @@ bool remove_cmd(Cli *cli) {
perror("Error renaming file.\n");
return false;
}
// Cleanup cli
return true;
}

0 comments on commit 9b135ec

Please sign in to comment.