|
| 1 | +// SPDX-FileCopyrightText: Copyright The Lima Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package plugin |
| 5 | + |
| 6 | +import ( |
| 7 | + "bufio" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "regexp" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "github.com/sirupsen/logrus" |
| 14 | + |
| 15 | + "github.com/lima-vm/lima/v2/pkg/usrlocalsharelima" |
| 16 | +) |
| 17 | + |
| 18 | +type Plugin struct { |
| 19 | + Name string `json:"name"` |
| 20 | + Path string `json:"path"` |
| 21 | + Description string `json:"description,omitempty"` |
| 22 | +} |
| 23 | + |
| 24 | +func DiscoverPlugins() ([]Plugin, error) { |
| 25 | + var plugins []Plugin |
| 26 | + seen := make(map[string]bool) |
| 27 | + |
| 28 | + dirs := getPluginDirectories() |
| 29 | + |
| 30 | + for _, dir := range dirs { |
| 31 | + pluginsInDir, err := scanDirectory(dir) |
| 32 | + if err != nil { |
| 33 | + logrus.Debugf("Failed to scan directory %s: %v", dir, err) |
| 34 | + continue |
| 35 | + } |
| 36 | + |
| 37 | + for _, plugin := range pluginsInDir { |
| 38 | + if !seen[plugin.Name] { |
| 39 | + plugins = append(plugins, plugin) |
| 40 | + seen[plugin.Name] = true |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + return plugins, nil |
| 46 | +} |
| 47 | + |
| 48 | +func getPluginDirectories() []string { |
| 49 | + var dirs []string |
| 50 | + |
| 51 | + if prefixDir, err := usrlocalsharelima.Prefix(); err == nil { |
| 52 | + libexecDir := filepath.Join(prefixDir, "libexec", "lima") |
| 53 | + if _, err := os.Stat(libexecDir); err == nil { |
| 54 | + dirs = append(dirs, libexecDir) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + pathEnv := os.Getenv("PATH") |
| 59 | + if pathEnv != "" { |
| 60 | + pathDirs := filepath.SplitList(pathEnv) |
| 61 | + dirs = append(dirs, pathDirs...) |
| 62 | + } |
| 63 | + |
| 64 | + return dirs |
| 65 | +} |
| 66 | + |
| 67 | +func scanDirectory(dir string) ([]Plugin, error) { |
| 68 | + var plugins []Plugin |
| 69 | + |
| 70 | + entries, err := os.ReadDir(dir) |
| 71 | + if err != nil { |
| 72 | + return nil, err |
| 73 | + } |
| 74 | + |
| 75 | + for _, entry := range entries { |
| 76 | + if entry.IsDir() { |
| 77 | + continue |
| 78 | + } |
| 79 | + |
| 80 | + name := entry.Name() |
| 81 | + if !strings.HasPrefix(name, "limactl-") { |
| 82 | + continue |
| 83 | + } |
| 84 | + |
| 85 | + pluginName := strings.TrimPrefix(name, "limactl-") |
| 86 | + |
| 87 | + if strings.Contains(pluginName, ".") { |
| 88 | + if filepath.Ext(name) == ".exe" { |
| 89 | + pluginName = strings.TrimSuffix(pluginName, ".exe") |
| 90 | + } else { |
| 91 | + continue |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + fullPath := filepath.Join(dir, name) |
| 96 | + |
| 97 | + if !isExecutable(fullPath) { |
| 98 | + continue |
| 99 | + } |
| 100 | + |
| 101 | + plugin := Plugin{ |
| 102 | + Name: pluginName, |
| 103 | + Path: fullPath, |
| 104 | + } |
| 105 | + |
| 106 | + if desc := getPluginDescription(fullPath); desc != "" { |
| 107 | + plugin.Description = desc |
| 108 | + } |
| 109 | + |
| 110 | + plugins = append(plugins, plugin) |
| 111 | + } |
| 112 | + |
| 113 | + return plugins, nil |
| 114 | +} |
| 115 | + |
| 116 | +func isExecutable(path string) bool { |
| 117 | + info, err := os.Stat(path) |
| 118 | + if err != nil { |
| 119 | + return false |
| 120 | + } |
| 121 | + |
| 122 | + mode := info.Mode() |
| 123 | + if mode&0o111 != 0 { |
| 124 | + return true |
| 125 | + } |
| 126 | + |
| 127 | + if filepath.Ext(path) == ".exe" { |
| 128 | + return true |
| 129 | + } |
| 130 | + |
| 131 | + return false |
| 132 | +} |
| 133 | + |
| 134 | +func getPluginDescription(path string) string { |
| 135 | + file, err := os.Open(path) |
| 136 | + if err != nil { |
| 137 | + return "" |
| 138 | + } |
| 139 | + defer file.Close() |
| 140 | + |
| 141 | + scanner := bufio.NewScanner(file) |
| 142 | + var lines []string |
| 143 | + lineCount := 0 |
| 144 | + |
| 145 | + for scanner.Scan() && lineCount < 20 { |
| 146 | + line := strings.TrimSpace(scanner.Text()) |
| 147 | + if line != "" && !strings.HasPrefix(line, "#!") { |
| 148 | + lines = append(lines, line) |
| 149 | + } |
| 150 | + lineCount++ |
| 151 | + } |
| 152 | + |
| 153 | + limactlRegex := regexp.MustCompile(`limactl\s+(\w+)`) |
| 154 | + |
| 155 | + for _, line := range lines { |
| 156 | + if strings.HasPrefix(line, "#") { |
| 157 | + continue |
| 158 | + } |
| 159 | + |
| 160 | + matches := limactlRegex.FindStringSubmatch(line) |
| 161 | + if len(matches) > 1 { |
| 162 | + commandName := matches[1] |
| 163 | + return "Alias for " + commandName |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + return "" |
| 168 | +} |
| 169 | + |
| 170 | +func UpdatePathForPlugins() error { |
| 171 | + prefixDir, err := usrlocalsharelima.Prefix() |
| 172 | + if err != nil { |
| 173 | + return err |
| 174 | + } |
| 175 | + |
| 176 | + libexecDir := filepath.Join(prefixDir, "libexec", "lima") |
| 177 | + |
| 178 | + if _, err := os.Stat(libexecDir); err == nil { |
| 179 | + currentPath := os.Getenv("PATH") |
| 180 | + |
| 181 | + if !strings.Contains(currentPath, libexecDir) { |
| 182 | + newPath := libexecDir + string(filepath.ListSeparator) + currentPath |
| 183 | + if err := os.Setenv("PATH", newPath); err != nil { |
| 184 | + return err |
| 185 | + } |
| 186 | + logrus.Debugf("Added %s to PATH for plugin discovery", libexecDir) |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + if exe, err := os.Executable(); err == nil { |
| 191 | + binDir := filepath.Dir(exe) |
| 192 | + currentPath := os.Getenv("PATH") |
| 193 | + if !strings.Contains(currentPath, binDir) { |
| 194 | + newPath := binDir + string(filepath.ListSeparator) + currentPath |
| 195 | + if err := os.Setenv("PATH", newPath); err != nil { |
| 196 | + return err |
| 197 | + } |
| 198 | + logrus.Debugf("Added %s to PATH", binDir) |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + return nil |
| 203 | +} |
0 commit comments