Skip to content

Commit 6eb7f24

Browse files
committed
Add includes flag to include dependent proto files (hjames9#145)
1 parent 1d452d1 commit 6eb7f24

7 files changed

Lines changed: 181 additions & 124 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ Options:
7373
--lockdir [.] directory of proto.lock file
7474
--protoroot [.] root of directory tree containing proto files
7575
--uptodate [false] enforce that proto.lock file is up-to-date with proto files
76+
--includes comma-separated list of additional filepaths to include
7677
```
7778

7879
## Related Projects & Users

cmd/protolock/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Options:
3434
--lockdir [.] directory of proto.lock file
3535
--protoroot [.] root of directory tree containing proto files
3636
--uptodate [false] enforce that proto.lock file is up-to-date with proto files
37+
--includes comma-separated list of additional filepaths to include
3738
`
3839

3940
var (
@@ -46,6 +47,7 @@ var (
4647
lockDir = options.String("lockdir", ".", "directory of proto.lock file")
4748
protoRoot = options.String("protoroot", ".", "root of directory tree containing proto files")
4849
upToDate = options.Bool("uptodate", false, "enforce that proto.lock file is up-to-date with proto files")
50+
includes = options.String("includes", "", "comma-separated list of additional filepaths to include")
4951
)
5052

5153
func main() {
@@ -66,6 +68,7 @@ func main() {
6668
*ignore,
6769
*upToDate,
6870
*debug,
71+
*includes,
6972
)
7073
if err != nil {
7174
fmt.Println(err)

config.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package protolock
33
import (
44
"os"
55
"path/filepath"
6+
"strings"
67
)
78

89
type Config struct {
@@ -11,11 +12,12 @@ type Config struct {
1112
Ignore string
1213
UpToDate bool
1314
Debug bool
15+
Includes []string
1416
}
1517

1618
func NewConfig(
1719
lockDir, protoRoot, ignores string,
18-
upToDate, debug bool,
20+
upToDate, debug bool, includes string,
1921
) (*Config, error) {
2022
l, err := filepath.Abs(lockDir)
2123
if err != nil {
@@ -26,11 +28,24 @@ func NewConfig(
2628
return nil, err
2729
}
2830

31+
var includesAbs []string
32+
if len(includes) > 0 {
33+
includesOrig := strings.Split(includes, ",")
34+
for _, c := range includesOrig {
35+
i, err := filepath.Abs(c)
36+
if err != nil {
37+
return nil, err
38+
}
39+
includesAbs = append(includesAbs, i)
40+
}
41+
}
42+
2943
return &Config{
3044
LockDir: l,
3145
ProtoRoot: p,
3246
Ignore: ignores,
3347
UpToDate: upToDate,
48+
Includes: includesAbs,
3449
}, nil
3550
}
3651

parse.go

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ type Protolock struct {
2121
}
2222

2323
type Definition struct {
24-
Filepath Protopath `json:"protopath,omitempty"`
25-
Def Entry `json:"def,omitempty"`
24+
Filepath *Protopath `json:"protopath,omitempty"`
25+
Def Entry `json:"def,omitempty"`
2626
}
2727

2828
type Entry struct {
@@ -49,14 +49,14 @@ type Option struct {
4949
}
5050

5151
type Message struct {
52-
Name string `json:"name,omitempty"`
53-
Fields []Field `json:"fields,omitempty"`
54-
Maps []Map `json:"maps,omitempty"`
55-
ReservedIDs []int `json:"reserved_ids,omitempty"`
56-
ReservedNames []string `json:"reserved_names,omitempty"`
57-
Filepath Protopath `json:"filepath,omitempty"`
58-
Messages []Message `json:"messages,omitempty"`
59-
Options []Option `json:"options,omitempty"`
52+
Name string `json:"name,omitempty"`
53+
Fields []Field `json:"fields,omitempty"`
54+
Maps []Map `json:"maps,omitempty"`
55+
ReservedIDs []int `json:"reserved_ids,omitempty"`
56+
ReservedNames []string `json:"reserved_names,omitempty"`
57+
Filepath *Protopath `json:"filepath,omitempty"`
58+
Messages []Message `json:"messages,omitempty"`
59+
Options []Option `json:"options,omitempty"`
6060
}
6161

6262
type EnumField struct {
@@ -90,9 +90,9 @@ type Field struct {
9090
}
9191

9292
type Service struct {
93-
Name string `json:"name,omitempty"`
94-
RPCs []RPC `json:"rpcs,omitempty"`
95-
Filepath Protopath `json:"filepath,omitempty"`
93+
Name string `json:"name,omitempty"`
94+
RPCs []RPC `json:"rpcs,omitempty"`
95+
Filepath *Protopath `json:"filepath,omitempty"`
9696
}
9797

9898
type RPC struct {
@@ -111,13 +111,13 @@ type Report struct {
111111
}
112112

113113
type Warning struct {
114-
Filepath Protopath `json:"filepath,omitempty"`
115-
Message string `json:"message,omitempty"`
116-
RuleName string `json:"rulename,omitempty"`
114+
Filepath *Protopath `json:"filepath,omitempty"`
115+
Message string `json:"message,omitempty"`
116+
RuleName string `json:"rulename,omitempty"`
117117
}
118118

119119
type ProtoFile struct {
120-
ProtoPath Protopath
120+
ProtoPath *Protopath
121121
Entry Entry
122122
}
123123

@@ -512,10 +512,10 @@ func Compare(current, update Protolock) (*Report, error) {
512512
}
513513

514514
// getProtoFiles finds recursively all .proto files to be processed.
515-
func getProtoFiles(root string, ignores string) ([]string, error) {
515+
func getProtoFiles(root string, ignores string, includes []string) ([]string, error) {
516516
protoFiles := []string{}
517517

518-
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
518+
protoWalker := func(path string, info os.FileInfo, err error) error {
519519
if err != nil {
520520
return err
521521
}
@@ -547,11 +547,22 @@ func getProtoFiles(root string, ignores string) ([]string, error) {
547547
protoFiles = append(protoFiles, path)
548548

549549
return nil
550-
})
550+
}
551+
552+
err := filepath.Walk(root, protoWalker)
551553
if err != nil {
552554
return nil, err
553555
}
554556

557+
if len(includes) > 0 {
558+
for _, i := range includes {
559+
err := filepath.Walk(i, protoWalker)
560+
if err != nil {
561+
return nil, err
562+
}
563+
}
564+
}
565+
555566
return protoFiles, nil
556567
}
557568

@@ -566,7 +577,7 @@ func getUpdatedLock(cfg Config) (*Protolock, error) {
566577
return nil, err
567578
}
568579

569-
protoFiles, err := getProtoFiles(root, cfg.Ignore)
580+
protoFiles, err := getProtoFiles(root, cfg.Ignore, cfg.Includes)
570581
if err != nil {
571582
return nil, err
572583
}
@@ -593,9 +604,27 @@ func getUpdatedLock(cfg Config) (*Protolock, error) {
593604
}
594605

595606
localPath := strings.TrimPrefix(path, root)
607+
isProtoRootFile := localPath != path
596608
localPath = strings.TrimPrefix(localPath, string(filepath.Separator))
609+
610+
var pathType string
611+
if isProtoRootFile {
612+
pathType = "protodir"
613+
} else {
614+
includesPos := 0
615+
for pos, include := range cfg.Includes {
616+
if strings.HasPrefix(localPath, include) {
617+
includesPos = pos
618+
break
619+
}
620+
}
621+
pathType = fmt.Sprintf("includes%d", includesPos)
622+
localPath = strings.TrimPrefix(path, cfg.Includes[includesPos])
623+
localPath = strings.TrimPrefix(localPath, string(filepath.Separator))
624+
}
625+
597626
protoFile := ProtoFile{
598-
ProtoPath: ProtoPath(Protopath(localPath)),
627+
ProtoPath: &Protopath{localPath, pathType},
599628
Entry: entry,
600629
}
601630
files = append(files, protoFile)

protopath.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,31 @@ const (
1515
)
1616

1717
// Protopath is a type to assist in OS filepath transformations
18-
type Protopath string
18+
type Protopath struct {
19+
PathName string `json:"path_name,omitempty"`
20+
PathType string `json:"path_type,omitempty"`
21+
}
1922

2023
// OSPath converts a path in the Protopath format to the OS path format
2124
func OSPath(ProtoPath Protopath) Protopath {
22-
return Protopath(
23-
strings.Replace(string(ProtoPath), ProtoSep, FileSep, -1),
24-
)
25+
return Protopath{
26+
strings.Replace(string(ProtoPath.PathName), ProtoSep, FileSep, -1),
27+
ProtoPath.PathType,
28+
}
2529
}
2630

2731
// ProtoPath converts a path in the OS path format to Protopath format
2832
func ProtoPath(OSPath Protopath) Protopath {
29-
return Protopath(
30-
strings.Replace(string(OSPath), FileSep, ProtoSep, -1),
31-
)
33+
return Protopath{
34+
strings.Replace(string(OSPath.PathName), FileSep, ProtoSep, -1),
35+
OSPath.PathType,
36+
}
3237
}
3338

3439
func (p Protopath) String() string {
35-
return string(p)
40+
return string(p.PathName)
41+
}
42+
43+
func ProtopathPtr(p Protopath) *Protopath {
44+
return &p
3645
}

report.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ func HandleReport(report *Report, w io.Writer, err error) (int, error) {
2828

2929
func orderByPathAndMessage(warnings []Warning) {
3030
sort.Slice(warnings, func(i, j int) bool {
31-
if warnings[i].Filepath < warnings[j].Filepath {
31+
if warnings[i].Filepath.PathName < warnings[j].Filepath.PathName {
3232
return true
3333
}
34-
if warnings[i].Filepath > warnings[j].Filepath {
34+
if warnings[i].Filepath.PathName > warnings[j].Filepath.PathName {
3535
return false
3636
}
3737
return warnings[i].Message < warnings[j].Message

0 commit comments

Comments
 (0)