mirror of
https://github.com/mudler/luet.git
synced 2025-08-31 14:52:02 +00:00
Add http client for installer #3
This commit is contained in:
@@ -61,7 +61,6 @@ func Execute() {
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
cobra.OnInitialize(initConfig)
|
||||
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.luet.yaml)")
|
||||
RootCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "verbose output")
|
||||
|
1
go.mod
1
go.mod
@@ -8,6 +8,7 @@ require (
|
||||
github.com/Sabayon/pkgs-checker v0.4.1
|
||||
github.com/asdine/storm v0.0.0-20190418133842-e0f77eada154
|
||||
github.com/briandowns/spinner v1.7.0
|
||||
github.com/cavaliercoder/grab v2.0.0+incompatible
|
||||
github.com/crillab/gophersat v1.1.7
|
||||
github.com/docker/docker v0.7.3-0.20180827131323-0c5f8d2b9b23
|
||||
github.com/ghodss/yaml v1.0.0
|
||||
|
2
go.sum
2
go.sum
@@ -35,6 +35,8 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24
|
||||
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
|
||||
github.com/briandowns/spinner v1.7.0 h1:aan1hBBOoscry2TXAkgtxkJiq7Se0+9pt+TUWaPrB4g=
|
||||
github.com/briandowns/spinner v1.7.0/go.mod h1://Zf9tMcxfRUA36V23M6YGEAv+kECGfvpnLTnb8n4XQ=
|
||||
github.com/cavaliercoder/grab v2.0.0+incompatible h1:wZHbBQx56+Yxjx2TCGDcenhh3cJn7cCLMfkEPmySTSE=
|
||||
github.com/cavaliercoder/grab v2.0.0+incompatible/go.mod h1:tTBkfNqSBfuMmMBFaO2phgyhdYhiZQ/+iXCZDzcDsMI=
|
||||
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
|
||||
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
|
||||
github.com/containerd/containerd v1.2.4 h1:qN8LCvw+KA5wVCOnHspD/n2K9cJ34+YOs05qBBWhHiw=
|
||||
|
97
pkg/installer/client/http.go
Normal file
97
pkg/installer/client/http.go
Normal file
@@ -0,0 +1,97 @@
|
||||
// Copyright © 2019 Ettore Di Giacinto <mudler@gentoo.org>
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation; either version 2 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License along
|
||||
// with this program; if not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package client
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
||||
. "github.com/mudler/luet/pkg/logger"
|
||||
|
||||
"github.com/mudler/luet/pkg/compiler"
|
||||
"github.com/mudler/luet/pkg/helpers"
|
||||
|
||||
"github.com/cavaliercoder/grab"
|
||||
)
|
||||
|
||||
type HttpClient struct {
|
||||
RepoData RepoData
|
||||
}
|
||||
|
||||
func NewHttpClient(r RepoData) *HttpClient {
|
||||
return &HttpClient{RepoData: r}
|
||||
}
|
||||
|
||||
func (c *HttpClient) DownloadArtifact(artifact compiler.Artifact) (compiler.Artifact, error) {
|
||||
artifactName := path.Base(artifact.GetPath())
|
||||
Info("Downloading artifact", artifactName, "from", c.RepoData.Uri)
|
||||
|
||||
temp, err := ioutil.TempDir(os.TempDir(), "tree")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
file, err := ioutil.TempFile(temp, "HttpClient")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
u, err := url.Parse(c.RepoData.Uri)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
u.Path = path.Join(u.Path, artifactName)
|
||||
|
||||
_, err = grab.Get(temp, u.String())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = helpers.CopyFile(filepath.Join(temp, artifactName), file.Name())
|
||||
|
||||
return compiler.NewPackageArtifact(file.Name()), nil
|
||||
}
|
||||
|
||||
func (c *HttpClient) DownloadFile(name string) (string, error) {
|
||||
temp, err := ioutil.TempDir(os.TempDir(), "tree")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
file, err := ioutil.TempFile(os.TempDir(), "HttpClient")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
//defer os.Remove(file.Name())
|
||||
u, err := url.Parse(c.RepoData.Uri)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
u.Path = path.Join(u.Path, name)
|
||||
|
||||
Info("Downloading", u.String())
|
||||
|
||||
_, err = grab.Get(temp, u.String())
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
err = helpers.CopyFile(filepath.Join(temp, name), file.Name())
|
||||
|
||||
return file.Name(), err
|
||||
}
|
@@ -192,6 +192,8 @@ func (r *LuetRepository) Client() Client {
|
||||
switch r.GetType() {
|
||||
case "local":
|
||||
return client.NewLocalClient(client.RepoData{Uri: r.GetUri()})
|
||||
case "http":
|
||||
return client.NewHttpClient(client.RepoData{Uri: r.GetUri()})
|
||||
}
|
||||
|
||||
return nil
|
||||
|
Reference in New Issue
Block a user