Skip to content

Commit

Permalink
refactoring (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdalar authored Dec 2, 2023
1 parent 5fbd3a5 commit 50a1352
Show file tree
Hide file tree
Showing 14 changed files with 227 additions and 101 deletions.
6 changes: 6 additions & 0 deletions .pp/parameters_dev.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
local_file:
FilePath: parameters_dev.json
azure_blob:
StorageAccountName: stparampiper
ContainerName: abc
BlobName: parampiper_dev.json
6 changes: 6 additions & 0 deletions .pp/parameters_tst.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
local_file:
FilePath: parameters_tst.json
azure_blob:
StorageAccountName: stparampiper
ContainerName: abc
BlobName: parampiper_tst.json
6 changes: 0 additions & 6 deletions .pp/parampiper.yaml

This file was deleted.

86 changes: 0 additions & 86 deletions cmd/common.go

This file was deleted.

6 changes: 4 additions & 2 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"

"github.com/cdalar/parampiper/pkg/common"
"github.com/spf13/cobra"
)

Expand All @@ -30,9 +31,10 @@ var getCmd = &cobra.Command{
log.Println("[DEBUG] Parameter name: ", args[0])
param.Name = args[0]
}
} else {
log.Println("[DEBUG] Parameter name: ", param.Name)
}

param.Name = args[0]
parameters, err := provider.Read()
if err != nil {
log.Println(err)
Expand All @@ -51,7 +53,7 @@ var getCmd = &cobra.Command{
fmt.Print(p.ToYAML())
case "table":
tmpl := "NAME\tVALUE\tINFO\n{{.Name}}\t{{.Value}}\t{{.Info}}\n"
TabWriter(p, tmpl)
common.TabWriter(p, tmpl)
default:
fmt.Print(p.Value, "\n")
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"log"

"github.com/cdalar/parampiper/pkg/common"
"github.com/spf13/cobra"
)

Expand All @@ -23,6 +24,6 @@ var listCmd = &cobra.Command{

log.Println("[DEBUG]", parameters)
tmpl := "NAME\tVALUE\tINFO\n{{range .}}{{.Name}}\t{{.Value}}\t{{.Info}}\n{{end}}"
TabWriter(parameters, tmpl)
common.TabWriter(parameters, tmpl)
},
}
5 changes: 4 additions & 1 deletion cmd/out.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"log"

