diff options
Diffstat (limited to 'src/sub_commands/claim.rs')
| -rw-r--r-- | src/sub_commands/claim.rs | 152 |
1 files changed, 0 insertions, 152 deletions
diff --git a/src/sub_commands/claim.rs b/src/sub_commands/claim.rs deleted file mode 100644 index a95021d..0000000 --- a/src/sub_commands/claim.rs +++ /dev/null | |||
| @@ -1,152 +0,0 @@ | |||
| 1 | use anyhow::{Context, Result}; | ||
| 2 | |||
| 3 | use super::send::send_events; | ||
| 4 | #[cfg(not(test))] | ||
| 5 | use crate::client::Client; | ||
| 6 | #[cfg(test)] | ||
| 7 | use crate::client::MockConnect; | ||
| 8 | use crate::{ | ||
| 9 | cli_interactor::{Interactor, InteractorPrompt, PromptInputParms}, | ||
| 10 | client::Connect, | ||
| 11 | git::{Repo, RepoActions}, | ||
| 12 | login, | ||
| 13 | repo_ref::{extract_pks, get_repo_config_from_yaml, save_repo_config_to_yaml, RepoRef}, | ||
| 14 | Cli, | ||
| 15 | }; | ||
| 16 | |||
| 17 | #[derive(Debug, clap::Args)] | ||
| 18 | pub struct SubCommandArgs { | ||
| 19 | #[clap(short, long)] | ||
| 20 | /// name of repository | ||
| 21 | title: Option<String>, | ||
| 22 | #[clap(short, long)] | ||
| 23 | /// optional description | ||
| 24 | description: Option<String>, | ||
| 25 | #[clap(short, long, value_parser, num_args = 1..)] | ||
| 26 | /// homepage | ||
| 27 | web: Vec<String>, | ||
| 28 | #[clap(short, long, value_parser, num_args = 1..)] | ||
| 29 | /// relays contributors push patches and comments to | ||
| 30 | relays: Vec<String>, | ||
| 31 | } | ||
| 32 | |||
| 33 | pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { | ||
| 34 | let git_repo = Repo::discover().context("cannot find a git repository")?; | ||
| 35 | |||
| 36 | let root_commit = git_repo | ||
| 37 | .get_root_commit() | ||
| 38 | .context("failed to get root commit of the repository")?; | ||
| 39 | |||
| 40 | // TODO: check for empty repo | ||
| 41 | // TODO: check for existing maintaiers file | ||
| 42 | |||
| 43 | let repo_config_result = get_repo_config_from_yaml(&git_repo); | ||
| 44 | // TODO: check for other claims | ||
| 45 | |||
| 46 | let name = match &args.title { | ||
| 47 | Some(t) => t.clone(), | ||
| 48 | None => Interactor::default().input(PromptInputParms::default().with_prompt("name"))?, | ||
| 49 | }; | ||
| 50 | |||
| 51 | let description = match &args.description { | ||
| 52 | Some(t) => t.clone(), | ||
| 53 | None => Interactor::default() | ||
| 54 | .input(PromptInputParms::default().with_prompt("description (Optional)"))?, | ||
| 55 | }; | ||
| 56 | |||
| 57 | let git_server = git_repo | ||
| 58 | .get_origin_url() | ||
| 59 | .context( | ||
| 60 | "to claim the repository it must be available on a publically accessable git server", | ||
| 61 | ) | ||
| 62 | .context("no git remote origin configured")?; | ||
| 63 | |||
| 64 | let web: Vec<String> = if args.web.is_empty() { | ||
| 65 | Interactor::default() | ||
| 66 | .input(PromptInputParms::default().with_prompt("description (Optional)"))? | ||
| 67 | .split(' ') | ||
| 68 | .map(std::string::ToString::to_string) | ||
| 69 | .collect() | ||
| 70 | } else { | ||
| 71 | args.web.clone() | ||
| 72 | }; | ||
| 73 | #[cfg(not(test))] | ||
| 74 | let mut client = Client::default(); | ||
| 75 | #[cfg(test)] | ||
| 76 | let mut client = <MockConnect as std::default::Default>::default(); | ||
| 77 | |||
| 78 | let (keys, user_ref) = login::launch(&cli_args.nsec, &cli_args.password, Some(&client)).await?; | ||
| 79 | |||
| 80 | client.set_keys(&keys).await; | ||
| 81 | |||
| 82 | let mut maintainers = vec![keys.public_key()]; | ||
| 83 | |||
| 84 | let mut repo_relays: Vec<String> = if !args.relays.is_empty() { | ||
| 85 | args.relays.clone() | ||
| 86 | } else if let Ok(config) = &repo_config_result { | ||
| 87 | config.relays.clone() | ||
| 88 | } else { | ||
| 89 | // TODO: choice input defaulting to user relay list filtered by non paid relays | ||
| 90 | // TODO: allow manual input for more relays | ||
| 91 | // TODO: reccommend some free relays | ||
| 92 | user_ref.relays.write() | ||
| 93 | }; | ||
| 94 | |||
| 95 | // TODO: add blaster for just repo event and don't add it to repo relays | ||
| 96 | // TODO: fix the tests to support blaster | ||
| 97 | if std::env::var("NGITTEST").is_err() | ||
| 98 | && !repo_relays | ||
| 99 | .iter() | ||
| 100 | .any(|s| s.contains("nostr.mutinywallet.com")) | ||
| 101 | { | ||
| 102 | repo_relays.push("wss://nostr.mutinywallet.com".to_string()); | ||
| 103 | } | ||
| 104 | |||
| 105 | if let Ok(config) = &repo_config_result { | ||
| 106 | maintainers = extract_pks(config.maintainers.clone())?; | ||
| 107 | } | ||
| 108 | |||
| 109 | // if yaml file doesnt exist or needs updating | ||
| 110 | if match &repo_config_result { | ||
| 111 | Ok(config) => { | ||
| 112 | !(extract_pks(config.maintainers.clone())?.eq(&maintainers) | ||
| 113 | && config.relays.eq(&repo_relays)) | ||
| 114 | } | ||
| 115 | Err(_) => true, | ||
| 116 | } { | ||
| 117 | save_repo_config_to_yaml(&git_repo, maintainers.clone(), repo_relays.clone())?; | ||
| 118 | println!( | ||
| 119 | "maintainers.yaml {}. commit and push.", | ||
| 120 | if repo_config_result.is_err() { | ||
| 121 | "created" | ||
| 122 | } else { | ||
| 123 | "updated" | ||
| 124 | } | ||
| 125 | ); | ||
| 126 | } | ||
| 127 | |||
| 128 | println!("publishing repostory reference..."); | ||
| 129 | |||
| 130 | let repo_event = RepoRef { | ||
| 131 | identifier: root_commit.to_string()[..7].to_string(), | ||
| 132 | name, | ||
| 133 | description, | ||
| 134 | root_commit: root_commit.to_string(), | ||
| 135 | git_server, | ||
| 136 | web, | ||
| 137 | relays: repo_relays.clone(), | ||
| 138 | maintainers, | ||
| 139 | } | ||
| 140 | .to_event(&keys)?; | ||
| 141 | |||
| 142 | send_events( | ||
| 143 | &client, | ||
| 144 | vec![repo_event], | ||
| 145 | user_ref.relays.write(), | ||
| 146 | repo_relays, | ||
| 147 | !cli_args.disable_cli_spinners, | ||
| 148 | ) | ||
| 149 | .await?; | ||
| 150 | |||
| 151 | Ok(()) | ||
| 152 | } | ||