Skip to content

Commit 695de14

Browse files
committed
v0.0.9: misc improvements
1 parent 7530a81 commit 695de14

File tree

15 files changed

+531
-313
lines changed

15 files changed

+531
-313
lines changed

api/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ func (s *Session) FetchCredentials(ctx context.Context, orgSlug, realmSlug strin
178178
var creds struct {
179179
Items []Credential `json:"items,omitempty"`
180180
}
181+
181182
if err := s.get(ctx, fetchCredentialsPath(orgSlug, realmSlug), &creds); err != nil {
182183
return nil, err
183184
}

detection/detection.go

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package detection
22

33
import (
4+
"io/fs"
5+
46
"github.com/anchordotdev/cli/anchorcli"
57
)
68

@@ -33,26 +35,32 @@ func (s Confidence) String() string {
3335

3436
var (
3537
DefaultDetectors = []Detector{
36-
RubyDetector,
37-
GoDetector,
38-
JavascriptDetector,
39-
PythonDetector,
38+
Ruby,
39+
Go,
40+
Javascript,
41+
Python,
4042
}
4143

4244
DetectorsByFlag = map[string]Detector{
43-
"django": DjangoDetector,
44-
"flask": FlaskDetector,
45-
"go": GoDetector,
46-
"javascript": JavascriptDetector,
47-
"python": PythonDetector,
48-
"rails": RailsDetector,
49-
"ruby": RubyDetector,
50-
"sinatra": SinatraDetector,
45+
"django": Django,
46+
"flask": Flask,
47+
"go": Go,
48+
"javascript": Javascript,
49+
"nextjs": NextJS,
50+
"python": Python,
51+
"rails": Rails,
52+
"ruby": Ruby,
53+
"sinatra": Sinatra,
5154
}
5255

5356
PositiveDetectionMessage = "%s project detected with confidence level %s!\n"
5457
)
5558

59+
type FS interface {
60+
fs.ReadFileFS
61+
fs.StatFS
62+
}
63+
5664
// Match holds the detection result, confidence, and follow-up detectors
5765
type Match struct {
5866
Detector Detector
@@ -68,14 +76,14 @@ type Match struct {
6876
// Detector interface represents a project detector
6977
type Detector interface {
7078
GetTitle() string
71-
Detect(directory string) (Match, error)
79+
Detect(FS) (Match, error)
7280
FollowUp() []Detector
7381
}
7482

75-
func Perform(detectors []Detector, dir string) (Results, error) {
83+
func Perform(detectors []Detector, dirFS FS) (Results, error) {
7684
res := make(Results)
7785
for _, detector := range detectors {
78-
match, err := detector.Detect(dir)
86+
match, err := detector.Detect(dirFS)
7987
if err != nil {
8088
return nil, err
8189
}
@@ -86,7 +94,7 @@ func Perform(detectors []Detector, dir string) (Results, error) {
8694

8795
res[match.Confidence] = append(res[match.Confidence], match)
8896

89-
if followupResults, err := Perform(match.FollowUpDetectors, dir); err == nil {
97+
if followupResults, err := Perform(match.FollowUpDetectors, dirFS); err == nil {
9098
res.merge(followupResults)
9199
} else {
92100
return nil, err

detection/detection_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,21 @@ func TestDefaultDetectors(t *testing.T) {
4141
}
4242

4343
fakeFS := fstest.MapFS{
44-
"app/Gemfile": &fstest.MapFile{Data: []byte(""), Mode: 0644},
45-
"app/Gemfile.lock": &fstest.MapFile{Data: []byte(""), Mode: 0644},
46-
"app/package.json": &fstest.MapFile{Data: []byte(""), Mode: 0644},
47-
"app/requirements.txt": &fstest.MapFile{Data: []byte(""), Mode: 0644},
48-
"app/main.go": &fstest.MapFile{Data: []byte(""), Mode: 0644},
49-
"app/index.js": &fstest.MapFile{Data: []byte(""), Mode: 0644},
50-
"app/app.py": &fstest.MapFile{Data: []byte(""), Mode: 0644},
44+
"Gemfile": &fstest.MapFile{Data: []byte(""), Mode: 0644},
45+
"Gemfile.lock": &fstest.MapFile{Data: []byte(""), Mode: 0644},
46+
"package.json": &fstest.MapFile{Data: []byte(""), Mode: 0644},
47+
"requirements.txt": &fstest.MapFile{Data: []byte(""), Mode: 0644},
48+
"main.go": &fstest.MapFile{Data: []byte(""), Mode: 0644},
49+
"index.js": &fstest.MapFile{Data: []byte(""), Mode: 0644},
50+
"app.py": &fstest.MapFile{Data: []byte(""), Mode: 0644},
5151
}
5252

5353
for _, detector := range DefaultDetectors {
5454
t.Run(detector.GetTitle(), func(t *testing.T) {
5555
// Assume all detectors are FileDetectors right now
5656
det := detector.(*FileDetector)
57-
det.FileSystem = fakeFS
58-
match, err := det.Detect("app")
57+
58+
match, err := det.Detect(fakeFS)
5959
if err != nil {
6060
t.Fatal(err)
6161
}

detection/filesystem.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"errors"
55
"io/fs"
66
"os"
7-
"path/filepath"
87
"slices"
98

109
"github.com/anchordotdev/cli/anchorcli"
@@ -16,7 +15,6 @@ type FileDetector struct {
1615
Paths []string
1716
RequiredFiles []string
1817
FollowUpDetectors []Detector
19-
FileSystem fs.StatFS
2018
AnchorCategory *anchorcli.Category
2119
}
2220

@@ -26,16 +24,11 @@ func (fd FileDetector) GetTitle() string {
2624
}
2725

2826
// Detect checks if the directory contains any of the specified files
29-
func (fd FileDetector) Detect(directory string) (Match, error) {
30-
if fd.FileSystem == nil {
31-
fd.FileSystem = &osFS{}
32-
}
33-
27+
func (fd FileDetector) Detect(dirFS FS) (Match, error) {
3428
var matchedPaths []string
3529

3630
for _, path := range fd.Paths {
37-
fullPath := filepath.Join(directory, path)
38-
if _, err := fd.FileSystem.Stat(fullPath); err == nil {
31+
if _, err := dirFS.Stat(path); err == nil {
3932
matchedPaths = append(matchedPaths, path)
4033
} else if !os.IsNotExist(err) {
4134
return Match{}, errors.Join(err, errors.New("project file detection failure"))

0 commit comments

Comments
 (0)