本文整理汇总了Golang中io/ioutil.ReadDir函数的典型用法代码示例。如果您正苦于以下问题:Golang ReadDir函数的具体用法?Golang ReadDir怎么用?Golang ReadDir使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ReadDir函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: LoadAvailableManpages
/*
* Holy moly, what a mess!
*/
func LoadAvailableManpages(basedir string) map[int][]string {
availablePages := make(map[int][]string)
sectionDirs, err := ioutil.ReadDir(basedir)
if err != nil {
log.Print(err)
}
for _, sectionDir := range sectionDirs {
if sectionDir.IsDir() {
sectionDirPath := path.Join(basedir, sectionDir.Name())
files, err := ioutil.ReadDir(sectionDirPath)
if err != nil {
log.Print(err)
}
sectionNumber, err := strconv.Atoi(sectionDir.Name()[len(sectionDir.Name())-1:])
if err != nil {
log.Print(err)
}
for _, file := range files {
if !file.IsDir() && strings.HasSuffix(file.Name(), ".html") {
if _, ok := availablePages[sectionNumber]; !ok {
availablePages[sectionNumber] = []string{}
}
availablePages[sectionNumber] = append(
availablePages[sectionNumber],
path.Join(sectionDirPath, file.Name()))
}
}
}
}
return availablePages
}
示例2: suggestPaths
func suggestPaths(tok string) (cnt int) {
root, base := path.Split(tok)
if isDir(tok) {
root, base = tok, ""
}
fis, _ := ioutil.ReadDir(root)
if root == "" {
fis, _ = ioutil.ReadDir(".")
}
for _, fi := range fis {
if strings.Index(fi.Name(), base) != 0 {
// Skip paths don't match the partial token
continue
}
if strings.Index(fi.Name(), ".") == 0 && len(base) == 0 {
// Skip hidden folders unless there is a partial match
continue
}
if fi.IsDir() {
fmt.Println(root + fi.Name() + string(os.PathSeparator))
cnt++
} else {
fmt.Println(root + fi.Name() + " ")
cnt++
}
}
return cnt
}
示例3: Mounted
// Mounted shows any volumes that belong to volplugin on the host, in
// their native representation. They yield a *Mount.
func (d *Driver) Mounted(time.Duration) ([]*storage.Mount, error) {
mounts := []*storage.Mount{}
fis, err := ioutil.ReadDir(d.mountpath)
if os.IsNotExist(err) {
return mounts, os.MkdirAll(d.mountpath, 0700)
} else if err != nil {
return nil, errored.Errorf("Reading policy tree for mounts").Combine(err)
}
for _, fi := range fis {
volumes, err := ioutil.ReadDir(filepath.Join(d.mountpath, fi.Name()))
if err != nil {
return nil, errored.Errorf("Reading mounted volumes for policy %q", fi.Name()).Combine(err)
}
for _, vol := range volumes {
rel := filepath.Join(d.mountpath, fi.Name(), vol.Name())
if err != nil {
return nil, errored.Errorf("Calculating mount information for %q/%q", fi.Name(), vol.Name()).Combine(err)
}
mounts = append(mounts, &storage.Mount{
Path: rel,
Volume: storage.Volume{
Name: rel,
Source: "null",
},
})
}
}
return mounts, nil
}
示例4: deleteEmptyFoldersImpl
func deleteEmptyFoldersImpl(root string, level int) error {
fis, err := ioutil.ReadDir(root)
if err != nil {
return err
}
foundPlain := false
for _, sfi := range fis {
if sfi.IsDir() {
err = deleteEmptyFoldersImpl(filepath.Join(root, sfi.Name()), level+1)
if err != nil {
return err
}
} else {
foundPlain = true
}
}
if !foundPlain && level > 0 {
fis, err = ioutil.ReadDir(root)
if err != nil {
return err
}
if len(fis) == 0 {
err = os.Remove(root)
if err != nil {
return err
}
}
}
return nil
}
示例5: checkDirectory
func checkDirectory(dir string, expected map[string]string) error {
list, err := ioutil.ReadDir(dir)
if err != nil {
return err
}
for _, fi := range list {
if !fi.IsDir() {
return fmt.Errorf("%s is not a directory", fi.Name())
}
dirName := fi.Name()
if _, err := strconv.Atoi(dirName); err != nil {
return fmt.Errorf("Name is not a number: %v", err)
}
expectedFileName, ok := expected[dirName]
if !ok {
return fmt.Errorf("Unexpected name found: %s", dirName)
}
subList, err := ioutil.ReadDir(filepath.Join(dir, dirName))
if err != nil {
return fmt.Errorf("Failed to get a list of files in %s: %v",
dirName, err)
}
filesCount := len(subList)
if filesCount != 1 {
return fmt.Errorf("Expected only 1 file in %s, got %d",
dirName, filesCount)
}
gottenFileName := subList[0].Name()
if gottenFileName != expectedFileName {
return fmt.Errorf("Expected %s in %s, got %s",
expectedFileName, dirName, gottenFileName)
}
}
return nil
}
示例6: TestKeyReadWriterMigrate
func TestKeyReadWriterMigrate(t *testing.T) {
cert, key, err := testutils.CreateRootCertAndKey("cn")
require.NoError(t, err)
tempdir, err := ioutil.TempDir("", "KeyReadWriter")
require.NoError(t, err)
defer os.RemoveAll(tempdir)
path := ca.NewConfigPaths(filepath.Join(tempdir))
// if the key exists in an old location, migrate it from there.
tempKeyPath := filepath.Join(filepath.Dir(path.Node.Key), "."+filepath.Base(path.Node.Key))
require.NoError(t, ioutil.WriteFile(path.Node.Cert, cert, 0644))
require.NoError(t, ioutil.WriteFile(tempKeyPath, key, 0600))
krw := ca.NewKeyReadWriter(path.Node, nil, nil)
require.NoError(t, krw.Migrate())
_, err = os.Stat(tempKeyPath)
require.True(t, os.IsNotExist(err)) // it's been moved to the right place
_, _, err = krw.Read()
require.NoError(t, err)
// migrate does not affect any existing files
dirList, err := ioutil.ReadDir(filepath.Dir(path.Node.Key))
require.NoError(t, err)
require.NoError(t, krw.Migrate())
dirList2, err := ioutil.ReadDir(filepath.Dir(path.Node.Key))
require.NoError(t, err)
require.Equal(t, dirList, dirList2)
_, _, err = krw.Read()
require.NoError(t, err)
}
示例7: TestUnionFsFdLeak
func TestUnionFsFdLeak(t *testing.T) {
beforeEntries, err := ioutil.ReadDir("/proc/self/fd")
if err != nil {
t.Fatalf("ReadDir failed: %v", err)
}
wd, clean := setupUfs(t)
err = ioutil.WriteFile(wd+"/ro/file", []byte("blablabla"), 0644)
if err != nil {
t.Fatalf("WriteFile failed: %v", err)
}
setRecursiveWritable(t, wd+"/ro", false)
contents, err := ioutil.ReadFile(wd + "/mnt/file")
if err != nil {
t.Fatalf("ReadFile failed: %v", err)
}
err = ioutil.WriteFile(wd+"/mnt/file", contents, 0644)
if err != nil {
t.Fatalf("WriteFile failed: %v", err)
}
clean()
afterEntries, err := ioutil.ReadDir("/proc/self/fd")
if err != nil {
t.Fatalf("ReadDir failed: %v", err)
}
if len(afterEntries) != len(beforeEntries) {
t.Errorf("/proc/self/fd changed size: after %v before %v", len(beforeEntries), len(afterEntries))
}
}
示例8: DirectoryContents
// DirectoryContents returns a two-dimensional slice with full-path directories
// to send to dupl for evaluation. The first index addresses the specific lab,
// and the second index addresses the specific student. If a student does not
// have the lab directory, the 2D slice will save the directory as an empty
// string. The second return argument indicates whether or not the function was
// successful. DirectoryContents takes as input baseDir, the location of the
// student directories, and labs, a slice of the labs.
func DirectoryContents(baseDir string, labs []LabInfo) ([][]string, bool) {
// Try to read the base directory
contents, err := ioutil.ReadDir(baseDir)
if err != nil {
fmt.Printf("Error reading directory %s: %s\n", baseDir, err)
return nil, false
}
var studentDirs []string
// Get a list of all the student directories (full path)
for _, item := range contents {
if item.IsDir() {
studentDirs = append(studentDirs, filepath.Join(baseDir, item.Name()))
}
}
studentsLabDirs := make([][]string, len(labs))
// For each lab
for i := range studentsLabDirs {
studentsLabDirs[i] = make([]string, len(studentDirs))
// For each student
for j := range studentsLabDirs[i] {
tempDir := filepath.Join(studentDirs[j], labs[i].Name)
_, err := ioutil.ReadDir(tempDir)
if err != nil {
studentsLabDirs[i][j] = ""
} else {
studentsLabDirs[i][j] = tempDir
}
}
}
return studentsLabDirs, true
}
示例9: TestLiveness
func TestLiveness(t *testing.T) {
dir := testutil.TempDir()
defer os.RemoveAll(dir)
root := nodefs.NewDefaultNode()
s, _, err := nodefs.MountRoot(dir, root, nil)
if err != nil {
t.Fatalf("MountRoot: %v", err)
}
go s.Serve()
if err := s.WaitMount(); err != nil {
t.Fatal("WaitMount", err)
}
defer s.Unmount()
if _, err := ioutil.ReadDir(dir); err != nil {
t.Fatalf("ReadDir: %v", err)
}
// We previously encountered a sitation where a finalizer would close our fd out from under us. Try to force both finalizers to run and object destruction to complete.
runtime.GC()
runtime.GC()
if _, err := ioutil.ReadDir(dir); err != nil {
t.Fatalf("ReadDir: %v", err)
}
}
示例10: New
// New is the constructor for a new engine.
func New(root, conditions string) (*Engine, error) {
hostname, err := os.Hostname()
if err != nil {
return nil, err
}
engine := &Engine{Root: root, Hostname: hostname}
if _, err := os.Stat(path.Join(engine.Root, "logic")); err == nil {
logic, err := ioutil.ReadDir(path.Join(engine.Root, "logic"))
if err != nil {
return nil, err
}
engine.Logic = logic
}
if _, err := os.Stat(path.Join(engine.Root, "stacks")); err == nil {
stacks, err := ioutil.ReadDir(path.Join(engine.Root, "stacks"))
if err != nil {
return nil, err
}
engine.Stacks = stacks
}
engine.DryRun = true
engine.PythonPath = "python"
engine.PipPath = "pip"
engine.jsVM = otto.New()
engine.SetConditions(conditions)
return engine, nil
}
示例11: SearchInstance
func SearchInstance(name string) (instanceName, instancePlatform string) {
instanceName = ""
instancePlatform = ""
rootDir := filepath.Join(HomePath(), ".capstan", "instances")
platforms, _ := ioutil.ReadDir(rootDir)
for _, platform := range platforms {
if !platform.IsDir() {
continue
}
platformDir := filepath.Join(rootDir, platform.Name())
instances, _ := ioutil.ReadDir(platformDir)
for _, instance := range instances {
if !instance.IsDir() {
continue
}
if name != instance.Name() {
continue
}
instanceName = instance.Name()
instancePlatform = platform.Name()
return
}
}
return
}
示例12: cleanup
func cleanup(directory string) {
temp, err := ioutil.TempDir("", "")
isError(err, "")
files, err := ioutil.ReadDir(directory)
isError(err, "")
if 1 == len(files) {
folder := filepath.Join(directory, files[0].Name())
// Move to a tmp directory
files, err = ioutil.ReadDir(folder)
isError(err, "")
for _, file := range files {
var cmd *exec.Cmd
cmd = exec.Command("mv", file.Name(), temp)
cmd.Dir = folder
output, err := cmd.CombinedOutput()
isError(err, string(output))
}
err = os.RemoveAll(folder)
isError(err, "")
// move to the directory
files, err = ioutil.ReadDir(temp)
isError(err, "")
for _, file := range files {
var cmd *exec.Cmd
cmd = exec.Command("mv", file.Name(), directory)
cmd.Dir = temp
output, err := cmd.CombinedOutput()
isError(err, string(output))
}
}
}
示例13: listFiles
// list input files
func (in *inputs) listFiles() error {
list_l1, err := ioutil.ReadDir(in.Directory)
if err != nil {
return err
}
for _, f_l1 := range list_l1 {
if f_l1.Mode()&os.ModeSymlink == os.ModeSymlink {
followed_file, err := os.Readlink(in.Directory + "/" + f_l1.Name())
if err != nil {
return err
}
if followed_file[0] != '/' {
followed_file = in.Directory + "/" + followed_file
}
f_l1, err = os.Lstat(followed_file)
if err != nil {
return err
}
logs.WithField("followed_link", f_l1.Name()).Trace("Followed Link")
}
if f_l1.IsDir() {
list_l2, err := ioutil.ReadDir(in.Directory + "/" + f_l1.Name())
if err != nil {
return err
}
for _, f_l2 := range list_l2 {
in.Files = append(in.Files, f_l1.Name()+"/"+f_l2.Name())
}
} else {
in.Files = append(in.Files, f_l1.Name())
}
}
return nil
}
示例14: getFolders
func getFolders(path string) (map[string][]string, error) {
folders := make(map[string][]string)
codecFileInfos, err := ioutil.ReadDir(path)
if err != nil {
return nil, err
}
for _, codecFileInfo := range codecFileInfos {
if !codecFileInfo.IsDir() {
continue
}
codecFolderName := codecFileInfo.Name()
codecPath := filepath.Join(path, codecFolderName)
caseDirInfos, err := ioutil.ReadDir(codecPath)
if err != nil {
return nil, err
}
for _, caseDirInfo := range caseDirInfos {
if !caseDirInfo.IsDir() {
continue
}
casePath := filepath.Join(codecPath, caseDirInfo.Name())
folders[codecFolderName] = append(folders[codecFolderName], casePath)
}
}
return folders, nil
}
示例15: getLogList
func getLogList() ([]string, error) {
var (
files []string
err error
)
fileData, err := ioutil.ReadDir("dalek/logs")
for _, f := range fileData {
if f.IsDir() {
console, manifest := false, false
fileData, err := ioutil.ReadDir("dalek/logs/" + f.Name())
if err != nil {
return files, err
}
for _, f := range fileData {
if f.Name() == "manifest.json" {
manifest = true
} else if f.Name() == "console.log" {
console = true
}
}
if manifest && console {
files = append(files, f.Name())
}
}
}
return files, err
}