-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(preferences): ✨ add support for setting MQTT preferences via the…
… command-line
- Loading branch information
Showing
4 changed files
with
108 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright (c) 2024 Joshua Rich <joshua.rich@gmail.com> | ||
// | ||
// This software is released under the MIT License. | ||
// https://opensource.org/licenses/MIT | ||
|
||
package cli | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"path/filepath" | ||
|
||
"github.com/adrg/xdg" | ||
|
||
"github.com/joshuar/go-hass-agent/internal/preferences" | ||
) | ||
|
||
var ErrMQTTServerRequired = errors.New("mqtt-server not specified") | ||
|
||
type ConfigCmd struct { | ||
Path string `kong:"hidden"` | ||
MQTTConfig `kong:"help='Set MQTT options.'"` | ||
} | ||
|
||
type MQTTConfig preferences.MQTT | ||
|
||
func (r *ConfigCmd) Run(ctx *Context) error { | ||
r.Path = filepath.Join(xdg.ConfigHome, ctx.AppID) | ||
|
||
prefs, err := preferences.Load(r.Path) | ||
if err != nil { | ||
return fmt.Errorf("config: load preferences: %w", err) | ||
} | ||
|
||
r.MQTTEnabled = true | ||
prefs.MQTT = (*preferences.MQTT)(&r.MQTTConfig) | ||
|
||
err = prefs.Save() | ||
if err != nil { | ||
return fmt.Errorf("config: save preferences: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (r *MQTTConfig) Validate() error { | ||
err := validate.Struct(r) | ||
if err != nil { | ||
return fmt.Errorf("%w: %s", ErrValidationFailed, parseValidationErrors(err)) | ||
} | ||
|
||
if r.MQTTServer == "" { | ||
return ErrMQTTServerRequired | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (c) 2024 Joshua Rich <joshua.rich@gmail.com> | ||
// | ||
// This software is released under the MIT License. | ||
// https://opensource.org/licenses/MIT | ||
|
||
package cli | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
|
||
"github.com/go-playground/validator/v10" | ||
) | ||
|
||
var validate *validator.Validate | ||
|
||
var ErrValidationFailed = errors.New("validation failed") | ||
|
||
func init() { | ||
validate = validator.New(validator.WithRequiredStructEnabled()) | ||
} | ||
|
||
//nolint:errorlint | ||
//revive:disable:unhandled-error | ||
func parseValidationErrors(validation error) string { | ||
validationErrs, ok := validation.(validator.ValidationErrors) | ||
if !ok { | ||
return "internal validation error" | ||
} | ||
|
||
var message strings.Builder | ||
|
||
for _, err := range validationErrs { | ||
switch err.Tag() { | ||
case "required": | ||
message.WriteString(err.Field() + " is required") | ||
default: | ||
message.WriteString(err.Field() + " should match " + err.Tag()) | ||
} | ||
|
||
message.WriteRune(' ') | ||
} | ||
|
||
return message.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters