-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
549e470
commit e61686e
Showing
5 changed files
with
210 additions
and
212 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package lib | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"os" | ||
"strings" | ||
) | ||
|
||
// DownloadFromURL : Downloads the binary from the source url | ||
func DownloadFromURL(installLocation string, url string) (string, error) { | ||
tokens := strings.Split(url, "/") | ||
fileName := tokens[len(tokens)-1] | ||
fmt.Println("Downloading", url, "to", fileName) | ||
fmt.Println("Downloading ...") | ||
// TODO: check file existence first with io.IsExist | ||
output, err := os.Create(installLocation + fileName) | ||
if err != nil { | ||
fmt.Println("Error while creating", installLocation+fileName, "-", err) | ||
return "", err | ||
} | ||
defer output.Close() | ||
|
||
response, err := http.Get(url) | ||
if err != nil { | ||
fmt.Println("Error while downloading", url, "-", err) | ||
return "", err | ||
} | ||
defer response.Body.Close() | ||
|
||
n, err := io.Copy(output, response.Body) | ||
if err != nil { | ||
fmt.Println("Error while downloading", url, "-", err) | ||
return "", err | ||
} | ||
|
||
fmt.Println(n, "bytes downloaded.") | ||
return installLocation + fileName, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package lib | ||
|
||
import ( | ||
"archive/zip" | ||
"fmt" | ||
"io" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
// RenameFile : rename file name | ||
func RenameFile(src string, dest string) { | ||
err := os.Rename(src, dest) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
} | ||
|
||
// RemoveFiles : remove file | ||
func RemoveFiles(src string) { | ||
files, err := filepath.Glob(src) | ||
if err != nil { | ||
|
||
panic(err) | ||
} | ||
for _, f := range files { | ||
if err := os.Remove(f); err != nil { | ||
panic(err) | ||
} | ||
} | ||
} | ||
|
||
// CheckFileExist : check if file exist in directory | ||
func CheckFileExist(file string) bool { | ||
_, err := os.Stat(file) | ||
if err != nil { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
// Unzip will decompress a zip archive, moving all files and folders | ||
// within the zip file (parameter 1) to an output directory (parameter 2). | ||
func Unzip(src string, dest string) ([]string, error) { | ||
|
||
var filenames []string | ||
|
||
r, err := zip.OpenReader(src) | ||
if err != nil { | ||
return filenames, err | ||
} | ||
defer r.Close() | ||
|
||
for _, f := range r.File { | ||
|
||
rc, err := f.Open() | ||
if err != nil { | ||
return filenames, err | ||
} | ||
defer rc.Close() | ||
|
||
// Store filename/path for returning and using later on | ||
fpath := filepath.Join(dest, f.Name) | ||
filenames = append(filenames, fpath) | ||
|
||
if f.FileInfo().IsDir() { | ||
|
||
// Make Folder | ||
os.MkdirAll(fpath, os.ModePerm) | ||
|
||
} else { | ||
|
||
// Make File | ||
if err = os.MkdirAll(filepath.Dir(fpath), os.ModePerm); err != nil { | ||
return filenames, err | ||
} | ||
|
||
outFile, err := os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode()) | ||
if err != nil { | ||
return filenames, err | ||
} | ||
|
||
_, err = io.Copy(outFile, rc) | ||
|
||
// Close the file without defer to close before next iteration of loop | ||
outFile.Close() | ||
|
||
if err != nil { | ||
return filenames, err | ||
} | ||
|
||
} | ||
} | ||
return filenames, nil | ||
} | ||
|
||
//CreateDirIfNotExist : create directory if directory does not exist | ||
func CreateDirIfNotExist(dir string) { | ||
if _, err := os.Stat(dir); os.IsNotExist(err) { | ||
log.Printf("Creating directory for teraform: %v", dir) | ||
err = os.MkdirAll(dir, 0755) | ||
if err != nil { | ||
fmt.Printf("Unable to create directory for teraform: %v", dir) | ||
panic(err) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package lib | ||
|
||
import ( | ||
"os" | ||
) | ||
|
||
//CreateSymlink : create symlink | ||
func CreateSymlink(cwd string, dir string) error { | ||
|
||
if err := os.Symlink(cwd, dir); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
//RemoveSymlink : remove symlink | ||
func RemoveSymlink(symlinkPath string) error { | ||
|
||
if _, err := os.Lstat(symlinkPath); err != nil { | ||
return err | ||
} | ||
os.Remove(symlinkPath) | ||
return nil | ||
} |
Oops, something went wrong.