Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
92 lines
1.7 KiB
Go
92 lines
1.7 KiB
Go
package directory
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
|
|
"github.com/cortezaproject/corteza/server/pkg/envoy"
|
|
"github.com/cortezaproject/corteza/server/pkg/envoy/resource"
|
|
)
|
|
|
|
type (
|
|
Decoder interface {
|
|
CanDecodeExt(string) bool
|
|
CanDecodeFile(io.Reader) bool
|
|
Decode(context.Context, io.Reader, *envoy.DecoderOpts) ([]resource.Interface, error)
|
|
}
|
|
)
|
|
|
|
// DecodeDirectory is a helper to run the decoding process over the entire directory
|
|
func Decode(ctx context.Context, p string, decoders ...Decoder) ([]resource.Interface, error) {
|
|
var (
|
|
f *os.File
|
|
|
|
// decoded nodes
|
|
dnn []resource.Interface
|
|
|
|
// agregated resources
|
|
nn = make([]resource.Interface, 0, 100)
|
|
)
|
|
|
|
if len(decoders) == 0 {
|
|
return nil, fmt.Errorf("no decoders provided")
|
|
}
|
|
|
|
return nn, filepath.Walk(p, func(p string, info os.FileInfo, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if info.IsDir() {
|
|
return nil
|
|
}
|
|
|
|
if f, err = os.Open(p); err != nil {
|
|
return err
|
|
}
|
|
defer f.Close()
|
|
|
|
for _, d := range decoders {
|
|
if !d.CanDecodeFile(f) {
|
|
// decoder cannot handle this file
|
|
// Make sure to reset it, as the above check consumes the reader
|
|
if _, err = f.Seek(0, 0); err != nil {
|
|
return err
|
|
}
|
|
|
|
continue
|
|
}
|
|
|
|
if !d.CanDecodeExt(info.Name()) {
|
|
// this decoder cannot handle this extension
|
|
continue
|
|
}
|
|
|
|
if _, err = f.Seek(0, 0); err != nil {
|
|
return err
|
|
}
|
|
|
|
dir, fn := path.Split(p)
|
|
do := &envoy.DecoderOpts{
|
|
Name: fn,
|
|
Path: dir,
|
|
}
|
|
|
|
if dnn, err = d.Decode(ctx, f, do); err != nil {
|
|
return fmt.Errorf("failed to decode %s: %w", info.Name(), err)
|
|
}
|
|
|
|
nn = append(nn, dnn...)
|
|
|
|
// found compatible decoder
|
|
return nil
|
|
}
|
|
|
|
return nil
|
|
})
|
|
}
|