-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathlocations.go
128 lines (107 loc) · 3.47 KB
/
locations.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
package install
import (
"github.com/itchio/butler/butlerd"
"github.com/itchio/butler/database/models"
"github.com/itchio/butler/endpoints/fetch"
"github.com/pkg/errors"
uuid "github.com/satori/go.uuid"
)
func InstallLocationsGetByID(rc *butlerd.RequestContext, params *butlerd.InstallLocationsGetByIDParams) (*butlerd.InstallLocationsGetByIDResult, error) {
if params.ID == "" {
return nil, errors.Errorf("id must be set")
}
il := models.InstallLocationByID(rc.DB(), params.ID)
if il == nil {
return nil, errors.Errorf("install location (%s) not found", params.ID)
}
res := &butlerd.InstallLocationsGetByIDResult{
InstallLocation: fetch.FormatInstallLocation(rc, il),
}
return res, nil
}
func InstallLocationsList(rc *butlerd.RequestContext, params *butlerd.InstallLocationsListParams) (*butlerd.InstallLocationsListResult, error) {
var locations []*models.InstallLocation
err := rc.DB().Find(&locations).Error
if err != nil {
return nil, errors.WithStack(err)
}
var flocs []*butlerd.InstallLocationSummary
for _, il := range locations {
flocs = append(flocs, fetch.FormatInstallLocation(rc, il))
}
res := &butlerd.InstallLocationsListResult{
InstallLocations: flocs,
}
return res, nil
}
func InstallLocationsAdd(rc *butlerd.RequestContext, params *butlerd.InstallLocationsAddParams) (*butlerd.InstallLocationsAddResult, error) {
consumer := rc.Consumer
hadID := false
if params.ID == "" {
hadID = true
freshUuid, err := uuid.NewV4()
if err != nil {
return nil, errors.WithStack(err)
}
params.ID = freshUuid.String()
}
if params.Path == "" {
return nil, errors.New("path must be set")
}
if hadID {
existing := models.InstallLocationByID(rc.DB(), params.ID)
if existing != nil {
if existing.Path == params.Path {
consumer.Statf("(%s) exists, and has same path (%s), doing nothing", params.ID, params.Path)
res := &butlerd.InstallLocationsAddResult{}
return res, nil
}
return nil, errors.Errorf("(%s) exists but has path (%s) - we were passed (%s)", params.ID, existing.Path, params.Path)
}
}
il := &models.InstallLocation{
ID: params.ID,
Path: params.Path,
}
err := rc.DB().Save(il).Error
if err != nil {
return nil, errors.WithStack(err)
}
res := &butlerd.InstallLocationsAddResult{}
return res, nil
}
func InstallLocationsRemove(rc *butlerd.RequestContext, params *butlerd.InstallLocationsRemoveParams) (*butlerd.InstallLocationsRemoveResult, error) {
consumer := rc.Consumer
if params.ID == "" {
return nil, errors.Errorf("id must be set")
}
il := models.InstallLocationByID(rc.DB(), params.ID)
if il == nil {
consumer.Statf("Install location (%s) does not exist, doing nothing")
res := &butlerd.InstallLocationsRemoveResult{}
return res, nil
}
var caveCount int64
err := rc.DB().Model(&models.Cave{}).Where("install_location_id = ?", il.ID).Count(&caveCount).Error
if err != nil {
return nil, errors.WithStack(err)
}
if caveCount > 0 {
// TODO: suggest moving to another install location
return nil, errors.Errorf("Refusing to remove install location (%s) because it is not empty", params.ID)
}
var locationCount int64
err = rc.DB().Model(&models.InstallLocation{}).Count(&locationCount).Error
if err != nil {
return nil, errors.WithStack(err)
}
if locationCount == 1 {
return nil, errors.Errorf("Refusing to remove last install location")
}
err = rc.DB().Delete(il).Error
if err != nil {
return nil, errors.WithStack(err)
}
res := &butlerd.InstallLocationsRemoveResult{}
return res, nil
}