Skip to content

feat: add YAML support to server #6934

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 42 commits into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
0d08b84
big wip
ammario Mar 30, 2023
fce8e5f
SimpleString isomorphism!!!
ammario Mar 30, 2023
2844497
Support scalars
ammario Mar 30, 2023
4960df8
ComplexObjects work!
ammario Mar 30, 2023
2a2c926
golden files work!
ammario Mar 30, 2023
056437f
Fixup YAML types
ammario Mar 30, 2023
c2428ff
give default value in comment
ammario Mar 30, 2023
8b210dc
Merge remote-tracking branch 'origin/main' into yaml
ammario Mar 30, 2023
26d7f21
Merge remote-tracking branch 'origin/main' into yaml
ammario Mar 31, 2023
38fa539
Add values.YAMLConfigPath
ammario Mar 31, 2023
64b167a
Merge remote-tracking branch 'origin/main' into yaml
ammario Mar 31, 2023
7edea99
Add YAML to core clibase parsing
ammario Mar 31, 2023
3ab35c1
Server Test WIP
ammario Mar 31, 2023
38155da
More WIP
ammario Mar 31, 2023
451c149
Merge remote-tracking branch 'origin/main' into yaml
ammario Apr 1, 2023
c72f67c
hmm
ammario Apr 1, 2023
9d61ca7
Cant find a clean way to do this..
ammario Apr 4, 2023
5b61b7b
Mediocre solution
ammario Apr 4, 2023
4923d92
hmm
ammario Apr 4, 2023
b6f982c
Merge remote-tracking branch 'origin/main' into yaml
ammario Apr 4, 2023
781786b
New, better YAML
ammario Apr 6, 2023
b03999a
clibase passes
ammario Apr 6, 2023
d018838
Fix UnknownOptions errors
ammario Apr 6, 2023
fe93d7f
Work on nil normalization
ammario Apr 6, 2023
0af47bb
Server tests pass!
ammario Apr 6, 2023
4a1df77
Merge remote-tracking branch 'origin/main' into yaml
ammario Apr 6, 2023
64255b3
make gen + self review cleanup
ammario Apr 6, 2023
99d6068
Generate docs
ammario Apr 6, 2023
300484b
make gen
ammario Apr 6, 2023
4cf5a21
Add --debug-options
ammario Apr 6, 2023
abc92d9
Normalize golden files
ammario Apr 6, 2023
a958324
fix log path
ammario Apr 7, 2023
8ead9fc
minor fix
ammario Apr 7, 2023
6eb19f6
Fix mutability bug in PrepareAll
ammario Apr 7, 2023
f77460f
Address review comments
ammario Apr 7, 2023
4e63bd5
Merge remote-tracking branch 'origin/main' into yaml
ammario Apr 7, 2023
2a96c70
Fix windows?
ammario Apr 7, 2023
7ae7cad
Small improvements
ammario Apr 7, 2023
09d5a35
Reduce YAML ident to 2
ammario Apr 7, 2023
d6506c6
Log mystery error
ammario Apr 7, 2023
551e55d
fixup! Log mystery error
ammario Apr 7, 2023
c3f3317
ecdsa
ammario Apr 7, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
big wip
  • Loading branch information
ammario committed Mar 30, 2023
commit 0d08b844123f54c47f3fa4114432e59ec7f7398c
118 changes: 118 additions & 0 deletions cli/clibase/yaml.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package clibase

