Skip to content

Commit 652c73b

Browse files
committed
Some comments, updated master
2 parents b9bcab6 + 60f8321 commit 652c73b

File tree

11 files changed

+131
-26
lines changed

11 files changed

+131
-26
lines changed

CHANGELOG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
# Changelog
22

3-
## [0.4.0] - 2019-05-07
3+
## [0.4.1] - 2019-05-11
4+
### Removed
5+
6+
* Removed `--endpoint` from bitbucket subcommand. It is taken care of via a global param now.
7+
8+
## [0.4.0] - 2019-05-11
9+
### Added
10+
* Added `--ssh_remote_format`, `--set_remote`, and `--remote_name` commands. These are global commands and must be used before the subcommand. Example: `gitpub --ssh_remote_format github -n "name"` Check README for more info.
11+
412
### Changed
513

614
* This version encompasses changes to the CLI interface. It was re-written from structopt to use a clap app.

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "gitpub"
3-
version = "0.4.0"
3+
version = "0.4.1"
44
authors = ["Logan Saso <[email protected]>"]
55
description = "A CLI app to create remote git repositories."
66
repository = "https://github.com/loganintech/gitpub"

README.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,28 @@ _Note:_ Environment variables can also be passed directly via CLI parameters, th
77
Usage:
88

99
```
10-
Git Publish 0.4.0
10+
Git Publish 0.4.1
1111
Logan Saso <[email protected]>
1212
A small program to create remote git repositories from the command line.
1313
1414
USAGE:
15-
gitpub [OPTIONS] <SUBCOMMAND>
15+
gitpub [FLAGS] [OPTIONS] <SUBCOMMAND>
1616
1717
FLAGS:
18-
-h, --help Prints help information
19-
-V, --version Prints version information
18+
-h, --help Prints help information
19+
--set_remote Sets the remote of the local dir after successful creation.
20+
--ssh_remote_format Attempts to convert the git remote url into ssh format. If it fails (the provider doesn't support ssh format), the remote isn't set.
21+
-V, --version Prints version information
2022
2123
OPTIONS:
22-
--endpoint <endpoint>
24+
--endpoint <endpoint> Sets a custom endpoint to POST to, useful if you want a private instance and know the api matches one gitpub supports.
25+
--remote_name <remote_name> Designates a custom name for setting remote. Defaults to origin.
2326
2427
SUBCOMMANDS:
2528
bitbucket Create a repo on bitbucket.
2629
github Create a repo on github.
2730
gitlab Create a repo on gitlab.
2831
help Prints this message or the help of the given subcommand(s)
29-
3032
```
3133

3234
## Github Setup
@@ -38,7 +40,7 @@ _Note:_ If you want to create org repositories the token also requires `org` sco
3840

3941
### Github
4042
```
41-
gitpub-github 0.4.0
43+
gitpub-github 0.4.1
4244
Create a repo on github.
4345
4446
USAGE:
@@ -78,7 +80,7 @@ _Note_: Optionally set the `GITLAB_USERNAME` environment variable to enable prin
7880

7981
### Gitlab
8082
```
81-
gitpub-gitlab 0.4.0
83+
gitpub-gitlab 0.4.1
8284
Create a repo on gitlab.
8385
8486
USAGE:
@@ -129,7 +131,7 @@ OPTIONS:
129131
### Bitbucket
130132