"github.com/cdalar/parampiper/pkg/common"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -35,10 +36,12 @@ var outCmd = &cobra.Command{
switch outputType {
case "tfvars":
tmpl = "{{range .}}{{.Name}} = \"{{.Value}}\"\n{{end}}"
case "export":
tmpl = "{{range .}}export {{.Name | upperCase}}=\"{{.Value}}\"\n{{end}}"
default:
tmpl = "NAME\tVALUE\tINFO\n{{range .}}{{.Name}}\t{{.Value}}\t{{.Info}}\n{{end}}"
}

TabWriter(parameters, tmpl)
common.TabWriter(parameters, tmpl)
},
}
6 changes: 3 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/cdalar/parampiper/internal/data"
"github.com/cdalar/parampiper/pkg/common"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand All @@ -18,7 +19,7 @@ var (
log.Println("[DEBUG] Args: " + strings.Join(os.Args, ","))
log.Println("[DEBUG] Configuration File: " + configFilePath)
if len(os.Args) > 1 && os.Args[1] != "init" && os.Args[1] != "version" {
ReadConfig(configFilePath)
common.ReadConfig(configFilePath)
}
log.Println("[DEBUG]", viper.AllSettings())
dataProvider, isEnvExits := os.LookupEnv("PP_DATA")
Expand Down Expand Up @@ -53,7 +54,6 @@ var (

func init() {
rootCmd.PersistentFlags().StringVarP(&configFilePath, "config", "c", ".pp/parampiper.yaml", "Configuration file")

}

// Execute executes the root command.
Expand All @@ -64,7 +64,7 @@ func Execute() error {
func checkDataProvider(dataProvider string) {
log.Println("[DEBUG] Using: " + dataProvider)
if dataProvider != "" {
if !Contains(providerList, dataProvider) {
if !common.Contains(providerList, dataProvider) {
log.Println("Provider (" + dataProvider + ") is not Supported\nPlease use one of the following: " + strings.Join(providerList, ","))
os.Exit(1)
}
Expand Down
6 changes: 6 additions & 0 deletions cmd/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"log"

"github.com/cdalar/parampiper/internal/data"
"github.com/cdalar/parampiper/pkg/common"
"github.com/spf13/cobra"
)

Expand All @@ -28,6 +29,11 @@ var addCmd = &cobra.Command{
Short: "Add/Update Parameter",
Run: func(cmd *cobra.Command, args []string) {
log.Println("[DEBUG] Add/Update Parameters")
unsupportChars := []rune{'-', '*', ',', '.', ':', ';', '<', '>', '?', '\\', '|', ' '}
if common.ContainsAny(param.Name, unsupportChars) {
log.Println("[ERROR] Parameter name cannot contain any of the following characters:", string(unsupportChars))
return
}
parameters, err := provider.Read()
if err != nil {
log.Println(err)
Expand Down
16 changes: 15 additions & 1 deletion internal/data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"log"
"sort"
"strings"

"gopkg.in/yaml.v2"
Expand All @@ -29,11 +30,24 @@ func (p *Parameters) Add(prm Parameter) {
}
isExists, paramPos := p.IfExists(prm)
if isExists {
if prm.Value == "" {
log.Println("[DEBUG] Parameter value is empty")
log.Println("[DEBUG] Keep Parameter Value: ", (*p)[paramPos].Value)
prm.Value = (*p)[paramPos].Value
}
if prm.Info == "" {
log.Println("[DEBUG] Parameter info is empty")
log.Println("[DEBUG] Keep Parameter Info: ", (*p)[paramPos].Info)
prm.Info = (*p)[paramPos].Info
}
(*p)[paramPos] = prm
return
} else {
*p = append(*p, prm)
}
// Sort the parameters by Name
sort.Slice(*p, func(i, j int) bool {
return (*p)[i].Name < (*p)[j].Name
})
}

func (p *Parameters) Remove(parameterList string) {
Expand Down
12 changes: 12 additions & 0 deletions internal/data/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ func TestParameters_Add(t *testing.T) {
param: Parameter{Name: "", Value: "value2", Info: "info2"},
expected: Parameters{{Name: "param1", Value: "value1", Info: "info1"}},
},
{
name: "Update parameter with Empty Value",
params: Parameters{{Name: "param1", Value: "value1", Info: "info1"}},
param: Parameter{Name: "param1", Value: "", Info: "info2"},
expected: Parameters{{Name: "param1", Value: "value1", Info: "info2"}},
},
{
name: "Update parameter with Empty Info value",
params: Parameters{{Name: "param1", Value: "value1", Info: "info1"}},
param: Parameter{Name: "param1", Value: "value2"},
expected: Parameters{{Name: "param1", Value: "value2", Info: "info1"}},
},
}

for _, tc := range testCases {
Expand Down
37 changes: 37 additions & 0 deletions parameters_dev.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[
{
"name": "environment",
"value": "dev",
"info": ""
},
{
"name": "location",
"value": "westeurope",
"info": ""
},
{
"name": "resource_group_name",
"value": "dev_testing",
"info": ""
},
{
"name": "t1",
"value": "value1",
"info": ""
},
{
"name": "t2",
"value": "t2",
"info": ""
},
{
"name": "t3",
"value": "dev3",
"info": ""
},
{
"name": "testing",
"value": "devValue",
"info": ""
}
]
37 changes: 37 additions & 0 deletions parameters_tst.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[
{
"name": "environment",
"value": "tst",
"info": ""
},
{
"name": "location",
"value": "westeurope",
"info": ""
},
{
"name": "resource_group_name",
"value": "tst_testing",
"info": ""
},
{
"name": "t1",
"value": "value1",
"info": ""
},
{
"name": "t2",
"value": "tst_t2",
"info": ""
},
{
"name": "t3",
"value": "v3",
"info": ""
},
{
"name": "testing",
"value": "tst_testValue",
"info": ""
}
]
Loading

0 comments on commit 50a1352

Please sign in to comment.