Skip to content

Commit

Permalink
Support load config from stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
wzshiming committed Jun 28, 2023
1 parent af0249e commit bfda65f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
22 changes: 17 additions & 5 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,19 @@ func loadRawMessages(src []string) ([]json.RawMessage, error) {
var raws []json.RawMessage

for _, p := range src {
if p == "-" {
r, err := loadRaw(os.Stdin)
if err != nil {
return nil, err
}
raws = append(raws, r...)
continue
}
p, err := path.Expand(p)
if err != nil {
return nil, err
}
r, err := loadRawConfig(p)
r, err := loadRawFromFile(p)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -426,19 +434,23 @@ func FilterWithoutTypeFromContext[T InternalObject](ctx context.Context) (out []
return FilterWithoutType[T](objs)
}

func loadRawConfig(p string) ([]json.RawMessage, error) {
var raws []json.RawMessage
func loadRawFromFile(p string) ([]json.RawMessage, error) {
file, err := os.Open(p)
if err != nil {
return nil, err
}
defer func() {
_ = file.Close()
}()
decoder := utilyaml.NewYAMLToJSONDecoder(file)
return loadRaw(file)
}

func loadRaw(r io.Reader) ([]json.RawMessage, error) {
var raws []json.RawMessage
decoder := utilyaml.NewYAMLToJSONDecoder(r)
for {
var raw json.RawMessage
err = decoder.Decode(&raw)
err := decoder.Decode(&raw)
if err != nil {
if errors.Is(err, io.EOF) {
break
Expand Down
3 changes: 3 additions & 0 deletions pkg/config/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ func InitFlags(ctx context.Context, flags *pflag.FlagSet) (context.Context, erro
}
configPaths := make([]string, 0, len(*config))
for _, c := range *config {
if c == "-" {
configPaths = append(configPaths, c)
}
configPath, err := path.Expand(c)
if err != nil {
return nil, err
Expand Down

0 comments on commit bfda65f

Please sign in to comment.