Skip to content

Commit

Permalink
feat: Add ghr open command
Browse files Browse the repository at this point in the history
  • Loading branch information
siketyan committed Nov 22, 2022
1 parent 3d51e90 commit 2b7e229
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Commands:
clone Clones a Git repository to local
delete Deletes a repository from local
init Initialises a Git repository in local
open Opens a repository in an application
path Prints the path to root, owner, or a repository
profile Manages profiles to use in repositories
shell Writes a shell script to extend ghr features
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::url::Url;

#[derive(Debug, Parser)]
pub struct Cmd {
/// URL or pattern of the repository to clone.
/// URL or pattern of the repository to delete.
repo: String,
}

Expand Down
4 changes: 4 additions & 0 deletions src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod cd;
mod clone;
mod delete;
mod init;
mod open;
mod path;
mod profile;
mod shell;
Expand All @@ -19,6 +20,8 @@ pub enum Action {
Delete(delete::Cmd),
/// Initialises a Git repository in local.
Init(init::Cmd),
/// Opens a repository in an application.
Open(open::Cmd),
/// Prints the path to root, owner, or a repository.
Path(path::Cmd),
/// Manages profiles to use in repositories.
Expand All @@ -41,6 +44,7 @@ impl Cli {
Clone(cmd) => cmd.run().await,
Delete(cmd) => cmd.run().await,
Init(cmd) => cmd.run(),
Open(cmd) => cmd.run(),
Path(cmd) => cmd.run(),
Profile(cmd) => cmd.run(),
Shell(cmd) => cmd.run(),
Expand Down
33 changes: 33 additions & 0 deletions src/cmd/open.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use std::path::PathBuf;
use std::str::FromStr;

use anyhow::Result;
use clap::Parser;

use crate::config::Config;
use crate::path::Path;
use crate::root::Root;
use crate::url::Url;

#[derive(Debug, Parser)]
pub struct Cmd {
/// URL or pattern of the repository to open application in.
repo: String,

/// Name of the application entry.
application: String,
}

impl Cmd {
pub fn run(self) -> Result<()> {
let root = Root::find()?;
let config = Config::load_from(&root)?;

let url = Url::from_str(&self.repo)?;
let path = PathBuf::from(Path::resolve(&root, &url));

config.applications.open(&self.application, path)?;

Ok(())
}
}

0 comments on commit 2b7e229

Please sign in to comment.