Fixes to mkdir example

* Adjusting name of `ssh_fx_failure` variable to match go formatting
* Fixing logic error for absolute paths
This commit is contained in:
holtwilkins 2017-08-18 11:04:12 +10:00 committed by GitHub
parent 6c3cc26627
commit 3b9263e3f5
1 changed files with 14 additions and 2 deletions

View File

@ -104,14 +104,26 @@ func ExampleClient_Mkdir_parents() {
} }
defer client.Close() defer client.Close()
ssh_fx_failure := uint32(4) sshFxFailure := uint32(4)
mkdirParents := func(client *sftp.Client, dir string) (err error) { mkdirParents := func(client *sftp.Client, dir string) (err error) {
var parents string var parents string
if path.IsAbs(dir) {
// Otherwise, an absolute path given below would be turned in to a relative one
// by splitting on "/"
parents = "/"
}
for _, name := range strings.Split(dir, "/") { for _, name := range strings.Split(dir, "/") {
if name == "" {
// Paths with double-/ in them should just move along
// this will also catch the case of the first character being a "/", i.e. an absolute path
continue
}
parents = path.Join(parents, name) parents = path.Join(parents, name)
err = client.Mkdir(parents) err = client.Mkdir(parents)
if status, ok := err.(*sftp.StatusError); ok { if status, ok := err.(*sftp.StatusError); ok {
if status.Code == ssh_fx_failure { if status.Code == sshFxFailure {
var fi os.FileInfo var fi os.FileInfo
fi, err = client.Stat(parents) fi, err = client.Stat(parents)
if err == nil { if err == nil {