Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'config print'. #727

Merged
merged 4 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 50 additions & 4 deletions src/cli/cmds/config.toit
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ create_config_commands config/Config cache/Cache ui/Ui -> List:
--short_help="Configure Artemis tool."

show_cmd := cli.Command "show"
--short_help="Show the current configuration."
--short_help="Prints the current configuration."
--run=:: show_config config ui

config_cmd.add show_cmd

(create_server_config_commands config ui).do: config_cmd.add it
Expand Down Expand Up @@ -61,7 +60,6 @@ create_server_config_commands config/Config ui/Ui -> List:
--default=true
--short_help="Set the broker as the default broker.",
]

config_broker_cmd.add add_cmd

add_cmd.add
Expand Down Expand Up @@ -120,7 +118,55 @@ create_server_config_commands config/Config ui/Ui -> List:
return [config_broker_cmd]

show_config config/Config ui/Ui:
throw "UNIMPLEMENTED"
default_device := config.get CONFIG_DEVICE_DEFAULT_KEY
default_broker := config.get CONFIG_BROKER_DEFAULT_KEY
default_org := config.get CONFIG_ORGANIZATION_DEFAULT_KEY
servers := config.get CONFIG_SERVERS_KEY
auths := config.get CONFIG_SERVER_AUTHS_KEY

json_output := :
result := {
"path" : config.path
}

if default_device: result["default-device"] = default_device
if default_broker: result["default-broker"] = default_broker
if default_org: result["default-org"] = default_org
if servers: result["servers"] = servers
if auths:
// Store the auths with the servers.
result_servers := result.get "servers" --init=: {:}
auths.do: | server_name auth |
server := result_servers.get server_name --init=: {:}
if auth is Map:
["access_token", "refresh_token"].do: | token_name |
if auth.contains token_name and auth[token_name].size > 35:
auth[token_name] = auth[token_name][0..30] + "..."
server["auth"] = auth
result

human_output := : | printer/Printer |
result := {
"Configuration file" : config.path
}
if default_device: result["Default device"] = default_device
if default_broker: result["Default broker"] = default_broker
if default_org: result["Default organization"] = default_org
if servers:
// TODO(florian): make the servers nicer.
result["Servers"] = servers
if auths:
// Store the auths with the servers.
result_servers := result.get "Servers" --init=: {:}
auths.do: | server_name auth |
server := result_servers.get server_name --init=: {:}
server["auth"] = auth
printer.emit result

ui.do --kind=Ui.RESULT: | printer/Printer |
printer.emit_structured
--json=json_output
--stdout=human_output

default_server parsed/cli.Parsed config/Config ui/Ui:
config_key := parsed["artemis"] ? CONFIG_ARTEMIS_DEFAULT_KEY : CONFIG_BROKER_DEFAULT_KEY
Expand Down
3 changes: 3 additions & 0 deletions src/cli/config.toit
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ import fs
import .utils

APP_NAME ::= "artemis"

// When adding a new config don't forget to update the 'config show' command.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// When adding a new config don't forget to update the 'config show' command.
// When adding a new config don't forget to update the 'config print' command.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

going with 'show'.

CONFIG_DEVICE_DEFAULT_KEY ::= "device.default"
CONFIG_BROKER_DEFAULT_KEY ::= "server.broker.default"
CONFIG_ARTEMIS_DEFAULT_KEY ::= "server.artemis.default"
CONFIG_SERVERS_KEY ::= "servers"
CONFIG_SERVER_AUTHS_KEY ::= "auths"
CONFIG_ORGANIZATION_DEFAULT_KEY ::= "organization.default"
// When adding a new config don't forget to update the 'config show' command.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// When adding a new config don't forget to update the 'config show' command.
// When adding a new config don't forget to update the 'config print' command.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

going with 'show'.


class ConfigLocalStorage implements supabase.LocalStorage:
config_/Config
Expand Down
69 changes: 69 additions & 0 deletions tests/cmd_config_test.toit
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright (C) 2022 Toitware ApS.

