From eeba1af2e49d9c0e68c084290f097c0379604d78 Mon Sep 17 00:00:00 2001 From: Christian Fritz Date: Thu, 17 Aug 2023 22:33:29 +0200 Subject: [PATCH] Replaces use of deprecated functions with their successors. --- cmd/completion.go | 2 +- pkg/knx/config.go | 146 +++++++++++++++++++++++----------------------- 2 files changed, 74 insertions(+), 74 deletions(-) diff --git a/cmd/completion.go b/cmd/completion.go index 2777709..cfc1d80 100644 --- a/cmd/completion.go +++ b/cmd/completion.go @@ -66,7 +66,7 @@ PowerShell: `, DisableFlagsInUseLine: true, ValidArgs: []string{"bash", "zsh", "fish", "powershell", "ps1"}, - Args: cobra.ExactValidArgs(1), + Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs), Run: func(cmd *cobra.Command, args []string) { switch args[0] { case "bash": diff --git a/pkg/knx/config.go b/pkg/knx/config.go index e70d3df..695819b 100644 --- a/pkg/knx/config.go +++ b/pkg/knx/config.go @@ -15,60 +15,60 @@ package knx import ( - "encoding/json" - "fmt" - "io/ioutil" - "strings" - "time" + "encoding/json" + "fmt" + "os" + "strings" + "time" - "github.com/ghodss/yaml" + "github.com/ghodss/yaml" ) // Config defines the structure of the configuration file which defines which // KNX Group Addresses were mapped into prometheus metrics. type Config struct { - Connection Connection `json:",omitempty"` - // MetricsPrefix is a short prefix which will be added in front of the actual metric name. - MetricsPrefix string - AddressConfigs GroupAddressConfigSet + Connection Connection `json:",omitempty"` + // MetricsPrefix is a short prefix which will be added in front of the actual metric name. + MetricsPrefix string + AddressConfigs GroupAddressConfigSet } // ReadConfig reads the given configuration file and returns the parsed Config object. func ReadConfig(configFile string) (*Config, error) { - content, err := ioutil.ReadFile(configFile) - if err != nil { - return nil, fmt.Errorf("can not read group address configuration: %s", err) - } - config := Config{} - err = yaml.Unmarshal(content, &config) - if err != nil { - return nil, fmt.Errorf("can not read config file %s: %s", configFile, err) - } - return &config, nil + content, err := os.ReadFile(configFile) + if err != nil { + return nil, fmt.Errorf("can not read group address configuration: %s", err) + } + config := Config{} + err = yaml.Unmarshal(content, &config) + if err != nil { + return nil, fmt.Errorf("can not read config file %s: %s", configFile, err) + } + return &config, nil } // NameForGa returns the full metric name for the given GroupAddress. func (c *Config) NameForGa(address GroupAddress) string { - gaConfig, ok := c.AddressConfigs[address] - if !ok { - return "" - } - return c.NameFor(gaConfig) + gaConfig, ok := c.AddressConfigs[address] + if !ok { + return "" + } + return c.NameFor(gaConfig) } // NameFor return s the full metric name for the given GroupAddressConfig. func (c *Config) NameFor(gaConfig GroupAddressConfig) string { - return c.MetricsPrefix + gaConfig.Name + return c.MetricsPrefix + gaConfig.Name } // Connection contains the information about how to connect to the KNX system and how to identify itself. type Connection struct { - // Type of the actual connection. Can be either Tunnel or Router - Type ConnectionType - // Endpoint defines the IP address or hostname and port to where it should connect. - Endpoint string - // PhysicalAddress defines how the knx-exporter should identify itself within the KNX system. - PhysicalAddress PhysicalAddress + // Type of the actual connection. Can be either Tunnel or Router + Type ConnectionType + // Endpoint defines the IP address or hostname and port to where it should connect. + Endpoint string + // PhysicalAddress defines how the knx-exporter should identify itself within the KNX system. + PhysicalAddress PhysicalAddress } type ConnectionType string @@ -77,62 +77,62 @@ const Tunnel = ConnectionType("Tunnel") const Router = ConnectionType("Router") func (t ConnectionType) MarshalJSON() ([]byte, error) { - return json.Marshal(string(t)) + return json.Marshal(string(t)) } func (t *ConnectionType) UnmarshalJSON(data []byte) error { - var str string - if err := json.Unmarshal(data, &str); err != nil { - return err - } - switch strings.ToLower(str) { - case "tunnel": - *t = Tunnel - case "router": - *t = Router - default: - return fmt.Errorf("invalid connection type given: \"%s\"", str) - } - return nil + var str string + if err := json.Unmarshal(data, &str); err != nil { + return err + } + switch strings.ToLower(str) { + case "tunnel": + *t = Tunnel + case "router": + *t = Router + default: + return fmt.Errorf("invalid connection type given: \"%s\"", str) + } + return nil } type Duration time.Duration func (d Duration) MarshalJSON() ([]byte, error) { - return json.Marshal(time.Duration(d).String()) + return json.Marshal(time.Duration(d).String()) } func (d *Duration) UnmarshalJSON(data []byte) error { - var str string - if err := json.Unmarshal(data, &str); err != nil { - return err - } - duration, err := time.ParseDuration(str) - if err != nil { - return err - } - *d = Duration(duration) - return nil + var str string + if err := json.Unmarshal(data, &str); err != nil { + return err + } + duration, err := time.ParseDuration(str) + if err != nil { + return err + } + *d = Duration(duration) + return nil } // GroupAddressConfig defines all information to map a KNX group address to a prometheus metric. type GroupAddressConfig struct { - // Name defines the prometheus metric name without the MetricsPrefix. - Name string - // Comment to identify the group address. - Comment string `json:",omitempty"` - // DPT defines the DPT at the knx bus. This is required to parse the values correctly. - DPT string - // MetricType is the type that prometheus uses when exporting it. i.e. gauge or counter - MetricType string - // Export the metric to prometheus - Export bool - // ReadActive allows the exporter to actively send `GroupValueRead` telegrams to actively poll the value instead waiting for it. - ReadActive bool `json:",omitempty"` - // MaxAge of a value until it will actively send a `GroupValueRead` telegram to read the value if ReadActive is set to true. - MaxAge Duration `json:",omitempty"` - // Labels defines static labels that should be set when exporting the metric using prometheus. - Labels map[string]string `json:",omitempty"` + // Name defines the prometheus metric name without the MetricsPrefix. + Name string + // Comment to identify the group address. + Comment string `json:",omitempty"` + // DPT defines the DPT at the knx bus. This is required to parse the values correctly. + DPT string + // MetricType is the type that prometheus uses when exporting it. i.e. gauge or counter + MetricType string + // Export the metric to prometheus + Export bool + // ReadActive allows the exporter to actively send `GroupValueRead` telegrams to actively poll the value instead waiting for it. + ReadActive bool `json:",omitempty"` + // MaxAge of a value until it will actively send a `GroupValueRead` telegram to read the value if ReadActive is set to true. + MaxAge Duration `json:",omitempty"` + // Labels defines static labels that should be set when exporting the metric using prometheus. + Labels map[string]string `json:",omitempty"` } // GroupAddressConfigSet is a shortcut type for the group address config map.