Skip to content

Commit

Permalink
feat: add --workspace option, add convenience binaries
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisGorbachev committed Jul 17, 2024
1 parent c8f5e61 commit fa94373
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 86 deletions.
60 changes: 60 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ readme = "README.md"
keywords = ["github", "utils"]
categories = ["command-line-utilities", "development-tools"]

[package.metadata.navigator]
tagline = "my-awesome-android-app"
summary = ""
announcement = ""

[dependencies]
anyhow = "1.0.86"
clap = { version = "4.3.24", features = ["derive"] }
fs-err = "2.11.0"
derive_setters = "0.1.6"
83 changes: 0 additions & 83 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,83 +0,0 @@
<!-- DO NOT EDIT -->
<!-- This file is automatically generated by README.ts. -->
<!-- Edit README.ts if you want to make changes. -->

# Create Rust GitHub repo

[![Build](https://github.com/DenisGorbachev/create-rust-github-repo/actions/workflows/ci.yml/badge.svg)](https://github.com/DenisGorbachev/create-rust-github-repo)
[![Documentation](https://docs.rs/create-rust-github-repo/badge.svg)](https://docs.rs/create-rust-github-repo)

## Overview

`create-rust-github-repo` is a CLI program that creates a new repository on GitHub, clones it locally, initializes a Rust project, copies the configs from a pre-existing directory.

## Examples

```shell
# Create a GitHub repo & init a Rust project
create-rust-github-repo --name my-new-project

# Copy configs from existing project
create-rust-github-repo --name my-new-project --copy-configs-from ~/workspace/my-existing-project

# Clone to a specific directory
create-rust-github-repo --name my-new-project --dir ~/workspace/my-new-project

# Create a public repo
create-rust-github-repo --name my-new-project --public

# Create a lib instead of bin
create-rust-github-repo --name my-new-project --cargo-init-args '--lib'
```

## Features

* Uses existing `gh`, `git`, `cargo` commands
* Forwards the flags to commands
* Can be used as a library

## Installation

```shell
cargo install create-rust-github-repo
```

## Usage

```
Usage: create-rust-github-repo [OPTIONS] --name <NAME>
Options:
-n, --name <NAME>
Repository name
-d, --dir <DIR>
Target directory for cloning the repository (must include the repo name) (defaults to "{current_dir}/{repo_name}")
-v, --visibility <VISIBILITY>
Repository visibility [default: private] [possible values: public, private, internal]
-c, --copy-configs-from <COPY_CONFIGS_FROM>
Source directory for configuration files
--git-commit-message <GIT_COMMIT_MESSAGE>
Message for git commit [default: "Add configs"]
--extra-configs <EXTRA_CONFIGS>
Extra config file paths (relative to `source` directory)
--gh-repo-create-args <GH_REPO_CREATE_ARGS>
Forwarded arguments for `gh repo create`
--gh-repo-clone-args <GH_REPO_CLONE_ARGS>
Forwarded arguments for `gh repo clone`
--cargo-init-args <CARGO_INIT_ARGS>
Forwarded arguments for `cargo init`
--cargo-build-args <CARGO_BUILD_ARGS>
Forwarded arguments for `cargo build`
--git-commit-args <GIT_COMMIT_ARGS>
Forwarded arguments for `git commit`
--git-push-args <GIT_PUSH_ARGS>
Forwarded arguments for `git push`
-h, --help
Print help
```

## License

[Apache License 2.0](LICENSE-APACHE) or [MIT License](LICENSE-MIT) at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
9 changes: 9 additions & 0 deletions src/bin/create-rust-github-private-bin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use clap::Parser;
use create_rust_github_repo::{CreateRustGithubRepo, RepoVisibility};

fn main() -> anyhow::Result<()> {
CreateRustGithubRepo::parse()
.visibility(RepoVisibility::Private)
.cargo_init_args(["--bin".to_string()])
.run()
}
9 changes: 9 additions & 0 deletions src/bin/create-rust-github-private-lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use clap::Parser;
use create_rust_github_repo::{CreateRustGithubRepo, RepoVisibility};

fn main() -> anyhow::Result<()> {
CreateRustGithubRepo::parse()
.visibility(RepoVisibility::Private)
.cargo_init_args(["--lib".to_string()])
.run()
}
9 changes: 9 additions & 0 deletions src/bin/create-rust-github-public-bin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use clap::Parser;
use create_rust_github_repo::{CreateRustGithubRepo, RepoVisibility};

fn main() -> anyhow::Result<()> {
CreateRustGithubRepo::parse()
.visibility(RepoVisibility::Public)
.cargo_init_args(["--bin".to_string()])
.run()
}
9 changes: 9 additions & 0 deletions src/bin/create-rust-github-public-lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use clap::Parser;
use create_rust_github_repo::{CreateRustGithubRepo, RepoVisibility};

fn main() -> anyhow::Result<()> {
CreateRustGithubRepo::parse()
.visibility(RepoVisibility::Public)
.cargo_init_args(["--lib".to_string()])
.run()
}
14 changes: 11 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::process::{Command, ExitStatus};

use anyhow::Context;
use clap::{value_parser, Parser, ValueEnum};
use derive_setters::Setters;

#[derive(ValueEnum, Default, Eq, PartialEq, Hash, Clone, Copy, Debug)]
pub enum RepoVisibility {
Expand All @@ -25,14 +26,18 @@ impl RepoVisibility {
}
}

#[derive(Parser, Debug)]
#[derive(Parser, Setters, Debug)]
#[setters(into)]
pub struct CreateRustGithubRepo {
#[arg(long, short = 'n', help = "Repository name")]
name: String,

#[arg(long, short, help = "Target directory for cloning the repository (must include the repo name) (defaults to \"{current_dir}/{repo_name}\")", value_parser = value_parser!(PathBuf))]
#[arg(long, short, help = "Target directory for cloning the repository (must include the repo name) (defaults to \"{current_dir}/{repo_name}\") (see also: --workspace)", value_parser = value_parser!(PathBuf))]
dir: Option<PathBuf>,

#[arg(long, short, help = "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", value_parser = value_parser!(PathBuf))]
workspace: Option<PathBuf>,

#[arg(long, short = 'v', help = "Repository visibility", value_enum, default_value_t)]
visibility: RepoVisibility,

Expand Down Expand Up @@ -67,7 +72,10 @@ pub struct CreateRustGithubRepo {
impl CreateRustGithubRepo {
pub fn run(self) -> anyhow::Result<()> {
let current_dir = current_dir()?;
let dir = self.dir.unwrap_or(current_dir.join(&self.name));
let dir = self
.dir
.or_else(|| self.workspace.map(|workspace| workspace.join(&self.name)))
.unwrap_or(current_dir.join(&self.name));

// Create a GitHub repo
exec(
Expand Down

0 comments on commit fa94373

Please sign in to comment.