Skip to content

Commit 8bb2945

Browse files
committed
improve registry
1 parent 2a81758 commit 8bb2945

File tree

9 files changed

+195
-147
lines changed

9 files changed

+195
-147
lines changed

app/commands/install/appimagehub.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ type AppImageHubRepo struct {
1212
}
1313

1414
func NewAppImageHubRepo(target string) (Repo, error) {
15+
if strings.HasPrefix(target, "https://www.appimagehub.com/p/") {
16+
target = strings.Replace(target, "https://www.appimagehub.com/p/", "appimagehub:", 1)
17+
}
18+
1519
if !strings.HasPrefix(target, "appimagehub:") {
1620
return nil, InvalidTargetFormat
1721
}
@@ -53,7 +57,8 @@ func (a AppImageHubRepo) GetLatestRelease() (*Release, error) {
5357
Url: link.Data,
5458
}
5559

56-
if strings.HasSuffix(downloadLink.FileName, ".AppImage") {
60+
if strings.HasSuffix(downloadLink.FileName, ".AppImage") ||
61+
strings.HasSuffix(downloadLink.FileName, ".appimage") {
5762
downloadLinks = append(downloadLinks, downloadLink)
5863
}
5964
}
@@ -72,3 +77,7 @@ func (a AppImageHubRepo) Download(binaryUrl *utils.BinaryUrl, targetPath string)
7277
err = utils.DownloadAppImage(binaryUrl.Url, targetPath)
7378
return
7479
}
80+
81+
func (a AppImageHubRepo) FallBackUpdateInfo() string {
82+
return "ocs-v1-appimagehub-zsync|www.appimagehub.com/ocs/v1|" + a.ContentId + "|*.AppImage"
83+
}

app/commands/install/github.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,6 @@ func NewGitHubRepo(target string) (repo Repo, err error) {
5252
func (g GitHubRepo) Id() string {
5353
id := "github:" + g.User + "/" + g.Project
5454

55-
if g.Release != "" {
56-
id += "/" + g.Release
57-
} else {
58-
id += "/latest"
59-
}
60-
61-
if g.File != "" {
62-
id += "/" + g.File
63-
}
64-
6555
return id
6656
}
6757

@@ -103,3 +93,20 @@ func (g GitHubRepo) Download(binaryUrl *utils.BinaryUrl, targetPath string) (err
10393
err = utils.DownloadAppImage(binaryUrl.Url, targetPath)
10494
return
10595
}
96+
97+
func (g GitHubRepo) FallBackUpdateInfo() string {
98+
updateInfo := "gh-releases-direct|" + g.User + "|" + g.Project
99+
if g.Release == "" {
100+
updateInfo += "|latest"
101+
} else {
102+
updateInfo += "|" + g.Release
103+
}
104+
105+
if g.File == "" {
106+
updateInfo += "|*.AppImage"
107+
} else {
108+
updateInfo += "|" + g.File
109+
}
110+
111+
return updateInfo
112+
}

app/commands/install/install.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"appimage-manager/app/commands"
55
"appimage-manager/app/utils"
66
"fmt"
7+
updateUtils "github.com/AppImageCrafters/appimage-update/util"
78
"os"
89
)
910

@@ -20,6 +21,7 @@ type Repo interface {
2021
Id() string
2122
GetLatestRelease() (*Release, error)
2223
Download(binaryUrl *utils.BinaryUrl, targetPath string) error
24+
FallBackUpdateInfo() string
2325
}
2426

