dir

package
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 5, 2022 License: Apache-2.0 Imports: 4 Imported by: 12

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultPerms = 0700

DefaultPerms are defaults for new directory creation.

Functions

func AddSlash added in v0.4.2

func AddSlash(entries []string) []string

AddSlash adds a filepath.Separator to the end of all entries passed that are directories.

func Create added in v0.3.2

func Create(path string) error

Create a new directory with the DefaultPerms creating any new directories as well (see os.MkdirAll)

func Entries

func Entries(path string) []string

In returns a slice of strings with all the files in the directory at that path joined to their path (as is usually wanted). Returns an empty slice if empty or path doesn't point to a directory. See List.

Example
package main

import (
	"fmt"

	"github.com/rwxrob/fs/dir"
)

func main() {
	list := dir.Entries("testdata")
	fmt.Println(list)
}
Output:

[testdata/adir testdata/file testdata/other]

func EntriesWithSlash added in v0.4.2

func EntriesWithSlash(path string) []string

EntriesWithSlash returns Entries passed to AddSlash so that all directories will have a trailing slash.

func Exists added in v0.4.3

func Exists(path string) bool

Exists calls fs.Exists and further confirms that the path is a directory and not a file.

Example
package main

import (
	"fmt"

	"github.com/rwxrob/fs/dir"
)

func main() {
	fmt.Println(dir.Exists("testdata/exists"))
	fmt.Println(dir.Exists("testdata"))
}
Output:

false
true

func HereOrAbove added in v0.5.2

func HereOrAbove(name string) (string, error)

HereOrAbove returns the full path to the dir if the dir is found in the current working directory, or if not exists in any parent directory recursively.

Example (Above)
package main

import (
	"fmt"
	"os"
	"path/filepath"
	"strings"

	"github.com/rwxrob/fs/dir"
)

func main() {
	_dir, _ := os.Getwd()
	defer func() { os.Chdir(_dir) }()
	os.Chdir("testdata/adir/anotherdir")

	path, err := dir.HereOrAbove("adir")
	if err != nil {
		fmt.Println(err)
	}
	d := strings.Split(path, string(filepath.Separator))
	fmt.Println(d[len(d)-2:])

}
Output:

[testdata adir]
Example (Here)
package main

import (
	"fmt"
	"os"
	"path/filepath"
	"strings"

	"github.com/rwxrob/fs/dir"
)

func main() {
	_dir, _ := os.Getwd()
	defer func() { os.Chdir(_dir) }()
	os.Chdir("testdata/adir")

	path, err := dir.HereOrAbove("anotherdir")
	if err != nil {
		fmt.Println(err)
	}
	d := strings.Split(path, string(filepath.Separator))
	fmt.Println(d[len(d)-2:])

}
Output:

[adir anotherdir]

func Name added in v0.5.0

func Name() string

Name returns the current working directory name or an empty string.

Example
package main

import (
	"fmt"
	"os"

	"github.com/rwxrob/fs/dir"
)

func main() {
	os.Chdir(`testdata`)
	fmt.Println(dir.Name())
	os.Chdir(`..`)
}
Output:

testdata

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL