Skip to content

Commit

Permalink
Use go-homedir everywhere
Browse files Browse the repository at this point in the history
Signed-off-by: Ahmed Kamal <[email protected]>
  • Loading branch information
kim0 committed Aug 26, 2023
1 parent 9a74553 commit 38dac39
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 21 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/hashicorp/hcl2 v0.0.0-20191002203319-fb75b3253c80
github.com/hashicorp/terraform-config-inspect v0.0.0-20211115214459-90acf1ca460f
github.com/manifoldco/promptui v0.9.0
github.com/mitchellh/go-homedir v1.1.0
github.com/pborman/getopt v1.1.0
github.com/spf13/viper v1.12.0
golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamh
github.com/magiconair/properties v1.8.6/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
github.com/manifoldco/promptui v0.9.0 h1:3V4HzJk1TtXW1MTZMP7mdlwbBpIinw3HztaIlYthEiA=
github.com/manifoldco/promptui v0.9.0/go.mod h1:ka04sppxSGFAtxX0qhlYQjISsg9mR4GWtQEhdbn6Pgg=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
github.com/mitchellh/go-wordwrap v1.0.0 h1:6GlHJ/LTGMrIJbwgdqdl2eEH8o+Exx/0m8ir9Gns0u4=
github.com/mitchellh/go-wordwrap v1.0.0/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
Expand Down
3 changes: 2 additions & 1 deletion lib/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"
"testing"

"github.com/mitchellh/go-homedir"
"github.com/warrensbox/terraform-switcher/lib"
)

Expand All @@ -21,7 +22,7 @@ func TestDownloadFromURL_FileNameMatch(t *testing.T) {
macOS := "_darwin_amd64.zip"

// get current user
homedir, errCurr := os.UserHomeDir()
homedir, errCurr := homedir.Dir()
if errCurr != nil {
log.Fatal(errCurr)
}
Expand Down
4 changes: 3 additions & 1 deletion lib/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"os"
"path/filepath"
"strings"

"github.com/mitchellh/go-homedir"
)

