-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a function to wait for the device to become ready
Signed-off-by: Christian Ege <[email protected]>
- Loading branch information
Showing
3 changed files
with
136 additions
and
6 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,52 @@ | ||
/* | ||
Copyright © 2023 Christian Ege <[email protected]> | ||
*/ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/graugans/go-ovp8xx/pkg/ovp8xx" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func waitForOnlineCommand(cmd *cobra.Command, args []string) error { | ||
var ok = false | ||
var err error | ||
helper, err := NewHelper(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
o3r := ovp8xx.NewClient( | ||
ovp8xx.WithHost(helper.hostname()), | ||
) | ||
|
||
timeout, err := cmd.Flags().GetUint("timeout") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if ok, err = o3r.IsAvailable(time.Duration(timeout) * time.Second); err != nil { | ||
return err | ||
} | ||
if ok { | ||
fmt.Printf("The device is available now\n") | ||
} | ||
return nil | ||
} | ||
|
||
// waitForOnlineCmd represents the wait command | ||
var waitForOnlineCmd = &cobra.Command{ | ||
Use: "WaitForOnline", | ||
Short: "Wait until the device is accessible", | ||
Long: `This command is maybe useful after a reboot or power on. | ||
It can be used to wait until the device can handle requests`, | ||
RunE: waitForOnlineCommand, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(waitForOnlineCmd) | ||
waitForOnlineCmd.Flags().Uint("timeout", 120, "Timeout in Seconds to wait until the device is accessible") | ||
} |
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