Skip to content

Commit

Permalink
SM-815: Switch Command Order (#76)
Browse files Browse the repository at this point in the history
* SM-815: Switch command order

* SM-815: Sort bws/src/main.rs enum variants in alphabetical order
  • Loading branch information
coltonhurst authored Jun 24, 2023
1 parent 32b968e commit 4d03abd
Showing 1 changed file with 165 additions and 62 deletions.
227 changes: 165 additions & 62 deletions crates/bws/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,39 +62,100 @@ struct Cli {

#[derive(Subcommand, Debug)]
enum Commands {
#[command(long_about = "List items")]
List {
#[command(long_about = "Configure the CLI", arg_required_else_help(true))]
Config {
name: Option<ProfileKey>,
value: Option<String>,

#[arg(short = 'd', long)]
delete: bool,
},
#[command(long_about = "Commands available on Projects")]
Project {
#[command(subcommand)]
cmd: ListCommand,
cmd: ProjectCommand,
},
#[command(long_about = "Retrieve a single item")]
Get {
#[command(long_about = "Commands available on Secrets")]
Secret {
#[command(subcommand)]
cmd: GetCommand,
cmd: SecretCommand,
},
#[command(long_about = "Create a single item")]
#[command(long_about = "Create a single item (deprecated)", hide(true))]
Create {
#[command(subcommand)]
cmd: CreateCommand,
},
#[command(long_about = "Edit a single item")]
#[command(long_about = "Delete one or more items (deprecated)", hide(true))]
Delete {
#[command(subcommand)]
cmd: DeleteCommand,
},
#[command(long_about = "Edit a single item (deprecated)", hide(true))]
Edit {
#[command(subcommand)]
cmd: EditCommand,
},
#[command(long_about = "Delete one or more items")]
Delete {
#[command(long_about = "Retrieve a single item (deprecated)", hide(true))]
Get {
#[command(subcommand)]
cmd: DeleteCommand,
cmd: GetCommand,
},
#[command(long_about = "Configure the CLI", arg_required_else_help(true))]
Config {
name: Option<ProfileKey>,
#[command(long_about = "List items (deprecated)", hide(true))]
List {
#[command(subcommand)]
cmd: ListCommand,
},
}

#[derive(Subcommand, Debug)]
enum SecretCommand {
Create {
key: String,
value: String,

#[arg(long, help = "An optional note to add to the secret")]
note: Option<String>,

#[arg(long, help = "The ID of the project this secret will be added to")]
project_id: Option<Uuid>,
},
Delete {
secret_ids: Vec<Uuid>,
},
Edit {
secret_id: Uuid,
#[arg(long, group = "edit_field")]
key: Option<String>,
#[arg(long, group = "edit_field")]
value: Option<String>,
#[arg(long, group = "edit_field")]
note: Option<String>,
},
Get {
secret_id: Uuid,
},
List {
project_id: Option<Uuid>,
},
}

#[arg(short = 'd', long)]
delete: bool,
#[derive(Subcommand, Debug)]
enum ProjectCommand {
Create {
name: String,
},
Delete {
project_ids: Vec<Uuid>,
},
Edit {
project_id: Uuid,
#[arg(long, group = "edit_field")]
name: String,
},
Get {
project_id: Uuid,
},
List,
}

#[derive(Subcommand, Debug)]
Expand All @@ -111,6 +172,9 @@ enum GetCommand {

#[derive(Subcommand, Debug)]
enum CreateCommand {
Project {
name: String,
},
Secret {
key: String,
value: String,
Expand All @@ -121,13 +185,16 @@ enum CreateCommand {
#[arg(long, help = "The ID of the project this secret will be added to")]
project_id: Option<Uuid>,
},
Project {
name: String,
},
}

#[derive(Subcommand, Debug)]
enum EditCommand {
#[clap(group = ArgGroup::new("edit_field").required(true).multiple(true))]
Project {
project_id: Uuid,
#[arg(long, group = "edit_field")]
name: String,
},
#[clap(group = ArgGroup::new("edit_field").required(true).multiple(true))]
Secret {
secret_id: Uuid,
Expand All @@ -138,18 +205,12 @@ enum EditCommand {
#[arg(long, group = "edit_field")]
note: Option<String>,
},
#[clap(group = ArgGroup::new("edit_field").required(true).multiple(true))]
Project {
project_id: Uuid,
#[arg(long, group = "edit_field")]
name: String,
},
}

#[derive(Subcommand, Debug)]
enum DeleteCommand {
Secret { secret_ids: Vec<Uuid> },
Project { project_ids: Vec<Uuid> },
Secret { secret_ids: Vec<Uuid> },
}

#[tokio::main(flavor = "current_thread")]
Expand Down Expand Up @@ -258,7 +319,10 @@ async fn process_commands() -> Result<()> {

// And finally we process all the commands which require authentication
match command {
Commands::List {
Commands::Project {
cmd: ProjectCommand::List,
}
| Commands::List {
cmd: ListCommand::Projects,
} => {
let projects = client
Expand All @@ -271,35 +335,10 @@ async fn process_commands() -> Result<()> {
serialize_response(projects, cli.output, color);
}

Commands::List {
cmd: ListCommand::Secrets { project_id },
} => {
let res = if let Some(project_id) = project_id {
client
.secrets()
.list_by_project(&SecretIdentifiersByProjectRequest {
project_id: project_id,
})
.await?
} else {
client
.secrets()
.list(&SecretIdentifiersRequest {
organization_id: organization_id.clone(),
})
.await?
};

let mut secrets = Vec::new();

for s in res.data {
let secret = client.secrets().get(&SecretGetRequest { id: s.id }).await?;
secrets.push(secret);
}
serialize_response(secrets, cli.output, color);
Commands::Project {
cmd: ProjectCommand::Get { project_id },
}

Commands::Get {
| Commands::Get {
cmd: GetCommand::Project { project_id },
} => {
let project = client
Expand All @@ -309,7 +348,10 @@ async fn process_commands() -> Result<()> {
serialize_response(project, cli.output, color);
}

Commands::Create {
Commands::Project {
cmd: ProjectCommand::Create { name },
}
| Commands::Create {
cmd: CreateCommand::Project { name },
} => {
let project = client
Expand All @@ -322,7 +364,10 @@ async fn process_commands() -> Result<()> {
serialize_response(project, cli.output, color);
}

Commands::Edit {
Commands::Project {
cmd: ProjectCommand::Edit { project_id, name },
}
| Commands::Edit {
cmd: EditCommand::Project { project_id, name },
} => {
let project = client
Expand All @@ -336,7 +381,10 @@ async fn process_commands() -> Result<()> {
serialize_response(project, cli.output, color);
}

Commands::Delete {
Commands::Project {
cmd: ProjectCommand::Delete { project_ids },
}
| Commands::Delete {
cmd: DeleteCommand::Project { project_ids },
} => {
let project_count = project_ids.len();
Expand All @@ -353,7 +401,41 @@ async fn process_commands() -> Result<()> {
}
}

Commands::Get {
Commands::Secret {
cmd: SecretCommand::List { project_id },
}
| Commands::List {
cmd: ListCommand::Secrets { project_id },
} => {
let res = if let Some(project_id) = project_id {
client
.secrets()
.list_by_project(&SecretIdentifiersByProjectRequest {
project_id: project_id,
})
.await?
} else {
client
.secrets()
.list(&SecretIdentifiersRequest {
organization_id: organization_id.clone(),
})
.await?
};

let mut secrets = Vec::new();

for s in res.data {
let secret = client.secrets().get(&SecretGetRequest { id: s.id }).await?;
secrets.push(secret);
}
serialize_response(secrets, cli.output, color);
}

Commands::Secret {
cmd: SecretCommand::Get { secret_id },
}
| Commands::Get {
cmd: GetCommand::Secret { secret_id },
} => {
let secret = client
Expand All @@ -363,7 +445,16 @@ async fn process_commands() -> Result<()> {
serialize_response(secret, cli.output, color);
}

Commands::Create {
Commands::Secret {
cmd:
SecretCommand::Create {
key,
value,
note,
project_id,
},
}
| Commands::Create {
cmd:
CreateCommand::Secret {
key,
Expand All @@ -385,7 +476,16 @@ async fn process_commands() -> Result<()> {
serialize_response(secret, cli.output, color);
}

Commands::Edit {
Commands::Secret {
cmd:
SecretCommand::Edit {
secret_id,
key,
value,
note,
},
}
| Commands::Edit {
cmd:
EditCommand::Secret {
secret_id,
Expand Down Expand Up @@ -414,7 +514,10 @@ async fn process_commands() -> Result<()> {
serialize_response(secret, cli.output, color);
}

Commands::Delete {
Commands::Secret {
cmd: SecretCommand::Delete { secret_ids },
}
| Commands::Delete {
cmd: DeleteCommand::Secret { secret_ids },
} => {
client
Expand Down

0 comments on commit 4d03abd

Please sign in to comment.