Skip to content

jurjevic/golf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

golf

The Go Language Facilitator tool is used to process in-file code while copying a source to a destination file. It is basically helpful when different files are required for other build destinations, but templating is not provided out-of-the-box. This gives you the possibility to have different content in files for e.g. local and server builds.

Install

#  Version: 0.10.0
# Required: Go 1.16+

go install github.com/jurjevic/golf@latest

Usage

golf source_file target_file [-i=<file>] [-v] [-- {initialzation}]
  • source_file is the input file which will be processed.
  • target_file will be created or overwritten with the processed input.
  • -i is optional and includes the file for evaluation before the processing is started. A local storage or HTTP/HTTPS URI is supported.
  • -v is optional and produces verbose outputs.
  • -- is optional and separates the initialization code, which is interpreted before the processing starts.

The source and destination files may be the same, so the source file will be overwritten.

# if you have cloned the golf repository, you can test it with ...
golf README.md README.tmp -v

Details

golf supports the Go language for processing text lines. Functions are introduced by ### with a whitespace before and after this token and are usually placed inside a comment in the source file. Depending on the source file type, comments may start if different charters and sequences. golf is not aware of any file types, so comments are agnostic to it and the developer has to take care of it.

The "strings" and "fmt" packages are dot imported by default. That means you don't have to use the package prefix to access their global functions. Other Go packages can be used to, but you would have to import them manually.

If the last statement process has a string type result, its value is used to replace the line. This can be easily avoided by using a different type at the end of the statement.

Built-in variables

// line represents the current line containing the string on the left side of '###'.
var line string

// fline represents the current line.
var line string

// token is the sliced line of strings splitted by whitespaces.
var token []string

// ftoken is the full line of strings splitted by whitespaces.
var ftoken []string

// repeat will execute the following line statements also for the next lines,
// until a new statement is defined.
var repeat bool

// next will execute the statement for the current and all next defined amount of lines. It will also stop, when a new statement is defined.
var next uint

// arg are optional key values pairs, which can be assign from
// outside at initialization time.
var arg map[string]interface{}

Built-in functions

// include the file for evaluation. This is similar with the -i option, but it is not evaluated before the processing. A local storage or HTTP/HTTPS URI is supported.
func include(resource string)

// isSet returns true if the key is found in the arg map
func isSet(key string) bool

Include files

Go files can be included before or while the processing is executed. Files can be located from a storage or website (e.g. http://...). With the given example, the statement # ### commentIf(true, line) can be used.

package sh

import "strings"

func commentIf(cond bool, line string) string {
	if cond {
		line = "# " + line
	}
	return line
}

func uncommentIf(cond bool, line string) string {
	if cond {
		line = strings.TrimLeft(line, " ")
		if strings.HasPrefix(line, "#") {
			line = line[1:]
			line = strings.TrimLeft(line, " ")
		}
	}
	return line
}

The Testable Examples below provide a short tutorial for this feature.

Examples

Simple line examples

world // ### "hello " + line
hello world //

Simple full line examples

world // ### "hello " + fline
hello world // ### "hello " + fline

Simple token examples

hello my friend // ### token[0] + " world"
hello world

Replace token examples

drive the rabbit home // ### token[2] = "cow"; Join(token, " ")
drive the cow home

Replace full token examples

drive the rabbit home // ### ftoken[2] = "cow"; Join(ftoken, " ")
drive the horse home // ### ftoken[2] = "horse"; Join(ftoken, " ")

Advanced token examples

hello my friend // ### "BE " + ToUpper(Join(token[1:], " "))
BE MY FRIEND //

Advanced processing examples

hello my friend // ### otherToken := token
let it be // ### anotherToken := token
goodbye dad  // ### token[0] + " and " + Join(anotherToken[:3], " ") + " " + Join(otherToken[1:], " ")
hello my friend // ### otherToken := token
let it be // ### anotherToken := token
goodbye and let it be my friend //

Ignore processing examples

what is wrong // ### s := Sprintf("nothing %s", Join(token[1:], " ")); false
what was wrong // ### s
what is wrong // ### s := Sprintf("nothing %s", Join(token[1:], " ")); false
nothing is wrong //

Repeat examples

Hello number one ### repeat = true; "# " + line
Hello number two
Hello number three
Hello number four
Hello number five ### line + " and five"
Hello number six
# Hello number one
# Hello number two
# Hello number three
# Hello number four
Hello number five and five
Hello number six

Next examples

// ### next = 2; "// " + line
red
blue
green
yellow
// //
// red
// blue
green
yellow

Custom processing examples

// ### func modExample(line string) string { return "_" + line + "_" }
Spaceship // ### modExample(token[0])
// ### func modExample(line string) string { return "_" + line + "_" }
_Spaceship_

Initialize examples

// the output will change depending whether you use an initialization:
// golf README.md README.tmp -v -- 'arg["second"] = true'
// ### if arg["second"] == true { line = "Loud out" }; line
// the output will change depending whether you use an initialization:
// golf README.md README.tmp -v -- 'arg["second"] = true'
//

Bash examples:

echo "hello my friend $1" # ### Join(token[:4], " ") + " $2\""
echo "hello my friend $2" #

Bad examples

Undefined processing token:

echo "hello" #### Join(token[1:], " ")
echo "world" ## Join(token[1:], " ")
echo "hello" #### Join(token[1:], " ")
echo "world" ## Join(token[1:], " ")

The processing is ignored, because the token ### is missing (including before and after a whitespace).

Testable simple example

  • red.sh Input file with simple example.
# run without processing
chmod +x red.sh && ./red.sh

# perform processing
golf red.sh blue.sh

# run processed example
chmod +x blue.sh && ./blue.sh

Testable advanced example

  • gen.sh Call example for golf command.
  • red.sh Input file with advanced examples.
  • sh.go Helper functions for shell processing.
# run without processing
chmod +x red.sh && ./red.sh

# perform processing
./gen.sh

# run processed example
chmod +x blue.sh && ./blue.sh

# perform another processing
./gen.sh green

# run green processed example
chmod +x green.sh && ./green.sh

Testable include example

  • gen.sh Call example for golf command.
  • red.sh Input file with advanced examples.
  • sh.go Helper functions for shell processing.
# run without processing
chmod +x red.sh && ./red.sh

# perform processing
./gen.sh

# run processed example
chmod +x blue.sh && ./blue.sh

# perform another processing
./gen.sh green

# run green processed example
chmod +x green.sh && ./green.sh

Credits

golf is uses github.com/traefik/yaegi as the Go language interpreter.

License

MIT

About

Go Language Facilitator

Resources

License

Stars

Watchers

Forks

Packages

No packages published