Skip to content
Snippets Groups Projects
Commit c9b0aae1 authored by Tamer Tas's avatar Tamer Tas
Browse files

pkg/cmd/download: remove zip/download/extract implementation

parent 71b4d0a9
No related branches found
No related tags found
No related merge requests found
package cmd
import (
"archive/zip"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
cli "github.com/spf13/cobra"
"github.com/tmrts/boilr/pkg/boilr"
"github.com/tmrts/boilr/pkg/cmd/util"
"github.com/tmrts/boilr/pkg/host"
"github.com/tmrts/boilr/pkg/util/exit"
"github.com/tmrts/boilr/pkg/util/git"
......@@ -22,78 +15,6 @@ import (
"github.com/tmrts/boilr/pkg/util/validate"
)
func downloadZip(URL, targetDir string) error {
f, err := ioutil.TempFile("", "boilr-download")
if err != nil {
return err
}
defer f.Close()
defer os.RemoveAll(f.Name())
resp, err := http.Get(URL)
if err != nil {
return err
}
defer resp.Body.Close()
if _, err := io.Copy(f, resp.Body); err != nil {
return err
}
if err := f.Close(); err != nil {
return err
}
r, err := zip.OpenReader(f.Name())
if err != nil {
return err
}
defer r.Close()
extractFile := func(f *zip.File, dest string) error {
rc, err := f.Open()
if err != nil {
return err
}
defer rc.Close()
// splits the first token of f.Name since it's zip file name
path := filepath.Join(dest, strings.SplitAfterN(f.Name, "/", 2)[1])
if f.FileInfo().IsDir() {
err := os.MkdirAll(path, f.Mode())
if err != nil {
return err
}
} else {
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return err
}
defer f.Close()
if _, err := io.Copy(f, rc); err != nil {
return err
}
}
return nil
}
for _, f := range r.File {
if err := extractFile(f, targetDir); err != nil {
return err
}
}
// TODO Wrap this function in a validation wrapper from top to bottom
if _, err := util.ValidateTemplate(targetDir); err != nil {
return err
}
return nil
}
// Download contains the cli-command for downloading templates from github.
var Download = &cli.Command{
Use: "download <template-repo> <template-tag>",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment