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
Prev Previous commit
Next Next commit
Server tests pass!
  • Loading branch information
ammario committed Apr 6, 2023
commit 0af47bbf1dd2cc462fa85b45effd6d2f58bf6bca
7 changes: 7 additions & 0 deletions cli/clibase/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"net"
"net/url"
"reflect"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -350,6 +351,12 @@ func (s *Struct[T]) MarshalYAML() (interface{}, error) {
}

func (s *Struct[T]) UnmarshalYAML(n *yaml.Node) error {
// HACK: for compatibility with flags, we set the value to nil if the node
// is empty and T is a slice.
if typ := reflect.TypeOf(s.Value); typ.Kind() == reflect.Slice && len(n.Content) == 0 {
reflect.ValueOf(&s.Value).Elem().Set(reflect.Zero(typ))
return nil
}
return n.Decode(&s.Value)
}

Expand Down
6 changes: 6 additions & 0 deletions cli/clibase/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,12 @@ func (o *Option) setFromYAMLNode(n *yaml.Node) error {
case yaml.ScalarNode:
return o.Value.Set(n.Value)
case yaml.SequenceNode:
// We treat empty values as nil for consistency with other option
// mechanisms.
if len(n.Content) == 0 {
o.Value = nil
return nil
}
return n.Decode(o.Value)
case yaml.MappingNode:
return xerrors.Errorf("mapping node must implement yaml.Unmarshaler")
Expand Down
44 changes: 9 additions & 35 deletions cli/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"net/url"
"os"
"path/filepath"
"reflect"
"runtime"
"strconv"
"strings"
Expand All @@ -36,7 +35,6 @@ import (
"gopkg.in/yaml.v3"

"github.com/coder/coder/cli"
"github.com/coder/coder/cli/clibase"
"github.com/coder/coder/cli/clitest"
"github.com/coder/coder/cli/config"
"github.com/coder/coder/coderd/coderdtest"
Expand Down Expand Up @@ -1470,46 +1468,22 @@ func TestServer(t *testing.T) {
client = codersdk.New(waitAccessURL(t, cfg))
_ = coderdtest.CreateFirstUser(t, client)
gotConfig, err := client.DeploymentConfig(ctx)
normalizeNilsInOptionSet(&wantConfig.Options)
normalizeNilsInOptionSet(&gotConfig.Options)
require.NoError(t, err, "config:\n%s\nargs: %+v", conf.String(), inv.Args)
gotConfig.Options.ByName("Config Path").Value.Set("")
require.EqualValues(t, wantConfig.Options, gotConfig.Options)
// We check the options individually for better error messages.
for i := range wantConfig.Options {
assert.Equal(
t, wantConfig.Options[i],
gotConfig.Options[i],
"option %q",
wantConfig.Options[i].Name,
)
}
w.RequireSuccess()
})
})
}

// The YAML process sets slice values to nil if they are empty, leading to
// false negatives when comparing equality.
func normalizeNilsInOptionSet(s *clibase.OptionSet) {
for i := range *s {
opt := &(*s)[i]
if opt.Value == nil {
continue
}

var (
v = reflect.Indirect(reflect.ValueOf(opt.Value))
kind = v.Type().Kind()
)

if opt.YAML == "supportLinks" {
fmt.Printf("supportLinks: %v, %v", kind, v.IsZero())
}

if kind == reflect.Slice && v.Len() > 0 {
continue
}

if kind == reflect.Struct && !v.IsZero() {
continue
}

opt.Value = nil
}
}

func generateTLSCertificate(t testing.TB, commonName ...string) (certPath, keyPath string) {
dir := t.TempDir()

Expand Down