Skip to content

Commit

Permalink
feat(cli): allow for environment variable arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
EdJoPaTo committed Jan 31, 2022
1 parent e846518 commit 5320de3
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ clap_complete = "3"
[dependencies]
anyhow = "1"
chrono = "0.4"
clap = { version = "3", features = ["cargo", "wrap_help"] }
clap = { version = "3", features = ["cargo", "env", "wrap_help"] }
crossterm = "0.22"
json = "0.12"
rand = "0.8"
Expand Down
105 changes: 66 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,96 +53,123 @@ mqttui publish -h "test.mosquitto.org" "topic" "payload"
```

```plaintext
MQTT TUI 0.13.0
MQTT TUI 0.14.0
EdJoPaTo <mqttui-rust@edjopato.de>
Subscribe to a MQTT Topic or publish something quickly from the terminal
USAGE:
mqttui [OPTIONS] [TOPIC] [SUBCOMMAND]
FLAGS:
-h, --help
Prints help information
-V, --version
Prints version information
ARGS:
<TOPIC>
Topic to watch
[env: MQTTUI_TOPIC=]
[default: #]
OPTIONS:
-b, --broker <HOST>
Host on which the MQTT Broker is running [default: localhost]
Host on which the MQTT Broker is running
[env: MQTTUI_BROKER=]
[default: localhost]
-h, --help
Print help information
-p, --port <INT>
Port on which the MQTT Broker is running
[env: MQTTUI_PORT=]
[default: 1883]
--password <STRING>
Password to access the mqtt broker. Passing the password via command line is
insecure as the password can be read from the history!
-p, --port <INT>
Port on which the MQTT Broker is running [default: 1883]
[env: MQTTUI_PASSWORD=]
-u, --username <STRING>
Username to access the mqtt broker. Anonymous access when not supplied.
[env: MQTTUI_USERNAME=]
ARGS:
<TOPIC>
Topic to watch [default: #]
-V, --version
Print version information
SUBCOMMANDS:
help Prints this message or the help of the given subcommand(s)
publish Publish a value quickly
help
Print this message or the help of the given subcommand(s)
publish
Publish a value quickly [aliases: p, pub]
```

```plaintext
mqttui-publish
Publish a value quickly
USAGE:
mqttui publish [FLAGS] [OPTIONS] <TOPIC> <PAYLOAD>
mqttui publish [OPTIONS] <TOPIC> <PAYLOAD>
FLAGS:
-h, --help
Prints help information
ARGS:
<TOPIC>
Topic to publish to
-r, --retain
Publish the MQTT message retained
<PAYLOAD>
Payload to be published
-V, --version
Prints version information
OPTIONS:
-b, --broker <HOST>
Host on which the MQTT Broker is running
-v, --verbose
Show full MQTT communication
[env: MQTTUI_BROKER=]
[default: localhost]
-h, --help
Print help information
OPTIONS:
-b, --broker <HOST>
Host on which the MQTT Broker is running [default: localhost]
-p, --port <INT>
Port on which the MQTT Broker is running
[env: MQTTUI_PORT=]
[default: 1883]
--password <STRING>
Password to access the mqtt broker. Passing the password via command line is
insecure as the password can be read from the history!
-p, --port <INT>
Port on which the MQTT Broker is running [default: 1883]
[env: MQTTUI_PASSWORD=]
-r, --retain
Publish the MQTT message retained
[env: MQTTUI_RETAIN=]
-u, --username <STRING>
Username to access the mqtt broker. Anonymous access when not supplied.
[env: MQTTUI_USERNAME=]
ARGS:
<TOPIC>
Topic to publish to
<PAYLOAD>
Payload to be published
-v, --verbose
Show full MQTT communication
```

Tip: Create an alias for the host you are working on:
Tip: Create an alias for the broker you are working on:
```bash
alias mqttui-home='mqttui --broker pi-home.local'

# Use the alias without having to specify the host every time
# Use the alias without having to specify the broker every time
mqttui-home "topic"
```

You can also create an environment variable for this:
```bash
export MQTTUI_BROKER=pi-home.local

# Use the command without specifying the broker every time
mqttui "topic
```
# Interesting Alternatives
- [thomasnordquist/MQTT-Explorer](https://github.com/thomasnordquist/MQTT-Explorer)
Expand Down
6 changes: 6 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub fn build() -> App<'static> {
Arg::new("retain")
.short('r')
.long("retain")
.env("MQTTUI_RETAIN")
.help("Publish the MQTT message retained"),
)
.arg(
Expand All @@ -38,6 +39,7 @@ pub fn build() -> App<'static> {
Arg::new("Host")
.short('b')
.long("broker")
.env("MQTTUI_BROKER")
.value_name("HOST")
.global(true)
.takes_value(true)
Expand All @@ -48,6 +50,7 @@ pub fn build() -> App<'static> {
Arg::new("Port")
.short('p')
.long("port")
.env("MQTTUI_PORT")
.value_name("INT")
.global(true)
.takes_value(true)
Expand All @@ -58,6 +61,7 @@ pub fn build() -> App<'static> {
Arg::new("Username")
.short('u')
.long("username")
.env("MQTTUI_USERNAME")
.value_name("STRING")
.global(true)
.takes_value(true)
Expand All @@ -70,6 +74,7 @@ pub fn build() -> App<'static> {
.arg(
Arg::new("Password")
.long("password")
.env("MQTTUI_PASSWORD")
.value_name("STRING")
.global(true)
.takes_value(true)
Expand All @@ -82,6 +87,7 @@ pub fn build() -> App<'static> {
.arg(
Arg::new("Topic")
.value_name("TOPIC")
.env("MQTTUI_TOPIC")
.takes_value(true)
.default_value("#")
.help("Topic to watch"),
Expand Down

0 comments on commit 5320de3

Please sign in to comment.