-
Notifications
You must be signed in to change notification settings - Fork 259
/
file_type.go
99 lines (89 loc) · 2.77 KB
/
file_type.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright 2020-2024 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bufmodule
import (
"fmt"
"strconv"
"github.com/bufbuild/buf/private/pkg/normalpath"
)
const (
FileTypeProto FileType = iota + 1
FileTypeDoc
FileTypeLicense
)
var (
fileTypeToString = map[FileType]string{
FileTypeProto: "proto",
FileTypeDoc: "doc",
FileTypeLicense: "license",
}
stringToFileType = map[string]FileType{
"proto": FileTypeProto,
"doc": FileTypeDoc,
"license": FileTypeLicense,
}
)
type FileType int
func (c FileType) String() string {
s, ok := fileTypeToString[c]
if !ok {
return strconv.Itoa(int(c))
}
return s
}
// ParseFileType parses the file type from its string representation.
//
// This reverses FileType.String().
//
// Returns an error of type *ParseError if thie string could not be parsed.
func ParseFileType(s string) (FileType, error) {
c, ok := stringToFileType[s]
if !ok {
return 0, &ParseError{
typeString: "module file type",
input: s,
err: fmt.Errorf("unknown type: %q", s),
}
}
return c, nil
}
// FileType returns the FileType for the given path.
//
// Returns error if the path cannot be classified as a FileType, that is if it is not a
// .proto file, license file, or documentation file.
//
// Note that license and documentation files must be at the root, and cannot be in subdirectories. That is,
// subdir/LICENSE will not be classified as a FileTypeLicnese, but LICENSE will be.
func FileTypeForPath(path string) (FileType, error) {
if normalpath.Ext(path) == ".proto" {
return FileTypeProto, nil
}
if path == licenseFilePath {
return FileTypeLicense, nil
}
if _, ok := docFilePathMap[path]; ok {
return FileTypeDoc, nil
}
return 0, fmt.Errorf("could not classify FileType for path %q", path)
}
// IsValidModuleFilePath returns true if the given file path is a valid Module file path.
//
// This will be true if the file path represents a .proto file, license file, or documentation file.
//
// Note that license and documentation files must be at the root, and cannot be in subdirectories. That is,
// subdir/LICENSE is not a valid module file (including on push), but LICENSE is.
func IsValidModuleFilePath(filePath string) bool {
_, err := FileTypeForPath(filePath)
return err == nil
}