2527
func (cmd *InstallCmd) Run(*commands.Context) (err error) {
@@ -72,10 +74,23 @@ func (cmd *InstallCmd) parseTarget() (Repo, error) {
7274
return nil, InvalidTargetFormat
7375
}
7476

75-
func (cmd *InstallCmd) addToRegistry(targetFilePath string, source Repo) {
77+
func (cmd *InstallCmd) addToRegistry(targetFilePath string, repo Repo) {
78+
sha1, _ := utils.GetFileSHA1(targetFilePath)
79+
updateInfo, _ := updateUtils.ReadUpdateInfo(targetFilePath)
80+
if updateInfo == "" {
81+
updateInfo = repo.FallBackUpdateInfo()
82+
}
83+
84+
entry := utils.RegistryEntry{
85+
FilePath: targetFilePath,
86+
Repo: repo.Id(),
87+
FileSha1: sha1,
88+
UpdateInfo: updateInfo,
89+
}
90+
7691
registry, _ := utils.OpenRegistry()
7792
if registry != nil {
78-
_ = registry.Add(targetFilePath, source.Id())
93+
_ = registry.Add(entry)
7994
_ = registry.Close()
8095
}
8196
}

app/commands/list.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"bytes"
66
"github.com/juju/ansiterm"
77
"os"
8+
"path/filepath"
89
"sort"
910
)
1011

@@ -31,7 +32,7 @@ func (r *ListCmd) Run(*Context) error {
3132

3233
var lines [][]string
3334
for fileName, v := range registry.Entries {
34-
line := []string{v.Repo, fileName, v.SHA1}
35+
line := []string{v.Repo, filepath.Base(fileName), v.FileSha1}
3536
lines = append(lines, line)
3637
}
3738
sort.Slice(lines, func(i int, j int) bool {

app/commands/remove.go

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,37 @@ import (
88
"fmt"
99
"github.com/rainycape/dl"
1010
"os"
11-
"path/filepath"
1211
)
1312

1413
type RemoveCmd struct {
15-
Id string `arg name:"id" help:"Installation id or file name." type:"string"`
14+
Target string `arg name:"id" help:"Installation id or file name." type:"string"`
1615
}
1716

1817
func (cmd *RemoveCmd) Run(*Context) (err error) {
1918
registry, err := utils.OpenRegistry()
2019
if err != nil {
2120
return err
2221
}
22+
defer registry.Close()
23+
2324
registry.Update()
2425

25-
fileName, ok := registry.Lookup(cmd.Id)
26+
entry, ok := registry.Lookup(cmd.Target)
2627
if !ok {
27-
fileName = cmd.Id
28-
}
29-
30-
applicationsDir, err := utils.MakeApplicationsDirPath()
31-
if err != nil {
32-
return err
33-
}
34-
filePath := filepath.Join(applicationsDir, fileName)
35-
36-
if _, err := os.Stat(filePath); os.IsNotExist(err) {
37-
return fmt.Errorf("application not found \"" + cmd.Id + "\"")
28+
return fmt.Errorf("application not found \"" + cmd.Target + "\"")
3829
}
3930

40-
err = uninstallAppImage(filePath)
31+
err = uninstallAppImage(entry.FilePath)
4132
if err != nil {
4233
fmt.Println("Desktop deregistration failed: " + err.Error())
4334
}
4435

45-
err = os.Remove(filePath)
36+
err = os.Remove(entry.FilePath)
4637
if err != nil {
4738
return fmt.Errorf("Unable to remove AppImage file: " + err.Error())
4839
}
49-
fmt.Println("Application removed: " + fileName)
40+
fmt.Println("Application removed: " + entry.FilePath)
41+
registry.Remove(entry.FilePath)
5042
return err
5143
}
5244

app/commands/update.go

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package commands
22

33
import (
4-
"fmt"
5-
"os"
6-
"path/filepath"
7-
"strings"
8-
94
"appimage-manager/app/utils"
5+
"errors"
6+
"fmt"
7+
updateUtils "github.com/AppImageCrafters/appimage-update/util"
108

119
"github.com/AppImageCrafters/appimage-update"
1210
)
@@ -18,6 +16,8 @@ type UpdateCmd struct {
1816
All bool `help:"Update all applications."`
1917
}
2018

19+
var NoUpdateInfo = errors.New("there is no update information")
20+
2121
func (cmd *UpdateCmd) Run(*Context) (err error) {
2222
if cmd.All {
2323
cmd.Targets, err = getAllTargets()
@@ -27,32 +27,32 @@ func (cmd *UpdateCmd) Run(*Context) (err error) {
2727
}
2828

2929
for _, target := range cmd.Targets {
30-
filePath, err := cmd.getBundleFilePath(target)
30+
entry, err := cmd.getRegistryEntry(target)
3131
if err != nil {
32-
println(err.Error())
32+
println(err)
3333
continue
3434
}
3535

36-
updateMethod, err := update.NewUpdaterFor(filePath)
36+
updateMethod, err := update.NewUpdateForUpdateString(entry.UpdateInfo, entry.FilePath)
3737
if err != nil {
3838
println(err.Error())
3939
continue
4040
}
4141

42-
fmt.Println("Looking for updates of: ", filePath)
42+
fmt.Println("Looking for updates of: ", entry.FilePath)
4343
updateAvailable, err := updateMethod.Lookup()
4444
if err != nil {
4545
println(err.Error())
4646
continue
4747
}
4848

4949
if !updateAvailable {
50-
fmt.Println("No updates were found for: ", filePath)
50+
fmt.Println("No updates were found for: ", entry.FilePath)
5151
continue
5252
}
5353

5454
if cmd.Check {
55-
fmt.Println("Update available for: ", filePath)
55+
fmt.Println("Update available for: ", entry.FilePath)
5656
continue
5757
}
5858

@@ -68,49 +68,38 @@ func (cmd *UpdateCmd) Run(*Context) (err error) {
6868
return nil
6969
}
7070

71-
func getAllTargets() ([]string, error) {
71+
func (cmd *UpdateCmd) getRegistryEntry(target string) (utils.RegistryEntry, error) {
7272
registry, err := utils.OpenRegistry()
7373
if err != nil {
74-
return nil, err
75-
}
76-
registry.Update()
77-
78-
paths := make([]string, len(registry.Entries))
79-
for k := range registry.Entries {
80-
paths = append(paths, k)
74+
return utils.RegistryEntry{}, err
8175
}
76+
defer registry.Close()
8277

83-
return paths, nil
84-
}
78+
entry, _ := registry.Lookup(target)
8579

86-
func (cmd *UpdateCmd) getBundleFilePath(target string) (string, error) {
87-
if strings.HasPrefix(target, "file://") {
88-
cmd.Targets = cmd.Targets[7:]
80+
if entry.UpdateInfo == "" {
81+
entry.UpdateInfo, _ = updateUtils.ReadUpdateInfo(target)
82+
entry.FilePath = target
8983
}
9084

91-
if _, err := os.Stat(target); err == nil {
92-
return target, nil
85+
if entry.UpdateInfo == "" {
86+
return entry, NoUpdateInfo
87+
} else {
88+
return entry, nil
9389
}
90+
}
9491

92+
func getAllTargets() ([]string, error) {
9593
registry, err := utils.OpenRegistry()
9694
if err != nil {
97-
return "", err
95+
return nil, err
9896
}
9997
registry.Update()
10098

101-
fileName, ok := registry.Lookup(target)
102-
if !ok {
103-
fileName = target
104-
}
105-
106-
applicationsDir, err := utils.MakeApplicationsDirPath()
107-
if err != nil {
108-
return "", err
99+
paths := make([]string, len(registry.Entries))
100+
for k := range registry.Entries {
101+
paths = append(paths, k)
109102
}
110-
filePath := filepath.Join(applicationsDir, fileName)
111103

112-
if _, err := os.Stat(filePath); os.IsNotExist(err) {
113-
return "", fmt.Errorf("application not found \"" + target + "\"")
114-
}
115-
return filePath, nil
104+
return paths, nil
116105
}

0 commit comments

Comments
 (0)