Skip to content

Commit

Permalink
cli/repo: Plug in repo update
Browse files Browse the repository at this point in the history
Signed-off-by: Ikey Doherty <[email protected]>
  • Loading branch information
ikeycode committed Nov 5, 2023
1 parent 30ff4b5 commit 2dacfc4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
25 changes: 25 additions & 0 deletions moss/src/cli/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ enum Action<'a> {
Add(&'a Path, String, Url, String, Priority),
// Root, Id
Remove(&'a Path, String),
// Root, Id
Update(&'a Path, Option<String>),
}

/// Return a command for handling `repo` subcommands
Expand Down Expand Up @@ -60,6 +62,12 @@ pub fn command() -> Command {
.about("Remove a repository for the system")
.arg(arg!(<NAME> "repo name").value_parser(clap::value_parser!(String))),
)
.subcommand(
Command::new("update")
.about("Update the system repositories")
.long_about("If no repository is named, update them all")
.arg(arg!([NAME] "repo name").value_parser(clap::value_parser!(String))),
)
}

/// Handle subcommands to `repo`
Expand All @@ -76,6 +84,9 @@ pub async fn handle(args: &ArgMatches, root: &Path) -> Result<(), Error> {
Some(("remove", cmd_args)) => {
Action::Remove(root, cmd_args.get_one::<String>("NAME").cloned().unwrap())
}
Some(("update", cmd_args)) => {
Action::Update(root, cmd_args.get_one::<String>("NAME").cloned())
}
_ => unreachable!(),
};

Expand All @@ -86,6 +97,7 @@ pub async fn handle(args: &ArgMatches, root: &Path) -> Result<(), Error> {
add(root, name, uri, comment, priority).await
}
Action::Remove(_, _) => unimplemented!(),
Action::Update(root, name) => update(root, name).await,
}
}

Expand Down Expand Up @@ -137,6 +149,19 @@ async fn list(root: &Path) -> Result<(), Error> {
Ok(())
}

/// Update specific repos or all
async fn update(root: &Path, which: Option<String>) -> Result<(), Error> {
let installation = Installation::open(root);
let mut manager = repository::Manager::new(installation).await?;

match which {
Some(repo) => manager.refresh(&repository::Id::new(repo)).await?,
None => manager.refresh_all().await?,
}

Ok(())
}

#[derive(Debug, Error)]
pub enum Error {
#[error("repo manager")]
Expand Down
11 changes: 11 additions & 0 deletions moss/src/repository/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ impl Manager {
Ok(())
}

/// Refresh a [`Repository`] by Id
pub async fn refresh(&mut self, id: &repository::Id) -> Result<(), Error> {
if let Some(repo) = self.repositories.get(id) {
refresh_index(id, repo, &self.installation).await
} else {
Err(Error::UnknownRepo(id.clone()))
}
}

/// Returns the active repositories held by this manager
pub(crate) fn active(&self) -> impl Iterator<Item = repository::Active> + '_ {
self.repositories.values().cloned()
Expand Down Expand Up @@ -208,6 +217,8 @@ pub enum Error {
Database(#[from] meta::Error),
#[error("save config")]
SaveConfig(#[source] config::SaveError),
#[error("unknown repo")]
UnknownRepo(repository::Id),
}

impl From<package::MissingMetaError> for Error {
Expand Down

0 comments on commit 2dacfc4

Please sign in to comment.