pm install downloads packages

This commit is contained in:
Stephen McQuay 2018-03-03 22:52:01 -08:00
parent f3c3ce11a4
commit 68942b6cdd
Signed by: sm
GPG Key ID: 4E4B72F479BA3CE5
2 changed files with 52 additions and 4 deletions

View File

@ -29,9 +29,14 @@ func (m Meta) Valid() (bool, error) {
return true, nil
}
// Pkg returns the string name the .pkg should have on disk.
func (m Meta) Pkg() string {
return fmt.Sprintf("%s-%s.pkg", m.Name, m.Version)
}
// URL returns the http location of this package.
func (m Meta) URL() string {
return fmt.Sprintf("%s/%s-%s.pkg", m.Remote.String(), m.Name, m.Version)
return fmt.Sprintf("%s/%s", m.Remote.String(), m.Pkg())
}
func (m Meta) String() string {

View File

@ -1,12 +1,19 @@
package pkg
import (
"log"
"io"
"net/http"
"os"
"path/filepath"
"github.com/pkg/errors"
"mcquay.me/fs"
"mcquay.me/pm"
"mcquay.me/pm/db"
)
const cache = "var/cache/pm"
// Install fetches and installs pkgs from appropriate remotes.
func Install(root string, pkgs []string) error {
av, err := db.LoadAvailable(root)
@ -18,8 +25,44 @@ func Install(root string, pkgs []string) error {
if err != nil {
return errors.Wrap(err, "checking ability to install")
}
for _, m := range ms {
log.Printf("fake install %v", m)
cacheDir := filepath.Join(root, cache)
if !fs.Exists(cacheDir) {
if err := os.MkdirAll(cacheDir, 0755); err != nil {
return errors.Wrap(err, "creating non-existent cache dir")
}
}
if !fs.IsDir(cacheDir) {
return errors.Errorf("%q is not a directory!", cacheDir)
}
if err := download(cacheDir, ms); err != nil {
return errors.Wrap(err, "downloading")
}
return errors.New("NYI")
}
func download(cache string, ms pm.Metas) error {
// TODO (sm): concurrently fetch
for _, m := range ms {
resp, err := http.Get(m.URL())
if err != nil {
return errors.Wrap(err, "http get")
}
fn := filepath.Join(cache, m.Pkg())
f, err := os.Create(fn)
if err != nil {
return errors.Wrap(err, "creating")
}
if n, err := io.Copy(f, resp.Body); err != nil {
return errors.Wrapf(err, "copy %q to disk after %d bytes", m.URL(), n)
}
if err := resp.Body.Close(); err != nil {
return errors.Wrap(err, "closing resp body")
}
}
return nil
}