replacer

package
v1.4.41 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2026 License: MIT Imports: 9 Imported by: 0

README

replacer

A library of replacement file content, supports replacement of files in local directories and embedded directory files via embed.


Example of use

import "github.com/18721889353/sunshine/pkg/replacer"

//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)
	}
    subDirs := []string{"testDir/replace"}
    subFiles := []string{"testDir/foo.txt"}
    ignoreDirs := []string{"testDir/ignore"}
    ignoreFiles := []string{"test.txt"}
    fields := []Field{
        {
            Old: "1234",
            New: "....",
        },
        {
            Old:             "abcdef",
            New:             "hello_",
            IsCaseSensitive: true,
        },
    }
    r.SetSubDirsAndFiles(subDirs, subFiles...) // process only specified subdirectories and files
	r.SetIgnoreDirs(ignoreDirs...)   // specify the directory in the subdirectory where processing is ignored
	r.SetIgnoreFiles(ignoreFiles...)   // specify the files in the subdirectory to be ignored for processing
	r.SetReplacementFields(fields)   // set replacement fields
	r.SetOutPath("", "test")             // set output directory, if empty, generate file output folder based on name and time
	err = r.SaveFiles()                   // save the replaced file
	if err != nil {
		panic(err)
	}

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

Documentation

Overview

Package replacer 是一个用于替换文件内容的库,支持本地目录和嵌入目录文件的替换。

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 替换字段信息结构体,定义单个字段的替换规则

字段说明:

Old             - 要被替换的旧字符串
New             - 替换后的新字符串
IsCaseSensitive - 是否区分大小写匹配
                  当为 true 且首字符为字母时,会自动生成首字母大写和小写两个替换规则

type Replacer

type Replacer interface {
	// SetReplacementFields 设置替换字段
	SetReplacementFields(fields []Field)
	// SetSubDirsAndFiles 设置子目录和文件列表,其他目录中的文件将被忽略
	SetSubDirsAndFiles(subDirs []string, subFiles ...string)
	// SetIgnoreSubDirs 设置忽略的子目录
	SetIgnoreSubDirs(dirs ...string)
	// SetIgnoreSubFiles 设置忽略的文件
	SetIgnoreSubFiles(filenames ...string)
	// SetOutputDir 设置输出目录
	SetOutputDir(absDir string, name ...string) error
	// GetOutputDir 获取输出目录
	GetOutputDir() string
	// GetSourcePath 获取源目录
	GetSourcePath() string
	// SaveFiles 保存文件
	SaveFiles() error
	// ReadFile 读取文件内容
	ReadFile(filename string) ([]byte, error)
	// GetFiles 获取文件列表
	GetFiles() []string
	// SaveTemplateFiles 保存模板文件
	SaveTemplateFiles(m map[string]interface{}, parentDir ...string) error
}

Replacer 接口定义了文件替换器的行为

func New

func New(path string) (Replacer, error)

New 创建使用本地目录的替换器

参数:

path - 本地模板目录路径

返回值:

Replacer - 替换器实例
error - 如果目录不存在或路径解析失败则返回错误

Jump to

Keyboard shortcuts

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