Skip to content

Commit b9064ec

Browse files
authored
feat: Add support for diroctories from buf v2 and easyp as well
1 parent c31ddcf commit b9064ec

3 files changed

Lines changed: 87 additions & 36 deletions

File tree

internal/adapters/module_config/read_buf_work.go

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ package moduleconfig
22

33
import (
44
"context"
5-
"errors"
65
"fmt"
7-
"log/slog"
86
"strings"
97

108
"gopkg.in/yaml.v3"
@@ -13,28 +11,70 @@ import (
1311
"github.com/easyp-tech/easyp/internal/core/models"
1412
)
1513

16-
type bufWork struct {
14+
type bufV1Config struct {
1715
Directories []string `yaml:"directories"`
1816
}
1917

18+
type bufV2Config struct {
19+
Modules []struct {
20+
Path string `yaml:"path"`
21+
Name string `yaml:"name"`
22+
Lint struct {
23+
IgnoreOnly struct {
24+
PACKAGEVERSIONSUFFIX []string `yaml:"PACKAGE_VERSION_SUFFIX"`
25+
} `yaml:"ignore_only"`
26+
} `yaml:"lint"`
27+
} `yaml:"modules"`
28+
}
29+
2030
const (
21-
bufWorkFile = "buf.work.yaml"
31+
bufV1ConfigFile = "buf.work.yaml"
32+
bufV2ConfigFile = "buf.yaml"
2233
)
2334

24-
func readBufWork(ctx context.Context, repo repository.Repo, revision models.Revision) (bufWork, error) {
25-
content, err := repo.ReadFile(ctx, revision, bufWorkFile)
35+
func readBufWork(ctx context.Context, repo repository.Repo, revision models.Revision) (models.ModuleConfig, error) {
36+
bufV1, err := readBufV1(ctx, repo, revision)
37+
if err == nil {
38+
return bufV1, nil
39+
}
40+
41+
bufV2, err := readBufV2(ctx, repo, revision)
42+
return bufV2, err
43+
}
44+
45+
func readBufV1(ctx context.Context, repo repository.Repo, revision models.Revision) (models.ModuleConfig, error) {
46+
content, err := repo.ReadFile(ctx, revision, bufV1ConfigFile)
47+
if err != nil {
48+
return models.ModuleConfig{}, fmt.Errorf("repo.ReadFile: %w", err)
49+
}
50+
51+
buf := bufV1Config{}
52+
if err := yaml.NewDecoder(strings.NewReader(content)).Decode(&buf); err != nil {
53+
return models.ModuleConfig{}, fmt.Errorf("yaml.NewDecoder: %w", err)
54+
}
55+
56+
return models.ModuleConfig{
57+
Directories: buf.Directories,
58+
}, nil
59+
}
60+
61+
func readBufV2(ctx context.Context, repo repository.Repo, revision models.Revision) (models.ModuleConfig, error) {
62+
content, err := repo.ReadFile(ctx, revision, bufV2ConfigFile)
2663
if err != nil {
27-
if errors.Is(err, models.ErrFileNotFound) {
28-
slog.Debug("buf config not found")
29-
return bufWork{}, nil
30-
}
31-
return bufWork{}, fmt.Errorf("repo.ReadFile: %w", err)
64+
return models.ModuleConfig{}, fmt.Errorf("repo.ReadFile: %w", err)
3265
}
3366

34-
buf := bufWork{}
67+
buf := bufV2Config{}
3568
if err := yaml.NewDecoder(strings.NewReader(content)).Decode(&buf); err != nil {
36-
return bufWork{}, fmt.Errorf("yaml.NewDecoder: %w", err)
69+
return models.ModuleConfig{}, fmt.Errorf("yaml.NewDecoder: %w", err)
70+
}
71+
72+
dirs := make([]string, 0, len(buf.Modules))
73+
for _, module := range buf.Modules {
74+
dirs = append(dirs, module.Path)
3775
}
3876

39-
return buf, nil
77+
return models.ModuleConfig{
78+
Directories: dirs,
79+
}, nil
4080
}

internal/adapters/module_config/read_easyp.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,38 @@ package moduleconfig
22

33
import (
44
"context"
5-
"errors"
65
"fmt"
7-
"log/slog"
86

97
"github.com/easyp-tech/easyp/internal/adapters/repository"
108
"github.com/easyp-tech/easyp/internal/config"
119
"github.com/easyp-tech/easyp/internal/core/models"
1210
)
1311

1412
// readEasyp read easyp's config from repository
15-
func readEasyp(ctx context.Context, repo repository.Repo, revision models.Revision) ([]models.Module, error) {
13+
func readEasyp(ctx context.Context, repo repository.Repo, revision models.Revision) (models.ModuleConfig, error) {
1614
content, err := repo.ReadFile(ctx, revision, config.DefaultFileName)
1715
if err != nil {
18-
if errors.Is(err, models.ErrFileNotFound) {
19-
slog.Debug("easyp.yaml not found in dependency (this is normal)")
20-
return nil, nil
21-
}
22-
return nil, fmt.Errorf("repo.ReadFile: %w", err)
16+
return models.ModuleConfig{}, fmt.Errorf("repo.ReadFile: %w", err)
2317
}
2418

25-
// Use unified parsing function with environment variable support
26-
cfg, err := config.ParseConfig([]byte(content))
19+
easyp, err := config.ParseConfig([]byte(content))
2720
if err != nil {
28-
return nil, fmt.Errorf("config.ParseConfig: %w", err)
21+
return models.ModuleConfig{}, fmt.Errorf("config.ParseConfig: %w", err)
2922
}
3023

31-
modules := make([]models.Module, 0, len(cfg.Deps))
32-
for _, dep := range cfg.Deps {
24+
modules := make([]models.Module, 0, len(easyp.Deps))
25+
for _, dep := range easyp.Deps {
3326
module := models.NewModule(dep)
3427
modules = append(modules, module)
3528
}
3629

37-
return modules, nil
30+
dirs := make([]string, 0, len(easyp.Generate.Inputs))
31+
for _, input := range easyp.Generate.Inputs {
32+
dirs = append(dirs, input.InputFilesDir.Root)
33+
}
34+
35+
return models.ModuleConfig{
36+
Dependencies: modules,
37+
Directories: dirs,
38+
}, nil
3839
}

internal/adapters/module_config/read_from_repo.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package moduleconfig
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
7+
"log/slog"
68

79
"github.com/easyp-tech/easyp/internal/adapters/repository"
810
"github.com/easyp-tech/easyp/internal/core/models"
@@ -12,19 +14,27 @@ import (
1214
func (c *ModuleConfig) ReadFromRepo(
1315
ctx context.Context, repo repository.Repo, revision models.Revision,
1416
) (models.ModuleConfig, error) {
17+
// buf
18+
slog.Debug("Start read buf config")
19+
1520
buf, err := readBufWork(ctx, repo, revision)
16-
if err != nil {
21+
if err == nil {
22+
return buf, nil
23+
}
24+
if !errors.Is(err, models.ErrFileNotFound) {
1725
return models.ModuleConfig{}, fmt.Errorf("readBufWork: %w", err)
1826
}
1927

20-
modules, err := readEasyp(ctx, repo, revision)
21-
if err != nil {
28+
// easyp
29+
slog.Debug("Start read easyp config")
30+
31+
easyp, err := readEasyp(ctx, repo, revision)
32+
if err == nil {
33+
return easyp, nil
34+
}
35+
if !errors.Is(err, models.ErrFileNotFound) {
2236
return models.ModuleConfig{}, fmt.Errorf("readEasyp: %w", err)
2337
}
2438

25-
moduleConfig := models.ModuleConfig{
26-
Directories: buf.Directories,
27-
Dependencies: modules,
28-
}
29-
return moduleConfig, nil
39+
return models.ModuleConfig{}, nil
3040
}

0 commit comments

Comments
 (0)