Skip to content

Commit

Permalink
chore: support config
Browse files Browse the repository at this point in the history
  • Loading branch information
vic3lord committed Oct 15, 2024
1 parent 71c126b commit f33c435
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bufile
25 changes: 25 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package config

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

type Config struct {
Modules []string `json:"modules"`
}

func Parse(path string) (Config, error) {
var cfg Config
f, err := os.ReadFile(path)
if err != nil {
return cfg, fmt.Errorf("reading config file: %w", err)
}

err = json.Unmarshal(f, &cfg)
if err != nil {
return cfg, fmt.Errorf("parsing config file: %w", err)
}
return cfg, nil
}
5 changes: 5 additions & 0 deletions example.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"modules": [
"buf.build/vic3lord/bufile"
]
}
24 changes: 22 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,35 @@ package main

import (
"context"
"flag"
"log"
"log/slog"
"os"

"github.com/vic3lord/bufile/config"
"github.com/vic3lord/bufile/route"
)

var (
configFile = flag.String("config", "bufile.json", "Path to config file")
)

func main() {
err := route.Generate(context.Background(), os.Stdout)
flag.Parse()
cfg, err := config.Parse(*configFile)
if err != nil {
log.Fatalf("failed to generate routes: %v", err)
log.Fatalf("Failed to parse config: %v", err)
}

ctx := context.Background()
for _, mod := range cfg.Modules {
err = route.Generate(ctx, mod, os.Stdout)
if err != nil {
l := slog.With(
slog.String("module", mod),
slog.String("err", err.Error()),
)
l.Error("failed to generate routes")
}
}
}
2 changes: 1 addition & 1 deletion route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type Rule struct {
//go:embed template.yaml
var routesTemplate string

func Generate(ctx context.Context, w io.Writer) error {
func Generate(ctx context.Context, mod string, w io.Writer) error {
tmpl, err := template.New("routes-template").Parse(routesTemplate)
if err != nil {
return err
Expand Down

0 comments on commit f33c435

Please sign in to comment.