Newer
Older
package commands
import (
"fmt"
"os"
"path/filepath"
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
"github.com/AppImageCrafters/appimage-update"
)
type UpdateCmd struct {
Id string `arg name:"id" help:"Installation id or file name." type:"string"`
}
func (cmd *UpdateCmd) Run(*Context) (err error) {
filePath, err := cmd.getApplicationFilePath()
if err != nil {
return err
}
updateMethod, err := update.NewUpdaterFor(filePath)
if err != nil {
return err
}
fmt.Println("Looking for updates of: ", filePath)
updateAvailable, err := updateMethod.Lookup()
if err != nil {
fmt.Println(err.Error())
return
}
if !updateAvailable {
fmt.Println("No updates were found for: ", filePath)
return
}
result, err := updateMethod.Download()
if err != nil {
fmt.Println("Error: ", err.Error())
return
}
fmt.Println("Update downloaded to: " + result)
return nil
}
func (cmd *UpdateCmd) getApplicationFilePath() (string, error) {
registry, err := utils.OpenRegistry()
if err != nil {
return "", err
}
registry.Update()
fileName, ok := registry.Lookup(cmd.Id)
if !ok {
fileName = cmd.Id
}
applicationsDir, err := utils.MakeApplicationsDirPath()
if err != nil {
return "", err
}
filePath := filepath.Join(applicationsDir, fileName)
if _, err := os.Stat(filePath); os.IsNotExist(err) {
return "", fmt.Errorf("application not found \"" + cmd.Id + "\"")
}
return filePath, nil
}