dir

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2022 License: MIT Imports: 9 Imported by: 3

README

dir

workdir util package.

中文参考: Go 工作目录

Quick Start

import "github.com/x-mod/dir"

//初始化
workdir := dir.New(
    dir.Root("/path/to/workdir"), // 默认工作目录 .
    dir.Folder("config"),         // 增加子文件夹 config
    dir.Folder("logs"),           // 增加子文件夹 logs
)

if err := workdir.Open(); err != nil {
    return err
}

//常规操作
//判断子目录是否存在
//func (d *Dir) DirExists(elems ...string) (bool, error)
exist, err := workdir.DirExists("logs", "20220515") // 判断 [root]/logs/20220515

//判断文件是否存在
//func (d *Dir) Exists(elems ...string) (bool, error)
exist, err := workdir.Exists("logs", "20220515","info.log") // 判断 [root]/logs/20220515/info.log

//获取目录内的文件名
// func (d *Dir) Files(elems ...string) ([]string, error)
files, err := workdir.Files("config")

//获取目录内的子文件夹
// func (d *Dir) Folders(elems ...string) ([]string, error)
folders, err := workdir.Folders("logs")

//判断文件夹是否为空
// func (d *Dir) IsEmpty(elems ...string) (bool, error)
empty, err := workdir.IsEmpty("config")

//创建子目录
//func (d *Dir) Mkdir(elems ...string) (string, error)
pathname, err := workdir.Mkdir("logs", "20220515", "errors") // 创建 [root]/logs/20220515/errors/

//获取绝对路径
//func (d *Dir) Path(elems ...string) string
abspath := workdir.Path("logs", "20220515","info.log") // abs[root]/logs/20220515/info.log

//删除文件夹
//func (d *Dir) Remove(elems ...string) error
workdir.Remove("configs")

//删除非空文件夹
//func (d *Dir) RemoveAll(elems ...string) error
workdir.RemoveAll("logs")

Embed Resource

in main.go:

package main

import (
    "github.com/x-mod/dir"
)

//go:embed embed/config/*
var embeddir embed.FS

func main() {
	dir.EmbedFS = embeddir //全局设置内嵌
	
    ...
}

then copy embed files to the workdir directory:

import "github.com/x-mod/dir"

//初始化工作目录
workdir := dir.New(
    dir.Root("/path/to/workdir"), // 默认工作目录 .
    dir.Folder("config"),         // 增加子文件夹 config
)
if err := workdir.Open(); err != nil {
    return err
}

//将内嵌资源复制到 config 子目录
if err := workdir.EmbedSkip(1, "embed"); err != nil {
    return fmt.Errorf("workdir embed: %w", err)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var EmbedFS embed.FS

Functions

This section is empty.

Types

type Dir

type Dir struct {
	// contains filtered or unexported fields
}

func New

func New(opts ...Option) *Dir

func (*Dir) CopyDir added in v0.1.0

func (d *Dir) CopyDir(root string, elems ...string) error

func (*Dir) CopyFile added in v0.1.0

func (d *Dir) CopyFile(file string, elems ...string) error

func (*Dir) DirExists

func (d *Dir) DirExists(elems ...string) (bool, error)

func (*Dir) Embed

func (d *Dir) Embed(elems ...string) error

func (*Dir) EmbedSkip

func (d *Dir) EmbedSkip(skip int, elems ...string) error

func (*Dir) Exists

func (d *Dir) Exists(elems ...string) (bool, error)

func (*Dir) Files

func (d *Dir) Files(elems ...string) ([]string, error)

func (*Dir) Folders

func (d *Dir) Folders(elems ...string) ([]string, error)

func (*Dir) IsEmpty

func (d *Dir) IsEmpty(elems ...string) (bool, error)
func (d *Dir) Link(filename string, elems ...string) error

func (*Dir) Mkdir

func (d *Dir) Mkdir(elems ...string) (string, error)

func (*Dir) Open

func (d *Dir) Open() error

func (*Dir) Path

func (d *Dir) Path(elems ...string) string

func (*Dir) Remove

func (d *Dir) Remove(elems ...string) error

func (*Dir) RemoveAll

func (d *Dir) RemoveAll(elems ...string) error

func (*Dir) Stat

func (d *Dir) Stat(elems ...string) (os.FileInfo, error)

func (*Dir) String

func (d *Dir) String() string
func (d *Dir) SymLink(source string, target string) error
func (d *Dir) Unlink(elems ...string) error

type Option

type Option func(*Dir)

func CreateMode

func CreateMode(mode os.FileMode) Option

func Folder

func Folder(elems ...string) Option

func ForceEmbed

func ForceEmbed(v bool) Option

func Root

func Root(root string) Option

Jump to

Keyboard shortcuts

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