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

Docker Commands for Beginners

Docker is similar to VMWare Workstation and Oracle VM VirtualBox. It enables host operating system (for example, Windows) to run guest operating system(s) on top of it.

Docker requires you to enable Hyper-V on Windows 10 Pro, while VirtualBox requires you to disable Hyper-V on Windows 10 Pro, so you cannot run both virtualization technology on the same OS.

Docker is widely preferred because of its efficiency in using computer resources.

  1. List all images
    docker images -a
  2. Remove an image
    docker rmi 
  3. List all containers
    docker ps -a
    docker container ls -a
  4. Pull an image
    docker pull :
    docker pull ubuntu:latest
  5. Run a container
    docker run -d -it --name  
    docker run -d -it --name myubuntu ubuntu
  6. Stop a container
    docker stop 
  7. Remove a container
    docker rm 
  8. Attach to a running container
    docker attach 
  9. Commit a container (save it as an image)
    docker commit  
  10. Execute command
    docker exec  
    docker exec myubuntu bash -c "ls -aF /home; cat /etc/passwd"
  11. Expose container ports to host
    docker run -it -d -p : 
    docker run -it -d -p 9100:9100 -p 9090:9090 --name mycentos centos