// RenameFile : rename file name
Expand Down Expand Up @@ -237,7 +239,7 @@ func GetCurrentDirectory() string {
// GetHomeDirectory : return the home directory
func GetHomeDirectory() string {

homedir, errHome := os.UserHomeDir()
homedir, errHome := homedir.Dir()
if errHome != nil {
log.Printf("Failed to get home directory %v\n", errHome)
os.Exit(1)
Expand Down
21 changes: 11 additions & 10 deletions lib/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"testing"
"time"

"github.com/mitchellh/go-homedir"
"github.com/warrensbox/terraform-switcher/lib"
)

Expand All @@ -25,7 +26,7 @@ func TestRenameFile(t *testing.T) {
installPath := "/.terraform.versions_test/"
version := "0.0.7"

homedir, errCurr := os.UserHomeDir()
homedir, errCurr := homedir.Dir()
if errCurr != nil {
log.Fatal(errCurr)
}
Expand Down Expand Up @@ -71,7 +72,7 @@ func TestRemoveFiles(t *testing.T) {
installFile := lib.ConvertExecutableExt("terraform")
installPath := "/.terraform.versions_test/"

homedir, errCurr := os.UserHomeDir()
homedir, errCurr := homedir.Dir()
if errCurr != nil {
log.Fatal(errCurr)
}
Expand Down Expand Up @@ -110,7 +111,7 @@ func TestUnzip(t *testing.T) {

fmt.Println(absPath)

homedir, errCurr := os.UserHomeDir()
homedir, errCurr := homedir.Dir()
if errCurr != nil {
log.Fatal(errCurr)
}
Expand Down Expand Up @@ -142,7 +143,7 @@ func TestUnzip(t *testing.T) {
func TestCreateDirIfNotExist(t *testing.T) {
installPath := "/.terraform.versions_test/"

homedir, errCurr := os.UserHomeDir()
homedir, errCurr := homedir.Dir()
if errCurr != nil {
log.Fatal(errCurr)
}
Expand Down Expand Up @@ -177,7 +178,7 @@ func TestWriteLines(t *testing.T) {
semverRegex := regexp.MustCompile(`\A\d+(\.\d+){2}(-\w+\d*)?\z`)
//semverRegex := regexp.MustCompile(`\A\d+(\.\d+){2}\z`)

homedir, errCurr := os.UserHomeDir()
homedir, errCurr := homedir.Dir()
if errCurr != nil {
log.Fatal(errCurr)
}
Expand Down Expand Up @@ -245,7 +246,7 @@ func TestReadLines(t *testing.T) {
recentFile := "RECENT"
semverRegex := regexp.MustCompile(`\A\d+(\.\d+){2}(-\w+\d*)?\z`)

homedir, errCurr := os.UserHomeDir()
homedir, errCurr := homedir.Dir()
if errCurr != nil {
log.Fatal(errCurr)
}
Expand Down Expand Up @@ -298,7 +299,7 @@ func TestIsDirEmpty(t *testing.T) {

installPath := "/.terraform.versions_test/"

homedir, errCurr := os.UserHomeDir()
homedir, errCurr := homedir.Dir()
if errCurr != nil {
log.Fatal(errCurr)
}
Expand Down Expand Up @@ -333,7 +334,7 @@ func TestCheckDirHasTFBin(t *testing.T) {
installPath := "/.terraform.versions_test/"
installFilePrefix := "terraform"

homedir, errCurr := os.UserHomeDir()
homedir, errCurr := homedir.Dir()
if errCurr != nil {
log.Fatal(errCurr)
}
Expand Down Expand Up @@ -362,7 +363,7 @@ func TestPath(t *testing.T) {
installPath := "/.terraform.versions_test"
installFile := lib.ConvertExecutableExt("terraform")

homedir, errCurr := os.UserHomeDir()
homedir, errCurr := homedir.Dir()
if errCurr != nil {
log.Fatal(errCurr)
}
Expand Down Expand Up @@ -403,7 +404,7 @@ func TestGetFileName(t *testing.T) {

// TestConvertExecutableExt : convert executable binary with extension
func TestConvertExecutableExt(t *testing.T) {
homedir, errCurr := os.UserHomeDir()
homedir, errCurr := homedir.Dir()
if errCurr != nil {
log.Fatal(errCurr)
}
Expand Down
5 changes: 3 additions & 2 deletions lib/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"

"github.com/hashicorp/go-version"
"github.com/mitchellh/go-homedir"
)

const (
Expand Down Expand Up @@ -57,7 +58,7 @@ func initialize() {
// will create a directory in the home location if it does not exist
func GetInstallLocation() string {
/* get current user */
homedir, errCurr := os.UserHomeDir()
homedir, errCurr := homedir.Dir()
if errCurr != nil {
log.Fatal(errCurr)
}
Expand Down Expand Up @@ -276,7 +277,7 @@ func ConvertExecutableExt(fpath string) string {
// If not, create $HOME/bin. Ask users to add $HOME/bin to $PATH and return $HOME/bin as install location
func InstallableBinLocation(userBinPath string) string {

homedir, errCurr := os.UserHomeDir()
homedir, errCurr := homedir.Dir()
if errCurr != nil {
log.Fatal(errCurr)
}
Expand Down
4 changes: 3 additions & 1 deletion lib/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"os/user"
"runtime"
"testing"

"github.com/mitchellh/go-homedir"
)

// TestAddRecent : Create a file, check filename exist,
Expand All @@ -30,7 +32,7 @@ func TestInstall(t *testing.T) {
t.Errorf("Unable to get user %v [unexpected]", errCurr)
}

_, errCurr = os.UserHomeDir()
_, errCurr = homedir.Dir()
if errCurr != nil {
t.Errorf("Unable to get user home directory: %v [unexpected]", errCurr)
}
Expand Down
7 changes: 4 additions & 3 deletions lib/symlink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"path/filepath"
"testing"

"github.com/mitchellh/go-homedir"
"github.com/warrensbox/terraform-switcher/lib"
)

Expand All @@ -17,7 +18,7 @@ func TestCreateSymlink(t *testing.T) {

testSymlinkDest := "/test-tfswitcher-dest"

homedir, errCurr := os.UserHomeDir()
homedir, errCurr := homedir.Dir()
if errCurr != nil {
log.Fatal(errCurr)
}
Expand Down Expand Up @@ -55,7 +56,7 @@ func TestRemoveSymlink(t *testing.T) {

testSymlinkDest := "/test-tfswitcher-dest"

homedir, errCurr := os.UserHomeDir()
homedir, errCurr := homedir.Dir()
if errCurr != nil {
log.Fatal(errCurr)
}
Expand Down Expand Up @@ -90,7 +91,7 @@ func TestCheckSymlink(t *testing.T) {

testSymlinkDest := "/test-tgshifter-dest"

homedir, errCurr := os.UserHomeDir()
homedir, errCurr := homedir.Dir()
if errCurr != nil {
log.Fatal(errCurr)
}
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/hashicorp/hcl2/gohcl"
"github.com/hashicorp/hcl2/hclparse"
"github.com/hashicorp/terraform-config-inspect/tfconfig"
"github.com/mitchellh/go-homedir"

"github.com/manifoldco/promptui"
"github.com/pborman/getopt"
Expand Down Expand Up @@ -70,7 +71,7 @@ func main() {
getopt.Parse()
args := getopt.Args()

homedir, err := os.UserHomeDir()
homedir, err := homedir.Dir()
if err != nil {
fmt.Printf("Unable to get user's home directory")
os.Exit(1)
Expand Down
5 changes: 3 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package main_test

import (
"os"
"os/user"
"testing"

"github.com/mitchellh/go-homedir"
)

// TestMain : check to see if user exist
Expand All @@ -15,7 +16,7 @@ func TestMain(t *testing.T) {
if errCurr != nil {
t.Errorf("Unable to get user %v [unexpected]", errCurr)
}
_, errCurr = os.UserHomeDir()
_, errCurr = homedir.Dir()
if errCurr != nil {
t.Errorf("Unable to get user home directory: %v [unexpected]", errCurr)
}
Expand Down

0 comments on commit 38dac39

Please sign in to comment.