Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: doesnt stop progress if tools folder already exists #156

Merged
merged 3 commits into from
Jul 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 19 additions & 60 deletions internal/downloadManager/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"archive/tar"
"archive/zip"
"compress/gzip"
"errors"
"fmt"
"github.com/kubefirst/kubefirst/configs"
"github.com/kubefirst/kubefirst/pkg"
"io"
"io/fs"
"log"
"net/http"
"os"
Expand All @@ -17,22 +19,24 @@ import (

func DownloadTools(config *configs.Config, trackers map[string]*pkg.ActionTracker) error {

toolsDir := fmt.Sprintf("%s/tools", config.K1FolderPath)
toolsDirPath := fmt.Sprintf("%s/tools", config.K1FolderPath)

err := os.Mkdir(toolsDir, 0777)
if err != nil {
log.Printf("error creating directory %s, error is: %s\n", toolsDir, err)
// create folder if it doesn't exist
if _, err := os.Stat(toolsDirPath); errors.Is(err, fs.ErrNotExist) {
err = os.Mkdir(toolsDirPath, 0777)
if err != nil {
return err
}
}

kubectlVersion := config.KubectlVersion
kubectlDownloadUrl := fmt.Sprintf(
"https://dl.k8s.io/release/%s/bin/%s/%s/kubectl",
kubectlVersion,
config.KubectlVersion,
config.LocalOs,
config.LocalArchitecture,
)

err = downloadFile(config.KubectlClientPath, kubectlDownloadUrl)
err := downloadFile(config.KubectlClientPath, kubectlDownloadUrl)
if err != nil {
return err
}
Expand Down Expand Up @@ -86,7 +90,7 @@ func DownloadTools(config *configs.Config, trackers map[string]*pkg.ActionTracke
if err != nil {
return err
}
os.RemoveAll(fmt.Sprintf("%s/terraform.zip", toolsDir))
os.RemoveAll(fmt.Sprintf("%s/terraform.zip", toolsDirPath))

trackers[pkg.DownloadDependencies].Tracker.Increment(int64(1))

Expand Down Expand Up @@ -136,27 +140,28 @@ func DownloadTools(config *configs.Config, trackers map[string]*pkg.ActionTracke
return nil
}

func downloadFile(filepath string, url string) (err error) {
// Create the file
out, err := os.Create(filepath)
// downloadFile Downloads a file from the "url" parameter, localFilename is the file destination in the local machine.
func downloadFile(localFilename string, url string) error {
// create local file
out, err := os.Create(localFilename)
if err != nil {
return err
}
defer out.Close()

// Get the data
// get data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()

// Check server response
// check server response
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("bad status: %s", resp.Status)
}

// Writer the body to file
// writer the body to the file
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
Expand Down Expand Up @@ -204,52 +209,6 @@ func extractFileFromTarGz(gzipStream io.Reader, tarAddress string, targetFilePat
}
}

func extractTarGz(gzipStream io.Reader) {
uncompressedStream, err := gzip.NewReader(gzipStream)
if err != nil {
log.Fatal("extractTarGz: NewReader failed")
}

tarReader := tar.NewReader(uncompressedStream)

for {
header, err := tarReader.Next()

if err == io.EOF {
break
}
if err != nil {
log.Println("extractTarGz: Next() failed: %s", err.Error())
}
p, _ := filepath.Abs(header.Name)
if !strings.Contains(p, "..") {

switch header.Typeflag {
case tar.TypeDir:
if err := os.Mkdir(header.Name, 0755); err != nil {
log.Println("extractTarGz: Mkdir() failed: %s", err.Error())
}
case tar.TypeReg:
outFile, err := os.Create(header.Name)
if err != nil {
log.Println("extractTarGz: Create() failed: %s", err.Error())
}
if _, err := io.Copy(outFile, tarReader); err != nil {
log.Println("extractTarGz: Copy() failed: %s", err.Error())
}
outFile.Close()

default:
log.Println(
"extractTarGz: uknown type: %s in %s",
header.Typeflag,
header.Name)
}
}

}
}

func unzip(zipFilepath string, unzipDirectory string) {
dst := unzipDirectory
archive, err := zip.OpenReader(zipFilepath)
Expand Down
4 changes: 3 additions & 1 deletion pkg/common.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package pkg

import "net/http"
import (
"net/http"
)

type HTTPDoer interface {
Do(req *http.Request) (*http.Response, error)
Expand Down