Skip to content

Commit

Permalink
Params as any, not string
Browse files Browse the repository at this point in the history
Having params as string make it ugly to pass configs, json or yaml encoded strings inside yaml is not highlighted in editors, to make UX slightly better allowed to pass here a proper struct. Backward compatibility is preserved.

---

Pull Request resolved: #108

Co-authored-by: tserakhau <[email protected]>
commit_hash:11ce7493818d49752ea9fd53fbcf08865ac0a2cf
  • Loading branch information
laskoviymishka authored and robot-piglet committed Nov 20, 2024
1 parent 4d8176a commit 1382bb9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
12 changes: 6 additions & 6 deletions cmd/trcli/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ func ParseTransferYaml(rawData []byte) (*TransferYamlView, error) {
}
for _, v := range os.Environ() {
pair := strings.SplitN(v, "=", 2)
transfer.Src.Params = strings.ReplaceAll(transfer.Src.Params, fmt.Sprintf("${%v}", pair[0]), pair[1])
transfer.Dst.Params = strings.ReplaceAll(transfer.Dst.Params, fmt.Sprintf("${%v}", pair[0]), pair[1])
transfer.Src.Params = strings.ReplaceAll(transfer.Src.RawParams(), fmt.Sprintf("${%v}", pair[0]), pair[1])
transfer.Dst.Params = strings.ReplaceAll(transfer.Dst.RawParams(), fmt.Sprintf("${%v}", pair[0]), pair[1])
}
res, err := sig_yaml.YAMLToJSON([]byte(transfer.Src.Params))
res, err := sig_yaml.YAMLToJSON([]byte(transfer.Src.RawParams()))
if err == nil {
transfer.Src.Params = string(res)
}
res, err = sig_yaml.YAMLToJSON([]byte(transfer.Dst.Params))
res, err = sig_yaml.YAMLToJSON([]byte(transfer.Dst.RawParams()))
if err == nil {
transfer.Dst.Params = string(res)
}
Expand Down Expand Up @@ -103,11 +103,11 @@ func ParseTablesYaml(rawData []byte) (*UploadTables, error) {
}

func source(tr *TransferYamlView) (model.Source, error) {
return model.NewSource(tr.Src.Type, tr.Src.Params)
return model.NewSource(tr.Src.Type, tr.Src.RawParams())
}

func target(tr *TransferYamlView) (model.Destination, error) {
return model.NewDestination(tr.Dst.Type, tr.Dst.Params)
return model.NewDestination(tr.Dst.Type, tr.Dst.RawParams())
}

func transfer(source model.Source, target model.Destination, tr *TransferYamlView) *model.Transfer {
Expand Down
24 changes: 23 additions & 1 deletion cmd/trcli/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ dst:
assert.Equal(t, "{\"Password\":\"secret2\"}", transfer.Dst.Params)
}

func TestParserTransferYaml_WithYaml(t *testing.T) {
func TestParserTransferYaml_WithRawYaml(t *testing.T) {
require.NoError(t, os.Setenv("FOO", "secret1"))
require.NoError(t, os.Setenv("BAR", "secret2"))
defer os.Unsetenv("FOO")
Expand All @@ -51,3 +51,25 @@ dst:
assert.Equal(t, "{\"Password\":\"secret1\"}", transfer.Src.Params)
assert.Equal(t, "{\"Password\":\"secret2\"}", transfer.Dst.Params)
}

func TestParserTransferYaml_WithYaml(t *testing.T) {
require.NoError(t, os.Setenv("FOO", "secret1"))
require.NoError(t, os.Setenv("BAR", "secret2"))
defer os.Unsetenv("FOO")
defer os.Unsetenv("BAR")

transfer, err := ParseTransferYaml([]byte(`
src:
type: src_type
params:
Password: ${FOO}
dst:
type: dst_type
params:
Password: ${BAR}
`))
require.NoError(t, err)

assert.Equal(t, "{\"Password\":\"secret1\"}", transfer.Src.Params)
assert.Equal(t, "{\"Password\":\"secret2\"}", transfer.Dst.Params)
}
15 changes: 14 additions & 1 deletion cmd/trcli/config/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,25 @@ import (
"github.com/doublecloud/transfer/pkg/abstract"
"github.com/doublecloud/transfer/pkg/abstract/model"
"github.com/doublecloud/transfer/pkg/transformer"
"gopkg.in/yaml.v2"
)

type Endpoint struct {
ID, Name string
Type abstract.ProviderType
Params string
Params any
}

func (e Endpoint) RawParams() string {
switch p := e.Params.(type) {
case []byte:
return string(p)
case string:
return p
default:
data, _ := yaml.Marshal(p)
return string(data)
}
}

type Runtime struct {
Expand Down

0 comments on commit 1382bb9

Please sign in to comment.