2018-03-02 23:23:13 -08:00
|
|
|
package db
|
2018-02-28 20:00:25 -08:00
|
|
|
|
|
|
|
import (
|
2018-02-28 23:20:34 -08:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2018-02-28 20:00:25 -08:00
|
|
|
"io"
|
2018-02-28 23:20:34 -08:00
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"mcquay.me/fs"
|
2018-02-28 20:00:25 -08:00
|
|
|
)
|
|
|
|
|
2018-02-28 23:20:34 -08:00
|
|
|
// DB is a slice of available URI
|
|
|
|
type DB []url.URL
|
|
|
|
|
2018-03-02 23:23:11 -08:00
|
|
|
const rn = "var/lib/pm/remotes.json"
|
2018-02-28 23:20:34 -08:00
|
|
|
|
2018-03-02 23:23:13 -08:00
|
|
|
// AddRemotes appends the provided uri to the list of configured remotes.
|
|
|
|
func AddRemotes(root string, uris []string) error {
|
2018-02-28 23:20:34 -08:00
|
|
|
db, err := load(root)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "loading")
|
|
|
|
}
|
|
|
|
|
|
|
|
dbm := map[string]bool{}
|
|
|
|
for _, u := range db {
|
|
|
|
dbm[u.String()] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, uri := range uris {
|
|
|
|
pu, err := url.Parse(uri)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "url parse")
|
|
|
|
}
|
|
|
|
|
|
|
|
u := strip(*pu)
|
|
|
|
|
|
|
|
if _, ok := dbm[u.String()]; ok {
|
|
|
|
return fmt.Errorf("%q already in db", u.String())
|
|
|
|
}
|
|
|
|
db = append(db, u)
|
|
|
|
}
|
|
|
|
|
|
|
|
return save(root, db)
|
2018-02-28 20:00:25 -08:00
|
|
|
}
|
|
|
|
|
2018-03-02 23:23:13 -08:00
|
|
|
// RemoveRemotes removes the given uri from the list of configured remotes.
|
|
|
|
func RemoveRemotes(root string, uris []string) error {
|
2018-02-28 23:20:34 -08:00
|
|
|
db, err := load(root)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "loading")
|
|
|
|
}
|
|
|
|
|
|
|
|
rms := map[string]bool{}
|
|
|
|
for _, uri := range uris {
|
|
|
|
pu, err := url.Parse(uri)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "url parse")
|
|
|
|
}
|
|
|
|
|
|
|
|
u := strip(*pu)
|
|
|
|
|
|
|
|
rms[u.String()] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
o := DB{}
|
|
|
|
for _, d := range db {
|
|
|
|
if _, ok := rms[d.String()]; !ok {
|
|
|
|
o = append(o, d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(o) == len(db) {
|
|
|
|
return errors.New("found no matching remotes")
|
|
|
|
}
|
|
|
|
|
|
|
|
return save(root, o)
|
2018-02-28 20:00:25 -08:00
|
|
|
}
|
|
|
|
|
2018-03-02 23:23:13 -08:00
|
|
|
// ListRemotes prints all configured remotes to w.
|
|
|
|
func ListRemotes(root string, w io.Writer) error {
|
2018-02-28 23:20:34 -08:00
|
|
|
db, err := load(root)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "loading")
|
|
|
|
}
|
|
|
|
for _, u := range db {
|
|
|
|
fmt.Fprintf(w, "%s\n", u.String())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func load(root string) (DB, error) {
|
|
|
|
r := DB{}
|
2018-03-02 23:23:11 -08:00
|
|
|
dbn := filepath.Join(root, rn)
|
2018-02-28 23:20:34 -08:00
|
|
|
|
|
|
|
if !fs.Exists(dbn) {
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
2018-03-02 23:23:11 -08:00
|
|
|
f, err := os.Open(filepath.Join(root, rn))
|
2018-02-28 23:20:34 -08:00
|
|
|
if err != nil {
|
|
|
|
return r, errors.Wrap(err, "open")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.NewDecoder(f).Decode(&r); err != nil {
|
|
|
|
return r, errors.Wrap(err, "decoding db")
|
|
|
|
}
|
|
|
|
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func save(root string, db DB) error {
|
2018-03-02 23:23:11 -08:00
|
|
|
f, err := os.Create(filepath.Join(root, rn))
|
2018-02-28 23:20:34 -08:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "create")
|
|
|
|
}
|
|
|
|
enc := json.NewEncoder(f)
|
|
|
|
enc.SetIndent("", "\t")
|
|
|
|
|
|
|
|
if err := enc.Encode(&db); err != nil {
|
|
|
|
return errors.Wrap(err, "decoding db")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
return errors.Wrap(err, "close db")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// strip removes all fields we don't currently need.
|
|
|
|
func strip(u url.URL) url.URL {
|
|
|
|
return url.URL{
|
|
|
|
Scheme: u.Scheme,
|
|
|
|
Host: u.Host,
|
|
|
|
Path: u.Path,
|
|
|
|
}
|
2018-02-28 20:00:25 -08:00
|
|
|
}
|
2018-02-28 23:20:34 -08:00
|
|
|
|
|
|
|
func mkdirs(root string) error {
|
2018-03-02 23:23:11 -08:00
|
|
|
d, _ := filepath.Split(filepath.Join(root, rn))
|
2018-02-28 23:20:34 -08:00
|
|
|
if !fs.Exists(d) {
|
|
|
|
if err := os.MkdirAll(d, 0700); err != nil {
|
|
|
|
return errors.Wrap(err, "mk pm dir")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|