Skip to content

Commit

Permalink
do not "download" local files
Browse files Browse the repository at this point in the history
  • Loading branch information
justenwalker committed Apr 29, 2021
1 parent 4dfbac7 commit 26e12eb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 28 deletions.
2 changes: 1 addition & 1 deletion internal/app/kube_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func createContext(s *state) error {
// bearer token
tokenPath := "bearer.token"
if s.Settings.BearerToken && s.Settings.BearerTokenPath != "" {
downloadFile(s.Settings.BearerTokenPath, "", tokenPath)
tokenPath = downloadFile(s.Settings.BearerTokenPath, "", tokenPath)
}

// connecting to the cluster
Expand Down
39 changes: 12 additions & 27 deletions internal/app/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,9 @@ func sliceContains(slice []string, s string) bool {
return false
}

// downloadFile downloads a file from a URL, GCS, Azure or AWS buckets or local file system
// and saves it with a given outfile name and in a given dir
// if downloaded, returns the outfile name. If the file path is local file system path, it is copied to current directory.
// downloadFile downloads a file from a URL, GCS, Azure or AWS buckets and saves it with a
// given outfile name and in a given dir
// If the file path is local file system path, it returns the absolute path to the file
func downloadFile(file string, dir string, outfile string) string {
u, err := url.Parse(file)
if err != nil {
Expand All @@ -220,12 +220,17 @@ func downloadFile(file string, dir string, outfile string) string {
case "az":
azure.ReadFile(u.Host, u.Path, outfile, flags.noColors)
default:
log.Verbose(file + " will be used from local file system.")
toCopy := file
if !filepath.IsAbs(file) {
toCopy, _ = filepath.Abs(filepath.Join(dir, file))
file, err = filepath.Abs(filepath.Join(dir, file))
if err != nil {
log.Fatal("could not get absolute path to " + file + " : " + err.Error())
}
}
log.Verbose(file + " will be used from local file system.")
if _, err = os.Stat(file); err != nil {
log.Fatal("could not stat " + file + " : " + err.Error())
}
copyFile(toCopy, outfile)
return file
}

return outfile
Expand All @@ -252,26 +257,6 @@ func downloadFileFromURL(url string, filepath string) error {
return err
}

// copyFile copies a file from source to destination
func copyFile(source string, destination string) {
from, err := os.Open(source)
if err != nil {
log.Fatal("while copying " + source + " to " + destination + " : " + err.Error())
}
defer from.Close()

to, err := os.OpenFile(destination, os.O_RDWR|os.O_CREATE, 0o666)
if err != nil {
log.Fatal("while copying " + source + " to " + destination + " : " + err.Error())
}
defer to.Close()

_, err = io.Copy(to, from)
if err != nil {
log.Fatal("while copying " + source + " to " + destination + " : " + err.Error())
}
}

// deleteFile deletes a file
func deleteFile(path string) {
log.Info("Cleaning up... deleting " + path)
Expand Down

0 comments on commit 26e12eb

Please sign in to comment.