-
-
Notifications
You must be signed in to change notification settings - Fork 737
/
packages.go
134 lines (108 loc) · 3.07 KB
/
packages.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package task
import (
"context"
"fmt"
"github.com/stashapp/stash/pkg/job"
"github.com/stashapp/stash/pkg/logger"
"github.com/stashapp/stash/pkg/models"
"github.com/stashapp/stash/pkg/pkg"
)
type PackagesJob struct {
PackageManager *pkg.Manager
OnComplete func()
}
func (j *PackagesJob) installPackage(ctx context.Context, p models.PackageSpecInput, progress *job.Progress) error {
defer progress.Increment()
if err := j.PackageManager.Install(ctx, p); err != nil {
return fmt.Errorf("installing package: %w", err)
}
return nil
}
type InstallPackagesJob struct {
PackagesJob
Packages []*models.PackageSpecInput
}
func (j *InstallPackagesJob) Execute(ctx context.Context, progress *job.Progress) {
progress.SetTotal(len(j.Packages))
for _, p := range j.Packages {
if job.IsCancelled(ctx) {
logger.Info("Cancelled installing packages")
return
}
logger.Infof("Installing package %s", p.ID)
taskDesc := fmt.Sprintf("Installing %s", p.ID)
progress.ExecuteTask(taskDesc, func() {
if err := j.installPackage(ctx, *p, progress); err != nil {
logger.Errorf("Error installing package %s from %s: %v", p.ID, p.SourceURL, err)
}
})
}
if j.OnComplete != nil {
j.OnComplete()
}
logger.Infof("Finished installing packages")
}
type UpdatePackagesJob struct {
PackagesJob
Packages []*models.PackageSpecInput
}
func (j *UpdatePackagesJob) Execute(ctx context.Context, progress *job.Progress) {
// if no packages are specified, update all
if len(j.Packages) == 0 {
installed, err := j.PackageManager.InstalledStatus(ctx)
if err != nil {
logger.Errorf("Error getting installed packages: %v", err)
return
}
for _, p := range installed {
if p.Upgradable() {
j.Packages = append(j.Packages, &models.PackageSpecInput{
ID: p.Local.ID,
SourceURL: p.Remote.Repository.Path(),
})
}
}
}
progress.SetTotal(len(j.Packages))
for _, p := range j.Packages {
if job.IsCancelled(ctx) {
logger.Info("Cancelled updating packages")
return
}
logger.Infof("Updating package %s", p.ID)
taskDesc := fmt.Sprintf("Updating %s", p.ID)
progress.ExecuteTask(taskDesc, func() {
if err := j.installPackage(ctx, *p, progress); err != nil {
logger.Errorf("Error updating package %s from %s: %v", p.ID, p.SourceURL, err)
}
})
}
if j.OnComplete != nil {
j.OnComplete()
}
logger.Infof("Finished updating packages")
}
type UninstallPackagesJob struct {
PackagesJob
Packages []*models.PackageSpecInput
}
func (j *UninstallPackagesJob) Execute(ctx context.Context, progress *job.Progress) {
progress.SetTotal(len(j.Packages))
for _, p := range j.Packages {
if job.IsCancelled(ctx) {
logger.Info("Cancelled installing packages")
return
}
logger.Infof("Uninstalling package %s", p.ID)
taskDesc := fmt.Sprintf("Uninstalling %s", p.ID)
progress.ExecuteTask(taskDesc, func() {
if err := j.PackageManager.Uninstall(ctx, *p); err != nil {
logger.Errorf("Error uninstalling package %s: %v", p.ID, err)
}
})
}
if j.OnComplete != nil {
j.OnComplete()
}
logger.Infof("Finished uninstalling packages")
}