Skip to content

Commit

Permalink
move global prompts to struct method
Browse files Browse the repository at this point in the history
  • Loading branch information
brchri committed Jan 12, 2024
1 parent e2eae1a commit 773931d
Showing 1 changed file with 29 additions and 33 deletions.
62 changes: 29 additions & 33 deletions cmd/app/console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,54 +93,58 @@ func RunWizard() {
}

func runGlobalPrompts() interface{} {
type Global struct {
TrackerMqttSettings struct {
Connection struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
ClientId string `yaml:"client_id,omitempty"`
User string `yaml:"user"`
Pass string `yaml:"pass"`
UseTls bool `yaml:"use_tls,omitempty"`
SkipTlsVerify bool `yaml:"skip_tls_verify"`
} `yaml:"connection"`
} `yaml:"tracker_mqtt_settings"`
Cooldown int `yaml:"cooldown,omitempty"`
}
asciiArt.NewFigure("Global Config", "", false).Print()

response := promptUser(
global := Global{}

global.TrackerMqttSettings.Connection.Host = promptUser(
question{
prompt: "\nWhat is the DNS or IP of your tracker's MQTT broker (e.g. teslamate)? Note - if running in a container, you should not use localhost or 127.0.0.1",
validResponseRegex: ".+",
},
)
tracker_connection := map[string]interface{}{
"host": response,
}
response = promptUser(
response := promptUser(
question{
prompt: "What is the port of your tracker's MQTT broker (e.g. teslamate)? [1883]",
validResponseRegex: "^\\d{1,5}$",
invalidResponseMessage: "Please enter a number between 1 and 65534",
defaultResponse: "1883",
},
)
port, _ := strconv.ParseFloat(response, 64)
tracker_connection["port"] = port
response = promptUser(
port, _ := strconv.Atoi(response)
global.TrackerMqttSettings.Connection.Port = port
global.TrackerMqttSettings.Connection.ClientId = promptUser(
question{
prompt: "Please enter the MQTT client ID to connect to the broker (can be left blank to auto generate at runtime): []",
validResponseRegex: ".*",
},
)
if response != "" {
tracker_connection["client_id"] = response
}
response = promptUser(
global.TrackerMqttSettings.Connection.User = promptUser(
question{
prompt: "If your MQTT broker requires authentication, please enter the username: []",
validResponseRegex: ".*",
},
)
if response != "" {
tracker_connection["user"] = response
}
response = promptUser(
global.TrackerMqttSettings.Connection.Pass = promptUser(
question{
prompt: "If your MQTT broker requires authentication, please enter the password (your typing will not be masked!); you can also enter this later: []",
validResponseRegex: ".*",
},
)
if response != "" {
tracker_connection["pass"] = response
}
response = promptUser(
question{
prompt: "Does your broker require TLS? [y|N]",
Expand All @@ -149,8 +153,8 @@ func runGlobalPrompts() interface{} {
defaultResponse: "n",
},
)
if match, _ := regexp.MatchString("y|Y", response); match {
tracker_connection["use_tls"] = true
if isYesRegex.MatchString(response) {
global.TrackerMqttSettings.Connection.UseTls = true
response = promptUser(
question{
prompt: "Do you want to skip TLS verification (useful for self-signed certificates)? [y|N]",
Expand All @@ -159,19 +163,11 @@ func runGlobalPrompts() interface{} {
defaultResponse: "n",
},
)
if match, _ := regexp.MatchString("y|Y", response); match {
tracker_connection["skip_tls_verify"] = true
if isYesRegex.MatchString(response) {
global.TrackerMqttSettings.Connection.SkipTlsVerify = true
}
}

tracker_mqtt_settings := map[string]interface{}{
"connection": tracker_connection,
}

global_config := map[string]interface{}{
"tracker_mqtt_settings": tracker_mqtt_settings,
}

response = promptUser(
question{
prompt: "Set the number (in minutes) that there should be a global cooldown for each door. This prevents any door from being operated for a set time after any previous operation. []",
Expand All @@ -181,9 +177,9 @@ func runGlobalPrompts() interface{} {
)
if len(response) > 0 {
cooldown, _ := strconv.Atoi(response)
global_config["cooldown"] = cooldown
global.Cooldown = cooldown
}
return global_config
return global
}

func runGarageDoorsPrompts() []interface{} {
Expand Down

0 comments on commit 773931d

Please sign in to comment.