forked from stephen-fox/suw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
suw.go
executable file
·89 lines (70 loc) · 1.85 KB
/
suw.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package suw
import (
"github.com/stephen-fox/versionutil"
)
const (
DefaultExecutablePath = executableParentPath + executableName
ErrorNoSuchUpdate = "The specified update does not exist"
ErrorUpdatesServerUnreachable = "The Apple updates server is unreachable"
executableParentPath = "/usr/sbin/"
executableName = "softwareupdate"
listUpdatesArg = "-l"
installUpdateArg = "-i"
verboseArg = "--verbose"
)
type Update struct {
Name string
ApplicationName string
Version versionutil.Version
SizeMegabytes uint64
IsRestartNeeded bool
}
func (o Update) HasUpdateSize() bool {
return o.SizeMegabytes > 0
}
var (
TargetCliApi CliApi = GetDefaultCliApi()
)
// GetUpdates gets all available updates.
func GetUpdates() ([]Update, error) {
output, err := TargetCliApi.Execute(listUpdatesArg)
if err != nil {
return []Update{}, err
}
var updates []Update
for i, l := range output {
nextLine := ""
if i < len(output) - 1 {
nextLine = output[i+1]
}
isUpdate, update := TargetCliApi.IsUpdate(l, nextLine)
if isUpdate {
updates = append(updates, update)
}
}
return updates, nil
}
// InstallUpdates installs an update.
func InstallUpdate(updateName string) error {
return InstallUpdateVerbose(updateName, nil)
}
// InstallUpdateVerbose installs an update and provides installation progress
// percentages to the specified channel.
func InstallUpdateVerbose(updateName string, progressPercentages chan int) error {
outputs := make(chan string)
go func() {
for line := range outputs {
if progressPercentages != nil {
isProgress, percent := TargetCliApi.IsInstallProgress(line)
if isProgress {
progressPercentages <- percent
}
}
}
}()
err := TargetCliApi.ExecuteToChan(outputs, verboseArg, installUpdateArg, updateName)
if err != nil {
return err
}
return nil
}