From aa2f246c31321c8e2aa15706e327d9ba396a79c4 Mon Sep 17 00:00:00 2001 From: Johannes Brunswicker Date: Thu, 4 Apr 2024 12:34:37 +0200 Subject: [PATCH] - replace log. with logger. --- lib/common.go | 3 +-- lib/defaults.go | 3 +-- lib/download_test.go | 5 ++-- lib/files.go | 8 +++--- lib/files_test.go | 37 +++++++++++++-------------- lib/install.go | 12 ++------- lib/list_versions.go | 9 +++---- lib/list_versions_test.go | 53 ++++++++++++++++++--------------------- lib/symlink.go | 11 ++++---- lib/symlink_test.go | 19 ++++++-------- main.go | 2 +- 11 files changed, 69 insertions(+), 93 deletions(-) diff --git a/lib/common.go b/lib/common.go index 461aa067..b82a8f16 100644 --- a/lib/common.go +++ b/lib/common.go @@ -1,7 +1,6 @@ package lib import ( - "log" "os" "path/filepath" ) @@ -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) diff --git a/lib/defaults.go b/lib/defaults.go index f8465487..2c076351 100644 --- a/lib/defaults.go +++ b/lib/defaults.go @@ -2,7 +2,6 @@ package lib import ( "github.com/mitchellh/go-homedir" - "log" "runtime" ) @@ -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" } diff --git a/lib/download_test.go b/lib/download_test.go index 38f6a087..4129fb64 100644 --- a/lib/download_test.go +++ b/lib/download_test.go @@ -3,7 +3,6 @@ package lib import ( "fmt" "github.com/mitchellh/go-homedir" - "log" "net/url" "os" "path/filepath" @@ -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) @@ -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) { diff --git a/lib/files.go b/lib/files.go index 497e0f86..23e08d2a 100644 --- a/lib/files.go +++ b/lib/files.go @@ -6,7 +6,6 @@ import ( "bytes" "io" "io/ioutil" - "log" "os" "path/filepath" "strings" @@ -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() @@ -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 { @@ -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 diff --git a/lib/files_test.go b/lib/files_test.go index 290756e6..b1f7db65 100644 --- a/lib/files_test.go +++ b/lib/files_test.go @@ -4,7 +4,6 @@ import ( "bufio" "bytes" "io" - "log" "os" "path/filepath" "regexp" @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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 @@ -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) @@ -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 } } @@ -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) @@ -259,13 +258,13 @@ 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 } } @@ -273,12 +272,12 @@ func TestReadLines(t *testing.T) { 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 } } @@ -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) @@ -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) @@ -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) @@ -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/" diff --git a/lib/install.go b/lib/install.go index b3b509f8..5bd17d76 100644 --- a/lib/install.go +++ b/lib/install.go @@ -2,7 +2,6 @@ package lib import ( "fmt" - "log" "os" "path/filepath" "runtime" @@ -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 @@ -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 @@ -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 "" } diff --git a/lib/list_versions.go b/lib/list_versions.go index 1a9c41c4..a5c8759f 100644 --- a/lib/list_versions.go +++ b/lib/list_versions.go @@ -3,7 +3,6 @@ package lib import ( "fmt" "io/ioutil" - "log" "net/http" "os" "reflect" @@ -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) diff --git a/lib/list_versions_test.go b/lib/list_versions_test.go index 0088f17a..6830cdb1 100644 --- a/lib/list_versions_test.go +++ b/lib/list_versions_test.go @@ -1,11 +1,8 @@ -package lib_test +package lib import ( - "log" "reflect" "testing" - - "github.com/warrensbox/terraform-switcher/lib" ) const ( @@ -16,7 +13,7 @@ const ( func TestGetTFList(t *testing.T) { listAll := true - list, _ := lib.GetTFList(hashiURL, listAll) + list, _ := GetTFList(hashiURL, listAll) val := "0.1.0" var exists bool @@ -33,120 +30,120 @@ func TestGetTFList(t *testing.T) { } if !exists { - log.Fatalf("Not able to find version: %s\n", val) + logger.Fatalf("Not able to find version: %s", val) } else { t.Log("Write versions exist (expected)") } } -//TestRemoveDuplicateVersions : test to removed duplicate +// TestRemoveDuplicateVersions : test to removed duplicate func TestRemoveDuplicateVersions(t *testing.T) { test_array := []string{"0.0.1", "0.0.2", "0.0.3", "0.0.1", "0.12.0-beta1", "0.12.0-beta1"} - list := lib.RemoveDuplicateVersions(test_array) + list := RemoveDuplicateVersions(test_array) if len(list) == len(test_array) { - log.Fatalf("Not able to remove duplicate: %s\n", test_array) + logger.Fatalf("Not able to remove duplicate: %s\n", test_array) } else { t.Log("Write versions exist (expected)") } } -//TestValidVersionFormat : test if func returns valid version format +// TestValidVersionFormat : test if func returns valid version format // more regex testing at https://rubular.com/r/UvWXui7EU2icSb func TestValidVersionFormat(t *testing.T) { var version string version = "0.11.8" - valid := lib.ValidVersionFormat(version) + valid := ValidVersionFormat(version) if valid == true { t.Logf("Valid version format : %s (expected)", version) } else { - log.Fatalf("Failed to verify version format: %s\n", version) + logger.Fatalf("Failed to verify version format: %s\n", version) } version = "1.11.9" - valid = lib.ValidVersionFormat(version) + valid = ValidVersionFormat(version) if valid == true { t.Logf("Valid version format : %s (expected)", version) } else { - log.Fatalf("Failed to verify version format: %s\n", version) + logger.Fatalf("Failed to verify version format: %s\n", version) } version = "1.11.a" - valid = lib.ValidVersionFormat(version) + valid = ValidVersionFormat(version) if valid == false { t.Logf("Invalid version format : %s (expected)", version) } else { - log.Fatalf("Failed to verify version format: %s\n", version) + logger.Fatalf("Failed to verify version format: %s\n", version) } version = "22323" - valid = lib.ValidVersionFormat(version) + valid = ValidVersionFormat(version) if valid == false { t.Logf("Invalid version format : %s (expected)", version) } else { - log.Fatalf("Failed to verify version format: %s\n", version) + logger.Fatalf("Failed to verify version format: %s\n", version) } version = "@^&*!)!" - valid = lib.ValidVersionFormat(version) + valid = ValidVersionFormat(version) if valid == false { t.Logf("Invalid version format : %s (expected)", version) } else { - log.Fatalf("Failed to verify version format: %s\n", version) + logger.Fatalf("Failed to verify version format: %s\n", version) } version = "1.11.9-beta1" - valid = lib.ValidVersionFormat(version) + valid = ValidVersionFormat(version) if valid == true { t.Logf("Valid version format : %s (expected)", version) } else { - log.Fatalf("Failed to verify version format: %s\n", version) + logger.Fatalf("Failed to verify version format: %s\n", version) } version = "0.12.0-rc2" - valid = lib.ValidVersionFormat(version) + valid = ValidVersionFormat(version) if valid == true { t.Logf("Valid version format : %s (expected)", version) } else { - log.Fatalf("Failed to verify version format: %s\n", version) + logger.Fatalf("Failed to verify version format: %s\n", version) } version = "1.11.4-boom" - valid = lib.ValidVersionFormat(version) + valid = ValidVersionFormat(version) if valid == true { t.Logf("Valid version format : %s (expected)", version) } else { - log.Fatalf("Failed to verify version format: %s\n", version) + logger.Fatalf("Failed to verify version format: %s\n", version) } version = "1.11.4-1" - valid = lib.ValidVersionFormat(version) + valid = ValidVersionFormat(version) if valid == false { t.Logf("Invalid version format : %s (expected)", version) } else { - log.Fatalf("Failed to verify version format: %s\n", version) + logger.Fatalf("Failed to verify version format: %s\n", version) } } diff --git a/lib/symlink.go b/lib/symlink.go index d5002682..19e48350 100644 --- a/lib/symlink.go +++ b/lib/symlink.go @@ -2,7 +2,6 @@ package lib import ( "io" - "log" "os" "runtime" ) @@ -14,14 +13,14 @@ func CreateSymlink(cwd string, dir string) { if runtime.GOOS == "windows" { r, err := os.Open(cwd) if err != nil { - log.Fatalf("Unable to open source binary: %s", cwd) + logger.Fatalf("Unable to open source binary: %s", cwd) os.Exit(1) } defer r.Close() w, err := os.Create(dir + ".exe") if err != nil { - log.Fatalf("Could not create target binary: %s", dir + ".exe") + logger.Fatalf("Could not create target binary: %s", dir+".exe") os.Exit(1) } defer func() { @@ -33,7 +32,7 @@ func CreateSymlink(cwd string, dir string) { } else { err := os.Symlink(cwd, dir) if err != nil { - log.Fatalf(` + logger.Fatalf(` Unable to create new symlink. Maybe symlink already exist. Try removing existing symlink manually. Try running "unlink %s" to remove existing symlink. @@ -50,7 +49,7 @@ func RemoveSymlink(symlinkPath string) { _, err := os.Lstat(symlinkPath) if err != nil { - log.Fatalf(` + logger.Fatalf(` Unable to stat symlink. Maybe symlink already exist. Try removing existing symlink manually. Try running "unlink %s" to remove existing symlink. @@ -62,7 +61,7 @@ func RemoveSymlink(symlinkPath string) { errRemove := os.Remove(symlinkPath) if errRemove != nil { - log.Fatalf(` + logger.Fatalf(` Unable to remove symlink. Maybe symlink already exist. Try removing existing symlink manually. Try running "unlink %s" to remove existing symlink. diff --git a/lib/symlink_test.go b/lib/symlink_test.go index 1abd0b2d..9430b8af 100644 --- a/lib/symlink_test.go +++ b/lib/symlink_test.go @@ -1,14 +1,11 @@ -package lib_test +package lib import ( "github.com/mitchellh/go-homedir" - "log" "os" "path/filepath" "runtime" "testing" - - "github.com/warrensbox/terraform-switcher/lib" ) // TestCreateSymlink : check if symlink exist-remove if exist, @@ -23,7 +20,7 @@ func TestCreateSymlink(t *testing.T) { home, err := homedir.Dir() if err != nil { - log.Fatalf("Could not detect home directory.") + logger.Fatalf("Could not detect home directory.") } symlinkPathSrc := filepath.Join(home, testSymlinkSrc) symlinkPathDest := filepath.Join(home, testSymlinkDest) @@ -31,7 +28,7 @@ func TestCreateSymlink(t *testing.T) { // Create file for test as windows does not like no source create, err := os.Create(symlinkPathDest) if err != nil { - log.Fatalf("Could not create test dest file for symlink at %v", symlinkPathDest) + logger.Fatalf("Could not create test dest file for symlink at %v", symlinkPathDest) } defer create.Close() @@ -47,7 +44,7 @@ func TestCreateSymlink(t *testing.T) { } } - lib.CreateSymlink(symlinkPathDest, symlinkPathSrc) + CreateSymlink(symlinkPathDest, symlinkPathSrc) if runtime.GOOS == "windows" { _, err := os.Stat(symlinkPathSrc + ".exe") @@ -81,7 +78,7 @@ func TestRemoveSymlink(t *testing.T) { homedir, errCurr := homedir.Dir() if errCurr != nil { - log.Fatal(errCurr) + logger.Fatal(errCurr) } symlinkPathSrc := filepath.Join(homedir, testSymlinkSrc) symlinkPathDest := filepath.Join(homedir, testSymlinkDest) @@ -96,7 +93,7 @@ func TestRemoveSymlink(t *testing.T) { } } - lib.RemoveSymlink(symlinkPathSrc) + RemoveSymlink(symlinkPathSrc) lnCheck, _ := os.Readlink(symlinkPathSrc) if lnCheck == symlinkPathDest { @@ -116,7 +113,7 @@ func TestCheckSymlink(t *testing.T) { homedir, errCurr := homedir.Dir() if errCurr != nil { - log.Fatal(errCurr) + logger.Fatal(errCurr) } symlinkPathSrc := filepath.Join(homedir, testSymlinkSrc) symlinkPathDest := filepath.Join(homedir, testSymlinkDest) @@ -130,7 +127,7 @@ func TestCheckSymlink(t *testing.T) { } } - symlinkExist := lib.CheckSymlink(symlinkPathSrc) + symlinkExist := CheckSymlink(symlinkPathSrc) if symlinkExist { t.Logf("Symlink does exist %v [expected]", ln) diff --git a/main.go b/main.go index bdce009c..00e4a5fe 100644 --- a/main.go +++ b/main.go @@ -72,7 +72,7 @@ func main() { homedir, err := homedir.Dir() if err != nil { - logger.Errorf("Unable to get home directory: %v", err) + logger.Fatalf("Unable to get home directory: %v", err) os.Exit(1) }