replacer

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2022 License: MIT Imports: 8 Imported by: 0

README

replacer

一个替换目录下文件内容库,支持本地目录下文件和通过embed嵌入目录文件替换。


使用示例

//go:embed dir
var fs embed.FS

func demo(){
	//r, err := replacer.New("dir")
	//if err != nil {
	//	panic(err)
	//}
	r, err := replacer.NewWithFS("dir", fs)
	if err != nil {
		panic(err)
	}

	ignoreFiles := []string{}
	fields := []replacer.Field{
		{
			Old: "1234",
			New: "8080",
		},
		{
			Old:             "abcde",
			New:             "hello",
			IsCaseSensitive: true,  // abcde-->hello, Abcde-->Hello
		},
	}
	r.SetSubDirs(subPaths...)          // 只处理指定子目录,优先级最高
	r.SetIgnoreDirs(ignoreDirs...)   // 指定子目录下忽略处理的目录
	r.SetIgnoreFiles(ignoreFiles...)   // 指定子目录下忽略处理的文件
	r.SetReplacementFields(fields)   // 设置替换文本
	r.SetOutPath("", "test")             // 设置输出目录,如果为空,根据名称和时间生成文件输出文件夹
	err = r.SaveFiles()                   // 保存替换后文件
	if err != nil {
		panic(err)
	}

	fmt.Printf("save files successfully, out = %s\n", replacer.GetOutPath())
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Field

type Field struct {
	Old             string // 模板字段
	New             string // 新字段
	IsCaseSensitive bool   // 第一个字母是否区分大小写
}

Field 替换字段信息

type Replacer

type Replacer interface {
	SetReplacementFields(fields []Field)
	SetSubDirsAndFiles(subDirs []string, subFiles ...string)
	SetIgnoreSubDirs(dirs ...string)
	SetIgnoreSubFiles(filenames ...string)
	SetOutputDir(absDir string, name ...string) error
	GetOutputDir() string
	GetSourcePath() string
	SaveFiles() error
	ReadFile(filename string) ([]byte, error)
}

Replacer 接口

func New

func New(path string) (Replacer, error)

New 根据指定路径创建replacer

func NewFS added in v1.4.0

func NewFS(path string, fs embed.FS) (Replacer, error)

NewFS 根据嵌入的路径创建replacer

Jump to

Keyboard shortcuts

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