mirror of
https://github.com/SAP/jenkins-library.git
synced 2025-03-03 15:02:35 +02:00
feat (cpe) ability to write an empty string to an existing cpe string value (#3243)
* empty cpe values from disk and cpe map on condition * changing log level * changing log level from info to edbug * changing empty logic for empty string * adding toBeEmptied condition Co-authored-by: Your Name <you@example.com> Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
This commit is contained in:
parent
395bdc58f6
commit
da1327ab9a
@ -4,12 +4,13 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/SAP/jenkins-library/pkg/log"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/SAP/jenkins-library/pkg/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CPEMap represents the common pipeline environment map
|
// CPEMap represents the common pipeline environment map
|
||||||
@ -82,19 +83,40 @@ func dirToMap(m map[string]interface{}, dirPath, prefix string) error {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// load file content and unmarshal it if needed
|
// load file content and unmarshal it if needed
|
||||||
mapKey, value, err := readFileContent(path.Join(dirPath, dirItem.Name()))
|
mapKey, value, toBeEmptied, err := readFileContent(path.Join(dirPath, dirItem.Name()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
m[path.Join(prefix, mapKey)] = value
|
if toBeEmptied {
|
||||||
|
err := addEmptyValueToFile(path.Join(dirPath, dirItem.Name()))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
log.Entry().Debugf("Writing empty contents to file on disk: %v", path.Join(dirPath, dirItem.Name()))
|
||||||
|
|
||||||
|
m[path.Join(prefix, mapKey)] = ""
|
||||||
|
|
||||||
|
} else {
|
||||||
|
m[path.Join(prefix, mapKey)] = value
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func readFileContent(fullPath string) (string, interface{}, error) {
|
func addEmptyValueToFile(fullPath string) error {
|
||||||
|
err := ioutil.WriteFile(fullPath, []byte(""), 0666)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func readFileContent(fullPath string) (string, interface{}, bool, error) {
|
||||||
|
toBeEmptied := false
|
||||||
|
|
||||||
fileContent, err := ioutil.ReadFile(fullPath)
|
fileContent, err := ioutil.ReadFile(fullPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", nil, err
|
return "", nil, toBeEmptied, err
|
||||||
}
|
}
|
||||||
fileName := filepath.Base(fullPath)
|
fileName := filepath.Base(fullPath)
|
||||||
|
|
||||||
@ -105,9 +127,12 @@ func readFileContent(fullPath string) (string, interface{}, error) {
|
|||||||
decoder.UseNumber()
|
decoder.UseNumber()
|
||||||
err = decoder.Decode(&value)
|
err = decoder.Decode(&value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", nil, err
|
return "", nil, toBeEmptied, err
|
||||||
}
|
}
|
||||||
return strings.TrimSuffix(fileName, ".json"), value, nil
|
return strings.TrimSuffix(fileName, ".json"), value, toBeEmptied, nil
|
||||||
}
|
}
|
||||||
return fileName, string(fileContent), nil
|
if string(fileContent) == "toBeEmptied" {
|
||||||
|
toBeEmptied = true
|
||||||
|
}
|
||||||
|
return fileName, string(fileContent), toBeEmptied, nil
|
||||||
}
|
}
|
||||||
|
@ -3,11 +3,12 @@ package piperenv
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_writeMapToDisk(t *testing.T) {
|
func Test_writeMapToDisk(t *testing.T) {
|
||||||
@ -73,6 +74,8 @@ func TestCPEMap_LoadFromDisk(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
err = ioutil.WriteFile(path.Join(subPath, "Bruce"), []byte("Wayne"), 0644)
|
err = ioutil.WriteFile(path.Join(subPath, "Bruce"), []byte("Wayne"), 0644)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
err = ioutil.WriteFile(path.Join(subPath, "Robin"), []byte("toBeEmptied"), 0644)
|
||||||
|
require.NoError(t, err)
|
||||||
err = ioutil.WriteFile(path.Join(subPath, "Test.json"), []byte("54"), 0644)
|
err = ioutil.WriteFile(path.Join(subPath, "Test.json"), []byte("54"), 0644)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
@ -82,6 +85,7 @@ func TestCPEMap_LoadFromDisk(t *testing.T) {
|
|||||||
|
|
||||||
require.Equal(t, "Bar", cpe["Foo"])
|
require.Equal(t, "Bar", cpe["Foo"])
|
||||||
require.Equal(t, "World", cpe["Hello"])
|
require.Equal(t, "World", cpe["Hello"])
|
||||||
|
require.Equal(t, "", cpe["Batman/Robin"])
|
||||||
require.Equal(t, "Wayne", cpe["Batman/Bruce"])
|
require.Equal(t, "Wayne", cpe["Batman/Bruce"])
|
||||||
require.Equal(t, json.Number("54"), cpe["Batman/Test"])
|
require.Equal(t, json.Number("54"), cpe["Batman/Test"])
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user