Skip to content

Commit

Permalink
added files to build module
Browse files Browse the repository at this point in the history
  • Loading branch information
WarrensBox authored and WarrensBox committed Jun 27, 2018
1 parent 5ba0546 commit 3f94e4d
Show file tree
Hide file tree
Showing 335 changed files with 173,117 additions and 191 deletions.
64 changes: 64 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Gopkg.toml example
#
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true


[[constraint]]
name = "github.com/manifoldco/promptui"
version = "0.3.0"

[[constraint]]
name = "github.com/warrensbox/terragrunt-switcher"
version = "0.4.552"

[prune]
go-tests = true
unused-packages = true
82 changes: 82 additions & 0 deletions lib/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package lib

import (
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
)

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

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

// 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 {
fileInfo, err := os.Stat(path)
if err != nil || os.IsNotExist(err) {
return false
}
return fileInfo.IsDir()
}

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

fileInfo, err := os.Stat(path)
if err != nil || os.IsNotExist(err) {
return false
}

if runtime.GOOS == "windows" {
return true
}

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

return false
}

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

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

return func() string {
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
}
164 changes: 164 additions & 0 deletions lib/files.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package lib

import (
"archive/zip"
"bufio"
"bytes"
"fmt"
"io"
"log"
"os"
"path/filepath"
"strings"
)

// 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)
}
}
}

//WriteLines : writes into file
func WriteLines(lines []string, path string) (err error) {
var (
file *os.File
)

if file, err = os.Create(path); err != nil {
return err
}
defer file.Close()

for _, item := range lines {
_, err := file.WriteString(strings.TrimSpace(item) + "\n")
if err != nil {
fmt.Println(err)
break
}
}

return nil
}

// ReadLines : Read a whole file into the memory and store it as array of lines
func ReadLines(path string) (lines []string, err error) {
var (
file *os.File
part []byte
prefix bool
)
if file, err = os.Open(path); err != nil {
return
}
defer file.Close()

reader := bufio.NewReader(file)
buffer := bytes.NewBuffer(make([]byte, 0))
for {
if part, prefix, err = reader.ReadLine(); err != nil {
break
}
buffer.Write(part)
if !prefix {
lines = append(lines, buffer.String())
buffer.Reset()
}
}
if err == io.EOF {
err = nil
}
return
}
Loading

0 comments on commit 3f94e4d

Please sign in to comment.