GoTextReplace

package module
v0.0.0-...-bef5645 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2023 License: MIT Imports: 7 Imported by: 0

README

GoTextReplace

GoTextReplace is a powerful Go package designed to facilitate bulk text replacement, making it effortless to find and replace content in multiple files. Whether you're managing configuration files, templating, or any other text-based document manipulation, GoTextReplace has you covered.

Minimum Go Version Requirement: 1.15

Quick Start

To quickly get started with GoTextReplace, follow these simple steps:

  1. Import the package:

    import "github.com/Saranyan-98/GoTextReplace"
    
  2. Initialize the TextReplace struct with your configuration:

replacer := GoTextReplace.TextReplace{
    Filename: "sample.txt",
    YAMLfile: "tags.yaml",
}
  1. Use the TextReplace method to execute bulk text replacement:
err := replacer.Run()
if err != nil {
    fmt.Println(err)
}
Sample YAML file

Here's an example of a YAML configuration file (tags.yaml):

name: saranyan
company: XYZ
Sample Text file

The source file (sample.txt) contains handlebar tags that need replacement:

My name is {{name}} and I work at {{company}}
Custom Output File

You can also specify a custom output file and path for the modified content:

replacer := GoTextReplace.TextReplace{
    Filename:       "sample.txt",
    YAMLfile:       "tags.yaml",
    OutputFileName: "final.txt",
    OutputPath:     "./output",
}

err := replacer.Run()
if err != nil {
    fmt.Println(err)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckError

func CheckError(err error)

Generic Function to Check the errors

func InterfaceToString

func InterfaceToString(value interface{}) string

Generic Function to convert Interface to String

Types

type Tags

type Tags struct {
	Names []string
}

type TextReplace

type TextReplace struct {
	Tags           Tags
	Filename       string
	FileObj        *os.File
	YAMLfile       string
	OutputPath     string
	OutputFileName string
	Keys           map[string]interface{}
}

YAML file and source file path should be assigned to the Struct, then Mapper method needs to be called to execute the task

func (*TextReplace) GetYAMLValues

func (t *TextReplace) GetYAMLValues() (map[string]interface{}, error)

Get YAML Key-Values from file

func (*TextReplace) Reader

func (t *TextReplace) Reader() (Tags, *os.File, error)

Read the file and get the Tags

func (*TextReplace) Replace

func (t *TextReplace) Replace() error

Replace the Tags with corresponding Values from the YAML

func (*TextReplace) Run

func (t *TextReplace) Run() error

Final Mapper Function which executes the logics in sync

func (*TextReplace) Validate

func (t *TextReplace) Validate() error

Validate to check whether all the keys present in the file is present in the YAML file

Jump to

Keyboard shortcuts

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