Skip to content

Commit

Permalink
Configuration file path through program options
Browse files Browse the repository at this point in the history
  • Loading branch information
bautisflow committed Mar 25, 2024
1 parent a8bf470 commit cfffbc1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 32 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Added

- Support for installing and demonizing client in procd - openwrt systems
- Configuration file path can now be set through program options

## [1.2.2] - 2023-10-03

Expand Down
33 changes: 28 additions & 5 deletions install/install_thinger_iotmp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,30 @@ if [ -n "${SSL_CERT_DIR+x}" ]; then
fi

if [ -z "${version+x}" ]; then
version="`wget --quiet -qO- --header="Accept: application/vnd.github.v3+json" https://"$_github_api_url"/repos/thinger-io/"$_repo"/releases/latest | grep "tag_name" | cut -d '"' -f4`"
if type curl > /dev/null; then
version=$(curl -s -L -H "Accept: application/vnd.github.v3+json" "https://$_github_api_url/repos/thinger-io/$_repo/releases/latest" | grep -o '"tag_name": "[^"]*' | cut -d'"' -f4)
else
# Check GNU wget is installed
ls -lah $(which wget) | grep -E 'busybox|uclient' > /dev/null 2>&1
if [ "$?" -eq 0 ]; then
echo "[ERROR] Failed installation due to missing GNU wget. Please install GNU wget or curl and relaunch the script"
exit 2
fi
version="`wget --quiet -qO- --header="Accept: application/vnd.github.v3+json" https://"$_github_api_url"/repos/thinger-io/"$_repo"/releases/latest | grep "tag_name" | cut -d '"' -f4`"
fi
fi

if [ "$INIT" == "procd" ]; then
if [ -f "$service_dir"/"$_module" ]; then
service "$_module" stop
service "$_module" disable
fi
wget -q --header="Accept: application/vnd.github.VERSION.raw" https://"$_github_api_url"/repos/thinger-io/"$_repo"/contents/install/procd/"$_module"?ref="$version" -P "$service_dir" -O "$service_dir"/"$_module"
if type curl > /dev/null; then
curl -s -L -H "Accept: application/vnd.github.VERSION.raw" "https://$_github_api_url/repos/thinger-io/$_repo/contents/install/procd/$_module?ref=$version" -o "$service_dir/$_module"
else
wget -q --header="Accept: application/vnd.github.VERSION.raw" https://"$_github_api_url"/repos/thinger-io/"$_repo"/contents/install/procd/"$_module"?ref="$version" -P "$service_dir" -O "$service_dir"/"$_module"
fi
chmod +x "$service_dir/$_module"
elif [ "$INIT" == "systemd" ]; then
# Download service file -> Before downloading binary
if [ -f "$service_dir"/"$_module".service ]; then
Expand All @@ -177,10 +192,18 @@ elif [ "$INIT" == "systemd" ]; then
fi

# Download bin
version_release_body=`wget --quiet -qO- --header="Accept: application/vnd.github.v3+json" https://"$_github_api_url"/repos/thinger-io/"$_repo"/releases/tags/"$version"`
if type curl > /dev/null; then
version_release_body=$(curl -s -H "Accept: application/vnd.github.v3+json" "https://$_github_api_url/repos/thinger-io/$_repo/releases/tags/$version")
else
version_release_body=`wget --quiet -qO- --header="Accept: application/vnd.github.v3+json" https://"$_github_api_url"/repos/thinger-io/"$_repo"/releases/tags/"$version"`
fi
download_url=`echo "$version_release_body" | grep "url.*$_arch" | cut -d '"' -f4`

wget -q --header="Accept: application/octec-stream" "$download_url" -O "$bin_dir/$_module"
if type curl > /dev/null; then
curl -s -L -H "Accept: application/octet-stream" "$download_url" -o "$bin_dir/$_module"
else
wget -q --header="Accept: application/octec-stream" "$download_url" -O "$bin_dir/$_module"
fi
chmod +x "$bin_dir"/"$_module"

# Provision iotmp client config
Expand All @@ -194,7 +217,7 @@ if [ ! -f "$config_filename" ]; then
echo "Using default seed"
KEY_HEX='4eee5a11576b5c532f068f71ab177d07'
else
KEY_HEX=$(echo -n "$seed" | xxd -p)
KEY_HEX=$(echo -n "$seed" | hexdump -ve '/1 "%02x"')
fi

MAC=`ip link show $(ip route show default | awk '/default/ {print $5}') | awk '/link\/ether/ {print $2}' | sed 's/://g' | tr '[:lower:]' '[:upper:]'`
Expand Down
43 changes: 16 additions & 27 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,20 @@ int main(int argc, char *argv[])
std::string credentials;
std::string hostname;
std::string transport;
std::string config_path;

namespace po = boost::program_options;

po::options_description desc("options_description [options]");
desc.add_options()
("help", "show this help")
("verbosity,v",
po::value<int>(&verbosity_level)->default_value(DEFAULT_VERBOSITY_LEVEL),
"set verbosity level")
("username,u",
po::value<std::string>(&username)->default_value(DEFAULT_USERNAME),
"username")
("device,d",
po::value<std::string>(&device)->default_value(DEFAULT_DEVICE),
"device identifier")
("password,p",
po::value<std::string>(&credentials)->default_value(DEFAULT_CREDENTIAL),
"device credential")
("host,h",
po::value<std::string>(&hostname)->default_value(DEFAULT_HOSTNAME),
"target hostname")
("transport,t",
po::value<std::string>(&transport)->default_value(DEFAULT_TRANSPORT),
"connection transport, i.e., 'websocket'");
("verbosity,v", po::value<int>(&verbosity_level)->default_value(DEFAULT_VERBOSITY_LEVEL), "set verbosity level")
("username,u", po::value<std::string>(&username)->default_value(DEFAULT_USERNAME), "username")
("device,d", po::value<std::string>(&device)->default_value(DEFAULT_DEVICE), "device identifier")
("password,p", po::value<std::string>(&credentials)->default_value(DEFAULT_CREDENTIAL), "device credential")
("host,h", po::value<std::string>(&hostname)->default_value(DEFAULT_HOSTNAME), "target hostname")
("transport,t", po::value<std::string>(&transport)->default_value(DEFAULT_TRANSPORT), "connection transport, i.e., 'websocket'")
("config,c", po::value<std::string>(&config_path)->default_value(std::filesystem::path{home} / ".thinger" / "iotmp.cfg"), "location of credentials");

// initialize default values and description
po::parse_command_line(argc, argv, desc);
Expand All @@ -82,17 +72,16 @@ int main(int argc, char *argv[])

// load them also from config file in $HOME/.thinger/iotmp.cfg
try {
const char* home = getenv("HOME");
if(home != nullptr){
std::filesystem::path config_file = std::filesystem::path{home} / ".thinger" / "iotmp.cfg";
if(exists(config_file)){
po::store(po::parse_config_file<char>(config_file.string().c_str(), desc), vm);
}
}else{
LOG_WARNING("cannot read HOME environment variable");
if (!config_path.empty()) {
auto config_file = std::filesystem::path{config_path};

if (exists(config_file)) {
po::store(po::parse_config_file<char>(config_file.string().c_str(), desc), vm);
po::notify(vm);
}
}
} catch (const po::reading_file& e) {
LOG_ERROR("error while loading config file: %s", e.what());
LOG_ERROR("error while loading config file: %s", e.what());
}

po::notify(vm);
Expand Down

0 comments on commit cfffbc1

Please sign in to comment.