Skip to content

Commit

Permalink
refactor: support git url for add (#54)
Browse files Browse the repository at this point in the history
* gobal agent

* downloader

* fix

* fix

* fix

* support url

* move subdir to args

* fix
  • Loading branch information
shixinhuang99 committed Jan 21, 2024
1 parent 0fd29a7 commit 003f99d
Show file tree
Hide file tree
Showing 9 changed files with 455 additions and 397 deletions.
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Commands:
list List all templates
remove Remove specified templates
mv Rename a template
add Add templates from GitHub repository
add Add template from GitHub repository
create Copy the template folder to the specified directory
token Configure or display your GitHub personal access token
help Print this message or the help of the given subcommand(s)
Expand All @@ -48,6 +48,31 @@ Options:

## Example

### Basic

```bash
sca add user/repo

# GitHub url
sca add https://github.com/user/repo.git

# subdir
sca add user/repo --subdir /path/to/dir

# branch
sca add user/repo --branch branch

# tag
sca add user/repo --tag tag

# commit
sca add user/repo --commit e763a43519ea4c209df2452c6e2a5b7dffdfdd3d
```

```bash
sca create repo
```

### Proxy support

```bash
Expand Down
92 changes: 90 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub enum Command {
/// Rename a template
Mv(MvArgs),

/// Add templates from GitHub repository
/// Add template from GitHub repository
Add(AddArgs),

/// Copy the template folder to the specified directory
Expand Down Expand Up @@ -72,8 +72,9 @@ pub struct MvArgs {
}

#[derive(Args, Debug)]
#[cfg_attr(test, derive(Clone))]
pub struct AddArgs {
/// owner/name/.../subdir?(branch|tag|commit)=...
/// owner/name/.../subdir
pub repository: String,

/// The depth to go when recursing repository
Expand All @@ -90,6 +91,22 @@ pub struct AddArgs {
/// subdir will be used as the name
#[arg(long)]
pub name: Option<String>,

/// Specify sub directory
#[arg(long)]
pub subdir: Option<String>,

/// Specify branch
#[arg(long)]
pub branch: Option<String>,

/// Specify tag
#[arg(long)]
pub tag: Option<String>,

/// Specify commit
#[arg(long)]
pub commit: Option<String>,
}

#[derive(Args, Debug)]
Expand Down Expand Up @@ -126,6 +143,77 @@ pub struct UninstallArgs {
pub keep_data: bool,
}

#[cfg(test)]
pub mod test_utils {
use super::AddArgs;

pub struct AddArgsMock {
args: AddArgs,
}

impl AddArgsMock {
pub fn new() -> Self {
Self {
args: AddArgs {
repository: "".to_string(),
depth: 0,
name: None,
subdir: None,
branch: None,
tag: None,
commit: None,
},
}
}

pub fn build(&self) -> AddArgs {
self.args.clone()
}

pub fn repository(&mut self, repository: &str) -> &mut Self {
self.args.repository = repository.to_string();

self
}

pub fn depth(&mut self, depth: u8) -> &mut Self {
self.args.depth = depth;

self
}

pub fn name(&mut self, name: &str) -> &mut Self {
self.args.name = Some(name.to_string());

self
}

pub fn subdir(&mut self, subdir: &str) -> &mut Self {
self.args.subdir = Some(subdir.to_string());

self
}

pub fn branch(&mut self, branch: &str) -> &mut Self {
self.args.branch = Some(branch.to_string());

self
}

pub fn tag(&mut self, tag: &str) -> &mut Self {
self.args.tag = Some(tag.to_string());

self
}

pub fn commit(&mut self, commit: &str) -> &mut Self {
self.args.commit = Some(commit.to_string());

self
}
}
}

#[cfg(test)]
mod tests {
use clap::CommandFactory;
Expand Down
13 changes: 0 additions & 13 deletions src/github_api/gql.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
use serde::{de::DeserializeOwned, Deserialize, Serialize};

use crate::debug;

#[derive(Serialize)]
pub struct GraphQLQuery {
pub query: &'static str,
pub variables: String,
}

impl GraphQLQuery {
pub fn new(query: &'static str, variables: String) -> Self {
debug!("GraphQL variables json: {}", variables);

Self {
query,
variables,
}
}
}

#[derive(Deserialize, Serialize, Debug)]
pub struct GraphQLResponse<T>
where
Expand Down

0 comments on commit 003f99d

Please sign in to comment.