replacer

package
v1.1.58 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2025 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 替换字段信息结构体

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)                              // 读取文件内容
	GetFiles() []string                                                    // 获取文件列表
	SaveTemplateFiles(m map[string]interface{}, parentDir ...string) error // 保存模板文件
}

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

func New

func New(path string) (Replacer, error)

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

func NewFS

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

NewFS 创建使用嵌入目录的替换器

Jump to

Keyboard shortcuts

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