Skip to content

Commit

Permalink
Merge pull request #15 from perriea/v0.0.4
Browse files Browse the repository at this point in the history
V0.1.0
  • Loading branch information
perriea authored May 13, 2017
2 parents 148a3be + 7e78852 commit 2e0eedc
Show file tree
Hide file tree
Showing 132 changed files with 84,178 additions and 331 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ RUN apk -Uuv add groff less python py-pip py-virtualenv git openssh make && \

RUN cd $tfversion_path && \
go build . && \
cp $tfversion_path/tfversion $terraform_path
cp $tfversion_path/tfversion /go/bin

VOLUME ['/root/.aws', '/root/.ssh']

WORKDIR /root/repo

RUN tfversion --install 0.9.4
RUN tfversion install 0.9.4
18 changes: 8 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# tfversion

`tfversion` is a command created to switch between different versions of [terraform](https://www.terraform.io).
Functional started from version 0.7.
Functional for all versions.

## Build Project

Expand All @@ -21,24 +21,22 @@ Functional started from version 0.7.
### Install

Pull image `docker pull perriea/tfversion`.
Execute command in the terminal : `docker run -it -v ~/.aws:/root/.aws -v ~/.ssh:/root/.ssh perriea/tfversion sh`.

Execute command in the terminal : `docker run -it -v ~/.aws:/root/.aws -v ~/.ssh:/root/.ssh perriea/tfversion sh`.

## Roadmap

- List the local versions of terraform and **show curent version**,

- Install script.

And other things ...

## Dependancies

- [kardianos/govendor](https://github.com/kardianos/govendor),
- [fatih/color](https://github.com/fatih/color).
- [fatih/color](https://github.com/fatih/color),
- [google/go-github](https://github.com/google/go-github),
- [aws/aws-sdk-go](https://github.com/aws/aws-sdk-go) (modules: Session, EC2, AWSErr).

## License

The MIT License (MIT)

Copyright (c) 2017 Aurelien Perrier

The MIT License (MIT)
Copyright (c) 2017 Aurelien PERRIER
31 changes: 0 additions & 31 deletions actions.go

This file was deleted.

51 changes: 51 additions & 0 deletions cloud/aws/session.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package aws

import (
"fmt"

"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/perriea/tfversion/error"
)

var (
sess *session.Session
ec2svc *ec2.EC2
err error
)

func TestConnect() {

// The SDK has support for the shared configuration file (~/.aws/config)
// Create a session to share configuration, and load external configuration.
sess = session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))

params := &ec2.DescribeInstancesInput{}

// Create the service's client with the session.
ec2svc = ec2.New(sess)
_, err := ec2svc.DescribeInstances(params)

if err != nil {
tferror.Run(2, "[WARN] Your AWS access is not correct")
if awsErr, ok := err.(awserr.Error); ok {

if reqErr, ok := err.(awserr.RequestFailure); ok {
// A service error occurred
fmt.Printf("%s : %s (%s)", awsErr.Code(), awsErr.Message(), reqErr.RequestID())
} else {
// Generic AWS Error with Code, Message, and original error (if any)
fmt.Printf("%s : %s\n%s", awsErr.Code(), awsErr.Message(), awsErr.OrigErr())
}

} else {
tferror.Panic(err)
}

} else {
tferror.Run(1, "Your AWS access is correct")
}
}
44 changes: 44 additions & 0 deletions cloud/cloud.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package tfcloud

import (
"flag"
"fmt"

"github.com/perriea/tfversion/cloud/aws"
)

var (
testaws bool
testgcp bool
testazure bool
err error
)

func Run(params []string) error {

cloud := flag.NewFlagSet("cloud", flag.ExitOnError)
cloud.BoolVar(&testaws, "aws", false, "Test connection on AWS)")
cloud.BoolVar(&testgcp, "gcp", false, "Test connection on GCP)")
cloud.BoolVar(&testazure, "azure", false, "Test connection on Azure)")
cloud.Parse(params)

if testaws && testgcp && testazure {
return fmt.Errorf("--aws, --gcp and --azure are mutually exclusive")
}

if len(params) != 1 {
return fmt.Errorf("Too many arguments ...")
}

if testaws {
aws.TestConnect()
} else if testgcp {
return fmt.Errorf("GCP test is not actived")
} else if testazure {
return fmt.Errorf("Azure test is not actived")
} else {
return fmt.Errorf("This provider doesn't exist !")
}

return nil
}
25 changes: 16 additions & 9 deletions github/list_releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@ import (

"github.com/google/go-github/github"
"github.com/perriea/tfversion/error"
"github.com/perriea/tfversion/system/network"
)

var (
releases []*github.RepositoryRelease
ctx context.Context
client *github.Client
err error
releases []*github.RepositoryRelease
ctx context.Context
client *github.Client
errNetwork bool
err error
)

func init() {
ctx = context.Background()
client = github.NewClient(nil)
errNetwork = false
}

func ListReleases() []*github.RepositoryRelease {
Expand All @@ -29,11 +32,15 @@ func ListReleases() []*github.RepositoryRelease {

func Lastversion(version string) (bool, *github.RepositoryRelease) {

releases = ListReleases()
// check if the internet connection is active
errNetwork = tfnetwork.Run()
if errNetwork {
releases = ListReleases()

if *releases[0].TagName == version {
return true, releases[0]
if *releases[0].TagName == version {
return true, releases[0]
}
return false, releases[0]
}

return false, releases[0]
return false, nil
}
122 changes: 61 additions & 61 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,87 +1,87 @@
package main

import (
"flag"
"fmt"
"os"

"github.com/perriea/tfversion/cloud"
"github.com/perriea/tfversion/error"
"github.com/perriea/tfversion/system/folders"
"github.com/perriea/tfversion/system/network"
"github.com/perriea/tfversion/terraform/download"
"github.com/perriea/tfversion/github"
"github.com/perriea/tfversion/terraform/install"
"github.com/perriea/tfversion/terraform/list"
"github.com/perriea/tfversion/terraform/uninstall"
)

type command struct {
desc string
usage string
Func cmdHandler
}

var commands = map[string]command{
// list commands
"install": command{"Install new versions or switch.", "[0.8.8 version of terraform]", tfinstall.Run},
"uninstall": command{"Clean cache (tmp files).", "[-a all], [-v version specific]", tfuninstall.Run},
"list": command{"List online or offline version of terraform.", "[-on list online], [-off list local]", tflist.Run},
"cloud": command{"Action cloud (Beta)", "[--aws test AWS]", tfcloud.Run},
}

var (
// Errors
errNetwork bool
check bool
err error
// Flag func version param
pathBin string
pathTmp string
install string
// Flag launch func List
listOnline bool
listOffline bool
cleanup bool
version string
err error
)

func init() {
// Simply error
check = false
errNetwork = false
// Paths
pathBin = "/terraform/bin/"
pathTmp = "/terraform/tmp/"
// Flags CLI
flag.BoolVar(&listOnline, "liston", false, "List online version of terraform")
flag.BoolVar(&listOffline, "listoff", false, "List local version of terraform")
flag.BoolVar(&cleanup, "cleanup", false, "Clean cache (tmp files)")
flag.StringVar(&install, "install", "0", "Version of terraform to install or switch")
flag.Parse()
// version app
version = "0.1.0"
}

func main() {
// Check if internet is available (releases.hashicorp.com)
errNetwork = tfnetwork.Run()
if listOnline == true {

if errNetwork {
// Show version terraform
tflist.ListOn()
} else {
// No network
tferror.Run(2, "[ERROR] No internet connection ...")
}
type cmdHandler func([]string) error

} else if listOffline == true {
// List all versions local
tflist.ListOff()
func doHelp() error {

} else if install != "0" {
keys := make([]string, 0, len(commands))
for k := range commands {
keys = append(keys, k)
}

if errNetwork {
// Lauch Terraform download
tffolder.CreateFolder(pathTmp, 0755)
check = tfdownload.Run(install)
fmt.Printf("tfversion v%s\n\n", version)
fmt.Printf("Usage: tfversion <command> [args]\n\n")
fmt.Printf("Common commands:\n")

// Check if download is done and install
if check {
tffolder.CreateFolder(pathBin, 0755)
tfinstall.Run(install)
}
for _, k := range keys {
fmt.Printf("%10s: %s\n", k, commands[k].desc)
fmt.Printf("\tUsage: %s %s\n", k, commands[k].usage)
}

} else {
// No network
tferror.Run(2, "[WARN] No internet connection ...")
}
fmt.Printf(" help: Show this help message\n\n")

} else if cleanup {
// Delete all cache
tflist.Cleanup()
// Show if the last version
lastrelease, release := tfgithub.Lastversion(version)
if !lastrelease && release != nil {
tferror.Run(2, fmt.Sprintf("Your version of tfversion is out of date !\nThe latest version is %s (%s)", *release.TagName, *release.HTMLURL))
}

return nil
}

func main() {

if len(os.Args) < 2 || os.Args[1] == "help" {
err = doHelp()
tferror.Panic(err)
return
}

if cmd, ok := commands[os.Args[1]]; ok {
if err = cmd.Func(os.Args[2:]); err != nil {
fmt.Fprintln(os.Stderr, err)
}
} else {
// Show version
ShowVersion()
fmt.Fprintf(os.Stderr, "Unknown command '%s'\n", os.Args[1])
fmt.Fprintf(os.Stderr, "Usage: %s command command_arguments\n", os.Args[0])
fmt.Fprintf(os.Stderr, "\tUse help command to list available commands\n")
fmt.Fprintf(os.Stderr, "\tUse command help to get commands accepting options\n")
}
}
5 changes: 2 additions & 3 deletions system/files/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package tffiles
import (
"io/ioutil"
"os/user"
"path/filepath"

"github.com/perriea/tfversion/error"
"github.com/perriea/tfversion/system/folders"
)

var (
Expand All @@ -22,7 +22,6 @@ func CreateText(version string) {

fileByte := []byte(version)

tffolder.CreateFolder(usr.HomeDir+"/terraform/tmp/", 0755)
err = ioutil.WriteFile(usr.HomeDir+"/terraform/tmp/version.txt", fileByte, 0600)
err = ioutil.WriteFile(filepath.Join(usr.HomeDir, "/terraform/tmp/.version"), fileByte, 0600)
tferror.Panic(err)
}
Loading

0 comments on commit 2e0eedc

Please sign in to comment.