File: ppa.go

package info (click to toggle)
aptly 1.6.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 49,928 kB
  • sloc: python: 10,398; sh: 252; makefile: 184
file content (55 lines) | stat: -rw-r--r-- 1,441 bytes parent folder | download | duplicates (6)
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
package deb

import (
	"fmt"
	"os/exec"
	"regexp"
	"strings"

	"github.com/aptly-dev/aptly/utils"
)

var ppaRegexp = regexp.MustCompile("^ppa:([^/]+)/(.+)$")

// ParsePPA converts ppa URL like ppa:user/ppa-name to full HTTP url
func ParsePPA(ppaURL string, config *utils.ConfigStructure) (url string, distribution string, components []string, err error) {
	matches := ppaRegexp.FindStringSubmatch(ppaURL)
	if matches == nil {
		err = fmt.Errorf("unable to parse ppa URL: %v", ppaURL)
		return
	}

	distributorID := config.PpaDistributorID
	if distributorID == "" {
		distributorID, err = getDistributorID()
		if err != nil {
			err = fmt.Errorf("unable to figure out Distributor ID: %s, please set config option ppaDistributorID", err)
			return
		}
	}

	codename := config.PpaCodename
	if codename == "" {
		codename, err = getCodename()
		if err != nil {
			err = fmt.Errorf("unable to figure out Codename: %s, please set config option ppaCodename", err)
			return
		}
	}

	distribution = codename
	components = []string{"main"}
	url = fmt.Sprintf("http://ppa.launchpad.net/%s/%s/%s", matches[1], matches[2], distributorID)

	return
}

func getCodename() (string, error) {
	out, err := exec.Command("lsb_release", "-sc").Output()
	return strings.TrimSpace(string(out)), err
}

func getDistributorID() (string, error) {
	out, err := exec.Command("lsb_release", "-si").Output()
	return strings.ToLower(strings.TrimSpace(string(out))), err
}