-
Notifications
You must be signed in to change notification settings - Fork 9
/
provider.go
105 lines (98 loc) · 2.86 KB
/
provider.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
//
// Copyright 2016-2018 Bryan T. Meyers <bmeyers@datadrake.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package git
import (
"bufio"
"bytes"
"github.com/DataDrake/cuppa/results"
"io"
"os/exec"
"strings"
"time"
)
// Provider provides a common interface for each of the backend providers
type Provider struct{}
// Name returns the name of this provider
func (p Provider) Name() string {
return "Git"
}
// Latest finds the newest release for a Git package
func (p Provider) Latest(name string) (*results.Result, results.Status) {
cmd := exec.Command("git", "ls-remote", "--tags", name)
buff := new(bytes.Buffer)
cmd.Stdout = buff
read := bufio.NewReader(buff)
err := cmd.Run()
line, _, err := read.ReadLine()
var r *results.Result
pieces := strings.Split(name, "/")
repoName := strings.Split(pieces[len(pieces)-1], ".")[0]
for err == nil {
pieces := strings.Split(string(line), "/")
tag := pieces[0]
if len(pieces) > 1 {
tag = pieces[len(pieces)-1]
}
if !strings.HasSuffix(tag, "{}") {
r = results.NewResult(repoName, tag, "git|"+name, time.Time{})
}
line, _, err = read.ReadLine()
}
if err != io.EOF || r == nil {
return nil, results.NotFound
}
return r, results.OK
}
// Match checks to see if this provider can handle this kind of query
func (p Provider) Match(query string) string {
if strings.HasPrefix(query, "git|") || strings.HasSuffix(query, ".git") {
pieces := strings.Split(query, "|")
if len(pieces) > 1 {
return pieces[1]
}
return pieces[0]
}
return ""
}
// Releases finds all matching releases for a Git package
func (p Provider) Releases(name string) (*results.ResultSet, results.Status) {
cmd := exec.Command("git", "ls-remote", "--tags", name)
buff := new(bytes.Buffer)
cmd.Stdout = buff
read := bufio.NewReader(buff)
err := cmd.Run()
line, _, err := read.ReadLine()
var r *results.Result
pieces := strings.Split(name, "/")
repoName := strings.Split(pieces[len(pieces)-1], ".")[0]
rs := results.NewResultSet(repoName)
for err == nil {
pieces := strings.Split(string(line), "/")
tag := pieces[0]
if len(pieces) > 1 {
tag = pieces[len(pieces)-1]
}
if !strings.HasSuffix(tag, "{}") {
r = results.NewResult(repoName, tag, "git|"+name, time.Time{})
rs.AddResult(r)
}
line, _, err = read.ReadLine()
}
if err != io.EOF || rs.Len() == 0 {
return nil, results.NotFound
}
return rs, results.OK
}