Skip to content

Commit

Permalink
Fix for errata config not loading for some commands, and not falling …
Browse files Browse the repository at this point in the history
…back on embedded errata
  • Loading branch information
hasty committed Oct 30, 2024
1 parent b857966 commit e079d34
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 24 deletions.
5 changes: 3 additions & 2 deletions cmd/dump/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"

"github.com/project-chip/alchemy/cmd/common"
"github.com/project-chip/alchemy/errata"
"github.com/project-chip/alchemy/internal/files"
"github.com/project-chip/alchemy/internal/parse"
"github.com/project-chip/alchemy/matter/spec"
Expand Down Expand Up @@ -44,12 +45,13 @@ var Command = &cobra.Command{
}
dumpElements(doc, doc.Elements(), 0)
} else if jsonOut {
errata.LoadErrataConfig(specRoot)
path, err := spec.NewDocPath(f, specRoot)
if err != nil {
return fmt.Errorf("error resolving doc path %s: %w", f, err)
}

doc, err := spec.ParseFile(path, ".", asciiSettings...)
doc, err := spec.ParseFile(path, specRoot, asciiSettings...)
if err != nil {
return fmt.Errorf("error opening doc %s: %w", f, err)
}
Expand All @@ -76,5 +78,4 @@ func init() {
Command.Flags().Bool("ascii", false, "dump asciidoc object model")
Command.Flags().Bool("json", false, "dump json object model")
Command.Flags().String("specRoot", "connectedhomeip-spec", "the src root of your clone of CHIP-Specifications/connectedhomeip-spec")

}
46 changes: 25 additions & 21 deletions errata/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,12 @@ import (
var defaultErrata []byte

func LoadErrataConfig(specRoot string) {
if specRoot == "" {
return
}
errataPath := filepath.Join(specRoot, ".github/alchemy/errata.yaml")
exists, err := files.Exists(errataPath)
if err != nil {
slog.Warn("error checking for errata path", slog.Any("error", err))
return
}
var b []byte
if !exists {
slog.Warn("errata file does not exist; using embedded errata", slog.Any("path", errataPath))
b := loadConfig(specRoot)
if b == nil {
b = defaultErrata
} else {
var err error
b, err = os.ReadFile(errataPath)
slog.Debug("Using errata overlay", slog.Any("path", errataPath))
if err != nil {
slog.Warn("error reading errata file", slog.Any("error", err))
return
}
}
var errataOverlay errataOverlay
err = yaml.Unmarshal(b, &errataOverlay)
err := yaml.Unmarshal(b, &errataOverlay)
if err != nil {
slog.Warn("error parsing errata file", slog.Any("error", err))
return
Expand All @@ -56,6 +38,28 @@ type errataOverlay struct {
Errata map[string]*Errata `yaml:"errata"`
}

func loadConfig(specRoot string) []byte {
if specRoot == "" {
return nil
}
errataPath := filepath.Join(specRoot, ".github/alchemy/errata.yaml")
exists, err := files.Exists(errataPath)
if err != nil {
slog.Warn("error checking for errata path", slog.Any("error", err))
return nil
}
if !exists {
return nil
}
b, err := os.ReadFile(errataPath)
slog.Debug("Using errata overlay", slog.Any("path", errataPath))
if err != nil {
slog.Warn("error reading errata file", slog.Any("error", err))
return nil
}
return b
}

func dumpConfig(errataPath string) {
errataOverlay := errataOverlay{Errata: Erratas}
d, err := yaml.Marshal(&errataOverlay)
Expand Down
7 changes: 6 additions & 1 deletion matter/spec/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ func NewSpecPath(path string, rootPath string) (asciidoc.Path, error) {
}
}
var err error
p.Relative, err = filepath.Rel(rootPath, p.Absolute)
var r string
r, err = filepath.Abs(rootPath)
if err != nil {
return p, err
}
p.Relative, err = filepath.Rel(r, p.Absolute)
return p, err
}

Expand Down

0 comments on commit e079d34

Please sign in to comment.