    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95

    use clap::Parser;

    use pasetoken_lib::pasetoken::ConfigPaSeToken;
    //use pasetors::footer::Footer;

    use crate::{PKG_NAME,PKG_VERSION};

    use crate::tools::generate_uuid;

    // Use `clap` to parse command line options with `derive` mode
    #[derive(Parser, Debug)]
    pub struct Cli {

        /// * Configuration TOML file-path to run `WebServer` REQUIRED
        #[clap(short = 'c', long = "config", value_parser, display_order=1)]
        pub config_path: Option<String>,

        /// Generate id key for identification
        #[clap(short = 'i', long = "id",  action, display_order=2)]
        pub create_id: bool,

        /// Path to generate PaSeTo public and secret keys to use for tokens
        #[clap(short = 'p', long = "pasetoken", value_parser, display_order=3)]
        pub pasetokeys_path: Option<String>,

        /// Settings-toml-file to Generate PaSeTo Token (-o to filepath)
        #[clap(short = 't', long = "token", value_parser, display_order=4)]
        pub make_token: Option<String>,

        /// Output path to save generated token with `make_token` (-t)
        #[clap(short = 'o', long = "output", value_parser, display_order=5)]
        pub output_path: Option<String>,

        /// Show version
        #[clap(short = 'v', long = "version", action, display_order=6)]
        pub version: bool,
    }

    /// Runs some options from command line like: genererate id, PaSeTo keys, Token.
    /// Set TOML config-path to load web-server settings
    pub fn parse_args() -> String {
        let args = Cli::parse();
        if let Some(pasetokeys_path) = args.pasetokeys_path {
            match pasetoken_lib::generate_keys(&pasetokeys_path,false) {
                Ok(_) => println!("PaSeTo keys generated to: {}", &pasetokeys_path),
                Err(e) => eprint!("Error generating keys: {}",e),
            };
            std::process::exit(0);
        }
        let output_path = if let Some(out_path) = args.output_path {
            out_path
        } else {
            String::from("")
        };
        if let Some(def_path) = args.make_token {
            match ConfigPaSeToken::token_from_file_defs(&def_path, &output_path) {
                Ok(token) => {
                    if output_path.is_empty() {
                        println!("{}",&token)
                    } else {
                        println!("Token from defs {} generated to {}",&def_path,&output_path)
                    }
                },
                Err(e) => {
                    if output_path.is_empty() {
                        eprintln!("Error generating token from file defs {} error: {}",
                            &def_path, e
                        )
                    } else {
                        eprintln!("Error generating token from file defs {} to {} error: {}",
                            &def_path, &output_path, e
                        )
                    }
                },
            };
            std::process::exit(0);
        }
        if args.create_id {
            println!("{}",generate_uuid(String::from("abcdef0123456789")));
            std::process::exit(0);
        }
        if args.version {
            println!("{} version: {}",PKG_NAME,PKG_VERSION);
            std::process::exit(0);
        }

        let config_path = args.config_path.unwrap_or(String::from(""));
        if config_path.is_empty() {
            eprintln!("No config-file found");
            std::process::exit(2);
        }
        config_path
    }