Skip to content

Commit

Permalink
Tidy up loader code a little and make more generic
Browse files Browse the repository at this point in the history
We weren't using some of this anyway and we could easily just load any
file into an object - so lets use it for catalog as well as the config
file.
  • Loading branch information
rliddler committed Oct 20, 2023
1 parent ab02aa8 commit 73148f1
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 90 deletions.
4 changes: 2 additions & 2 deletions cmd/tap-incident/cmd/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func loadCatalogOrError(ctx context.Context, catalogFile string) (catalog *tap.C
OUT("Failed to load catalog file!\n")
}()

catalog, err = tap.CatalogFileLoader(catalogFile).Load(ctx)
catalog, err = config.LoadAndParse(catalogFile, tap.Catalog{})
if err != nil {
return nil, errors.Wrap(err, "loading catalog")
}
Expand Down Expand Up @@ -167,7 +167,7 @@ func loadConfigOrError(ctx context.Context, configFile string) (cfg *config.Conf
return nil, errors.New("No config file set! (--config)")
}

cfg, err = config.FileLoader(configFile).Load(ctx)
cfg, err = config.LoadAndParse(configFile, config.Config{})
if err != nil {
return nil, errors.Wrap(err, "loading config")
}
Expand Down
7 changes: 0 additions & 7 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package config

import (
"context"

validation "github.com/go-ozzo/ozzo-validation"
)

Expand All @@ -17,8 +15,3 @@ func (c Config) Validate() error {
Error("must provide an api_key to authenticate against the incident.io API.")),
)
}

// Load returns currently loaded config
func (c Config) Load(context.Context) (Config, error) {
return Config(c), nil
}
30 changes: 13 additions & 17 deletions config/loader.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
package config

import (
"context"
"encoding/json"
"os"
)

type Loader interface {
Load(context.Context) (*Config, error)
}
"github.com/pkg/errors"
)

type LoaderFunc func(context.Context) (*Config, error)
func LoadAndParse[T any](path string, obj T) (*T, error) {
b, err := os.ReadFile(path)
if err != nil {
return &obj, errors.Wrapf(err, "unable to read file at path %v", path)
}

func (l LoaderFunc) Load(ctx context.Context) (*Config, error) {
return l(ctx)
return ParseContents(b, obj)
}

// FileLoader loads config from a filepath
type FileLoader string

func (l FileLoader) Load(context.Context) (*Config, error) {
data, err := os.ReadFile(string(l))
if err != nil {
return nil, err
func ParseContents[T any](content []byte, obj T) (*T, error) {
if err := json.Unmarshal(content, &obj); err != nil {
return &obj, err
}

return Parse(string(l), data)
return &obj, nil
}
28 changes: 0 additions & 28 deletions config/parse.go

This file was deleted.

36 changes: 0 additions & 36 deletions tap/catalog.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package tap

import (
"context"
"encoding/json"
"os"

"github.com/incident-io/singer-tap/model"
"github.com/pkg/errors"
)

// A catalog can contain several streams or "entries"
Expand Down Expand Up @@ -84,34 +79,3 @@ func NewDefaultCatalog(streams map[string]Stream) *Catalog {
Streams: entries,
}
}

type CatalogLoader interface {
Load(context.Context) (*Catalog, error)
}

type CatalogLoaderFunc func(context.Context) (*Catalog, error)

func (l CatalogLoaderFunc) Load(ctx context.Context) (*Catalog, error) {
return l(ctx)
}

// CatalogFileLoader loads Catalog from a filepath
type CatalogFileLoader string

func (l CatalogFileLoader) Load(context.Context) (*Catalog, error) {
data, err := os.ReadFile(string(l))
if err != nil {
return nil, err
}

return ParseCatalogFile(string(l), data)
}

func ParseCatalogFile(filename string, data []byte) (*Catalog, error) {
var catalog Catalog
if err := json.Unmarshal(data, &catalog); err != nil {
return nil, errors.Wrap(err, "parsing json")
}

return &catalog, nil
}

0 comments on commit 73148f1

Please sign in to comment.