diff --git a/internal/app/kube_helpers.go b/internal/app/kube_helpers.go index 0d505bb3..002ca56f 100644 --- a/internal/app/kube_helpers.go +++ b/internal/app/kube_helpers.go @@ -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 diff --git a/internal/app/utils.go b/internal/app/utils.go index 6dfbe503..55867345 100644 --- a/internal/app/utils.go +++ b/internal/app/utils.go @@ -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 { @@ -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 @@ -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)