Skip to content

Commit 73148f1

Browse files
committed
Tidy up loader code a little and make more generic
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.
1 parent ab02aa8 commit 73148f1

File tree

5 files changed

+15
-90
lines changed

5 files changed

+15
-90
lines changed

cmd/tap-incident/cmd/app.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func loadCatalogOrError(ctx context.Context, catalogFile string) (catalog *tap.C
137137
OUT("Failed to load catalog file!\n")
138138
}()
139139

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

170-
cfg, err = config.FileLoader(configFile).Load(ctx)
170+
cfg, err = config.LoadAndParse(configFile, config.Config{})
171171
if err != nil {
172172
return nil, errors.Wrap(err, "loading config")
173173
}

config/config.go

-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package config
22

33
import (
4-
"context"
5-
64
validation "github.com/go-ozzo/ozzo-validation"
75
)
86

@@ -17,8 +15,3 @@ func (c Config) Validate() error {
1715
Error("must provide an api_key to authenticate against the incident.io API.")),
1816
)
1917
}
20-
21-
// Load returns currently loaded config
22-
func (c Config) Load(context.Context) (Config, error) {
23-
return Config(c), nil
24-
}

config/loader.go

+13-17
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,24 @@
11
package config
22

33
import (
4-
"context"
4+
"encoding/json"
55
"os"
6-
)
76

8-
type Loader interface {
9-
Load(context.Context) (*Config, error)
10-
}
7+
"github.com/pkg/errors"
8+
)
119

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

14-
func (l LoaderFunc) Load(ctx context.Context) (*Config, error) {
15-
return l(ctx)
16+
return ParseContents(b, obj)
1617
}
1718

18-
// FileLoader loads config from a filepath
19-
type FileLoader string
20-
21-
func (l FileLoader) Load(context.Context) (*Config, error) {
22-
data, err := os.ReadFile(string(l))
23-
if err != nil {
24-
return nil, err
19+
func ParseContents[T any](content []byte, obj T) (*T, error) {
20+
if err := json.Unmarshal(content, &obj); err != nil {
21+
return &obj, err
2522
}
26-
27-
return Parse(string(l), data)
23+
return &obj, nil
2824
}

config/parse.go

-28
This file was deleted.

tap/catalog.go

-36
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
package tap
22

33
import (
4-
"context"
5-
"encoding/json"
6-
"os"
7-
84
"github.com/incident-io/singer-tap/model"
9-
"github.com/pkg/errors"
105
)
116

127
// A catalog can contain several streams or "entries"
@@ -84,34 +79,3 @@ func NewDefaultCatalog(streams map[string]Stream) *Catalog {
8479
Streams: entries,
8580
}
8681
}
87-
88-
type CatalogLoader interface {
89-
Load(context.Context) (*Catalog, error)
90-
}
91-
92-
type CatalogLoaderFunc func(context.Context) (*Catalog, error)
93-
94-
func (l CatalogLoaderFunc) Load(ctx context.Context) (*Catalog, error) {
95-
return l(ctx)
96-
}
97-
98-
// CatalogFileLoader loads Catalog from a filepath
99-
type CatalogFileLoader string
100-
101-
func (l CatalogFileLoader) Load(context.Context) (*Catalog, error) {
102-
data, err := os.ReadFile(string(l))
103-
if err != nil {
104-
return nil, err
105-
}
106-
107-
return ParseCatalogFile(string(l), data)
108-
}
109-
110-
func ParseCatalogFile(filename string, data []byte) (*Catalog, error) {
111-
var catalog Catalog
112-
if err := json.Unmarshal(data, &catalog); err != nil {
113-
return nil, errors.Wrap(err, "parsing json")
114-
}
115-
116-
return &catalog, nil
117-
}

0 commit comments

Comments
 (0)