Skip to content

Commit 5c4c177

Browse files
committed
Start rewriting YAML impementation
1 parent 1e23db0 commit 5c4c177

File tree

3 files changed

+93
-4
lines changed

3 files changed

+93
-4
lines changed

cli/deployment/config.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"github.com/coder/coder/codersdk"
2020
)
2121

22-
func newConfig() *codersdk.DeploymentConfig {
22+
func DefaultConfig() *codersdk.DeploymentConfig {
2323
return &codersdk.DeploymentConfig{
2424
AccessURL: &codersdk.DeploymentConfigField[string]{
2525
Name: "Access URL",
@@ -587,7 +587,7 @@ func newConfig() *codersdk.DeploymentConfig {
587587

588588
//nolint:revive
589589
func Config(configPath string, vip *viper.Viper) (*codersdk.DeploymentConfig, error) {
590-
dc := newConfig()
590+
dc := DefaultConfig()
591591
vip.SetEnvPrefix("CODER")
592592

593593
if configPath != "" {
@@ -756,7 +756,7 @@ func readSliceFromViper[T any](vip *viper.Viper, key string, value any) []T {
756756
}
757757

758758
func NewViper() *viper.Viper {
759-
dc := newConfig()
759+
dc := DefaultConfig()
760760
vip := viper.New()
761761
vip.SetEnvPrefix("coder")
762762
vip.SetEnvKeyReplacer(strings.NewReplacer("-", "_", ".", "_"))
@@ -800,7 +800,7 @@ func setViperDefaults(prefix string, vip *viper.Viper, target interface{}) {
800800

801801
//nolint:revive
802802
func AttachFlags(flagset *pflag.FlagSet, vip *viper.Viper, enterprise bool) {
803-
setFlags("", flagset, vip, newConfig(), enterprise)
803+
setFlags("", flagset, vip, DefaultConfig(), enterprise)
804804
}
805805

806806
//nolint:revive

cli/deployment/yaml.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package deployment
2+
3+
import (
4+
"reflect"
5+
"strconv"
6+
"strings"
7+
"time"
8+
9+
"golang.org/x/xerrors"
10+
"gopkg.in/yaml.v3"
11+
12+
"github.com/hashicorp/go-multierror"
13+
14+
"github.com/coder/coder/codersdk"
15+
)
16+
17+
type yamlSchema map[string]interface{}
18+
19+
func yamlDeploymentField(field reflect.Value) (*yaml.Node, error) {
20+
valueField := field.FieldByName("Value")
21+
var valueStr string
22+
switch v := valueField.Interface().(type) {
23+
case time.Duration:
24+
valueStr = v.String()
25+
case bool:
26+
valueStr = strconv.FormatBool(v)
27+
case string:
28+
valueStr = v
29+
default:
30+
return nil, xerrors.Errorf(
31+
"unsupported DeploymentField type: %s", valueField.Type().String(),
32+
)
33+
}
34+
return &yaml.Node{
35+
Kind: yaml.ScalarNode,
36+
Value: valueStr,
37+
}, nil
38+
}
39+
40+
// MarshalYAML converts the deployment config to its MarshalYAML representation.
41+
func MarshalYAML(config *codersdk.DeploymentConfig) ([]byte, error) {
42+
var (
43+
schema = yamlSchema{}
44+
configValue = reflect.ValueOf(config).Elem()
45+
merr *multierror.Error
46+
)
47+
for i := 0; i < configValue.NumField(); i++ {
48+
var (
49+
configField = configValue.Field(i).Elem()
50+
typeName = configField.Type().String()
51+
fieldName = configValue.Type().Field(i).Name
52+
)
53+
54+
switch {
55+
case strings.HasPrefix(typeName, "codersdk.DeploymentConfigField["):
56+
node, err := yamlDeploymentField(configField)
57+
if err != nil {
58+
merr = multierror.Append(merr, err)
59+
continue
60+
}
61+
schema[fieldName] = node
62+
default:
63+
merr = multierror.Append(merr, xerrors.Errorf("unsupported type: %s", typeName))
64+
}
65+
}
66+
byt, err := yaml.Marshal(schema)
67+
merr = multierror.Append(merr, err)
68+
return byt, merr.ErrorOrNil()
69+
}

cli/deployment/yaml_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package deployment_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
8+
"github.com/coder/coder/cli/deployment"
9+
)
10+
11+
func TestMarshalYAML(t *testing.T) {
12+
t.Parallel()
13+
14+
t.Run("Default", func(t *testing.T) {
15+
t.Parallel()
16+
byt, err := deployment.MarshalYAML(deployment.DefaultConfig())
17+
t.Logf("yaml:\n%s", string(byt))
18+
require.NoError(t, err)
19+
})
20+
}

0 commit comments

Comments
 (0)