Skip to content

Commit

Permalink
updated file structure and library
Browse files Browse the repository at this point in the history
  • Loading branch information
warrensbox committed May 21, 2018
1 parent 549e470 commit e61686e
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 212 deletions.
32 changes: 18 additions & 14 deletions lib/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,36 @@ import (
"strings"
)

// Command : type string
type Command struct {
name string
}

// NewCommand : get command
func NewCommand(name string) *Command {
return &Command{name: name}
}

func (this *Command) PathList() []string {
// PathList : get bin path list
func (cmd *Command) PathList() []string {
path := os.Getenv("PATH")
return strings.Split(path, string(os.PathListSeparator))
}

func isDir(path string) bool {
file_info, err := os.Stat(path)
fileInfo, err := os.Stat(path)
if err != nil || os.IsNotExist(err) {
return false
}
return file_info.IsDir()
return fileInfo.IsDir()
}

func isExecutable(path string) bool {
if isDir(path) {
return false
}

file_info, err := os.Stat(path)
fileInfo, err := os.Stat(path)
if err != nil || os.IsNotExist(err) {
return false
}
Expand All @@ -43,36 +46,37 @@ func isExecutable(path string) bool {
return true
}

if file_info.Mode()&0111 != 0 {
if fileInfo.Mode()&0111 != 0 {
return true
}

return false
}

func (this *Command) Find() func() string {
path_chan := make(chan string)
// Find : find all bin path
func (cmd *Command) Find() func() string {
pathChan := make(chan string)
go func() {
for _, p := range this.PathList() {
for _, p := range cmd.PathList() {
if !isDir(p) {
continue
}
file_list, err := ioutil.ReadDir(p)
fileList, err := ioutil.ReadDir(p)
if err != nil {
continue
}

for _, f := range file_list {
for _, f := range fileList {
path := filepath.Join(p, f.Name())
if isExecutable(path) && f.Name() == this.name {
path_chan <- path
if isExecutable(path) && f.Name() == cmd.name {
pathChan <- path
}
}
}
path_chan <- ""
pathChan <- ""
}()

return func() string {
return <-path_chan
return <-pathChan
}
}
40 changes: 40 additions & 0 deletions lib/download.go
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
}
109 changes: 109 additions & 0 deletions lib/files.go
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)
}
}
}
24 changes: 24 additions & 0 deletions lib/symlink.go
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
}
Loading

0 comments on commit e61686e

Please sign in to comment.