summaryrefslogtreecommitdiffstats
path: root/ipfs-cli/src/command/files.rs
blob: 00329a91b5d7983f76bb4cb7243ef2a84468ccdb (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// 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 command::{verify_file, EXPECTED_API, EXPECTED_FILE};
use futures::stream::Stream;
use ipfs_api::IpfsClient;
use std::fs::File;
use std::io::{self, Write};
use tokio_core::reactor::Core;


pub fn signature<'a, 'b>() -> App<'a, 'b> {
    clap_app!(
        @subcommand files =>
            (@setting SubcommandRequiredElseHelp)
            (@subcommand cp =>
                (about: "Copy files in MFS")
                (@arg SRC: +required "The source object to copy")
                (@arg DEST: +required "The destination to copy the object to")
            )
            (@subcommand flush =>
                (about: "Flush a path's data to disk")
                (@arg PATH: "The path to flush")
            )
            (@subcommand ls =>
                (about: "List directories in MFS")
                (@arg PATH: "The past to list")
            )
            (@subcommand mkdir =>
                (about: "Make directories in MFS")
                (@arg PATH: +required "The directory to create")
                (@arg parents: -p --parents "Create parents if the directory \
                    does not already exist")
            )
            (@subcommand mv =>
                (about: "Move files in MFS")
                (@arg SRC: +required "The source object to move")
                (@arg DEST: +required "The destination to move the object to")
            )
            (@subcommand read =>
                (about: "Read a file in MFS")
                (@arg PATH: +required "The path to read")
            )
            (@subcommand rm =>
                (about: "Remove a file in MFS")
                (@arg PATH: +required "The file to remove")
                (@arg recursive: -r --recursive "Recursively remove directories")
            )
            (@subcommand stat =>
                (about: "Display status for a file in MFS")
                (@arg PATH: +required "The file to get status for")
            )
            (@subcommand write =>
                (about: "Write a file to MFS")
                (@arg DEST: +required "The path to write to")
                (@arg INPUT: +required {verify_file} "The file to write")
                (@arg create: --create "Create the file if it does not exist")
                (@arg truncate: --truncate "Truncate the file before writing")
            )
    )
}


pub fn handle(core: &mut Core, client: &IpfsClient, args: &ArgMatches) {
    match args.subcommand() {
        ("cp", Some(args)) => {
            let src = args.value_of("SRC").unwrap();
            let dest = args.value_of("DEST").unwrap();

            core.run(client.files_cp(&src, &dest)).expect(EXPECTED_API);

            println!("");
            println!("  OK");
            println!("");
        }
        ("flush", Some(args)) => {
            let path = args.value_of("PATH");

            core.run(client.files_flush(&path)).expect(EXPECTED_API);

            println!("");
            println!("  OK");
            println!("");
        }
        ("ls", Some(args)) => {
            let path = args.value_of("PATH");
            let ls = core.run(client.files_ls(&path)).expect(EXPECTED_API);

            println!("");
            println!("  entries                :");
            for entry in ls.entries {
                println!("    name       : {}", entry.name);
                println!("    type       : {}", entry.typ);
                println!("    size       : {}", entry.size);
                println!("    hash       : {}", entry.hash);
                println!("");
            }
            println!("");
        }
        ("mkdir", Some(args)) => {
            let path = args.value_of("PATH").unwrap();

            core.run(client.files_mkdir(&path, args.is_present("parents")))
                .expect(EXPECTED_API);

            println!("");
            println!("  OK");
            println!("");
        }
        ("mv", Some(args)) => {
            let src = args.value_of("SRC").unwrap();
            let dest = args.value_of("DEST").unwrap();

            core.run(client.files_mv(&src, &dest)).expect(EXPECTED_API);

            println!("");
            println!("  OK");
            println!("");
        }
        ("read", Some(args)) => {
            let path = args.value_of("PATH").unwrap();
            let req = client.files_read(&path).for_each(|chunk| {
                io::stdout().write_all(&chunk).map_err(From::from)
            });

            core.run(req).expect(EXPECTED_API);
        }
        ("rm", Some(args)) => {
            let path = args.value_of("PATH").unwrap();
            let req = client.files_rm(&path, args.is_present("recursive"));

            core.run(req).expect(EXPECTED_API);

            println!("");
            println!("  OK");
            println!("");
        }
        ("stat", Some(args)) => {
            let path = args.value_of("PATH").unwrap();
            let stat = core.run(client.files_stat(&path)).expect(EXPECTED_API);

            println!("");
            println!("  hash                   : {}", stat.hash);
            println!("  size                   : {}", stat.size);
            println!("  cumulative_size        : {}", stat.cumulative_size);
            println!("  blocks                 : {}", stat.blocks);
            println!("  type                   : {}", stat.typ);
            println!("");
        }
        ("write", Some(args)) => {
            let dest = args.value_of("DEST").unwrap();
            let path = args.value_of("INPUT").unwrap();
            let file = File::open(path).expect(EXPECTED_FILE);
            let req = client.files_write(
                &dest,
                args.is_present("create"),
                args.is_present("truncate"),
                file,
            );

            core.run(req).expect(EXPECTED_API);

            println!("");
            println!("  OK");
            println!("");
        }
        _ => unreachable!(),
    }
}