Skip to content

Commit f9b0212

Browse files
committed
Add prefixes to available environment variables
The old environment variables are still supported, but deprecated.
1 parent 84a1fc5 commit f9b0212

File tree

2 files changed

+71
-13
lines changed

2 files changed

+71
-13
lines changed

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
cmake_minimum_required(VERSION 3.0)
22
project(linuxdeploy-plugin-appimage)
33

4-
set(CMAKE_CXX_STANDARD 11)
4+
set(CMAKE_CXX_STANDARD 17)
55
set(CMAKE_CXX_STANDARD_REQUIRED ON)
66

77
# can be enabled globally, we do not expose any libraries

src/main.cpp

+70-12
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
// system includes
2+
#include <cassert>
23
#include <cstring>
3-
#include <iostream>
44
#include <dirent.h>
5+
#include <functional>
6+
#include <iostream>
57
#include <libgen.h>
8+
#include <optional>
9+
#include <string>
610
#include <unistd.h>
711
#include <vector>
812

@@ -50,6 +54,41 @@ std::string findAppimagetool() {
5054
return "";
5155
}
5256

57+
/**
58+
* Fetch variable from environment by name(s).
59+
* The first name is treated as the currently accepted one, whereas the others are still supported but will trigger a
60+
* warning.
61+
* @param names One or more names
62+
* @return value if available, std::nullopt otherwise
63+
*/
64+
std::optional<std::string> getEnvVar(std::initializer_list<std::string> names) {
65+
assert(names.size() >= 1);
66+
67+
for (auto it = names.begin(); it != names.end(); ++it) {
68+
const auto* value = getenv(it->c_str());
69+
70+
if (value == nullptr) {
71+
continue;
72+
}
73+
74+
if (it != names.begin()) {
75+
std::cerr << "Warning: please use $" << *names.begin() << " instead of $" << *it << std::endl;
76+
}
77+
78+
return value;
79+
}
80+
81+
return std::nullopt;
82+
}
83+
84+
void doSomethingWithEnvVar(std::initializer_list<std::string> names, const std::function<void(const std::string&)>& todo) {
85+
const auto value = getEnvVar(names);
86+
87+
if (value.has_value()) {
88+
todo(value.value());
89+
}
90+
}
91+
5392
int main(const int argc, const char* const* const argv) {
5493
args::ArgumentParser parser("linuxdeploy-plugin-appimage");
5594

@@ -127,23 +166,42 @@ int main(const int argc, const char* const* const argv) {
127166
}
128167
}
129168

130-
if (getenv("VERBOSE") != nullptr) {
131-
args.push_back(strdup("-v"));
132-
}
169+
constexpr auto appimagetool_verbose_arg = "-v";
133170

134-
if (getenv("OUTPUT") != nullptr) {
171+
doSomethingWithEnvVar({"LDAI_VERBOSE", "VERBOSE"}, [&](const auto& value) {
172+
(void) value;
173+
args.push_back(strdup(appimagetool_verbose_arg));
174+
});
175+
176+
// the only exception to prefixing is $DEBUG, which is used across multiple linuxdeploy plugins
177+
doSomethingWithEnvVar({"DEBUG"}, [&](const auto& value) {
178+
(void) value;
179+
180+
// avoid duplicates
181+
if (std::find(args.begin(), args.end(), appimagetool_verbose_arg) == args.end()) {
182+
args.push_back(strdup(appimagetool_verbose_arg));
183+
}
184+
});
185+
186+
doSomethingWithEnvVar({"LDAI_OUTPUT", "OUTPUT"}, [&](const auto& value) {
187+
(void) value;
135188
args.push_back(strdup(getenv("OUTPUT")));
136-
}
189+
});
137190

138-
if (getenv("NO_APPSTREAM") != nullptr) {
191+
doSomethingWithEnvVar({"LDAI_NO_APPSTREAM", "NO_APPSTREAM"}, [&](const auto& value) {
192+
(void) value;
139193
args.push_back(strdup("--no-appstream"));
140-
}
194+
});
141195

142-
auto comp = getenv("APPIMAGE_COMP");
143-
if (comp != nullptr) {
196+
doSomethingWithEnvVar({"LDAI_COMP", "APPIMAGE_COMP"}, [&](const auto& value) {
144197
args.push_back(strdup("--comp"));
145-
args.push_back(strdup(comp));
146-
}
198+
args.push_back(strdup(value.c_str()));
199+
});
200+
201+
// $VERSION is already picked up by appimagetool
202+
doSomethingWithEnvVar({"LDAI_VERSION"}, [&](const auto& value) {
203+
setenv("VERSION", value.c_str(), true);
204+
});
147205

148206
args.push_back(nullptr);
149207

0 commit comments

Comments
 (0)