2022-02-09 19:42:28 +03:00
|
|
|
/*
|
|
|
|
_____ _____ _____ ____ ______ _____ ------
|
|
|
|
| | | | | | | | | | | | |
|
|
|
|
| | | | | | | | | | | | |
|
|
|
|
| --- | | | | |-----| |---- | | |-----| |----- ------
|
|
|
|
| | | | | | | | | | | | |
|
|
|
|
| ____| |_____ | ____| | ____| | |_____| _____| |_____ |_____
|
|
|
|
|
|
|
|
|
|
|
|
Licensed under the MIT License <http://opensource.org/licenses/MIT>.
|
|
|
|
|
2024-01-02 13:10:39 +03:00
|
|
|
Copyright © 2020-2024 Microsoft Corporation. All rights reserved.
|
2022-02-09 19:42:28 +03:00
|
|
|
Author : <blobfusedev@microsoft.com>
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
SOFTWARE
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2022-09-08 23:09:32 +03:00
|
|
|
"fmt"
|
2022-02-09 19:42:28 +03:00
|
|
|
"os"
|
|
|
|
"regexp"
|
2022-06-30 19:01:07 +03:00
|
|
|
"strings"
|
2022-02-09 19:42:28 +03:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type configGenOptions struct {
|
|
|
|
configFilePath string
|
|
|
|
outputConfigPath string
|
|
|
|
containerName string
|
|
|
|
tempDirPath string
|
|
|
|
}
|
|
|
|
|
|
|
|
var opts configGenOptions
|
|
|
|
var templatesDir = "testdata/config/"
|
|
|
|
|
|
|
|
var generateTestConfig = &cobra.Command{
|
2022-04-16 02:35:57 +03:00
|
|
|
Use: "gen-test-config",
|
|
|
|
Short: "Generate config file for testing given an output path.",
|
|
|
|
Long: "Generate config file for testing given an output path.",
|
|
|
|
SuggestFor: []string{"conv test config", "convert test config"},
|
|
|
|
Hidden: true,
|
|
|
|
Args: cobra.ExactArgs(0),
|
|
|
|
FlagErrorHandling: cobra.ExitOnError,
|
2022-02-09 19:42:28 +03:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2022-06-30 19:01:07 +03:00
|
|
|
var templateConfig []byte
|
|
|
|
var err error
|
2022-09-08 23:09:32 +03:00
|
|
|
|
2022-06-30 19:01:07 +03:00
|
|
|
if strings.Contains(opts.configFilePath, templatesDir) {
|
2023-05-10 12:23:05 +03:00
|
|
|
templateConfig, err = os.ReadFile(opts.configFilePath)
|
2022-06-30 19:01:07 +03:00
|
|
|
} else {
|
2023-05-10 12:23:05 +03:00
|
|
|
templateConfig, err = os.ReadFile(templatesDir + opts.configFilePath)
|
2022-06-30 19:01:07 +03:00
|
|
|
}
|
2022-09-08 23:09:32 +03:00
|
|
|
|
2022-02-09 19:42:28 +03:00
|
|
|
if err != nil {
|
2022-09-08 23:09:32 +03:00
|
|
|
return fmt.Errorf("failed to read file [%s]", err.Error())
|
2022-02-09 19:42:28 +03:00
|
|
|
}
|
2022-09-08 23:09:32 +03:00
|
|
|
|
2022-02-09 19:42:28 +03:00
|
|
|
// match all parameters in { }
|
|
|
|
re := regexp.MustCompile("{.*?}")
|
|
|
|
templateParams := re.FindAll(templateConfig, -1)
|
|
|
|
newConfig := string(templateConfig)
|
2022-09-08 23:09:32 +03:00
|
|
|
|
2022-02-09 19:42:28 +03:00
|
|
|
for _, param := range templateParams {
|
|
|
|
// { 0 } -> container name
|
|
|
|
// { 1 } -> temp path
|
|
|
|
if string(param) == "{ 0 }" {
|
|
|
|
re := regexp.MustCompile(string(param))
|
|
|
|
newConfig = re.ReplaceAllString(newConfig, opts.containerName)
|
|
|
|
} else if string(param) == "{ 1 }" {
|
|
|
|
re := regexp.MustCompile(string(param))
|
|
|
|
newConfig = re.ReplaceAllString(newConfig, opts.tempDirPath)
|
|
|
|
} else {
|
|
|
|
envVar := os.Getenv(string(param)[2 : len(string(param))-2])
|
|
|
|
re := regexp.MustCompile(string(param))
|
|
|
|
newConfig = re.ReplaceAllString(newConfig, envVar)
|
|
|
|
}
|
|
|
|
}
|
2022-09-08 23:09:32 +03:00
|
|
|
|
2022-02-09 19:42:28 +03:00
|
|
|
// write the config with the params to the output file
|
2023-05-10 12:23:05 +03:00
|
|
|
err = os.WriteFile(opts.outputConfigPath, []byte(newConfig), 0700)
|
2022-09-08 23:09:32 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to write file [%s]", err.Error())
|
2022-02-09 19:42:28 +03:00
|
|
|
}
|
2022-09-08 23:09:32 +03:00
|
|
|
|
2022-02-09 19:42:28 +03:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(generateTestConfig)
|
|
|
|
generateTestConfig.Flags().StringVar(&opts.configFilePath, "config-file", "", "Input config file.")
|
|
|
|
generateTestConfig.Flags().StringVar(&opts.outputConfigPath, "output-file", "", "Output config file path.")
|
|
|
|
generateTestConfig.Flags().StringVar(&opts.containerName, "container-name", "", "Container name.")
|
|
|
|
generateTestConfig.Flags().StringVar(&opts.tempDirPath, "temp-path", "", "Temporary file path.")
|
|
|
|
|
|
|
|
}
|