import (
"errors"

"github.com/iancoleman/strcase"
"github.com/mitchellh/go-wordwrap"
"golang.org/x/xerrors"
Expand Down Expand Up @@ -41,6 +43,8 @@ func deepMapNode(n *yaml.Node, path []string, headComment string) *yaml.Node {
//
// The node is returned to enable post-processing higher up in
// the stack.
//
// It is isomorphic with FromYAML.
func (s OptionSet) ToYAML() (*yaml.Node, error) {
root := yaml.Node{
Kind: yaml.MappingNode,
Expand Down Expand Up @@ -103,3 +107,117 @@ func (s OptionSet) ToYAML() (*yaml.Node, error) {
}
return &root, nil
}

// FromYAML converts the given YAML node into the option set.
// It is isomorphic with ToYAML.

func (s *OptionSet) FromYAML(n *yaml.Node) error {
return fromYAML(*s, nil, n)
}

func fromYAML(os OptionSet, ofGroup *Group, n *yaml.Node) error {
if n.Kind == yaml.DocumentNode && ofGroup == nil {
// The root may be a document node.
if len(n.Content) != 1 {
return xerrors.Errorf("expected one content node, got %d", len(n.Content))
}
return fromYAML(os, ofGroup, n.Content[0])
}

if n.Kind != yaml.MappingNode {
byt, _ := yaml.Marshal(n)
return xerrors.Errorf("expected mapping node, got type %v, contents:\n%v", n.Kind, string(byt))
}

var (
subGroupsByName = make(map[string]*Group)
optionsByName = make(map[string]*Option)
)
for i, opt := range os {
if opt.YAML == "" {
continue
}

// We only want to process options that are of the identified group,
// even if that group is nil.
if opt.Group != ofGroup {
if opt.Group.Parent == ofGroup {
subGroupsByName[opt.Group.Name] = opt.Group
}
continue
}

if _, ok := optionsByName[opt.YAML]; ok {
return xerrors.Errorf("duplicate option name %q", opt.YAML)
}

optionsByName[opt.YAML] = &os[i]
}

for k := range subGroupsByName {
if _, ok := optionsByName[k]; !ok {
continue
}
return xerrors.Errorf("there is both an option and a group with name %q", k)
}

var (
name string
merr error
)

for i, item := range n.Content {
if isName := i%2 == 0; isName {
if item.Kind != yaml.ScalarNode {
return xerrors.Errorf("expected scalar node for name, got %v", item.Kind)
}
name = item.Value
continue
}

switch item.Kind {
case yaml.MappingNode:
// Item is either a group or an option with a complex object.
if opt, ok := optionsByName[name]; ok {
unmarshaler, ok := opt.Value.(yaml.Unmarshaler)
if !ok {
return xerrors.Errorf("complex option %q must support unmarshaling", opt.Name)
}
err := unmarshaler.UnmarshalYAML(item)
if err != nil {
merr = errors.Join(merr, xerrors.Errorf("unmarshal %q: %w", opt.Name, err))
}
continue
}
if g, ok := subGroupsByName[name]; ok {
// Group, recurse.
err := fromYAML(os, g, item)
if err != nil {
merr = errors.Join(merr, xerrors.Errorf("group %q: %w", g.FullName(), err))
}
continue
}
merr = errors.Join(merr, xerrors.Errorf("unknown option or subgroup %q", name))
case yaml.ScalarNode:
opt, ok := optionsByName[name]
if !ok {
merr = errors.Join(merr, xerrors.Errorf("unknown option %q", name))
continue
}

unmarshaler, ok := opt.Value.(yaml.Unmarshaler)
if !ok {
err := opt.Value.Set(item.Value)
merr = errors.Join(merr, xerrors.Errorf("set %q: %w", opt.Name, err))
continue
}
err := unmarshaler.UnmarshalYAML(item)
if err != nil {
merr = errors.Join(merr, xerrors.Errorf("unmarshal %q: %w", opt.Name, err))
}
default:
return xerrors.Errorf("unexpected kind for value %v", item.Kind)
}
}
return merr
}
66 changes: 65 additions & 1 deletion cli/clibase/yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package clibase_test
import (
"testing"

"github.com/spf13/pflag"
"github.com/stretchr/testify/require"
"golang.org/x/exp/slices"
"gopkg.in/yaml.v3"

"github.com/coder/coder/cli/clibase"
)

func TestOption_ToYAML(t *testing.T) {
func TestOptionSet_ToYAML(t *testing.T) {
t.Parallel()

t.Run("RequireKey", func(t *testing.T) {
Expand Down Expand Up @@ -55,3 +57,65 @@ func TestOption_ToYAML(t *testing.T) {
t.Logf("Raw YAML:\n%s", string(byt))
})
}

func TestOptionSet_YAMLIsomorphism(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
name string
os clibase.OptionSet
zeroValue func() pflag.Value
}{
{
name: "SimpleString",
os: clibase.OptionSet{
{
Name: "Workspace Name",
Default: "billie",
Description: "The workspace's name.",
Group: &clibase.Group{Name: "Names"},
YAML: "workspaceName",
},
},
zeroValue: func() pflag.Value {
return clibase.StringOf(new(string))
},
},
} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

for i := range tc.os {
tc.os[i].Value = tc.zeroValue()
}
err := tc.os.SetDefaults()
require.NoError(t, err)

y, err := tc.os.ToYAML()
require.NoError(t, err)

toByt, err := yaml.Marshal(y)
require.NoError(t, err)

t.Logf("Raw YAML:\n%s", string(toByt))

var y2 yaml.Node
err = yaml.Unmarshal(toByt, &y2)
require.NoError(t, err)

os2 := slices.Clone(tc.os)
for i := range os2 {
os2[i].Value = tc.zeroValue()
}

// os2 values should be zeroed whereas tc.os should be
// set to defaults.
// This makes sure we aren't mixing pointers.
require.NotEqual(t, tc.os, os2)
err = os2.FromYAML(&y2)
require.NoError(t, err)

require.Equal(t, tc.os, os2)
})
}
}
4 changes: 4 additions & 0 deletions cli/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
return nil
}

if cfg.Config != "" {
cliui.Warnf(inv.Stderr, "YAML support is experimental and offers no compatibility guarantees.")
}

// Print deprecation warnings.
for _, opt := range opts {
if opt.UseInstead == nil {
Expand Down