-
Notifications
You must be signed in to change notification settings - Fork 492
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* improve egctl to support sending multi configs at once (fix #29) * add a visitor to do the YAML parsing * rollback function readFromFileOrStdin * Update comments. Co-authored-by: Bomin Zhang <[email protected]> * some improvements - change the signature of VisitorFunc - remove useless function readFromFileOrStdin Co-authored-by: Bomin Zhang <[email protected]>
- Loading branch information
Showing
4 changed files
with
94 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package command | ||
|
||
import ( | ||
"bufio" | ||
"io" | ||
"strings" | ||
|
||
"k8s.io/apimachinery/pkg/util/yaml" | ||
) | ||
|
||
// VisitorFunc executes visition logic | ||
type VisitorFunc func(*spec) | ||
|
||
// Visitor walk through the document via VisitorFunc | ||
type Visitor interface { | ||
Visit(VisitorFunc) | ||
} | ||
|
||
type spec struct { | ||
Kind string | ||
Name string | ||
doc string | ||
} | ||
|
||
type streamVisitor struct { | ||
io.Reader | ||
} | ||
|
||
// NewStreamVisitor returns a streamVisitor. | ||
func NewStreamVisitor(src string) *streamVisitor { | ||
return &streamVisitor{ | ||
Reader: strings.NewReader(src), | ||
} | ||
} | ||
|
||
type yamlDecoder struct { | ||
reader *yaml.YAMLReader | ||
doc string | ||
} | ||
|
||
func newYAMLDecoder(r io.Reader) *yamlDecoder { | ||
return &yamlDecoder{ | ||
reader: yaml.NewYAMLReader(bufio.NewReader(r)), | ||
} | ||
} | ||
|
||
// Decode reads a YAML document into bytes and tries to yaml.Unmarshal it. | ||
func (d *yamlDecoder) Decode(into interface{}) error { | ||
bytes, err := d.reader.Read() | ||
if err != nil && err != io.EOF { | ||
return err | ||
} | ||
d.doc = string(bytes) | ||
if len(bytes) != 0 { | ||
err = yaml.Unmarshal(bytes, into) | ||
} | ||
return err | ||
} | ||
|
||
// Visit implements Visitor over a stream. | ||
func (v *streamVisitor) Visit(fn VisitorFunc) { | ||
d := newYAMLDecoder(v.Reader) | ||
var validSpecs []spec | ||
for { | ||
var s spec | ||
if err := d.Decode(&s); err != nil { | ||
if err == io.EOF { | ||
break | ||
} else { | ||
ExitWithErrorf("error parsing %s: %v", d.doc, err) | ||
} | ||
} | ||
s.doc = d.doc | ||
//TODO can validate spec's Kind here | ||
validSpecs = append(validSpecs, s) | ||
} | ||
for _, s := range validSpecs { | ||
fn(&s) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters