Skip to content

Commit

Permalink
Merge pull request #398 from mattn/use-filepath
Browse files Browse the repository at this point in the history
use path/filepath instead of path
  • Loading branch information
ZiyaoQiao authored May 17, 2023
2 parents 7585442 + d43004b commit 7f4fba6
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 33 deletions.
7 changes: 4 additions & 3 deletions common/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"os"
"os/user"
"path"
"path/filepath"
"reflect"
"runtime"
"strings"
Expand Down Expand Up @@ -324,7 +325,7 @@ func getHomeFolder() string {
func DefaultConfigProvider() ConfigurationProvider {
defaultConfigFile := getDefaultConfigFilePath()
homeFolder := getHomeFolder()
secondaryConfigFile := path.Join(homeFolder, secondaryConfigDirName, defaultConfigFileName)
secondaryConfigFile := filepath.Join(homeFolder, secondaryConfigDirName, defaultConfigFileName)

defaultFileProvider, _ := ConfigurationProviderFromFile(defaultConfigFile, "")
secondaryFileProvider, _ := ConfigurationProviderFromFile(secondaryConfigFile, "")
Expand All @@ -337,7 +338,7 @@ func DefaultConfigProvider() ConfigurationProvider {

func getDefaultConfigFilePath() string {
homeFolder := getHomeFolder()
defaultConfigFile := path.Join(homeFolder, defaultConfigDirName, defaultConfigFileName)
defaultConfigFile := filepath.Join(homeFolder, defaultConfigDirName, defaultConfigFileName)
if _, err := os.Stat(defaultConfigFile); err == nil {
return defaultConfigFile
}
Expand Down Expand Up @@ -383,7 +384,7 @@ func setRawPath(u *url.URL) error {
func CustomProfileConfigProvider(customConfigPath string, profile string) ConfigurationProvider {
homeFolder := getHomeFolder()
if customConfigPath == "" {
customConfigPath = path.Join(homeFolder, defaultConfigDirName, defaultConfigFileName)
customConfigPath = filepath.Join(homeFolder, defaultConfigDirName, defaultConfigFileName)
}
customFileProvider, _ := ConfigurationProviderFromFileWithProfile(customConfigPath, profile, "")
defaultFileProvider, _ := ConfigurationProviderFromFileWithProfile(customConfigPath, "DEFAULT", "")
Expand Down
4 changes: 2 additions & 2 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"io/ioutil"
"net/http"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"time"
Expand Down Expand Up @@ -244,7 +244,7 @@ func setRegionMetadataFromCfgFile(region *string) bool {
// Mark readCfgFile Flag as false since it has already been visited.
readCfgFile = false
homeFolder := getHomeFolder()
configFile := path.Join(homeFolder, regionMetadataCfgDirName, regionMetadataCfgFileName)
configFile := filepath.Join(homeFolder, regionMetadataCfgDirName, regionMetadataCfgFileName)
if jsonArr, ok := readAndParseConfigFile(&configFile); ok {
added := false
for _, jsonItem := range jsonArr {
Expand Down
16 changes: 8 additions & 8 deletions common/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"testing"

Expand Down Expand Up @@ -356,8 +356,8 @@ func TestStringToRegion(t *testing.T) {
"regionIdentifier" : "us-testregion-3"
}
]`
tmpLocation := path.Join(getHomeFolder(), ".oci", "regions-config.json")
tmpPath := path.Join(getHomeFolder(), ".oci")
tmpLocation := filepath.Join(getHomeFolder(), ".oci", "regions-config.json")
tmpPath := filepath.Join(getHomeFolder(), ".oci")

if _, err := os.Stat(tmpPath); err != nil && os.IsNotExist(err) {
if err := os.Mkdir(tmpPath, 0777); err != nil {
Expand Down Expand Up @@ -466,8 +466,8 @@ func TestSetRegionMetadataFromCfgFileWithNormalRegionName(t *testing.T) {
"regionIdentifier" : "us-testregion-3"
}
]`
tmpLocation := path.Join(getHomeFolder(), ".oci", "regions-config.json")
tmpPath := path.Join(getHomeFolder(), ".oci")
tmpLocation := filepath.Join(getHomeFolder(), ".oci", "regions-config.json")
tmpPath := filepath.Join(getHomeFolder(), ".oci")

if _, err := os.Stat(tmpPath); err != nil && os.IsNotExist(err) {
if err := os.Mkdir(tmpPath, 0777); err != nil {
Expand Down Expand Up @@ -513,8 +513,8 @@ func TestSetRegionMetadataFromCfgFileWithShortRegionName(t *testing.T) {
"regionIdentifier" : "us-testregion-3"
}
]`
tmpLocation := path.Join(getHomeFolder(), ".oci", "regions-config.json")
tmpPath := path.Join(getHomeFolder(), ".oci")
tmpLocation := filepath.Join(getHomeFolder(), ".oci", "regions-config.json")
tmpPath := filepath.Join(getHomeFolder(), ".oci")

if _, err := os.Stat(tmpPath); err != nil && os.IsNotExist(err) {
if err := os.Mkdir(tmpPath, 0777); err != nil {
Expand Down Expand Up @@ -547,7 +547,7 @@ func TestSetRegionMetadataFromCfgFileWithShortRegionName(t *testing.T) {

func TestSetRegionMetadataFromCfgFileWithInvalidFileContent(t *testing.T) {
fileContent := ""
tmpLocation := path.Join(getHomeFolder(), ".oci", "regions-config.json")
tmpLocation := filepath.Join(getHomeFolder(), ".oci", "regions-config.json")

if _, err := os.Stat(tmpLocation); err == nil || os.IsExist(err) {
os.Remove(tmpLocation)
Expand Down
8 changes: 4 additions & 4 deletions common/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"regexp"
"strings"
)
Expand Down Expand Up @@ -380,12 +380,12 @@ func parseConfigAtLine(start int, content []string) (info *configFileInfo, err e

// cleans and expands the path if it contains a tilde , returns the expanded path or the input path as is if not expansion
// was performed
func expandPath(filepath string) (expandedPath string) {
cleanedPath := path.Clean(filepath)
func expandPath(filename string) (expandedPath string) {
cleanedPath := filepath.Clean(filename)
expandedPath = cleanedPath
if strings.HasPrefix(cleanedPath, "~") {
rest := cleanedPath[2:]
expandedPath = path.Join(getHomeFolder(), rest)
expandedPath = filepath.Join(getHomeFolder(), rest)
}
return
}
Expand Down
8 changes: 4 additions & 4 deletions common/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"os"
"testing"

"path"
"path/filepath"
"strings"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -836,7 +836,7 @@ compartment = somecompartment
region=someregion
`

tmpKeyLocation := path.Join(getHomeFolder(), "testKey")
tmpKeyLocation := filepath.Join(getHomeFolder(), "testKey")
e := ioutil.WriteFile(tmpKeyLocation, []byte(testEncryptedPrivateKeyConf), 777)
if e != nil {
assert.FailNow(t, e.Error())
Expand Down Expand Up @@ -876,7 +876,7 @@ func TestExpandPath(t *testing.T) {
{
name: "should expand tilde and return appended home dir",
inPath: "~/somepath",
expectedPath: path.Join(home, "somepath"),
expectedPath: filepath.Join(home, "somepath"),
},
{
name: "should not do anything",
Expand All @@ -886,7 +886,7 @@ func TestExpandPath(t *testing.T) {
{
name: "should replace one tilde only",
inPath: "~/~/some/path",
expectedPath: path.Join(home, "~/some/path"),
expectedPath: filepath.Join(home, "~/some/path"),
},
}
for _, tio := range testIO {
Expand Down
20 changes: 10 additions & 10 deletions example/example_objectstorage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"log"
"net/http"
"os"
"path"
"path/filepath"

"github.com/oracle/oci-go-sdk/v65/common"
"github.com/oracle/oci-go-sdk/v65/example/helpers"
Expand All @@ -33,13 +33,13 @@ func ExampleObjectStorage_UploadFile() {
defer deleteBucket(ctx, c, namespace, bname)

contentlen := 1024 * 1000
filepath, filesize := helpers.WriteTempFileOfSize(int64(contentlen))
filename := path.Base(filepath)
fpath, filesize := helpers.WriteTempFileOfSize(int64(contentlen))
filename := filepath.Base(fpath)
defer func() {
os.Remove(filename)
}()

file, e := os.Open(filepath)
file, e := os.Open(fpath)
defer file.Close()
helpers.FatalIfError(e)

Expand Down Expand Up @@ -69,8 +69,8 @@ func ExampleObjectStorage_UploadManager_UploadFile() {
defer deleteBucket(ctx, c, namespace, bname)

contentlen := 1024 * 1024 * 300 // 300MB
filepath, _ := helpers.WriteTempFileOfSize(int64(contentlen))
filename := path.Base(filepath)
fpath, _ := helpers.WriteTempFileOfSize(int64(contentlen))
filename := filepath.Base(fpath)
defer os.Remove(filename)

uploadManager := transfer.NewUploadManager()
Expand All @@ -86,7 +86,7 @@ func ExampleObjectStorage_UploadManager_UploadFile() {
ObjectStorageClient: &c,
EnableMultipartChecksumVerification: common.Bool(true),
},
FilePath: filepath,
FilePath: fpath,
}

// if you want to overwrite default value, you can do it
Expand Down Expand Up @@ -137,16 +137,16 @@ func ExampleObjectStorage_UploadManager_Stream() {
defer deleteBucket(ctx, c, namespace, bname)

contentlen := 1024 * 1000 * 130 // 130MB
filepath, _ := helpers.WriteTempFileOfSize(int64(contentlen))
filename := path.Base(filepath)
fpath, _ := helpers.WriteTempFileOfSize(int64(contentlen))
filename := filepath.Base(fpath)
defer func() {
os.Remove(filename)
}()

uploadManager := transfer.NewUploadManager()
objectName := "sampleStreamUploadObj"

file, _ := os.Open(filepath)
file, _ := os.Open(fpath)
defer file.Close()

req := transfer.UploadStreamRequest{
Expand Down
4 changes: 2 additions & 2 deletions objectstorage/transfer/multipart_manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package transfer

import (
"os"
"path"
"path/filepath"
"testing"

"github.com/oracle/oci-go-sdk/v65/example/helpers"
Expand Down Expand Up @@ -70,7 +70,7 @@ func TestSplitFileParts(t *testing.T) {
assert.Equal(t, testData.expectedLastPartSize, parts[len(parts)-1].size)

file.Close()
os.Remove(path.Base(filePath))
os.Remove(filepath.Base(filePath))
close(done)
}
}

0 comments on commit 7f4fba6

Please sign in to comment.