Skip to content

Commit f8ad7ac

Browse files
committed
improved initConfig and toasterurl verify
1 parent 853cba3 commit f8ad7ac

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

cmd/cook.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ var cookCmd = &cobra.Command{
1919
Short: "Execute OpenTofu",
2020
Args: cobra.MinimumNArgs(1),
2121
Long: `Execute OpenTofu with generated config from inventory and parameters after --`,
22+
PreRun: func(cmd *cobra.Command, args []string) {
23+
initConfig()
24+
},
2225
Run: func(cmd *cobra.Command, args []string) {
2326
//Creating signal to be handled and send to the child tofu/terraform
2427
sigs := make(chan os.Signal, 2)
@@ -28,10 +31,35 @@ var cookCmd = &cobra.Command{
2831
// Creating Tofug shared structure and filling with values
2932
tofuguStruct := &utils.Tofugu{}
3033

34+
toasterUrl := os.Getenv("toasterurl")
35+
if toasterUrl != "" {
36+
// validate URL format and remove trailing slash if present
37+
if strings.HasSuffix(toasterUrl, "/") {
38+
toasterUrl = strings.TrimRight(toasterUrl, "/")
39+
}
40+
41+
// Basic validation for toasterUrl format
42+
if !strings.HasPrefix(toasterUrl, "https://") {
43+
log.Fatalf("Error: toasterUrl must start with https://")
44+
}
45+
46+
// Check if URL contains credentials and correct domain
47+
urlParts := strings.Split(strings.TrimPrefix(toasterUrl, "https://"), "@")
48+
if len(urlParts) != 2 || urlParts[1] != "toaster.altuhov.su" {
49+
log.Fatalf("Error: toasterUrl must be in format https://ACCOUNTID:PASSWORD@toaster.altuhov.su")
50+
}
51+
52+
// Validate credential part has both account ID and password
53+
credParts := strings.Split(urlParts[0], ":")
54+
if len(credParts) != 2 || credParts[0] == "" || credParts[1] == "" {
55+
log.Fatalf("Error: toasterUrl credentials must include both ACCOUNTID and PASSWORD")
56+
}
57+
}
58+
3159
tofuguStruct.TofiName, _ = cmd.Flags().GetString("tofi")
3260
tofuguStruct.OrgName, _ = cmd.Flags().GetString("org")
3361
tofuguStruct.Workspace, _ = cmd.Flags().GetString("workspace")
34-
tofuguStruct.ToasterUrl = os.Getenv("toasterurl")
62+
tofuguStruct.ToasterUrl = toasterUrl
3563
tofuguStruct.DimensionsFlags, _ = cmd.Flags().GetStringSlice("dimension")
3664
tofuguStruct.TofiPath, _ = filepath.Abs(tofuguStruct.GetStringFromViperByOrgOrDefault("tofies_path") + "/" + tofuguStruct.OrgName + "/" + tofuguStruct.TofiName)
3765
if tofuguStruct.GetStringFromViperByOrgOrDefault("shared_modules_path") != "" {

cmd/root.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func Execute() {
3636
}
3737

3838
func init() {
39-
cobra.OnInitialize(initConfig)
39+
//cobra.OnInitialize(initConfig)
4040
// Here you will define your flags and configuration settings.
4141
// Cobra supports persistent flags, which, if defined here,
4242
// will be global for your application.
@@ -68,6 +68,7 @@ func initConfig() {
6868

6969
// Search config in home directory with name ".cobra" (without extension).
7070
viper.AddConfigPath(home)
71+
viper.AddConfigPath(".")
7172
viper.SetConfigName(".tofugu")
7273
}
7374

@@ -77,5 +78,6 @@ func initConfig() {
7778
log.Println("TofuGu using config file:", viper.ConfigFileUsed())
7879
} else {
7980
log.Println(err.Error())
81+
log.Println("TofuGu using default config with inventory in examples/inventory and tofies in examples/tofies")
8082
}
8183
}

0 commit comments

Comments
 (0)