Skip to content

Commit d81a9d2

Browse files
feat!: forward full commands to shell
1 parent db8fddd commit d81a9d2

File tree

6 files changed

+68
-75
lines changed

6 files changed

+68
-75
lines changed

README.md

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -56,37 +56,36 @@ Options:
5656
Target directory for cloning the repository (must include the repo name) (defaults to "{current_dir}/{repo_name}") (see also: --workspace)
5757
-w, --workspace <WORKSPACE>
5858
Parent of the target directory for cloning the repository (must NOT include the repo name). If this option is specified, then the repo is cloned to "{workspace}/{repo_name}". The --dir option overrides this option
59-
--shell <SHELL>
59+
--shell-cmd <SHELL_CMD>
6060
Shell to use for executing commands [default: /bin/sh]
61-
-v, --visibility <VISIBILITY>
62-
Repository visibility [default: private] [possible values: public, private, internal]
6361
-c, --copy-configs-from <COPY_CONFIGS_FROM>
6462
Source directory for configuration files
65-
--git-commit-message <GIT_COMMIT_MESSAGE>
66-
Message for git commit [default: "Add configs"]
6763
--extra-configs <EXTRA_CONFIGS>
6864
Extra config file paths (relative to `source` directory)
6965
--repo-exists-cmd <REPO_EXISTS_CMD>
70-
Shell command to check if repo exists (supports substitutions - see help below) [default: "gh repo view --json nameWithOwner \"{{name}}\" | grep \"{{name}}\""]
71-
--gh-repo-create-args <GH_REPO_CREATE_ARGS>
72-
Forwarded arguments for `gh repo create`
73-
--gh-repo-clone-args <GH_REPO_CLONE_ARGS>
74-
Forwarded arguments for `gh repo clone`
75-
--cargo-init-args <CARGO_INIT_ARGS>
76-
Forwarded arguments for `cargo init`
77-
--cargo-build-args <CARGO_BUILD_ARGS>
78-
Forwarded arguments for `cargo build`
79-
--git-commit-args <GIT_COMMIT_ARGS>
80-
Forwarded arguments for `git commit`
81-
--git-push-args <GIT_PUSH_ARGS>
82-
Forwarded arguments for `git push`
66+
Shell command to check if repo exists (supports substitutions - see help below) [default: "gh repo view --json nameWithOwner {{name}} | grep {{name}}"]
67+
--repo-create-cmd <REPO_CREATE_CMD>
68+
Shell command to create a repo (supports substitutions - see help below) [default: "gh repo create --private {{name}}"]
69+
--repo-clone-cmd <REPO_CLONE_CMD>
70+
Shell command to clone a repo (supports substitutions - see help below) [default: "gh repo clone {{name}} {{dir}}"]
71+
--project-init-cmd <PROJECT_INIT_CMD>
72+
Shell command to initialize a project (supports substitutions - see help below) [default: "cargo init"]
73+
--project-test-cmd <PROJECT_TEST_CMD>
74+
Shell command to test a project (supports substitutions - see help below) [default: "cargo test"]
75+
--repo-add-args <REPO_ADD_ARGS>
76+
Shell command to add new files (supports substitutions - see help below) [default: "git add ."]
77+
--repo-commit-args <REPO_COMMIT_ARGS>
78+
Shell command to make a commit (supports substitutions - see help below) [default: "git commit -m \"Add configs\""]
79+
--repo-push-args <REPO_PUSH_ARGS>
80+
Shell command to push the commit (supports substitutions - see help below) [default: "git push"]
8381
-h, --help
8482
Print help
8583
-V, --version
8684
Print version
8785
8886
All command arg options support the following substitutions:
8987
* {{name}} - substituted with --name arg
88+
* {{dir}} - substituted with resolved directory for repo (the resolved value of --dir)
9089
```
9190

9291
## Additional binaries
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use clap::Parser;
2-
use create_rust_github_repo::{CreateRustGithubRepo, RepoVisibility};
2+
3+
use create_rust_github_repo::CreateRustGithubRepo;
34

45
fn main() -> anyhow::Result<()> {
56
CreateRustGithubRepo::parse()
6-
.visibility(RepoVisibility::Private)
7-
.cargo_init_args(["--bin".to_string()])
7+
.project_init_cmd("gh repo create --private {{name}}")
8+
.project_init_cmd("cargo init --bin")
89
.run()
910
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use clap::Parser;
2-
use create_rust_github_repo::{CreateRustGithubRepo, RepoVisibility};
2+
3+
use create_rust_github_repo::CreateRustGithubRepo;
34

45
fn main() -> anyhow::Result<()> {
56
CreateRustGithubRepo::parse()
6-
.visibility(RepoVisibility::Private)
7-
.cargo_init_args(["--lib".to_string()])
7+
.project_init_cmd("gh repo create --private {{name}}")
8+
.project_init_cmd("cargo init --lib")
89
.run()
910
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use clap::Parser;
2-
use create_rust_github_repo::{CreateRustGithubRepo, RepoVisibility};
2+
3+
use create_rust_github_repo::CreateRustGithubRepo;
34

45
fn main() -> anyhow::Result<()> {
56
CreateRustGithubRepo::parse()
6-
.visibility(RepoVisibility::Public)
7-
.cargo_init_args(["--bin".to_string()])
7+
.project_init_cmd("gh repo create --public {{name}}")
8+
.project_init_cmd("cargo init --bin")
89
.run()
910
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use clap::Parser;
2-
use create_rust_github_repo::{CreateRustGithubRepo, RepoVisibility};
2+
3+
use create_rust_github_repo::CreateRustGithubRepo;
34

45
fn main() -> anyhow::Result<()> {
56
CreateRustGithubRepo::parse()
6-
.visibility(RepoVisibility::Public)
7-
.cargo_init_args(["--lib".to_string()])
7+
.project_init_cmd("gh repo create --public {{name}}")
8+
.project_init_cmd("cargo init --lib")
89
.run()
910
}

src/lib.rs

Lines changed: 35 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl RepoVisibility {
2828
}
2929

3030
#[derive(Parser, Setters, Debug)]
31-
#[command(version, about, author, after_help = "All command arg options support the following substitutions:\n* {{name}} - substituted with --name arg\n")]
31+
#[command(version, about, author, after_help = "All command arg options support the following substitutions:\n* {{name}} - substituted with --name arg\n* {{dir}} - substituted with resolved directory for repo (the resolved value of --dir)\n")]
3232
#[setters(into)]
3333
pub struct CreateRustGithubRepo {
3434
#[arg(long, short = 'n', help = "Repository name")]
@@ -41,40 +41,37 @@ pub struct CreateRustGithubRepo {
4141
workspace: Option<PathBuf>,
4242

4343
#[arg(long, help = "Shell to use for executing commands", default_value = "/bin/sh")]
44-
shell: String,
45-
46-
#[arg(long, short = 'v', help = "Repository visibility", value_enum, default_value_t)]
47-
visibility: RepoVisibility,
44+
shell_cmd: String,
4845

4946
#[arg(long, short, help = "Source directory for configuration files", value_parser = value_parser!(PathBuf))]
5047
copy_configs_from: Option<PathBuf>,
5148

52-
#[arg(long, help = "Message for git commit", default_value = "Add configs")]
53-
git_commit_message: String,
54-
5549
#[arg(long, help = "Extra config file paths (relative to `source` directory)", value_delimiter = ',')]
5650
extra_configs: Vec<String>,
5751

58-
#[arg(long, help = "Shell command to check if repo exists (supports substitutions - see help below)", default_value = "gh repo view --json nameWithOwner \"{{name}}\" | grep \"{{name}}\"")]
52+
#[arg(long, help = "Shell command to check if repo exists (supports substitutions - see help below)", default_value = "gh repo view --json nameWithOwner {{name}} | grep {{name}}")]
5953
repo_exists_cmd: String,
6054

61-
#[arg(long, help = "Forwarded arguments for `gh repo create`", value_delimiter = ' ')]
62-
gh_repo_create_args: Vec<String>,
55+
#[arg(long, help = "Shell command to create a repo (supports substitutions - see help below)", default_value = "gh repo create --private {{name}}")]
56+
repo_create_cmd: String,
57+
58+
#[arg(long, help = "Shell command to clone a repo (supports substitutions - see help below)", default_value = "gh repo clone {{name}} {{dir}}")]
59+
repo_clone_cmd: String,
6360

64-
#[arg(long, help = "Forwarded arguments for `gh repo clone`", value_delimiter = ' ')]
65-
gh_repo_clone_args: Vec<String>,
61+
#[arg(long, help = "Shell command to initialize a project (supports substitutions - see help below)", default_value = "cargo init")]
62+
project_init_cmd: String,
6663

67-
#[arg(long, help = "Forwarded arguments for `cargo init`", value_delimiter = ' ')]
68-
cargo_init_args: Vec<String>,
64+
#[arg(long, help = "Shell command to test a project (supports substitutions - see help below)", default_value = "cargo test")]
65+
project_test_cmd: String,
6966

70-
#[arg(long, help = "Forwarded arguments for `cargo build`", value_delimiter = ' ')]
71-
cargo_build_args: Vec<String>,
67+
#[arg(long, help = "Shell command to add new files (supports substitutions - see help below)", default_value = "git add .")]
68+
repo_add_args: String,
7269

73-
#[arg(long, help = "Forwarded arguments for `git commit`", value_delimiter = ' ')]
74-
git_commit_args: Vec<String>,
70+
#[arg(long, help = "Shell command to make a commit (supports substitutions - see help below)", default_value = "git commit -m \"Add configs\"")]
71+
repo_commit_args: String,
7572

76-
#[arg(long, help = "Forwarded arguments for `git push`", value_delimiter = ' ')]
77-
git_push_args: Vec<String>,
73+
#[arg(long, help = "Shell command to push the commit (supports substitutions - see help below)", default_value = "git push")]
74+
repo_push_args: String,
7875
}
7976

8077
impl CreateRustGithubRepo {
@@ -84,31 +81,23 @@ impl CreateRustGithubRepo {
8481
.dir
8582
.or_else(|| self.workspace.map(|workspace| workspace.join(&self.name)))
8683
.unwrap_or(current_dir.join(&self.name));
84+
let dir_string = dir.display().to_string();
8785

88-
let substitutions = HashMap::<&'static str, &str>::from([("{{name}}", self.name.as_str())]);
86+
let substitutions = HashMap::<&'static str, &str>::from([
87+
("{{name}}", self.name.as_str()),
88+
("{{dir}}", dir_string.as_str()),
89+
]);
8990

90-
let repo_exists = success(&self.shell, ["-c"], [self.repo_exists_cmd], &current_dir, &substitutions)?;
91+
let repo_exists = success(&self.shell_cmd, ["-c"], [self.repo_exists_cmd], &current_dir, &substitutions)?;
9192

9293
if !repo_exists {
9394
// Create a GitHub repo
94-
exec(
95-
"gh",
96-
[
97-
"repo",
98-
"create",
99-
&self.name,
100-
self.visibility.to_gh_create_repo_flag(),
101-
],
102-
self.gh_repo_create_args,
103-
&current_dir,
104-
&substitutions,
105-
)
106-
.context("Failed to create GitHub repository")?;
95+
exec(&self.shell_cmd, ["-c"], [self.repo_create_cmd], &current_dir, &substitutions).context("Failed to create repository")?;
10796
}
10897

10998
if !dir.exists() {
11099
// Clone the repo
111-
exec("gh", ["repo", "clone", &self.name, dir.to_str().unwrap()], self.gh_repo_clone_args.into_iter(), &current_dir, &substitutions).context("Failed to clone repository")?;
100+
exec(&self.shell_cmd, ["-c"], [self.repo_clone_cmd], &current_dir, &substitutions).context("Failed to clone repository")?;
112101
} else {
113102
println!("Directory \"{}\" exists, skipping clone command", dir.display())
114103
}
@@ -117,7 +106,7 @@ impl CreateRustGithubRepo {
117106

118107
if !cargo_toml.exists() {
119108
// Run cargo init
120-
exec("cargo", ["init"], self.cargo_init_args.into_iter(), &dir, &substitutions).context("Failed to initialize Cargo project")?;
109+
exec(&self.shell_cmd, ["-c"], [self.project_init_cmd], &dir, &substitutions).context("Failed to initialize the project")?;
121110
} else {
122111
println!("Cargo.toml exists in \"{}\", skipping `cargo init` command", dir.display())
123112
}
@@ -130,16 +119,17 @@ impl CreateRustGithubRepo {
130119
copy_configs_if_not_exists(&copy_configs_from, &dir, configs).context("Failed to copy configuration files")?;
131120
}
132121

133-
// Run cargo build
134-
exec("cargo", ["build"], self.cargo_build_args.into_iter(), &dir, &substitutions).context("Failed to build Cargo project")?;
122+
// test
123+
exec(&self.shell_cmd, ["-c"], [self.project_test_cmd], &dir, &substitutions).context("Failed to test the project")?;
135124

136-
// Git commit
137-
exec("git", ["add", "."], Vec::<String>::new().into_iter(), &dir, &substitutions).context("Failed to stage files for commit")?;
125+
// add
126+
exec(&self.shell_cmd, ["-c"], [self.repo_add_args], &dir, &substitutions).context("Failed to add files for commit")?;
138127

139-
exec("git", ["commit", "-m", &self.git_commit_message], self.git_commit_args.into_iter(), &dir, &substitutions).context("Failed to commit changes")?;
128+
// commit
129+
exec(&self.shell_cmd, ["-c"], [self.repo_commit_args], &dir, &substitutions).context("Failed to commit changes")?;
140130

141-
// Git push
142-
exec("git", ["push"], self.git_push_args.into_iter(), &dir, &substitutions).context("Failed to push changes")?;
131+
// push
132+
exec(&self.shell_cmd, ["-c"], [self.repo_push_args], &dir, &substitutions).context("Failed to push changes")?;
143133

144134
Ok(())
145135
}

0 commit comments

Comments
 (0)