import artemis.shared.server_config show ServerConfigHttp
import expect show *
import .utils

main args:
with_fleet --args=args --count=1: | test_cli/TestCli fake_devices/List fleet_dir/string |
run_test test_cli fake_devices fleet_dir

run_test test_cli/TestCli fake_devices/List fleet_dir/string:
json_config := test_cli.run --json [
"config", "show"
]
expect_equals "$test_cli.tmp_dir/config" json_config["path"]
expect_equals "test-broker" json_config["default-broker"]
servers := json_config["servers"]
expect (servers.contains "test-broker")
expect (servers.contains "test-artemis-server")

broker_config := servers["test-broker"]
artemis_config := servers["test-artemis-server"]
expect_equals "toit-http" broker_config["type"]
expect_equals "toit-http" artemis_config["type"]

artemis_server_config := test_cli.artemis.server_config as ServerConfigHttp
broker_server_config := test_cli.broker.server_config as ServerConfigHttp

expect_equals artemis_server_config.host broker_server_config.host
test_cli.replacements[artemis_server_config.host] = "<HOST>"
expect_equals broker_server_config.host broker_config["host"]
expect_equals artemis_server_config.host artemis_config["host"]
expect_equals broker_server_config.port broker_config["port"]
test_cli.replacements["$broker_server_config.port"] = "<B-PORT>"
expect_equals artemis_server_config.port artemis_config["port"]
test_cli.replacements["$artemis_server_config.port"] = "<A-PORT>"

test_cli.replacements["$artemis_config["auth"]"] = "<ARTEMIS_AUTH>"

test_cli.run_gold "BAA-config-show"
"Print the test config"
[
"config", "show"
]

fake_device := fake_devices[0] as FakeDevice

test_cli.run [
"--fleet-root", fleet_dir,
"device", "default", "$fake_device.alias_id",
]

test_cli.run [
"--fleet-root", fleet_dir,
"org", "default", "$fake_device.organization_id",
]

json_config = test_cli.run --json [
"--fleet-root", fleet_dir,
"config", "show"
]
expect_equals "$fake_device.alias_id" json_config["default-device"]
expect_equals "$fake_device.organization_id" json_config["default-org"]

test_cli.run_gold "BBA-config-show-default-values-set"
"Print the test config with default values set"
[
"config", "show"
]
26 changes: 26 additions & 0 deletions tests/gold/cmd_config_test/BAA-config-show.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Print the test config
# [config, show]
Configuration file: TMP_DIR/config
Default broker: test-broker
Servers:
test-artemis-server:
type: toit-http
host: <HOST>
path: /
poll_interval: 500000
port: <A-PORT>
device_headers:
X-Artemis-Header: true
admin_headers:
X-Artemis-Header: true
auth: <ARTEMIS_AUTH>
test-broker:
type: toit-http
host: <HOST>
path: /
poll_interval: 500000
port: <B-PORT>
device_headers:
X-Artemis-Header: true
admin_headers:
X-Artemis-Header: true
28 changes: 28 additions & 0 deletions tests/gold/cmd_config_test/BBA-config-show-default-values-set.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Print the test config with default values set
# [config, show]
Configuration file: TMP_DIR/config
Default device: -={| UUID-FOR-FAKE-DEVICE 00000 |}=-
Default broker: test-broker
Default organization: 4b6d9e35-cae9-44c0-8da0-6b0e485987e2
Servers:
test-artemis-server:
type: toit-http
host: <HOST>
path: /
poll_interval: 500000
port: <A-PORT>
device_headers:
X-Artemis-Header: true
admin_headers:
X-Artemis-Header: true
auth: <ARTEMIS_AUTH>
test-broker:
type: toit-http
host: <HOST>
path: /
poll_interval: 500000
port: <B-PORT>
device_headers:
X-Artemis-Header: true
admin_headers:
X-Artemis-Header: true