github.com/singularityware/singularity@v3.1.1+incompatible/etc/conf/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  	"fmt"
    10  	"os"
    11  	"path/filepath"
    12  
    13  	"github.com/sylabs/singularity/internal/pkg/runtime/engines/config"
    14  	singularityConfig "github.com/sylabs/singularity/internal/pkg/runtime/engines/singularity/config"
    15  )
    16  
    17  func main() {
    18  	Args := os.Args
    19  
    20  	if len(Args) < 3 || len(Args) > 4 {
    21  		fmt.Println("Usage: go run ... <template> [infile] <outfile>")
    22  		os.Exit(1)
    23  	}
    24  
    25  	tmplPath := filepath.Clean(Args[1])
    26  	outPath := filepath.Clean(Args[2])
    27  
    28  	inPath := ""
    29  	if len(Args) == 4 {
    30  		inPath = filepath.Clean(Args[2])
    31  		outPath = filepath.Clean(Args[3])
    32  	}
    33  
    34  	genConf(tmplPath, inPath, outPath)
    35  }
    36  
    37  // genConf produces a singularity.conf file at out. It retains set configurations from in (leave blank for default)
    38  func genConf(tmpl, in, out string) {
    39  	inFile := in
    40  	// Parse current singularity.conf file into c
    41  	c := &singularityConfig.FileConfig{}
    42  	if _, err := os.Stat(in); os.IsNotExist(err) {
    43  		inFile = ""
    44  	}
    45  	if err := config.Parser(inFile, c); err != nil {
    46  		fmt.Printf("Unable to parse singularity.conf file: %s\n", err)
    47  		os.Exit(1)
    48  	}
    49  
    50  	newOutFile, err := os.OpenFile(out, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
    51  	if err != nil {
    52  		fmt.Printf("Unable to create file %s: %v\n", out, err)
    53  	}
    54  	defer newOutFile.Close()
    55  
    56  	if err := config.Generate(newOutFile, tmpl, c); err != nil {
    57  		fmt.Printf("Unable to generate config file: %v\n", err)
    58  		os.Exit(1)
    59  	}
    60  }