-
Notifications
You must be signed in to change notification settings - Fork 2
/
update_platform_command.go
70 lines (52 loc) · 1.56 KB
/
update_platform_command.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package cloudcmd
import (
"github.com/dpb587/hubitat-cli/cmd/cmdflags"
"github.com/dpb587/hubitat-cli/hub/hubutil"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
func NewUpdatePlatformCommand(cmdp *cmdflags.Persistent) *cobra.Command {
var fFollow bool
var cmd = &cobra.Command{
Use: "update-platform",
Short: "For installing the latest platform version",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
hubClient, err := cmdp.HubClient()
if err != nil {
return errors.Wrap(err, "getting hub client")
}
result, err := hubClient.CloudCheckForUpdate(ctx)
if err != nil {
return errors.Wrap(err, "checking")
}
if result.Status != "UPDATE_AVAILABLE" {
cmdp.Logger.V(0).Info("no update available", "update_status", result.Status)
return nil
}
cmdp.Logger.V(0).Info("update available", "update_version", result.Version)
err = hubClient.CloudUpdatePlatform(ctx)
if err != nil {
return errors.Wrap(err, "updating")
}
cmdp.Logger.V(0).Info("requested update")
if !fFollow {
return nil
}
cmdp.Logger.V(0).Info("applying update")
err = hubutil.WaitForUpdate(ctx, cmdp.Logger, hubClient)
if err != nil {
return errors.Wrap(err, "waiting for update")
}
err = hubutil.WaitForStart(ctx, cmdp.Logger, hubClient)
if err != nil {
return errors.Wrap(err, "waiting for start")
}
cmdp.Logger.V(0).Info("update complete")
return nil
},
}
cmd.Flags().BoolVarP(&fFollow, "follow", "f", false, "follow progress")
return cmd
}