-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilesystem.go
74 lines (60 loc) · 1.4 KB
/
filesystem.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
package main
import (
"io/fs"
"os"
"path/filepath"
tea "github.com/charmbracelet/bubbletea"
)
const TerraformDir = ".terraform"
func isTerraformProject(filesystem fs.FS, d fs.DirEntry, path string) (bool, error) {
if !d.IsDir() {
return false, nil
}
dir, err := fs.ReadDir(filesystem, path)
if err != nil {
return false, err
}
for _, currentDir := range dir {
if currentDir.IsDir() && currentDir.Name() == TerraformDir {
return true, nil
}
}
return false, nil
}
func findAllTerraformProjects(filesystem fs.FS) ([]Project, error) {
projects := []Project{}
fs.WalkDir(filesystem, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.Name() == TerraformDir {
return fs.SkipDir
}
isProject, err := isTerraformProject(filesystem, d, path)
if err != nil {
return err
} else if isProject {
info, err := d.Info()
if err != nil {
return err
}
project := Project{
Name: d.Name(),
Path: filepath.Join(SearchPath, path),
LastModified: info.ModTime(),
Output: "Run Terraform plan to view output",
Valid: "?",
}
projects = append(projects, project)
}
return nil
})
return projects, nil
}
func refreshProjects() tea.Msg {
projects, err := findAllTerraformProjects(os.DirFS(SearchPath))
if err != nil {
return ErrMsg{err}
}
return RefreshFinishedMsg(projects)
}