131133
```
132-
gitpub-bitbucket 0.4.0
134+
gitpub-bitbucket 0.4.1
133135
Create a repo on bitbucket.
134136
135137
USAGE:
@@ -145,7 +147,6 @@ FLAGS:
145147
146148
OPTIONS:
147149
-d, --description <description> A short description of the repository.
148-
-e, --endpoint <endpoint> Allows redirection of requests to enterprise providers.
149150
--fork_policy <fork_policy> Changes the allowed forking method for this repo. [possible values: allow_forks, no_public_forks, no_forks]
150151
-n, --name <name> The name of the new repository.
151152
--scm <scm> Control underlying source control method. [possible values: git, hg]

src/cli.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,30 @@ pub fn get_app() -> App<'static, 'static> {
5151
.author("Logan Saso <[email protected]>")
5252
.about("A small program to create remote git repositories from the command line.")
5353
.version(env!("CARGO_PKG_VERSION"))
54+
.subcommand(github::subcommand())
55+
.subcommand(gitlab::subcommand())
56+
.subcommand(bitbucket::subcommand())
5457
.arg(
5558
Arg::with_name("endpoint")
5659
.long("endpoint")
57-
.takes_value(true),
60+
.takes_value(true)
61+
.help("Sets a custom endpoint to POST to, useful if you want a private instance and know the api matches one gitpub supports.")
62+
.conflicts_with("set_remote"),
63+
)
64+
.arg(
65+
Arg::with_name("set_remote")
66+
.long("set_remote")
67+
.help("Sets the remote of the local dir after successful creation."),
68+
).arg(
69+
Arg::with_name("remote_name")
70+
.long("remote_name")
71+
.help("Designates a custom name for setting remote. Defaults to origin.")
72+
.default_value("origin")
73+
.hide_default_value(true),
74+
).arg(
75+
Arg::with_name("ssh_remote_format")
76+
.long("ssh_remote_format")
77+
.help("Attempts to convert the git remote url into ssh format. If it fails (the provider doesn't support ssh format), the remote isn't set.")
78+
.conflicts_with("endpoint"),
5879
)
59-
.subcommand(github::subcommand())
60-
.subcommand(gitlab::subcommand())
61-
.subcommand(bitbucket::subcommand())
6280
}

src/git.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use std::env::current_dir;
2+
use std::process::Command;
3+
4+
pub fn add_remote(name: &str, url: &str) -> bool {
5+
if can_create_remote(name) {
6+
if let Ok(dir) = current_dir() {
7+
if dir.ancestors().any(|p| p.join(".git").exists()) {
8+
return Command::new("git")
9+
.arg("remote")
10+
.arg("add")
11+
.arg(name)
12+
.arg(url)
13+
.output()
14+
.is_ok();
15+
}
16+
}
17+
}
18+
19+
false
20+
}
21+
22+
fn can_create_remote(name: &str) -> bool {
23+
if let Ok(out) = Command::new("git")
24+
.arg("remote")
25+
.arg("get-url")
26+
.arg(name)
27+
.output()
28+
{
29+
if let Ok(err) = String::from_utf8(out.stderr) {
30+
return err.starts_with("fatal"); //If it starts with fatal, that means the remote doesn't exist and we can create one
31+
}
32+
}
33+
34+
false
35+
}

src/main.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ use cli::Gitpo;
22
use reqwest::StatusCode;
33
use std::process::exit;
44
mod cli;
5+
mod git;
56
mod provider;
67

8+
use git::add_remote;
9+
710
fn main() -> Result<(), Box<dyn std::error::Error>> {
811
let matches = cli::get_app().get_matches();
912
let config = Gitpo::from_matches(&matches);
@@ -26,7 +29,23 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
2629
match status {
2730
StatusCode::OK | StatusCode::CREATED => {
2831
let apiloc = config.extract_url(&headers);
32+
let (remote_url, can_use_ssh) = match (
33+
config.ssh_url(&headers),
34+
matches.is_present("ssh_remote_format"),
35+
) {
36+
(Some(url), true) => (url, true),
37+
_ => (config.extract_url(&headers), false),
38+
};
2939
println!("Repo created: {}", apiloc);
40+
let remote_name = matches
41+
.value_of("remote_name")
42+
.expect("This should default to origin, so something is wrong.");
43+
if matches.is_present("set_remote")
44+
&& ((matches.is_present("ssh_remote_format") && can_use_ssh) || !can_use_ssh)
45+
&& !add_remote(remote_name, &remote_url)
46+
{
47+
eprintln!("Couldn't set remote.");
48+
}
3049
}
3150
StatusCode::UNPROCESSABLE_ENTITY | StatusCode::BAD_REQUEST => {
3251
eprintln!("The provider had an issue processing this request. Perhaps the repository already exists, or you're using an unsupported option. e.g. Enabling projects on a repo in an org that has them disabled.");

src/provider/bitbucket.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,18 @@ impl<'a> Provider for BitbucketArgs<'a> {
4949
fn auth_header(&self) -> String {
5050
"Authorization".to_string()
5151
}
52+
53+
fn ssh_url(&self, _: &reqwest::header::HeaderMap) -> Option<String> {
54+
Some(format!(
55+
"[email protected]:{}/{}.git",
56+
&self.username, &self.name
57+
))
58+
}
5259
}
5360

5461
pub fn subcommand() -> App<'static, 'static> {
5562
SubCommand::with_name("bitbucket")
56-
.version("0.4.0")
63+
.version(env!("CARGO_PKG_VERSION"))
5764
.about("Create a repo on bitbucket.")
5865
.arg(
5966
Arg::with_name("name")
@@ -116,12 +123,6 @@ pub fn subcommand() -> App<'static, 'static> {
116123
Arg::with_name("language")
117124
.long("language")
118125
.help("Give bitbucket a hint about the programming language.")
119-
).arg(
120-
Arg::with_name("endpoint")
121-
.short("e")
122-
.long("endpoint")
123-
.help("Allows redirection of requests to enterprise providers.")
124-
.takes_value(true)
125126
)
126127
}
127128

src/provider/github.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,25 @@ impl<'a> Provider for GithubArgs<'a> {
6060
fn auth_header(&self) -> String {
6161
"Authorization".to_string()
6262
}
63+
64+
fn ssh_url(&self, headers: &reqwest::header::HeaderMap) -> Option<String> {
65+
headers
66+
.get("location")
67+
.and_then(|x| x.to_str().ok())
68+
.map(|src| {
69+
Some({
70+
let mut res = src.replace("https://api.github.com/repos/", "[email protected]:");
71+
res.push_str(".git");
72+
res
73+
})
74+
})
75+
.unwrap_or(None)
76+
}
6377
}
6478

6579
pub fn subcommand() -> App<'static, 'static> {
6680
SubCommand::with_name("github")
67-
.version("0.4.0")
81+
.version(env!("CARGO_PKG_VERSION"))
6882
.about("Create a repo on github.")
6983
.arg(
7084
Arg::with_name("name")

src/provider/gitlab.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,18 @@ impl<'a> Provider for GitlabArgs<'a> {
8787
fn auth_header(&self) -> String {
8888
"Private-Token".to_string()
8989
}
90+
91+
fn ssh_url(&self, _: &reqwest::header::HeaderMap) -> Option<String> {
92+
match std::env::var("GITLAB_USERNAME") {
93+
Ok(u) => Some(format!("[email protected]:{}/{}.git", u, self.project_name())),
94+
_ => None,
95+
}
96+
}
9097
}
9198

9299
pub fn subcommand() -> App<'static, 'static> {
93100
SubCommand::with_name("gitlab")
94-
.version("0.4.0")
101+
.version(env!("CARGO_PKG_VERSION"))
95102
.about("Create a repo on gitlab.")
96103
.arg(
97104
Arg::with_name("name")

0 commit comments

Comments
 (0)