-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
102 lines (95 loc) · 2.51 KB
/
main.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
90
91
92
93
94
95
96
97
98
99
100
101
102
// libreoffice-installer installs LibreOffice on macOS
package main
import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"github.com/bjoernalbers/libreoffice-installer/app"
"github.com/bjoernalbers/libreoffice-installer/dmg"
"github.com/bjoernalbers/libreoffice-installer/download"
"github.com/hashicorp/go-version"
)
func init() {
log.SetFlags(0)
log.SetPrefix("libreoffice-installer: ")
}
func main() {
latestVersion, err := download.LatestVersion(download.VersionURL)
if err != nil {
log.Fatal(err)
}
volume := os.Args[3]
appPath := filepath.Join(volume, "/Applications/LibreOffice.app")
a := app.App{appPath}
if !needsInstallation(a, latestVersion) {
log.Printf("LibreOffice %s or newer is already installed", latestVersion)
return
}
arch := runtime.GOARCH
diskimage, err := download.DiskImage(latestVersion, arch)
if err != nil {
log.Fatal(err)
}
err = app.QuitLibreOffice()
if err != nil {
log.Fatal(err)
}
err = installApplication(appPath, diskimage)
if err != nil {
log.Fatal(err)
}
log.Printf("LibreOffice %s (%s) successfully installed", latestVersion, arch)
}
// needsInstallation returns true if installation of LibreOffice is required.
func needsInstallation(a app.App, version string) bool {
if a.IsMissing() {
return true
}
// If LibreOffice has been installed from the Mac App Store it must be
// replaced since that version is currently not fit for production:
// https://bugs.documentfoundation.org/show_bug.cgi?id=153927
if a.FromMacAppStore() {
return true
}
installedVersion, err := a.Version()
if err != nil || outdated(installedVersion, version) {
return true
}
return false
}
// outdated returns true if version 1 is less than version 2 or if the versions
// are not comparable.
func outdated(version1, version2 string) bool {
v1, err := version.NewVersion(version1)
if err != nil {
return true
}
v2, err := version.NewVersion(version2)
if err != nil {
return true
}
return v1.LessThan(v2)
}
// installApplication installs application from disk image to destination,
// which is the application's target bundle path.
func installApplication(destination, diskimage string) error {
mountpoint, err := dmg.Attach(diskimage)
if err != nil {
return err
}
defer dmg.Detach(mountpoint)
err = os.RemoveAll(destination)
if err != nil {
return err
}
source := filepath.Join(mountpoint, filepath.Base(destination))
cmd := exec.Command("cp", "-R", source, destination)
err = cmd.Run()
if err != nil {
return fmt.Errorf("%s: %v", cmd, err)
}
return nil
}