Skip to content

Commit

Permalink
Use filepath instead of path where possible
Browse files Browse the repository at this point in the history
-Path does not always work well with windows

[#79748230]
  • Loading branch information
tylerschultz committed Sep 29, 2014
1 parent a45acff commit 49beccf
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 26 deletions.
2 changes: 1 addition & 1 deletion cf/api/buildpack_bits.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func findBuildpackPath(zipFiles []*zip.File) (parentPath string, foundBuildpack
for _, file := range zipFiles {
if strings.HasSuffix(file.Name, needle) {
foundBuildpack = true
parentPath = path.Join(file.Name, "..", "..")
parentPath = filepath.Join(file.Name, "..", "..")
if parentPath == "." {
parentPath = ""
}
Expand Down
14 changes: 7 additions & 7 deletions cf/app_files/cf_ignore.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package app_files

import (
"path"
"path/filepath"
"strings"

"github.com/cloudfoundry/cli/glob"
Expand All @@ -28,7 +28,7 @@ func NewCfIgnore(text string) CfIgnore {
ignore = false
}

for _, p := range globsForPattern(path.Clean(line)) {
for _, p := range globsForPattern(filepath.Clean(line)) {
patterns = append(patterns, ignorePattern{ignore, p})
}
}
Expand All @@ -54,13 +54,13 @@ func (ignore cfIgnore) FileShouldBeIgnored(path string) bool {

func globsForPattern(pattern string) (globs []glob.Glob) {
globs = append(globs, glob.MustCompileGlob(pattern))
globs = append(globs, glob.MustCompileGlob(path.Join(pattern, "*")))
globs = append(globs, glob.MustCompileGlob(path.Join(pattern, "**", "*")))
globs = append(globs, glob.MustCompileGlob(filepath.Join(pattern, "*")))
globs = append(globs, glob.MustCompileGlob(filepath.Join(pattern, "**", "*")))

if !strings.HasPrefix(pattern, "/") {
globs = append(globs, glob.MustCompileGlob(path.Join("**", pattern)))
globs = append(globs, glob.MustCompileGlob(path.Join("**", pattern, "*")))
globs = append(globs, glob.MustCompileGlob(path.Join("**", pattern, "**", "*")))
globs = append(globs, glob.MustCompileGlob(filepath.Join("**", pattern)))
globs = append(globs, glob.MustCompileGlob(filepath.Join("**", pattern, "*")))
globs = append(globs, glob.MustCompileGlob(filepath.Join("**", pattern, "**", "*")))
}

return
Expand Down
14 changes: 7 additions & 7 deletions cf/commands/plugin/install_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package plugin
import (
"fmt"
"os"
"path"
"path/filepath"

"github.com/cloudfoundry/cli/cf/command_metadata"
"github.com/cloudfoundry/cli/cf/configuration"
Expand Down Expand Up @@ -45,7 +45,7 @@ func (cmd *PluginInstall) GetRequirements(_ requirements.Factory, c *cli.Context
func (cmd *PluginInstall) Run(c *cli.Context) {
pluginPath := c.Args()[0]

_, pluginName := path.Split(pluginPath)
_, pluginName := filepath.Split(pluginPath)

plugins := cmd.config.Plugins()

Expand All @@ -55,27 +55,27 @@ func (cmd *PluginInstall) Run(c *cli.Context) {

cmd.ui.Say(fmt.Sprintf(T("Installing plugin {{.PluginName}}...", map[string]interface{}{"PluginName": pluginName})))

homeDir := path.Join(cmd.config.UserHomePath(), ".cf", "plugin")
homeDir := filepath.Join(cmd.config.UserHomePath(), ".cf", "plugin")
err := os.MkdirAll(homeDir, 0700)
if err != nil {
cmd.ui.Failed(fmt.Sprintf(T("Could not create the plugin directory: \n{{.Error}}", map[string]interface{}{"Error": err.Error()})))
}

_, err = os.Stat(path.Join(homeDir, pluginName))
_, err = os.Stat(filepath.Join(homeDir, pluginName))
if err == nil || os.IsExist(err) {
cmd.ui.Failed(fmt.Sprintf(T("The file {{.PluginName}} already exists under the plugin directory.\n", map[string]interface{}{"PluginName": pluginName})))
} else if !os.IsNotExist(err) {
cmd.ui.Failed(fmt.Sprintf(T("Unexpected error has occurred:\n{{.Error}}", map[string]interface{}{"Error": err.Error()})))
}

err = fileutils.CopyFile(path.Join(homeDir, pluginName), pluginPath)
err = fileutils.CopyFile(filepath.Join(homeDir, pluginName), pluginPath)
if err != nil {
cmd.ui.Failed(fmt.Sprintf(T("Could not copy plugin binary: \n{{.Error}}", map[string]interface{}{"Error": err.Error()})))
}

os.Chmod(path.Join(homeDir, pluginName), 0700)
os.Chmod(filepath.Join(homeDir, pluginName), 0700)

cmd.config.SetPlugin(pluginName, path.Join(homeDir, pluginName))
cmd.config.SetPlugin(pluginName, filepath.Join(homeDir, pluginName))

cmd.ui.Ok()
cmd.ui.Say(fmt.Sprintf(T("Plugin {{.PluginName}} successfully installed.", map[string]interface{}{"PluginName": pluginName})))
Expand Down
22 changes: 11 additions & 11 deletions cf/commands/plugin/install_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package plugin_test
import (
"io/ioutil"
"os"
"path"
"path/filepath"
"runtime"

testconfig "github.com/cloudfoundry/cli/cf/configuration/fakes"
Expand Down Expand Up @@ -45,7 +45,7 @@ var _ = Describe("Install", func() {
var err error
homeDir, err = ioutil.TempDir(os.TempDir(), "plugin")
Expect(err).ToNot(HaveOccurred())
pluginDir = path.Join(homeDir, ".cf", "plugin")
pluginDir = filepath.Join(homeDir, ".cf", "plugin")

curDir, err = os.Getwd()
Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -79,15 +79,15 @@ var _ = Describe("Install", func() {
})

AfterEach(func() {
os.Remove(path.Join(curDir, pluginFile.Name()))
os.Remove(filepath.Join(curDir, pluginFile.Name()))
os.Remove(homeDir)
})

It("if a file with the plugin name already exists under ~/.cf/plugin/", func() {
err := fileutils.CopyFile(path.Join(pluginDir, pluginFile.Name()), path.Join(curDir, pluginFile.Name()))
err := fileutils.CopyFile(filepath.Join(pluginDir, pluginFile.Name()), filepath.Join(curDir, pluginFile.Name()))
Expect(err).NotTo(HaveOccurred())

runCommand(path.Join(curDir, pluginFile.Name()))
runCommand(filepath.Join(curDir, pluginFile.Name()))
Expect(ui.Outputs).To(ContainSubstrings(
[]string{"Installing plugin"},
[]string{"The file", pluginFile.Name(), "already exists"},
Expand All @@ -101,24 +101,24 @@ var _ = Describe("Install", func() {
BeforeEach(func() {
setupTempExecutable()
config.UserHomePathReturns(homeDir)
runCommand(path.Join(curDir, pluginFile.Name()))
runCommand(filepath.Join(curDir, pluginFile.Name()))
})

AfterEach(func() {
os.Remove(path.Join(curDir, pluginFile.Name()))
os.Remove(filepath.Join(curDir, pluginFile.Name()))
os.Remove(homeDir)
})

It("copies the plugin into directory ~/.cf/plugin/PLUGIN_NAME", func() {
_, err := os.Stat(path.Join(curDir, pluginFile.Name()))
_, err := os.Stat(filepath.Join(curDir, pluginFile.Name()))
Expect(err).ToNot(HaveOccurred())
_, err = os.Stat(path.Join(pluginDir, pluginFile.Name()))
_, err = os.Stat(filepath.Join(pluginDir, pluginFile.Name()))
Expect(err).ToNot(HaveOccurred())
})

if runtime.GOOS != "windows" {
It("Chmods the plugin so it is executable", func() {
fileInfo, err := os.Stat(path.Join(pluginDir, pluginFile.Name()))
fileInfo, err := os.Stat(filepath.Join(pluginDir, pluginFile.Name()))
Expect(err).ToNot(HaveOccurred())
Expect(int(fileInfo.Mode())).To(Equal(0700))
})
Expand All @@ -127,7 +127,7 @@ var _ = Describe("Install", func() {
It("populate the configuration map with the plugin name and location", func() {
pluginName, pluginPath := config.SetPluginArgsForCall(0)
Expect(pluginName).To(Equal(pluginFile.Name()))
Expect(pluginPath).To(Equal(path.Join(pluginDir, pluginFile.Name())))
Expect(pluginPath).To(Equal(filepath.Join(pluginDir, pluginFile.Name())))
Expect(ui.Outputs).To(ContainSubstrings(
[]string{"Installing plugin", pluginFile.Name()},
[]string{"OK"},
Expand Down

0 comments on commit 49beccf

Please sign in to comment.