pijul_org / pijul

create the remote command to manage saved remotes

By tomjankauski@gmail.com on May 25, 2019
This patch is not signed.
6eWwcqo4WokMatWqWrL9AGNvWTymELshm3mT6fjA7LsitJ3w1wHXUMc1uwuFEPqf16zze8NfEkYHimpCa9gPz5gA
This patch is in the following branches:
latest
master
testing

37

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

95

97

99

================================
pub mod remote_command;

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
pub mod remove;
pub mod revert;
pub mod rollback;
pub mod sign;
pub mod status;
pub mod tag;
pub mod unrecord;

mod fold_until;

use error::Error;
use libpijul::fs_representation::{RepoPath, RepoRoot};
use libpijul::Hash;
use libpijul::{fs_representation, Inode, Repository, Txn, DEFAULT_BRANCH};
use rand;
use std::borrow::Cow;
use std::env::current_dir;
use std::env::var;
use std::fs::{canonicalize, create_dir, metadata};
use std::io::{stderr, Write};
use std::path::{Path, PathBuf};
use std::process::exit;

pub fn all_command_invocations() -> Vec<StaticSubcommand> {
    return vec![
        log::invocation(),
        info::invocation(),
        init::invocation(),
        record::invocation(),
        unrecord::invocation(),
        add::invocation(),
        pull::invocation(),
        push::invocation(),
        apply::invocation(),
        clone::invocation(),
        remove::invocation(),
        mv::invocation(),
        ls::invocation(),
        revert::invocation(),
        patch::invocation(),
        fork::invocation(),
        branches::invocation(),
        prune::invocation(),
        checkout::invocation(),
        diff::invocation(),
        credit::invocation(),
        dist::invocation(),
        key::invocation(),
        rollback::invocation(),
        status::invocation(),
        dependencies::invocation(),
        tag::invocation(),
        sign::invocation(),
        generate_completions::invocation(),
        grep::invocation(),

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
        remote_command::invocation(),

================================
        remote_command::invocation(),

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    ];
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
use clap::{Arg, ArgMatches, SubCommand};
use commands::{default_explain, BasicOptions, StaticSubcommand};
use error::Error;
use meta::{Meta, Repository};

pub fn invocation() -> StaticSubcommand {
    return SubCommand::with_name("remote")
        .about("View and modify saved remote repositories.")
        .subcommand(default_subcommand())
        .subcommand(delete_subcommand())
        .subcommand(list_subcommand())
        .subcommand(set_subcommand());
}

pub fn list_subcommand() -> StaticSubcommand {
    SubCommand::with_name("list").about("List all saved repositories.")
}

pub fn set_subcommand() -> StaticSubcommand {
    SubCommand::with_name("set")
        .about("Add or update a remote.")
        .arg(
            Arg::with_name("name")
                .help("Short name to identify this repository.")
                .required(true),
        )
        .arg(
            Arg::with_name("remote")
                .help("URL to the remote repository.")
                .required(true),
        )
        .arg(
            Arg::with_name("port")
                .short("p")
                .long("port")
                .help("Port of the remote ssh server.")
                .takes_value(true)
                .validator(|val| {
                    let x: Result<u16, _> = val.parse();
                    match x {
                        Ok(_) => Ok(()),
                        Err(_) => Err(val),
                    }
                }),
        )
}

pub fn default_subcommand() -> StaticSubcommand {
    SubCommand::with_name("default")
        .about("Set default remote for push and pull.")
        .arg(
            Arg::with_name("name")
                .help("Name of the default repository.")
                .required(true),
        )
        .arg(
            Arg::with_name("push")
                .help("Only set the push repository (and other types set by flags)")
                .long("push"),
        )
        .arg(
            Arg::with_name("pull")
                .help("Only set the pull repository (and other types set by flags)")
                .long("pull"),
        )
}

pub fn delete_subcommand() -> StaticSubcommand {
    SubCommand::with_name("delete")
        .about("Delete a remote.")
        .arg(
            Arg::with_name("name")
                .help("Name of the repository to delete.")
                .required(true),
        )
}

pub fn run(arg_matches: &ArgMatches) -> Result<(), Error> {
    let opts = BasicOptions::from_args(arg_matches)?;
    match arg_matches.subcommand() {
        ("list", _) => run_list(&opts),
        ("set", Some(sub_args)) => run_set(&opts, sub_args),
        ("delete", Some(sub_args)) => run_delete(&opts, sub_args),
        ("default", Some(sub_args)) => run_default(&opts, sub_args),
        _ => Ok(()),
    }
}

pub fn run_default(opts: &BasicOptions, args: &ArgMatches) -> Result<(), Error> {
    let name = args.value_of("name").unwrap(); // required
    let arg_pull = args.is_present("pull");
    let arg_push = args.is_present("push");
    let mut meta = Meta::load(&opts.repo_root).unwrap_or(Meta::new());

    // validate that the name exists
    if !meta.remote.contains_key(name) {
        println!("{} is not a named remote repository.", name);
        println!("You can add a remote with pijul remote set, or list current remotes with pijul remote list");
        return Err(Error::MissingRemoteRepository);
    }

    // if all options are unset, default to all
    // otherwise only use the options that were set
    let (set_push, set_pull) = match (arg_pull, arg_push) {
        (false, false) => (true, true),
        _ => (arg_pull, arg_push),
    };

    if set_push {
        meta.push = Some(name.to_string());
    }

    if set_pull {
        meta.pull = Some(name.to_string());
    }

    meta.save(&opts.repo_root)?;
    Ok(())
}

pub fn run_list(opts: &BasicOptions) -> Result<(), Error> {
    let meta = Meta::load(&opts.repo_root).unwrap_or(Meta::new());
    for (name, remote) in meta.remote.iter() {
        println!("{} => {}", name, remote);
    }
    Ok(())
}

pub fn run_set(opts: &BasicOptions, args: &ArgMatches) -> Result<(), Error> {
    let name = args.value_of("name").unwrap(); // required argument
    let remote = args.value_of("remote").unwrap(); // required argument
    let mut meta = Meta::load(&opts.repo_root).unwrap_or(Meta::new());
    let repo: Repository = Repository {
        address: remote.to_string(),
        port: args.value_of("port").map(|x| x.parse().unwrap()),
    };
    meta.remote.insert(name.to_string(), repo);
    meta.save(&opts.repo_root)?;
    Ok(())
}

pub fn run_delete(opts: &BasicOptions, args: &ArgMatches) -> Result<(), Error> {
    let name = args.value_of("name").unwrap(); // required argument
    let mut meta = Meta::load(&opts.repo_root).unwrap_or(Meta::new());
    meta.remote.remove(name);

    // remove from default push/pull
    if meta
        .push
        .as_ref()
        .map(|push| push == &name)
        .unwrap_or(false)
    {
        meta.push = None;
    }
    if meta
        .pull
        .as_ref()
        .map(|pull| pull == &name)
        .unwrap_or(false)
    {
        meta.pull = None;
    }

    meta.save(&opts.repo_root)?;
    Ok(())
}

pub fn explain(res: Result<(), Error>) {
    default_explain(res)
}

106
107

109

================================
    "grep" => grep,
    "remote" => remote_command

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    );

36
37
38
39
40
41
42
43
44

46

================================
impl std::fmt::Display for Repository {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self.port {
            Some(port) => write!(f, "{}:{}", self.address, port),
            None => write!(f, "{}", self.address),
        }
    }
}


<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
#[derive(Debug, Serialize, Deserialize)]