feat(scan): get repository name of updatable pkgs for debian/ubuntu (#738)

This commit is contained in:
Kota Kanbe
2018-11-26 12:02:52 +09:00
committed by GitHub
parent 7585f9d537
commit 31bd6c0371
2 changed files with 26 additions and 7 deletions

View File

@@ -528,8 +528,8 @@ func (o *debian) fillCandidateVersion(updatables models.Packages) (err error) {
if !r.isSuccess() {
return fmt.Errorf("Failed to SSH: %s", r)
}
packChangelog := o.splitAptCachePolicy(r.Stdout)
for k, v := range packChangelog {
packAptPolicy := o.splitAptCachePolicy(r.Stdout)
for k, v := range packAptPolicy {
ver, err := o.parseAptCachePolicy(v, k)
if err != nil {
return fmt.Errorf("Failed to parse %s", err)
@@ -539,6 +539,7 @@ func (o *debian) fillCandidateVersion(updatables models.Packages) (err error) {
return fmt.Errorf("Not found: %s", k)
}
pack.NewVersion = ver.Candidate
pack.Repository = ver.Repo
updatables[k] = pack
}
return
@@ -909,27 +910,29 @@ func (o *debian) splitAptCachePolicy(stdout string) map[string]string {
lasti = i
}
packChangelog := map[string]string{}
packAptPolicy := map[string]string{}
for _, r := range splitted {
packName := r[:strings.Index(r, ":")]
packChangelog[packName] = r
packAptPolicy[packName] = r
}
return packChangelog
return packAptPolicy
}
type packCandidateVer struct {
Name string
Installed string
Candidate string
Repo string
}
// parseAptCachePolicy the stdout of parse pat-get cache policy
func (o *debian) parseAptCachePolicy(stdout, name string) (packCandidateVer, error) {
ver := packCandidateVer{Name: name}
lines := strings.Split(stdout, "\n")
isRepoline := false
for _, line := range lines {
fields := strings.Fields(line)
if len(fields) != 2 {
if len(fields) < 2 {
continue
}
switch fields[0] {
@@ -937,10 +940,23 @@ func (o *debian) parseAptCachePolicy(stdout, name string) (packCandidateVer, err
ver.Installed = fields[1]
case "Candidate:":
ver.Candidate = fields[1]
return ver, nil
goto nextline
default:
// nop
}
if ver.Candidate != "" && strings.Contains(line, ver.Candidate) {
isRepoline = true
goto nextline
}
if isRepoline {
ss := strings.Split(strings.TrimSpace(line), " ")
if len(ss) == 5 {
ver.Repo = ss[2]
return ver, nil
}
}
nextline:
}
return ver, fmt.Errorf("Unknown Format: %s", stdout)
}

View File

@@ -530,6 +530,7 @@ func TestParseAptCachePolicy(t *testing.T) {
Name: "openssl",
Installed: "1.0.2f-2ubuntu1",
Candidate: "1.0.2g-1ubuntu2",
Repo: "xenial/main",
},
},
{
@@ -550,6 +551,7 @@ func TestParseAptCachePolicy(t *testing.T) {
Name: "openssl",
Installed: "1.0.1f-1ubuntu2.16",
Candidate: "1.0.1f-1ubuntu2.17",
Repo: "trusty-updates/main",
},
},
{
@@ -570,6 +572,7 @@ func TestParseAptCachePolicy(t *testing.T) {
Name: "openssl",
Installed: "1.0.1-4ubuntu5.33",
Candidate: "1.0.1-4ubuntu5.34",
Repo: "precise-updates/main",
},
},
}