Skip to content

Commit

Permalink
Store Yip source in the YipConfig (#178)
Browse files Browse the repository at this point in the history
* Store Yip source in the YipConfig

Not used yet, but can be used down the line to identify the exact file
that its giving out errors

Signed-off-by: Itxaka <[email protected]>

* Show which file the error is coming from

Signed-off-by: Itxaka <[email protected]>

---------

Signed-off-by: Itxaka <[email protected]>
  • Loading branch information
Itxaka authored Oct 4, 2024
1 parent 8db2dfb commit 68c3b4d
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 10 deletions.
6 changes: 3 additions & 3 deletions pkg/executor/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (l opList) uniqueNames() {
}
}

func (e *DefaultExecutor) applyStage(stage schema.Stage, fs vfs.FS, console plugins.Console) error {
func (e *DefaultExecutor) applyStage(config schema.YipConfig, stage schema.Stage, fs vfs.FS, console plugins.Console) error {
var errs error
for _, p := range e.conditionals {
if err := p(e.logger, stage, fs, console); err != nil {
Expand All @@ -95,7 +95,7 @@ func (e *DefaultExecutor) applyStage(stage schema.Stage, fs vfs.FS, console plug

for _, p := range e.plugins {
if err := p(e.logger, stage, fs, console); err != nil {
e.logger.Error(err.Error())
e.logger.Errorf("Error on file %s on stage %s: %s", config.Source, stage.Name, err)
errs = multierror.Append(errs, err)
}
}
Expand Down Expand Up @@ -144,7 +144,7 @@ func (e *DefaultExecutor) genOpFromSchema(file, stage string, config schema.YipC
fn: func(ctx context.Context) error {
e.logger.Debugf("Reading '%s'", file)
e.logger.Debugf("Executing stage '%s'", opName)
return e.applyStage(stageLocal, fs, console)
return e.applyStage(config, stageLocal, fs, console)
},
name: opName,
options: []herd.OpOption{herd.WeakDeps},
Expand Down
1 change: 0 additions & 1 deletion pkg/plugins/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ func Commands(l logger.Interface, s schema.Stage, fs vfs.FS, console Console) er
for _, cmd := range s.Commands {
out, err := console.Run(templateSysData(l, cmd))
if err != nil {
l.Error(out, ": ", err.Error())
errs = multierror.Append(errs, err)
continue
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/schema/loader_cloudinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type cloudInit struct{}
// to a yip schema.
// As Yip supports multi-stages, it is encoded in the supplied one.
// fs is used to parse the user data required from /etc/passwd.
func (cloudInit) Load(s []byte, fs vfs.FS) (*YipConfig, error) {
func (cloudInit) Load(source string, s []byte, fs vfs.FS) (*YipConfig, error) {
cc, err := cloudconfig.NewCloudConfig(string(s))
if err != nil {
return nil, err
Expand Down Expand Up @@ -114,7 +114,7 @@ func (cloudInit) Load(s []byte, fs vfs.FS) (*YipConfig, error) {
}

// optimistically load data as yip yaml
yipConfig, err := yipYAML{}.Load(s, fs)
yipConfig, err := yipYAML{}.Load(source, s, fs)
if err == nil {
for k, v := range yipConfig.Stages {
result.Stages[k] = append(result.Stages[k], v...)
Expand Down
4 changes: 2 additions & 2 deletions pkg/schema/loader_yip.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
type yipYAML struct{}

// LoadFromYaml loads a yip config from bytes
func (yipYAML) Load(b []byte, fs vfs.FS) (*YipConfig, error) {
func (yipYAML) Load(source string, b []byte, fs vfs.FS) (*YipConfig, error) {
var yamlConfig YipConfig
err := yaml.Unmarshal(b, &yamlConfig)
if err != nil {
return nil, err
}

yamlConfig.Source = source
return &yamlConfig, nil
}
5 changes: 3 additions & 2 deletions pkg/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ type DNS struct {
}

type YipConfig struct {
Source string `yaml:"-"`
Name string `yaml:"name,omitempty"`
Stages map[string][]Stage `yaml:"stages,omitempty"`
}
Expand All @@ -187,7 +188,7 @@ type Loader func(s string, fs vfs.FS, m Modifier) ([]byte, error)
type Modifier func(s []byte) ([]byte, error)

type yipLoader interface {
Load([]byte, vfs.FS) (*YipConfig, error)
Load(string, []byte, vfs.FS) (*YipConfig, error)
}

func Load(s string, fs vfs.FS, l Loader, m Modifier) (*YipConfig, error) {
Expand All @@ -206,7 +207,7 @@ func Load(s string, fs vfs.FS, l Loader, m Modifier) (*YipConfig, error) {
if err != nil {
return nil, errors.Wrap(err, "invalid file type")
}
return loader.Load(data, fs)
return loader.Load(s, data, fs)
}

func detect(b []byte) (yipLoader, error) {
Expand Down

0 comments on commit 68c3b4d

Please sign in to comment.