replacer

package
v1.0.10 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2024 License: MIT Imports: 8 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 "gitee.com/the_sun_light/common_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 is a library of replacement file content, supports replacement of files in local directories and embedded directory files via embed.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Field

type Field struct {
	Old             string // old field
	New             string // new field
	IsCaseSensitive bool   // whether the first letter is case-sensitive
}

Field replace field information

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 interface

func New

func New(path string) (Replacer, error)

New create replacer with local directory

func NewFS

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

NewFS create replacer with embed.FS

Jump to

Keyboard shortcuts

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