-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathappcast.go
188 lines (152 loc) · 4.76 KB
/
appcast.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Package appcast provides functionality for working with appcasts to retrieve
// valuable information about software releases.
//
// Currently supports 3 providers: "GitHub Atom Feed", "SourceForge RSS Feed"
// and "Sparkle RSS Feed". However, it can be extended to your own needs
// if necessary.
//
// See README.md for more info.
package appcast
import (
"fmt"
"github.com/victorpopkov/go-appcast/appcaster"
"github.com/victorpopkov/go-appcast/provider"
"github.com/victorpopkov/go-appcast/provider/github"
"github.com/victorpopkov/go-appcast/provider/sourceforge"
"github.com/victorpopkov/go-appcast/provider/sparkle"
"github.com/victorpopkov/go-appcast/source"
)
// Appcaster is the interface that wraps the Appcast methods.
type Appcaster interface {
appcaster.Appcaster
LoadFromRemoteSource(i interface{}) (appcaster.Appcaster, []error)
LoadFromLocalSource(path string) (appcaster.Appcaster, []error)
}
// Appcast represents the appcast itself.
type Appcast struct {
appcaster.Appcast
}
// New returns a new Appcast instance pointer. The source can be passed as a
// parameter.
func New(src ...interface{}) *Appcast {
a := new(Appcast)
if len(src) > 0 {
src := src[0].(appcaster.Sourcer)
a.SetSource(src)
}
return a
}
// LoadFromRemoteSource creates a new RemoteSource instance and loads the data
// from the remote location by using the RemoteSource.Load method.
//
// It returns both: the supported provider-specific appcast implementing the
// Appcaster interface and an errors slice.
func (a *Appcast) LoadFromRemoteSource(i interface{}) (appcaster.Appcaster, []error) {
var errors []error
src, err := source.NewRemote(i)
if err != nil {
return nil, append(errors, err)
}
err = src.Load()
if err != nil {
return nil, append(errors, err)
}
a.SetSource(src)
a.GuessSourceProvider()
return a.Unmarshal()
}
// LoadFromLocalSource creates a new LocalSource instance and loads the data
// from the local file by using the LocalSource.Load method.
//
// It returns both: the supported provider-specific appcast implementing the
// Appcaster interface and an errors slice.
func (a *Appcast) LoadFromLocalSource(path string) (appcaster.Appcaster, []error) {
var errors []error
src := source.NewLocal(path)
err := src.Load()
if err != nil {
return nil, append(errors, err)
}
a.SetSource(src)
a.GuessSourceProvider()
return a.Unmarshal()
}
// LoadSource sets the Appcast.source.content field value depending on the
// source type. It should call the appropriate Appcast.Source.Load methods
// chain.
func (a *Appcast) LoadSource() error {
err := a.Appcast.LoadSource()
if err != nil {
return err
}
a.GuessSourceProvider()
return nil
}
// GuessSourceProvider attempts to guess the supported provider based on the
// Appcast.source.content.
func (a *Appcast) GuessSourceProvider() {
switch src := a.Source().(type) {
case *source.Remote:
src.SetProvider(provider.GuessProviderByUrl(src.Url()))
if src.Provider() == provider.Unknown {
src.SetProvider(provider.GuessProviderByContent(src.Content()))
}
case *source.Local:
src.SetProvider(provider.GuessProviderByContent(src.Content()))
default:
src.SetProvider(provider.Unknown)
}
}
// Unmarshal unmarshals the Appcast.source.content into the Appcast.releases by
// calling the appropriate provider-specific Unmarshal method from the supported
// providers.
//
// It returns both: the supported provider-specific appcast implementing the
// Appcaster interface and an errors slice.
func (a *Appcast) Unmarshal() (appcaster.Appcaster, []error) {
var appcast appcaster.Appcaster
var errors []error
p := a.Source().Provider()
switch p {
case provider.Sparkle:
appcast = &sparkle.Appcast{Appcast: a.Appcast}
break
case provider.SourceForge:
appcast = &sourceforge.Appcast{Appcast: a.Appcast}
break
case provider.GitHub:
appcast = &github.Appcast{Appcast: a.Appcast}
break
default:
name := p.String()
if name == "-" {
name = "Unknown"
}
errors = append(errors, fmt.Errorf("releases for the \"%s\" provider can't be unmarshaled", name))
return nil, errors
}
appcast, errors = appcast.Unmarshal()
a.Source().SetAppcast(appcast)
a.SetReleases(appcast.Releases())
return appcast, errors
}
// Uncomment uncomments the commented out lines by calling the appropriate
// provider-specific Uncomment method from the supported providers.
func (a *Appcast) Uncomment() error {
if a.Source() == nil {
return fmt.Errorf("no source")
}
p := a.Source().Provider()
switch p {
case provider.Sparkle:
appcast := sparkle.Appcast{Appcast: a.Appcast}
_ = appcast.Uncomment()
a.Source().SetContent(appcast.Appcast.Source().Content())
return nil
}
name := p.String()
if name == "-" {
name = "Unknown"
}
return fmt.Errorf("uncommenting is not available for the \"%s\" provider", name)
}