Skip to content

Commit

Permalink
Merge pull request #2 from rolfl/addCLI
Browse files Browse the repository at this point in the history
Enable env var default for units
  • Loading branch information
rolfl authored Mar 14, 2021
2 parents f85fb62 + 6d4722f commit a4dda51
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 8 deletions.
1 change: 1 addition & 0 deletions mbcli/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func client(access string) (modbus.Client, error) {
if parts[5] != "dtr" {
return nil, fmt.Errorf("DTR must be specified as 'dtr', not %v", parts[5])
}
dtr = true
}
unit, err := strconv.Atoi(parts[len(parts)-1])
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions mbcli/coils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

type CoilGetCommands struct {
Units []string `short:"u" long:"unit" description:"Unit(s) to contact" required:"true"`
Units []string `short:"u" long:"unit" description:"Unit(s) to contact" required:"true" env:"MBCLI_UNIT" env-delim:","`
Timeout int `short:"t" long:"timeout" default:"5" description:"Timeout (in seconds)"`
Args struct {
Addresses []string `required:"1"`
Expand All @@ -18,7 +18,7 @@ func (c *CoilGetCommands) Execute(args []string) error {
}

type CoilSetCommands struct {
Units []string `short:"u" long:"unit" description:"Unit(s) to contact" required:"true"`
Units []string `short:"u" long:"unit" description:"Unit(s) to contact" required:"true" env:"MBCLI_UNIT" env-delim:","`
Timeout int `short:"t" long:"timeout" default:"5" description:"Timeout (in seconds)"`
Args struct {
AddressValues []string `required:"1"`
Expand Down
2 changes: 1 addition & 1 deletion mbcli/diag.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type DiagnosticCommands struct {
Events bool `short:"e" long:"events" description:"Return the Event counter value"`
Clear bool `short:"C" long:"clear" description:"Reset the Event counter value"`
Timeout int `short:"t" long:"timeout" default:"5" description:"Timeout (in seconds)"`
Units []string `short:"u" long:"unit" description:"Unit(s) to contatc" required:"true"`
Units []string `short:"u" long:"unit" description:"Unit(s) to contact" required:"true" env:"MBCLI_UNIT" env-delim:","`
}

func (c *DiagnosticCommands) Execute(args []string) error {
Expand Down
2 changes: 1 addition & 1 deletion mbcli/discretes.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

type DiscreteGetCommands struct {
Units []string `short:"u" long:"unit" description:"Unit(s) to contact" required:"true"`
Units []string `short:"u" long:"unit" description:"Unit(s) to contact" required:"true" env:"MBCLI_UNIT" env-delim:","`
Timeout int `short:"t" long:"timeout" default:"5" description:"Timeout (in seconds)"`
Args struct {
Addresses []string `required:"1"`
Expand Down
5 changes: 4 additions & 1 deletion mbcli/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ func initializeConnections(units []string) error {

func genericClientReads(toget string, units []string, addressRefs []string, timeoutSec int) error {
// initialize the connections
initializeConnections(units)
err := initializeConnections(units)
if err != nil {
return err
}

timeout := time.Second * time.Duration(timeoutSec)
addresses, err := addressRanges(addressRefs)
Expand Down
4 changes: 2 additions & 2 deletions mbcli/holdings.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

type HoldingGetCommands struct {
Units []string `short:"u" long:"unit" description:"Unit(s) to contact" required:"true"`
Units []string `short:"u" long:"unit" description:"Unit(s) to contact" required:"true" env:"MBCLI_UNIT" env-delim:","`
Timeout int `short:"t" long:"timeout" default:"5" description:"Timeout (in seconds)"`
Args struct {
Addresses []string `required:"1"`
Expand All @@ -18,7 +18,7 @@ func (c *HoldingGetCommands) Execute(args []string) error {
}

type HoldingSetCommands struct {
Units []string `short:"u" long:"unit" description:"Unit(s) to contact" required:"true"`
Units []string `short:"u" long:"unit" description:"Unit(s) to contact" required:"true" env:"MBCLI_UNIT" env-delim:","`
Timeout int `short:"t" long:"timeout" default:"5" description:"Timeout (in seconds)"`
Args struct {
AddressValues []string `required:"1"`
Expand Down
2 changes: 1 addition & 1 deletion mbcli/inputs.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

type InputGetCommands struct {
Units []string `short:"u" long:"unit" description:"Unit(s) to contact" required:"true"`
Units []string `short:"u" long:"unit" description:"Unit(s) to contact" required:"true" env:"MBCLI_UNIT" env-delim:","`
Timeout int `short:"t" long:"timeout" default:"5" description:"Timeout (in seconds)"`
Args struct {
Addresses []string `required:"1"`
Expand Down

0 comments on commit a4dda51

Please sign in to comment.