/
gen.go
56 lines (45 loc) · 1.46 KB
/
gen.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright (c) 2018, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/sylabs/singularity/src/runtime/engines/config"
"github.com/sylabs/singularity/src/runtime/engines/singularity"
)
func main() {
Args := os.Args
if len(Args) < 3 || len(Args) > 4 {
fmt.Println("Usage: go run ... <template> [infile] <outfile>")
os.Exit(1)
}
tmplPath := filepath.Clean(Args[1])
outPath := filepath.Clean(Args[2])
inPath := ""
if len(Args) == 4 {
inPath = filepath.Clean(Args[2])
outPath = filepath.Clean(Args[3])
}
genConf(tmplPath, inPath, outPath)
}
// genConf produces a singularity.conf file at out. It retains set configurations from in (leave blank for default)
func genConf(tmpl, in, out string) {
// Parse current singularity.conf file into c
c := &singularity.FileConfig{}
if err := config.Parser(in, c); err != nil {
fmt.Printf("Unable to parse singularity.conf file: %s\n", err)
os.Exit(1)
}
newOutFile, err := os.OpenFile(out, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
fmt.Printf("Unable to create file %s: %v\n", out, err)
}
defer newOutFile.Close()
if err := config.Generate(newOutFile, tmpl, c); err != nil {
fmt.Printf("Unable to generate config file: %v\n", err)
os.Exit(1)
}
}