• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Blightmud / Blightmud / 18243380491

04 Oct 2025 10:52AM UTC coverage: 73.597% (+0.08%) from 73.514%
18243380491

push

github

web-flow
Fixes a number of clippy warnings (#1275)

8 of 11 new or added lines in 7 files covered. (72.73%)

1 existing line in 1 file now uncovered.

6609 of 8980 relevant lines covered (73.6%)

339.97 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

3.38
/src/lua/plugin/functions.rs
1
use std::{fs, path::PathBuf, sync::mpsc::Sender};
2

3
use anyhow::{bail, Result};
4
use git2::{
5
    build::{CloneLocal, RepoBuilder},
6
    Repository,
7
};
8

9
use crate::event::Event;
10

11
pub fn get_plugin_dir() -> PathBuf {
2✔
12
    let plugin_dir = crate::DATA_DIR.join("plugins");
2✔
13
    fs::create_dir_all(&plugin_dir).ok();
2✔
14
    plugin_dir
2✔
15
}
2✔
16

17
pub fn add_plugin(main_writer: Sender<Event>, url: &str, with_submodules: bool) {
×
18
    let url = url.to_string();
×
19
    std::thread::spawn(move || {
×
NEW
20
        if let Some(name) = url.split('/').next_back() {
×
21
            let dest = get_plugin_dir().join(name);
×
22
            let mut rbuilder = RepoBuilder::new();
×
23
            rbuilder.clone_local(CloneLocal::Auto);
×
24
            main_writer
×
25
                .send(Event::Info(format!(
×
26
                    "Downloading plugin: {name} from {url}"
×
27
                )))
×
28
                .unwrap();
×
29
            if let Err(err) = rbuilder.clone(&url, &dest) {
×
30
                match err.code() {
×
31
                    git2::ErrorCode::Exists => main_writer
×
32
                        .send(Event::Error("Plugin already exists".to_string()))
×
33
                        .unwrap(),
×
34
                    _ => main_writer.send(Event::Error(err.to_string())).unwrap(),
×
35
                }
36
            } else {
37
                main_writer
×
38
                    .send(Event::Info(format!("Downloaded plugin: {name}")))
×
39
                    .unwrap();
×
40
                if with_submodules {
×
41
                    main_writer
×
42
                        .send(Event::Info(format!("Getting the submodules for {name}.")))
×
43
                        .unwrap();
×
44
                    if let Ok(repo) = Repository::discover(&dest) {
×
45
                        match update_submodules(repo, true) {
×
46
                            Ok(()) => main_writer
×
47
                                .send(Event::Info("Plugin retrieval succeeded.".to_string()))
×
48
                                .unwrap(),
×
49
                            Err(e) => main_writer
×
50
                                .send(Event::Error(format!(
×
51
                                    "problems updating the submodules for this plugin: {e}"
×
52
                                )))
×
53
                                .unwrap(),
×
54
                        }
55
                    } else {
×
56
                        main_writer
×
57
                            .send(Event::Error("Problem opening repository.".to_string()))
×
58
                            .unwrap();
×
59
                    }
×
60
                }
×
61
            }
62
        } else {
×
63
            main_writer
×
64
                .send(Event::Error(format!("Invalid plugin repository: {url}")))
×
65
                .unwrap();
×
66
        }
×
67
    });
×
68
}
×
69

70
pub fn update_plugin(main_writer: Sender<Event>, name: &str) {
×
71
    let name = name.to_string();
×
72
    std::thread::spawn(move || {
×
73
        let updater = || -> Result<()> {
×
74
            let path = get_plugin_dir().join(&name);
×
75
            if path.is_dir() {
×
76
                let repo = Repository::discover(path)?;
×
77
                let mut origin_remote = repo.find_remote("origin")?;
×
78
                origin_remote.fetch(&["master", "main"], None, None)?;
×
79

80
                let oid = if let Ok(oid) = repo.refname_to_id("refs/remotes/origin/master") {
×
81
                    oid
×
82
                } else {
83
                    repo.refname_to_id("refs/remotes/origin/main")?
×
84
                };
85

86
                let object = repo.find_object(oid, None)?;
×
87
                repo.reset(&object, git2::ResetType::Hard, None)?;
×
88
                Ok(())
×
89
            } else {
90
                bail!("Invalid plugin name");
×
91
            }
92
        };
×
93
        main_writer
×
94
            .send(Event::Info(format!("Updating plugin: {}", &name)))
×
95
            .unwrap();
×
96
        if let Err(err) = updater() {
×
97
            main_writer
×
98
                .send(Event::Error(format!("Plugin update failed: {err}",)))
×
99
                .unwrap();
×
100
        } else {
×
101
            main_writer
×
102
                .send(Event::Info(format!("Updated plugin: {}", &name)))
×
103
                .unwrap();
×
104
            // Given that the reset hard cleaned out the repo, we need to remake it.
105
            if let Ok(repo) = Repository::discover(get_plugin_dir().join(&name)) {
×
106
                // Now we need to account for the submodules
107
                main_writer
×
108
                    .send(Event::Info("Updating the plugin's submodules.".to_string()))
×
109
                    .unwrap();
×
110
                match update_submodules(repo, false) {
×
111
                    Ok(()) => main_writer
×
112
                        .send(Event::Info(
×
113
                            "The update of the submodules was successful.".to_string(),
×
114
                        ))
×
115
                        .unwrap(),
×
116
                    Err(e) => main_writer
×
117
                        .send(Event::Error(format!("Error updating submodules; {e}")))
×
118
                        .unwrap(),
×
119
                }
120
            } else {
×
121
                main_writer
×
122
                    .send(Event::Error("Failed to open the repository.".to_string()))
×
123
                    .unwrap();
×
124
            }
×
125
        }
126
    });
×
127
}
×
128

129
pub fn load_plugin(name: &str, writer: &Sender<Event>) -> Result<()> {
×
130
    let path = get_plugin_dir().join(name).join("main.lua");
×
131
    if !path.exists() {
×
132
        bail!("Plugin '{}' doesn't contain a 'main.lua' file", name);
×
133
    } else if let Some(path_name) = path.to_str() {
×
134
        writer
×
135
            .send(Event::LoadScript(path_name.to_string()))
×
136
            .unwrap();
×
137
    } else {
×
138
        bail!("Invalid plugin path to main.lua");
×
139
    }
140
    Ok(())
×
141
}
×
142

143
pub fn remove_plugin(name: &str) -> Result<()> {
×
144
    if !name.contains("..") {
×
145
        let path = get_plugin_dir().join(name);
×
146
        if path.exists() {
×
147
            fs::remove_dir_all(path).expect("Plugin doesn't exist");
×
148
        }
×
149
        Ok(())
×
150
    } else {
151
        bail!("Invalid plugin name");
×
152
    }
153
}
×
154

155
pub fn get_plugins() -> Vec<String> {
×
156
    let mut plugins = vec![];
×
157
    if let Ok(paths) = fs::read_dir(get_plugin_dir()) {
×
158
        for path in paths.flatten() {
×
159
            if path.path().is_dir() {
×
160
                if let Some(name) = path.file_name().to_str() {
×
161
                    plugins.push(name.to_string());
×
162
                }
×
163
            }
×
164
        }
165
    }
×
166
    plugins
×
167
}
×
168
fn update_submodules(repository: Repository, should_initialize: bool) -> Result<()> {
×
169
    match repository.submodules() {
×
170
        Ok(sms) => {
×
171
            for mut sm in sms {
×
172
                if let Err(e) = sm.update(should_initialize, None) {
×
173
                    match e.message() {
×
174
                        "submodule is not initialized" => {
×
175
                            continue;
×
176
                        }
177
                        _ => {
178
                            bail!("Problem updating the submodule: {}.", e);
×
179
                        }
180
                    }
181
                }
×
182
            }
183
        }
184
        Err(e) => {
×
185
            bail!("Error getting access to the submodules: {}", e);
×
186
        }
187
    }
188
    Ok(())
×
189
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc