@@ -2,9 +2,7 @@ package moduleconfig
22
33import (
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+
2030const (
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}
0 commit comments