summaryrefslogtreecommitdiffstats
path: root/ipfs-cli/src/command/mod.rs
blob: 86f9aa547e49694e765d4aae3a101beaa8a0da25 (plain)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright 2017 rust-ipfs-api Developers
//
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.
//

use clap::{App, ArgMatches};
use futures::Future;
use ipfs_api::IpfsClient;
use std::error::Error;
use std::fs;
use std::path::Path;

pub type CommandExecutable = Box<Future<Item = (), Error = ()> + 'static + Send>;

pub const EXPECTED_FILE: &str = "expected to read input file";

/// Verifies that a path points to a file that exists, and not a directory.
///
pub fn verify_file<P>(path: P) -> Result<(), String>
where
    P: AsRef<Path>,
{
    match fs::metadata(path) {
        Ok(ref metadata) if metadata.is_file() => Ok(()),
        Ok(_) => Err("file must not be a directory".into()),
        Err(e) => Err(e.description().into()),
    }
}

pub trait CliCommand {
    /// Name of the command.
    ///
    const NAME: &'static str;

    /// Returns the signature of the application.
    ///
    fn signature<'a, 'b>() -> App<'a, 'b>;

    /// Creates a future representing the request to make.
    ///
    fn handle(client: &IpfsClient, args: &ArgMatches) -> CommandExecutable;
}

macro_rules! handle_case {
    // Base macro case. Converts an expression into a boxed future.
    //
    ($run: expr) => {
        {
            let future = $run;

            return Box::new(future.map_err(|e| eprintln!("{}", e)))
        }
    };
    // Base case for nested subcommand (e.g. /bootstrap/add).
    //
    (
        $subcommand: ident;
        ($key: pat) => { $(($inner_key: pat, $args: ident) => $run: expr),* }
    ) => {
        if let ($key, Some(args)) = $subcommand {
            let inner_subcommand = args.subcommand();

            $(
                handle_case!(inner_subcommand; ($inner_key, $args) => $run);
            )*
        }
    };
    // Base case for subcommand.
    //
    (
        $subcommand: ident;
        ($key: pat, $args: pat) => $run: expr
    ) => {
        if let ($key, Some($args)) = $subcommand {
            handle_case!($run)
        }
    };
    // Recursive case for nested subcommand.
    //
    (
        $subcommand: ident;
        ($key: pat) => { $(($inner_key: pat, $args: ident) => $run: expr),* },
        $($rest_args: tt => $rest_run: tt),*
    ) => {
        handle_case!($subcommand; ($key) => { $(($inner_key, $args) => $run),* });

        $(
            handle_case!($subcommand; $rest_args => $rest_run);
        )*
    };
    // Recursive case fo subcommand.
    //
    (
        $subcommand: ident;
        ($key: pat, $args: pat) => $run: expr,
        $($rest_args: tt => $rest_run: tt),*
    ) => {
        handle_case!($subcommand; ($key, $args) => $run);

        $(
            handle_case!($subcommand; $rest_args => $rest_run);
        )*
    }
}

macro_rules! handle {
    // Command with no subcommands.
    //
    (
        ($args: ident, $client: ident) => $run: expr
    ) => {
        fn handle(
            client: &::ipfs_api::IpfsClient,
            args: &::clap::ArgMatches,
        ) -> ::command::CommandExecutable {
            let $args = args;
            let $client = client;

            handle_case!($run)
        }
    };
    // Command with one or more subcommands.
    //
    (
        $client: ident;
        $($args: tt => $run: tt),*
    ) => {
        fn handle(
            client: &::ipfs_api::IpfsClient,
            args: &::clap::ArgMatches,
        ) -> ::command::CommandExecutable {
            let $client = client;
            let subcommand = args.subcommand();

            handle_case!(subcommand; $($args => $run),*);

            unreachable!()
        }
    }
}

pub mod add;
pub mod bitswap;
pub mod block;
pub mod bootstrap;
pub mod cat;
pub mod commands;
pub mod config;
pub mod dag;
pub mod dht;
pub mod diag;
pub mod dns;
pub mod file;
pub mod files;
pub mod filestore;
pub mod shutdown;
pub mod version;