github.com/hpcng/singularity@v3.1.1+incompatible/internal/pkg/buildcfg/confgen/gen.go (about)

     1  // Copyright (c) 2018-2019, Sylabs Inc. All rights reserved.
     2  // This software is licensed under a 3-clause BSD license. Please consult the
     3  // LICENSE.md file distributed with the sources of this project regarding your
     4  // rights to use or distribute this software.
     5  
     6  package main
     7  
     8  import (
     9  	"bufio"
    10  	"bytes"
    11  	"fmt"
    12  	"io/ioutil"
    13  	"os"
    14  	"strings"
    15  	"text/template"
    16  )
    17  
    18  func parseLine(s string) (d Define) {
    19  	d = Define{
    20  		Words: strings.Fields(s),
    21  	}
    22  
    23  	return
    24  }
    25  
    26  // Define is a struct that contains one line of configuration words.
    27  type Define struct {
    28  	Words []string
    29  }
    30  
    31  // WriteLine writes a line of configuration.
    32  func (d Define) WriteLine() (s string) {
    33  	s = "const " + d.Words[1] + " = " + d.Words[2]
    34  
    35  	if len(d.Words) > 3 {
    36  	}
    37  
    38  	for _, w := range d.Words[3:] {
    39  		s += " + " + w
    40  	}
    41  	return s
    42  }
    43  
    44  var confgenTemplate = template.Must(template.New("").Parse(`// Code generated by go generate; DO NOT EDIT.
    45  package buildcfg
    46  {{ range $i, $d := . }}
    47  {{$d.WriteLine -}}
    48  {{end}}
    49  `))
    50  
    51  func main() {
    52  	outFile, err := os.Create("config.go")
    53  	if err != nil {
    54  		fmt.Println(err)
    55  		return
    56  	}
    57  	defer outFile.Close()
    58  
    59  	inFile, err := ioutil.ReadFile(os.Args[1])
    60  	if err != nil {
    61  		fmt.Println(err)
    62  		return
    63  	}
    64  
    65  	header := []Define{}
    66  	s := bufio.NewScanner(bytes.NewReader(inFile))
    67  	for s.Scan() {
    68  		d := parseLine(s.Text())
    69  		if len(d.Words) > 2 && d.Words[0] == "#define" {
    70  			header = append(header, d)
    71  		}
    72  	}
    73  
    74  	confgenTemplate.Execute(outFile, header)
    75  }