Command called 'sprinkle' I have a command called sprinkle. I'm not going to explain anything about it, because I want whoever that reviews the code to also tell me how much of the command documentation they understood.

Here is what I need help with:


Is the documentation good enough? Is it too verbose?
Does the constant domainName help you understand the code below, or is it useless?
Should the transformations array be passed around instead of being defined as a global variable?
What else can I improve in the code?




// The sprinkle command allows the user to define some transformations in a file called test.txt.
// The user can then provide some domain names such as 'go' and the application will randomly transform it, using the transformations defined in test.txt.
// Every transformation in the test.txt file must have an asterisk (*) in place of the domain name. For example, if the test file contains:
//
// *-is-awesome.com
//
// love-*.net
//
// lets-*.org
// 
// Then, if the user enters 'go' in the command line, the output could be something like:
//
// go-is-awesome.com
package main

import (
    "bufio"
    "fmt"
    "log"
    "math/rand"
    "os"
    "strings"
    "time"
)

const (
    domainName = "*"
)

var transformations []string

func main() {
    var err error

    transformations, err = readLinesFromFile("test.txt")
    if err != nil {
        log.Fatal(err)
    }

    inputScanner := bufio.NewScanner(os.Stdin)
    for inputScanner.Scan() {
        fmt.Println(applyRandomTransformation(inputScanner.Text()))
    }
}

// readLinesFromFile reads a file into an array. Each element in the array is one line of the text file.
func readLinesFromFile(path string) ([]string, error) {
    var lines []string
    file, err := os.Open("test.txt")
    if err != nil {
        return nil, err
    }
    defer file.Close()

    scanner := bufio.NewScanner(file)
    for scanner.Scan() {
        lines = append(lines, scanner.Text())
    }

    return lines, nil
}

// applyRandomTransformation randomly chooses a transformation from the list of transformations, and replaces the '*' in the transformation with input
func applyRandomTransformation(input string) string {
    rand.Seed(time.Now().UnixNano())
    transformation := transformations[rand.Intn(len(transformations))]
    return strings.Replace(transformation, domainName, input, -1)
}
First, look at your documentation to see how will look to users:


  The Go Blog
  
  Godoc: documenting Go code


$ go doc sprinkle
The sprinkle command allows the user to define some transformations in a
file called test.txt. The user can then provide some domain names such as
'go' and the application will randomly transform it, using the
transformations defined in test.txt. Every transformation in the test.txt
file must have an asterisk (*) in place of the domain name. For example, if
the test file contains:

*-is-awesome.com

love-*.net

lets-*.org

Then, if the user enters 'go' in the command line, the output could be
something like:

go-is-awesome.com
$


Amongst other things, domain name is a misnomer.

Second, look at your sprinkle command help:

$ ./sprinkle -help
$


Third, read your code. And, fourth, run your code.

Find and fix several bugs (others have identified some of them). Since domainName is a misnomer, rename it to wildcard. 

Rewrite the sprinkle command go doc description:

$ go doc 
The sprinkle command transforms a random line of text from the sprinkle.txt
file by replacing asterisk (*) wildcard characters with text from stdin.

For example,

    $ cat sprinkle.txt
    *-is-awesome.com
    love-*.net
    lets-*.org
    $ echo go | ./sprinkle
    love-go.net
    $ echo go | ./sprinkle
    go-is-awesome.com
    $
$


TODO: provide sprinkle command help.