Export multi-line environment variable, replace a variable in file with the former using Go

There are times you want to replace a string in a file with a multi-line environment variable.

For example, you create a Jenkins pipeline and have a multi-line / text build parameter. You want to replace a string in file with the multi-line build parameter.

Using Linux utility such as sed and awk would be complicated because you need to escape some characters in the multi-line environment variable such as ‘=’, ‘/’, ‘\’ and so on.

There are reasons why Python 2.7 is installed by default in Linux distros like Ubuntu and RHEL, and there are reasons why sysadmins prefer to use Go instead of Python.

In this article I show an example how to replace a string with the multi-line environment variable from Jenkins using Go.

1. Write the multi-line environment variable to a file

#!/bin/bash
echo -e "$my_variable" > my_variable

2. Create a Go script to replace a string with the variable

package main

import (
	"bytes"
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"strings"
)

func main() {

}

3. Write codes to parse command line arguments

	var filepath, replace, sSourceTextFile string
	flag.StringVar(&filepath, "file", "myfile.txt", "source file containing text to replace")
	flag.StringVar(&replace, "replace", "foo", "string to replace")
	flag.StringVar(&source, "source", "variable", "source file containing multi-line string/text to be replaced with")
	flag.Parse()

The codes above add command line arguments to the Go script.
The first argument is filepath, which points to the file path having the string that we want to replace
The second argument is the string that we want to replace
The third argument is the file containing the multi-line environment variable that we want to replace the string with

4. Read the file into memory

	// read the source file into memory
	input, err := ioutil.ReadFile(filepath)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

5. Read the file containing multi-line environment variable into memory

	// read the source text file into memory
	text, err := ioutil.ReadFile(source)
	if err != nil {
		fmt.Print(err)
		os.Exit(1)
	}

6. Perform the string replacement

	output := bytes.Replace(input, []byte(replace), []byte(text), -1)

7. Write the output into the same file

	if err = ioutil.WriteFile(filepath, output, 0666); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

8. Compile the codes
go build script-name.go

9. Run/test
./script-name -filepath=myfile.txt -replace=replaceme -source=my_variable

Leave a Reply