//! Key related CLI utilities

use super::{generate::GenerateCmd, inspect_key::InspectKeyCmd};
use sc_cli::{Error, GenerateKeyCmdCommon, InsertKeyCmd, InspectNodeKeyCmd, SubstrateCli};

/// Key utilities for the cli.
#[derive(Debug, clap::Subcommand)]
pub enum KeySubcommand {
    /// Generate a random node key, write it to a file or stdout and write the
    /// corresponding peer-id to stderr
    GenerateNodeKey(GenerateKeyCmdCommon),

    /// Generate a Substrate-based random account
    Generate(GenerateCmd),

    /// Gets a public key and a SS58 address from the provided Secret URI
    Inspect(InspectKeyCmd),

    /// Load a node key from a file or stdin and print the corresponding peer-id
    InspectNodeKey(InspectNodeKeyCmd),

    /// Insert a key to the keystore of a node.
    Insert(InsertKeyCmd),
}

impl KeySubcommand {
    /// run the key subcommands
    pub fn run<C: SubstrateCli>(&self, cli: &C) -> Result<(), Error> {
        match self {
            KeySubcommand::GenerateNodeKey(cmd) => cmd.run(),
            KeySubcommand::Generate(cmd) => cmd.run(),
            KeySubcommand::Inspect(cmd) => cmd.run(),
            KeySubcommand::Insert(cmd) => cmd.run(cli),
            KeySubcommand::InspectNodeKey(cmd) => cmd.run(),
        }
    }
}
