summarylogtreecommitdiffstats
path: root/wrestic-1.6.1-no-self-update.patch
blob: df855653119a207e4a951ff2a52919004a349442 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
From b03eb6191fed8481874fae6731f2a90ec4f84a41 Mon Sep 17 00:00:00 2001
From: tippfehlr <tippfehlr@tippfehlr.eu>
Date: Fri, 19 Jan 2024 18:48:09 +0100
Subject: [PATCH] feat: add feature no-self-update

To disable self-update for package manager installs.

Rustup functions in the same way:
- https://github.com/rust-lang/rustup/blob/9955b9f889889a495b380e73709db5480c6a656b/Cargo.toml#L29
- https://github.com/rust-lang/rustup/blob/9955b9f889889a495b380e73709db5480c6a656b/src/cli/self_update.rs#L96
---
 Cargo.toml            |  3 ++
 src/modules/update.rs | 81 ++++++++++++++++++++++++-------------------
 2 files changed, 48 insertions(+), 36 deletions(-)

diff --git a/Cargo.toml b/Cargo.toml
index 9b22636..61454eb 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -27,3 +27,6 @@ serde_json = "1.0.115"
 sudo = "0.6.0"
 tar = "0.4.40"
 which = "6.0.1"
+
+[features]
+no-self-update = []
diff --git a/src/modules/update.rs b/src/modules/update.rs
index a05366a..52e7b80 100644
--- a/src/modules/update.rs
+++ b/src/modules/update.rs
@@ -121,50 +121,59 @@ fn remove_file_with_permission_check(file_path: &str) -> Result<()> {
 }
 
 pub fn update() -> Result<()> {
-    clear()?;
-    cprintln!("<c,u,s>UPDATER");
-    println!();
-
-    let current_executable = &current_exe()?;
-    let bin_path = current_executable.to_str().unwrap();
-    let tmp_path = "/tmp/wrestic.tar.gz";
-    let url = "https://api.github.com/repos/alvaro17f/wrestic/releases/latest";
-
-    if get_current_version()? >= get_latest_version(url)? {
-        cprintln!("<g,u>Wrestic is already up to date!\n");
-        pause()?
-    } else {
-        cprintln!(
-            "<y>Wrestic is outdated!\n<r>current: <k>{}\n<g>latest: <k>{}\n",
-            get_current_version()?,
-            get_latest_version(url)?
-        );
+    #[cfg(not(feature = "no-self-update"))]
+    const SELF_UPDATE: bool = true;
+    #[cfg(feature = "no-self-update")]
+    const SELF_UPDATE: bool = false;
+
+    if SELF_UPDATE {
+        clear()?;
+        cprintln!("<c,u,s>UPDATER");
+        println!();
+
+        let current_executable = &current_exe()?;
+        let bin_path = current_executable.to_str().unwrap();
+        let tmp_path = "/tmp/wrestic.tar.gz";
+        let url = "https://api.github.com/repos/alvaro17f/wrestic/releases/latest";
+
+        if get_current_version()? >= get_latest_version(url)? {
+            cprintln!("<g,u>Wrestic is already up to date!\n");
+            pause()?
+        } else {
+            cprintln!(
+                "<y>Wrestic is outdated!\n<r>current: <k>{}\n<g>latest: <k>{}\n",
+                get_current_version()?,
+                get_latest_version(url)?
+            );
 
-        let pb = ProgressBar::new_spinner();
-        pb.enable_steady_tick(Duration::from_millis(120));
-        pb.set_message("Updating wrestic...");
+            let pb = ProgressBar::new_spinner();
+            pb.enable_steady_tick(Duration::from_millis(120));
+            pb.set_message("Updating wrestic...");
 
-        let shell = get_current_shell()?;
+            let shell = get_current_shell()?;
 
-        download_latest_version(&shell, url, tmp_path)?;
+            download_latest_version(&shell, url, tmp_path)?;
 
-        pb.finish_and_clear();
+            pb.finish_and_clear();
 
-        remove_file_with_permission_check(bin_path)?;
+            remove_file_with_permission_check(bin_path)?;
 
-        pb.finish_and_clear();
+            pb.finish_and_clear();
 
-        if extract_wrestic(tmp_path, bin_path).is_err() {
-            Err(error!("Failed extracting the latest version of wrestic"))?;
-        }
+            if extract_wrestic(tmp_path, bin_path).is_err() {
+                Err(error!("Failed extracting the latest version of wrestic"))?;
+            }
 
-        pb.finish_and_clear();
+            pb.finish_and_clear();
 
-        cprintln!(
-            "<g,u>Wrestic has been updated to version <k>{}<g,u>!",
-            get_latest_version(url)?
-        );
-    }
+            cprintln!(
+                "<g,u>Wrestic has been updated to version <k>{}<g,u>!",
+                get_latest_version(url)?
+            );
+        }
 
-    Ok(())
+        Ok(())
+    } else {
+        Err(error!("Self-update is disabled for this build. You should probably use your system package manager to update"))
+    }
 }
-- 
2.44.0