Adds pm pull
This commit is contained in:
+57
-2
@@ -1,8 +1,63 @@
|
||||
package remote
|
||||
|
||||
import "github.com/pkg/errors"
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"mcquay.me/pm"
|
||||
)
|
||||
|
||||
const an = "var/lib/pm/available.json"
|
||||
|
||||
// Pull updates the available package database.
|
||||
func Pull(root string) error {
|
||||
return errors.New("NYI")
|
||||
db, err := load(root)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "loading db")
|
||||
}
|
||||
|
||||
o := pm.Available{}
|
||||
|
||||
// Order here is important: the guarantee made is that any packages that
|
||||
// exist in multiple remotes will be fetched by the first configured
|
||||
// remote, which is why we traverse the database in reverse.
|
||||
//
|
||||
// TODO (sm): make this concurrent
|
||||
for i := range db {
|
||||
u := db[len(db)-i-1]
|
||||
resp, err := http.Get(u.String() + "/available.json")
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "http get")
|
||||
}
|
||||
|
||||
a := pm.Available{}
|
||||
if err := json.NewDecoder(resp.Body).Decode(&a); err != nil {
|
||||
return errors.Wrap(err, "decode remote available")
|
||||
}
|
||||
a.SetRemote(u)
|
||||
o.Update(a)
|
||||
}
|
||||
if err := saveAvailable(root, o); err != nil {
|
||||
return errors.Wrap(err, "saving available db")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func saveAvailable(root string, db pm.Available) error {
|
||||
f, err := os.Create(filepath.Join(root, an))
|
||||
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
|
||||
}
|
||||
|
||||
+5
-5
@@ -15,7 +15,7 @@ import (
|
||||
// DB is a slice of available URI
|
||||
type DB []url.URL
|
||||
|
||||
const fn = "var/lib/pm/available.json"
|
||||
const rn = "var/lib/pm/remotes.json"
|
||||
|
||||
// Add appends the provided uri to the list of configured remotes.
|
||||
func Add(root string, uris []string) error {
|
||||
@@ -93,13 +93,13 @@ func List(root string, w io.Writer) error {
|
||||
|
||||
func load(root string) (DB, error) {
|
||||
r := DB{}
|
||||
dbn := filepath.Join(root, fn)
|
||||
dbn := filepath.Join(root, rn)
|
||||
|
||||
if !fs.Exists(dbn) {
|
||||
return r, nil
|
||||
}
|
||||
|
||||
f, err := os.Open(filepath.Join(root, fn))
|
||||
f, err := os.Open(filepath.Join(root, rn))
|
||||
if err != nil {
|
||||
return r, errors.Wrap(err, "open")
|
||||
}
|
||||
@@ -112,7 +112,7 @@ func load(root string) (DB, error) {
|
||||
}
|
||||
|
||||
func save(root string, db DB) error {
|
||||
f, err := os.Create(filepath.Join(root, fn))
|
||||
f, err := os.Create(filepath.Join(root, rn))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "create")
|
||||
}
|
||||
@@ -140,7 +140,7 @@ func strip(u url.URL) url.URL {
|
||||
}
|
||||
|
||||
func mkdirs(root string) error {
|
||||
d, _ := filepath.Split(filepath.Join(root, fn))
|
||||
d, _ := filepath.Split(filepath.Join(root, rn))
|
||||
if !fs.Exists(d) {
|
||||
if err := os.MkdirAll(d, 0700); err != nil {
|
||||
return errors.Wrap(err, "mk pm dir")
|
||||
|
||||
Reference in New Issue
Block a user