Skip to content

Commit

Permalink
Add untested explicit settings in rdp file
Browse files Browse the repository at this point in the history
  • Loading branch information
bolkedebruin committed Mar 21, 2024
1 parent 95a8623 commit 5d30deb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
28 changes: 24 additions & 4 deletions cmd/rdpgw/rdp/rdp.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"github.com/bolkedebruin/rdpgw/cmd/rdpgw/rdp/koanf/parsers/rdp"
"github.com/fatih/structs"
"github.com/go-viper/mapstructure/v2"
"github.com/knadh/koanf/providers/file"
"github.com/knadh/koanf/v2"
"log"
Expand Down Expand Up @@ -93,6 +94,7 @@ type RdpSettings struct {

type Builder struct {
Settings RdpSettings
Metadata mapstructure.Metadata
}

func NewBuilder() *Builder {
Expand All @@ -102,39 +104,47 @@ func NewBuilder() *Builder {

return &Builder{
Settings: c,
Metadata: mapstructure.Metadata{},
}
}

func NewBuilderFromFile(filename string) (*Builder, error) {
c := RdpSettings{}
initStruct(&c)
metadata := mapstructure.Metadata{}

decoderConfig := &mapstructure.DecoderConfig{
Result: &c,
Metadata: &metadata,
}

var k = koanf.New(".")
if err := k.Load(file.Provider(filename), rdp.Parser()); err != nil {
return nil, err
}
t := koanf.UnmarshalConf{Tag: "rdp"}
t := koanf.UnmarshalConf{Tag: "rdp", DecoderConfig: decoderConfig}

if err := k.UnmarshalWithConf("", &c, t); err != nil {
return nil, err
}
return &Builder{
Settings: c,
Metadata: metadata,
}, nil
}

func (rb *Builder) String() string {
var sb strings.Builder

addStructToString(rb.Settings, &sb)
addStructToString(rb.Settings, rb.Metadata, &sb)

return sb.String()
}

func addStructToString(st interface{}, sb *strings.Builder) {
func addStructToString(st interface{}, metadata mapstructure.Metadata, sb *strings.Builder) {
s := structs.New(st)
for _, f := range s.Fields() {
if isZero(f) {
if isZero(f) && !isSet(f, metadata) {
continue
}
sb.WriteString(f.Tag("rdp"))
Expand Down Expand Up @@ -195,6 +205,16 @@ func isZero(f *structs.Field) bool {
return f.IsZero()
}

func isSet(f *structs.Field, metadata mapstructure.Metadata) bool {
for _, v := range metadata.Unset {
if v == f.Name() {
log.Printf("field %s is unset", f.Name())
return true
}
}
return false
}

func initStruct(st interface{}) {
s := structs.New(st)
for _, f := range s.Fields() {
Expand Down
2 changes: 1 addition & 1 deletion cmd/rdpgw/rdp/rdp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestRdpBuilder(t *testing.T) {
t.Fatalf("%s does not contain `gatewayhostname:s:%s", s, GatewayHostName)
}
if strings.Contains(s, "autoreconnectionenabled") {
t.Fatalf("autoreconnectionenabled is in %s, but is default value", s)
t.Fatalf("autoreconnectionenabled is in %s, but it's default value", s)
}
if !strings.Contains(s, "smart sizing:i:1"+CRLF) {
t.Fatalf("%s does not contain smart sizing:i:1", s)
Expand Down

0 comments on commit 5d30deb

Please sign in to comment.