Skip to content

Commit

Permalink
✨ Exporters: add requires key in manifest to specify required binaries
Browse files Browse the repository at this point in the history
  • Loading branch information
gwennlbh committed Apr 14, 2024
1 parent ee9657a commit 342b78d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Requires key in exporter manifests to specify programs required to run the exporter

## [1.1.0] - 2024-04-14

### Added
Expand Down
19 changes: 19 additions & 0 deletions exporter_custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ type CustomExporter struct {
cwd string
}

func (e *CustomExporter) VerifyRequiredPrograms() error {
missingPrograms := make([]string, 0, len(e.manifest.Requires))
for _, program := range e.manifest.Requires {
_, err := exec.LookPath(program)
if err != nil {
missingPrograms = append(missingPrograms, program)
}
}
if len(missingPrograms) > 0 {
return fmt.Errorf("intall %s to use the %s exporter", strings.Join(missingPrograms, ", "), e.name)
}
return nil
}

func (e *CustomExporter) Name() string {
return e.name
}
Expand All @@ -28,6 +42,11 @@ func (e *CustomExporter) OptionsType() any {
}

func (e *CustomExporter) Before(ctx *RunContext, opts ExporterOptions) error {
ctx.LogDebug("Running before commands for %s", e.name)
err := e.VerifyRequiredPrograms()
if err != nil {
return err
}
return e.runCommands(ctx, e.manifest.Verbose, e.manifest.Before, map[string]any{})

}
Expand Down
4 changes: 4 additions & 0 deletions exporters.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ type ExporterManifest struct {

// If true, will show every command that is run
Verbose bool `yaml:"verbose,omitempty"`

// List of programs that are required to be available in the PATH for the exporter to run.
Requires []string `yaml:"requires,omitempty"`
}

// ExporterOptions validates then returns the configuration options for the given exporter.
Expand All @@ -69,6 +72,7 @@ func ValidateExporterOptions(exporter Exporter, opts ExporterOptions) error {
return nil
}


var BuiltinExporters = []Exporter{
&SqlExporter{},
&CustomExporter{},
Expand Down
4 changes: 4 additions & 0 deletions exporters/ssh.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
name: SSH Upload

requires:
- scp

data:
# set to true to only print the commands, not execute them
dry run: false
Expand Down

0 comments on commit 342b78d

Please sign in to comment.