Skip to content

Commit

Permalink
- replace log. with logger.
Browse files Browse the repository at this point in the history
  • Loading branch information
MatrixCrawler committed Apr 4, 2024
1 parent 6adaab1 commit aa2f246
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 93 deletions.
3 changes: 1 addition & 2 deletions lib/common.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package lib

import (
"log"
"os"
"path/filepath"
)
Expand Down Expand Up @@ -33,7 +32,7 @@ func createFile(path string) {

func createDirIfNotExist(dir string) {
if _, err := os.Stat(dir); os.IsNotExist(err) {
log.Printf("Creating directory for terraform: %v", dir)
logger.Infof("Creating directory for terraform: %v", dir)
err = os.MkdirAll(dir, 0755)
if err != nil {
logger.Panic("Unable to create %q directory for terraform: %v", dir, err)
Expand Down
3 changes: 1 addition & 2 deletions lib/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package lib

import (
"github.com/mitchellh/go-homedir"
"log"
"runtime"
)

Expand All @@ -12,7 +11,7 @@ func GetDefaultBin() string {
if runtime.GOOS == "windows" {
home, err := homedir.Dir()
if err != nil {
log.Fatalf("Could not detect home directory.")
logger.Fatal("Could not detect home directory.")
}
defaultBin = home + "/bin/terraform.exe"
}
Expand Down
5 changes: 2 additions & 3 deletions lib/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package lib
import (
"fmt"
"github.com/mitchellh/go-homedir"
"log"
"net/url"
"os"
"path/filepath"
Expand All @@ -22,7 +21,7 @@ func TestDownloadFromURL_FileNameMatch(t *testing.T) {

home, err := homedir.Dir()
if err != nil {
log.Fatalf("Could not detect home directory.")
logger.Fatalf("Could not detect home directory")
}

logger.Infof("Current home directory: %q", home)
Expand All @@ -32,7 +31,7 @@ func TestDownloadFromURL_FileNameMatch(t *testing.T) {
} else {
installLocation = installPath
}
fmt.Printf("Install Location: %v \n", installLocation)
logger.Infof("Install Location: %v", installLocation)

// create /.terraform.versions_test/ directory to store code
if _, err := os.Stat(installLocation); os.IsNotExist(err) {
Expand Down
8 changes: 3 additions & 5 deletions lib/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"bytes"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -170,7 +169,7 @@ func IsDirEmpty(name string) bool {

f, err := os.Open(name)
if err != nil {
log.Fatal(err)
logger.Fatal(err)
}
defer f.Close()

Expand All @@ -188,8 +187,7 @@ func CheckDirHasTGBin(dir, prefix string) bool {

files, err := ioutil.ReadDir(dir)
if err != nil {
log.Fatal(err)
//return exist, err
logger.Fatal(err)
}
res := []string{}
for _, f := range files {
Expand Down Expand Up @@ -227,7 +225,7 @@ func GetCurrentDirectory() string {

dir, err := os.Getwd() //get current directory
if err != nil {
log.Printf("Failed to get current directory %v\n", err)
logger.Fatalf("Failed to get current directory %v", err)
os.Exit(1)
}
return dir
Expand Down
37 changes: 18 additions & 19 deletions lib/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bufio"
"bytes"
"io"
"log"
"os"
"path/filepath"
"regexp"
Expand All @@ -26,7 +25,7 @@ func TestRenameFile(t *testing.T) {

homedir, errCurr := homedir.Dir()
if errCurr != nil {
log.Fatal(errCurr)
logger.Fatal(errCurr)
}
installLocation := filepath.Join(homedir, installPath)

Expand Down Expand Up @@ -72,7 +71,7 @@ func TestRemoveFiles(t *testing.T) {

homedir, errCurr := homedir.Dir()
if errCurr != nil {
log.Fatal(errCurr)
logger.Fatal(errCurr)
}
installLocation := filepath.Join(homedir, installPath)

Expand Down Expand Up @@ -111,7 +110,7 @@ func TestUnzip(t *testing.T) {

homedir, errCurr := homedir.Dir()
if errCurr != nil {
log.Fatal(errCurr)
logger.Fatal(errCurr)
}
installLocation := filepath.Join(homedir, installPath)

Expand Down Expand Up @@ -141,7 +140,7 @@ func TestCreateDirIfNotExist(t *testing.T) {

homedir, errCurr := homedir.Dir()
if errCurr != nil {
log.Fatal(errCurr)
logger.Fatal(errCurr)
}
installLocation := filepath.Join(homedir, installPath)

Expand Down Expand Up @@ -176,7 +175,7 @@ func TestWriteLines(t *testing.T) {

homedir, errCurr := homedir.Dir()
if errCurr != nil {
log.Fatal(errCurr)
logger.Fatal(errCurr)
}
installLocation := filepath.Join(homedir, installPath)

Expand All @@ -189,7 +188,7 @@ func TestWriteLines(t *testing.T) {

if errWrite != nil {
t.Logf("Write should work %v (unexpected)", errWrite)
log.Fatal(errWrite)
logger.Fatal(errWrite)
} else {
var (
file *os.File
Expand All @@ -199,7 +198,7 @@ func TestWriteLines(t *testing.T) {
lines []string
)
if file, errOpen = os.Open(recentFilePath); errOpen != nil {
log.Fatal(errOpen)
logger.Fatal(errOpen)
}

reader := bufio.NewReader(file)
Expand All @@ -219,12 +218,12 @@ func TestWriteLines(t *testing.T) {
}

if errRead != nil {
log.Fatalf("Error: %s\n", errRead)
logger.Fatalf("Error: %s", errRead)
}

for _, line := range lines {
if !semverRegex.MatchString(line) {
log.Fatalf("Write to file is not invalid: %s\n", line)
logger.Fatalf("Write to file is not invalid: %s", line)
break
}
}
Expand All @@ -244,7 +243,7 @@ func TestReadLines(t *testing.T) {

homedir, errCurr := homedir.Dir()
if errCurr != nil {
log.Fatal(errCurr)
logger.Fatal(errCurr)
}
installLocation := filepath.Join(homedir, installPath)

Expand All @@ -259,26 +258,26 @@ func TestReadLines(t *testing.T) {
)

if file, errCreate = os.Create(recentFilePath); errCreate != nil {
log.Fatalf("Error: %s\n", errCreate)
logger.Fatalf("Error: %s", errCreate)
}

for _, item := range test_array {
_, err := file.WriteString(strings.TrimSpace(item) + "\n")
if err != nil {
log.Fatalf("Error: %s\n", err)
logger.Fatalf("Error: %s", err)
break
}
}

lines, errRead := ReadLines(recentFilePath)

if errRead != nil {
log.Fatalf("Error: %s\n", errRead)
logger.Fatalf("Error: %s", errRead)
}

for _, line := range lines {
if !semverRegex.MatchString(line) {
log.Fatalf("Write to file is not invalid: %s\n", line)
logger.Fatalf("Write to file is not invalid: %s", line)
break
}
}
Expand All @@ -297,7 +296,7 @@ func TestIsDirEmpty(t *testing.T) {

homedir, errCurr := homedir.Dir()
if errCurr != nil {
log.Fatal(errCurr)
logger.Fatal(errCurr)
}
installLocation := filepath.Join(homedir, installPath)

Expand Down Expand Up @@ -332,7 +331,7 @@ func TestCheckDirHasTFBin(t *testing.T) {

homedir, errCurr := homedir.Dir()
if errCurr != nil {
log.Fatal(errCurr)
logger.Fatal(errCurr)
}
installLocation := filepath.Join(homedir, installPath)

Expand Down Expand Up @@ -361,7 +360,7 @@ func TestPath(t *testing.T) {

homedir, errCurr := homedir.Dir()
if errCurr != nil {
log.Fatal(errCurr)
logger.Fatal(errCurr)
}
installLocation := filepath.Join(homedir, installPath)

Expand Down Expand Up @@ -402,7 +401,7 @@ func TestGetFileName(t *testing.T) {
func TestConvertExecutableExt(t *testing.T) {
homedir, errCurr := homedir.Dir()
if errCurr != nil {
log.Fatal(errCurr)
logger.Fatal(errCurr)
}

installPath := "/.terraform.versions_test/"
Expand Down
12 changes: 2 additions & 10 deletions lib/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package lib

import (
"fmt"
"log"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -71,12 +70,6 @@ func GetInstallLocation() string {

// Install : Install the provided version in the argument
func Install(tfversion string, binPath string, mirrorURL string) {

// if !ValidVersionFormat(tfversion) {
// fmt.Printf("The provided terraform version format does not exist - %s. Try `tfswitch -l` to see all available versions.\n", tfversion)
// os.Exit(1)
// }

/* Check to see if user has permission to the default bin location which is "/usr/local/bin/terraform"
* If user does not have permission to default bin location, proceed to create $HOME/bin and install the tfswitch there
* Inform user that they don't have permission to default location, therefore tfswitch was installed in $HOME/bin
Expand Down Expand Up @@ -270,7 +263,7 @@ func InstallableBinLocation(userBinPath string) string {

homedir, errCurr := homedir.Dir()
if errCurr != nil {
log.Fatal(errCurr)
logger.Fatal(errCurr)
}

binDir := Path(userBinPath) //get path directory from binary path
Expand Down Expand Up @@ -301,8 +294,7 @@ func InstallableBinLocation(userBinPath string) string {
return filepath.Join(userBinPath)
}
}
logger.Warnf("Binary path (%q) does not exist", userBinPath)
logger.Noticef("Manually create bin directory %q and try again.", binDir)
logger.Fatalf("Binary path (%q) does not exist. Manually create bin directory %q and try again.", userBinPath, binDir)
os.Exit(1)
return ""
}
9 changes: 3 additions & 6 deletions lib/list_versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package lib
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"reflect"
Expand Down Expand Up @@ -114,22 +113,20 @@ func GetTFURLBody(mirrorURL string) ([]string, error) {
}
resp, errURL := http.Get(mirrorURL)
if errURL != nil {
log.Printf("[Error] : Getting url: %v", errURL)
logger.Fatalf("Error getting url: %v", errURL)
os.Exit(1)
return nil, errURL
}
defer resp.Body.Close()

if resp.StatusCode != 200 {
log.Printf("[Error] : Retrieving contents from url: %s", mirrorURL)
logger.Fatalf("Error retrieving contents from url: %s", mirrorURL)
os.Exit(1)
}

body, errBody := ioutil.ReadAll(resp.Body)
if errBody != nil {
log.Printf("[Error] : reading body: %v", errBody)
logger.Fatalf("Error reading body: %v", errBody)
os.Exit(1)
return nil, errBody
}

bodyString := string(body)
Expand Down
Loading

0 comments on commit aa2f246

Please sign in to comment.