|
| 1 | +// Copyright 2020 Contributors to the Parsec project. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +//! Base CLI implementation. |
| 5 | +
|
| 6 | +use crate::common::{PROJECT_AUTHOR, PROJECT_DESC, PROJECT_NAME, PROJECT_VERSION}; |
| 7 | +use crate::error::ParsecToolError; |
| 8 | +use crate::subcommands::{ParsecToolSubcommand, Subcommand}; |
| 9 | +use parsec_client::auth::AuthenticationData; |
| 10 | +use structopt::StructOpt; |
| 11 | + |
| 12 | +/// Struct representing the command-line interface of parsec-tool. |
| 13 | +#[derive(Debug, StructOpt)] |
| 14 | +#[structopt(name=PROJECT_NAME, about=PROJECT_DESC, author=PROJECT_AUTHOR, version=PROJECT_VERSION)] |
| 15 | +pub struct ParsecToolApp { |
| 16 | + /// How verbose should we be? |
| 17 | + #[structopt(short = "v", multiple = true, default_value = "0")] |
| 18 | + pub verbosity: u8, |
| 19 | + |
| 20 | + /// Sets the authentication secret -- will default to no authentication if unspecified. |
| 21 | + #[structopt(short = "a", long = "auth-secret")] |
| 22 | + pub auth_secret: Option<String>, |
| 23 | + |
| 24 | + /// The subcommand -- e.g., ping. |
| 25 | + #[structopt(subcommand)] |
| 26 | + pub subcommand: Subcommand, |
| 27 | +} |
| 28 | + |
| 29 | +impl ParsecToolApp { |
| 30 | + /// Run the requested subcommand. |
| 31 | + pub fn dispatch_subcommand(&self) -> Result<(), ParsecToolError> { |
| 32 | + match &self.subcommand { |
| 33 | + Subcommand::Ping(cmd) => cmd.run(self), |
| 34 | + Subcommand::ListProviders(cmd) => cmd.run(self), |
| 35 | + Subcommand::ListOpcodes(cmd) => cmd.run(self), |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + /// Given an optional string, generate the corresponding AuthenticationData instance. This is |
| 40 | + /// effectively a `FromStr` implementation for AuthenticationData. Passing in `None` will return |
| 41 | + /// AuthenticationData::None. Passing in `Some(s)` will give you an app identity whose secret is |
| 42 | + /// built from the string `s`. |
| 43 | + pub fn authentication_data(&self) -> AuthenticationData { |
| 44 | + match &self.auth_secret { |
| 45 | + None => AuthenticationData::None, |
| 46 | + Some(s) => AuthenticationData::AppIdentity(secrecy::Secret::new(s.into())), |
| 47 | + } |
| 48 | + } |
| 49 | +} |
0 commit comments