feat: rename sensor name
This commit is contained in:
parent
14ce354b32
commit
450dfc9052
39
cmd/sensor/rename.go
Normal file
39
cmd/sensor/rename.go
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
package sensor
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"git.cryptic.systems/fh-trier/go-flucky/pkg/config"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
var renameSensorCmd = &cobra.Command{
|
||||||
|
Use: "rename",
|
||||||
|
Short: "Rename Sensor",
|
||||||
|
Args: cobra.ExactArgs(2),
|
||||||
|
Example: fmt.Sprintf("flucky sensor rename indoor outdoor\nflucky sensor rename f98b00ea-a9b2-4e00-924f-113859d0af2d outdoor"),
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
// read configuration
|
||||||
|
fc, err := config.Read(cfg)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// rename sensor
|
||||||
|
err = fc.RenameSensor(args[0], args[1])
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// save new configuration
|
||||||
|
err = config.Write(fc, cfg)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
sensorCmd.AddCommand(renameSensorCmd)
|
||||||
|
}
|
@ -351,6 +351,18 @@ func (fc *FluckyConfig) RemoveRemote(nameOrUUID string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RenameSensor renames a sensor identified by the name or the UUID
|
||||||
|
func (fc *FluckyConfig) RenameSensor(oldName, newName string) error {
|
||||||
|
for _, sensor := range fc.Sensors {
|
||||||
|
if sensor.SensorName == oldName ||
|
||||||
|
sensor.SensorID == oldName {
|
||||||
|
sensor.SensorName = newName
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return fmt.Errorf("Could not find sensor %v and replace the name", oldName)
|
||||||
|
}
|
||||||
|
|
||||||
// ToJSON returns the struct as JSON string
|
// ToJSON returns the struct as JSON string
|
||||||
func (fc *FluckyConfig) ToJSON() (string, error) {
|
func (fc *FluckyConfig) ToJSON() (string, error) {
|
||||||
var b bytes.Buffer
|
var b bytes.Buffer
|
||||||
|
Loading…
Reference in New Issue
Block a user