Skip to content

Commit

Permalink
Merge pull request #7 from perriea/readme
Browse files Browse the repository at this point in the history
Readme
  • Loading branch information
perriea authored Apr 30, 2017
2 parents c832669 + 3b42a76 commit e70c526
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 66 deletions.
32 changes: 1 addition & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,10 @@ Functional started from version 0.7.
- Build `go build .`,
- Add in your `.bashrc` (Linux) or `.bash_profile` : `export PATH=$PATH:~/terraform/bin`

## Usage

### Install version

**Example :**
`tfversion --version 0.9.4`

**Result :**
``` shell
aurelien@localhost:~ tfversion --version 0.9.4
Attempting to download version: 0.9.4
Start download ...
Unzip file ...
Install the binary file ...
Installed 0.9.4, Thanks ! ♥
```

### List versions

**Example :**
`tfversion --list`

**Result :**
``` shell
tfversion --list
Versions availables of terraform (tfversion support <= 0.7) :
[0.9.4 0.9.3 0.9.2 0.9.1 0.9.0 0.9.0-beta2 0.9.0-beta1 0.8.8 0.8.7 0.8.6 0.8.5 0.8.4 0.8.3 0.8.2 0.8.1 0.8.0 0.8.0-rc3 0.8.0-rc2 0.8.0-rc1 0.8.0-beta2 0.8.0-beta1 0.7.13 0.7.12 0.7.11 0.7.10 0.7.9 0.7.8 0.7.7 0.7.6 0.7.5 0.7.4 0.7.3 0.7.2 0.7.1 0.7.0 0.7.0-rc4 0.7.0-rc3 0.7.0-rc2 0.7.0-rc1 0.6.16 0.6.15 0.6.14 0.6.13 0.6.12 0.6.11 0.6.10 0.6.9 0.6.8 0.6.7 0.6.6 0.6.5 0.6.4 0.6.3 0.6.2 0.6.1 0.6.0 0.5.3 0.5.1 0.5.0 0.4.2 0.4.1 0.4.0 0.3.7 0.3.6 0.3.5 0.3.1 0.3.0 0.2.2 0.2.1 0.2.0 0.1.1 0.1.0]
```

## Roadmap

- Switch between version without internet (if the zip archive is allready in folder tmp),
- List the local versions of terraform,
- Command cleanup tmp folder,
- List the local versions of terraform and **show curent version**,
- Install script.

And other things ...
Expand Down
2 changes: 2 additions & 0 deletions error/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ func Run(level int, message string) {
good.Println(err)
case 2:
warn.Println(err)
case 3:
fatal.Println(err)
default:
fmt.Println(err)
}
Expand Down
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var (
err error
// Flag func version param
path_bin string
tmp_bin string
path_tmp string
version string
// Flag launch func List
list_online bool
Expand All @@ -33,13 +33,13 @@ func init() {

// Paths
path_bin = "/terraform/bin/"
tmp_bin = "/terraform/tmp/"
path_tmp = "/terraform/tmp/"

// Flags CLI
flag.BoolVar(&list_online, "list-online", false, "List online version of terraform")
flag.BoolVar(&list_offline, "list-offline", false, "List local version of terraform")
flag.BoolVar(&cleanup, "cleanup", false, "Clean cache (tmp files)")
flag.StringVar(&version, "version", "0", "Version of terraform to install or switch")
flag.StringVar(&version, "install", "0", "Version of terraform to install or switch")
flag.Parse()
}

Expand All @@ -63,7 +63,7 @@ func main() {

if err_network {
// Lauch Terraform download
tffolder.Run(tmp_bin, 0755)
tffolder.Run(path_tmp, 0755)
check = tfdownload.Run(version)

// Check if download is done and install
Expand Down
63 changes: 32 additions & 31 deletions terraform/download/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ var (
client *http.Client
resp *http.Response
// Errors
err_fatal_msg string
err_msg string
err error
)

Expand All @@ -43,50 +41,53 @@ func init() {
usr, err := user.Current()
tferror.Panic(err)
do_path_tf = usr.HomeDir + "/terraform/tmp/terraform-%s.zip"

// Color response
err_fatal_msg = "[FATAL] Download impossible, this version doesn't exist !"
err_msg = "[ERROR] This version (%s) is not supported !"
}

func Run(version string) bool {

match, err := regexp.MatchString("[0-9]+\\.[0-6]+\\.[0-9]+(-(rc|beta)[0-9]+)?", version)
tferror.Panic(err)

if !match {
// Formulation URL Terraform Website
fmt.Printf("Attempting to download version: %s\n", version)
url_tf = fmt.Sprintf(do_url_tf, version, version, runtime.GOOS, runtime.GOARCH)
if _, err := os.Stat(fmt.Sprintf(do_path_tf, version)); os.IsNotExist(err) {

// Request GET URL
resp, err = client.Get(url_tf)
match, err := regexp.MatchString("[0-9]+\\.[0-6]+\\.[0-9]+(-(rc|beta)[0-9]+)?", version)
tferror.Panic(err)
defer resp.Body.Close()

// Verify code equal 200
if (err == nil) && (resp.StatusCode == 200) {
tferror.Run(1, "Start download ...")
path_tf = fmt.Sprintf(do_path_tf, version)
file_unzip, err = os.Create(path_tf)
tferror.Panic(err)
defer file_unzip.Close()
if !match {
// Formulation URL Terraform Website
fmt.Printf("Attempting to download version: %s\n", version)
url_tf = fmt.Sprintf(do_url_tf, version, version, runtime.GOOS, runtime.GOARCH)

// Copy reponse in file
_, err = io.Copy(file_unzip, resp.Body)
// Request GET URL
resp, err = client.Get(url_tf)
tferror.Panic(err)
defer resp.Body.Close()

// Verify code equal 200
if (err == nil) && (resp.StatusCode == 200) {
tferror.Run(1, "Start download ...")
path_tf = fmt.Sprintf(do_path_tf, version)
file_unzip, err = os.Create(path_tf)
tferror.Panic(err)
defer file_unzip.Close()

// Copy reponse in file
_, err = io.Copy(file_unzip, resp.Body)
tferror.Panic(err)

return true
return true

} else {
tferror.Run(3, "[ERROR] Download impossible, this version doesn't exist !")
return false
}

} else {
tferror.Run(3, err_fatal_msg)
tferror.Run(3, fmt.Sprintf("This version (%s) is not supported !", version))
return false
}

} else {
tferror.Run(3, err_msg)
return false
}

return false
} else {
tferror.Run(0, "Already in cache ...")
return true
}
}

0 comments on commit e70c526

Please sign in to comment.