-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: added env file support to kubeconfig in cfg (#469)
- Loading branch information
1 parent
1938705
commit f7dcbd7
Showing
9 changed files
with
204 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright (c) 2017-present SIGHUP s.r.l All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package parser | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
const ( | ||
Env = "env" | ||
File = "file" | ||
) | ||
|
||
var RelativePathRegexp = regexp.MustCompile(`^\.{1,}\/`) | ||
|
||
type ConfigParser struct { | ||
baseDir string | ||
} | ||
|
||
func NewConfigParser(baseDir string) *ConfigParser { | ||
return &ConfigParser{ | ||
baseDir: baseDir, | ||
} | ||
} | ||
|
||
func (p *ConfigParser) ParseDynamicValue(val any) (string, error) { | ||
strVal := fmt.Sprintf("%v", val) | ||
|
||
spl := strings.Split(strVal, "://") | ||
|
||
if len(spl) > 1 { | ||
source := strings.TrimPrefix(spl[0], "{") | ||
sourceValue := strings.TrimSuffix(spl[1], "}") | ||
|
||
switch source { | ||
case Env: | ||
envVar := os.Getenv(sourceValue) | ||
|
||
envVar = strings.TrimRight(envVar, "\n") | ||
|
||
return envVar, nil | ||
|
||
case File: | ||
// If the value is a relative path, we need to convert it to an absolute path. | ||
isRelativePath := RelativePathRegexp.MatchString(sourceValue) | ||
if isRelativePath { | ||
sourceValue = filepath.Clean(sourceValue) | ||
sourceValue = filepath.Join(p.baseDir, sourceValue) | ||
} | ||
|
||
content, err := readValueFromFile(sourceValue) | ||
if err != nil { | ||
return "", fmt.Errorf("%w", err) | ||
} | ||
|
||
content = strings.TrimRight(content, "\n") | ||
|
||
return content, nil | ||
|
||
default: | ||
return strVal, nil | ||
} | ||
} | ||
|
||
return strVal, nil | ||
} | ||
|
||
func readValueFromFile(path string) (string, error) { | ||
val, err := os.ReadFile(path) | ||
|
||
return string(val), err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright (c) 2017-present SIGHUP s.r.l All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
//go:build unit | ||
|
||
package parser_test | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path" | ||
"testing" | ||
|
||
"github.com/sighupio/furyctl/internal/parser" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewConfigParser(t *testing.T) { | ||
t.Parallel() | ||
|
||
cfgParser := parser.NewConfigParser("dummy/base/dir") | ||
|
||
assert.NotNil(t, cfgParser) | ||
} | ||
|
||
func TestConfigParser_ParseDynamicValue(t *testing.T) { | ||
t.Parallel() | ||
|
||
tmpDir, err := os.MkdirTemp("", "test") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
exampleStr := "test" | ||
|
||
err = os.WriteFile(tmpDir+"/test_file.txt", []byte(exampleStr), os.ModePerm) | ||
|
||
defer os.RemoveAll(tmpDir) | ||
|
||
assert.NoError(t, err) | ||
|
||
testCases := []struct { | ||
name string | ||
baseDir string | ||
envName string | ||
envValue string | ||
value any | ||
expected string | ||
}{ | ||
{ | ||
name: "parsing env", | ||
baseDir: "dummy/base/dir", | ||
envName: "TEST_ENV_VAR", | ||
envValue: "test", | ||
value: "{env://TEST_ENV_VAR}", | ||
expected: "test", | ||
}, | ||
{ | ||
name: "parsing file - relative path", | ||
baseDir: tmpDir, | ||
value: fmt.Sprintf("{file://./test_file.txt}"), | ||
expected: "test", | ||
}, | ||
{ | ||
name: "parsing file - absolute path", | ||
baseDir: tmpDir, | ||
value: fmt.Sprintf("{file://%s}", path.Join(tmpDir, "test_file.txt")), | ||
expected: "test", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
tc := tc | ||
|
||
t.Run(tc.name, func(t *testing.T) { | ||
cfgParser := parser.NewConfigParser(tc.baseDir) | ||
|
||
if tc.envName != "" { | ||
err := os.Setenv(tc.envName, tc.envValue) | ||
|
||
assert.NoError(t, err) | ||
} | ||
|
||
res, err := cfgParser.ParseDynamicValue(tc.value) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, tc.expected, res) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.