-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[plugins] Use reflikes for plugins (#1845)
## Summary This changes plugins to use new `reflike` struct. It brings it more in line with how flake refs work. For now, only some types are supported. ## How was it tested? CICD
- Loading branch information
1 parent
c3efb8f
commit 1cb6e09
Showing
10 changed files
with
204 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,14 @@ | ||
package plugin | ||
|
||
import ( | ||
"encoding/json" | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
"strings" | ||
|
||
"go.jetpack.io/devbox/internal/boxcli/usererr" | ||
"go.jetpack.io/devbox/internal/cachehash" | ||
"go.jetpack.io/devbox/internal/devpkg" | ||
) | ||
|
||
type Includable interface { | ||
CanonicalName() string | ||
Hash() string | ||
FileContent(subpath string) ([]byte, error) | ||
} | ||
|
||
func (m *Manager) ParseInclude(include string) (Includable, error) { | ||
includeType, name, _ := strings.Cut(include, ":") | ||
if name == "" { | ||
return nil, usererr.New("include name is required") | ||
} else if includeType == "plugin" { | ||
if t, name, _ := strings.Cut(include, ":"); t == "plugin" { | ||
return devpkg.PackageFromStringWithDefaults(name, m.lockfile), nil | ||
} else if includeType == "path" { | ||
absPath := filepath.Join(m.ProjectDir(), name) | ||
return newLocalPlugin(absPath) | ||
} else if includeType == "github" { | ||
return newGithubPlugin(name) | ||
} | ||
return nil, usererr.New("unknown include type %q", includeType) | ||
} | ||
|
||
type localPlugin struct { | ||
name string | ||
path string | ||
} | ||
|
||
var nameRegex = regexp.MustCompile(`^[a-zA-Z0-9_\- ]+$`) | ||
|
||
func newLocalPlugin(path string) (*localPlugin, error) { | ||
content, err := os.ReadFile(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
m := map[string]any{} | ||
if err := json.Unmarshal(content, &m); err != nil { | ||
return nil, err | ||
} | ||
name, ok := m["name"].(string) | ||
if !ok || name == "" { | ||
return nil, | ||
usererr.New("plugin %s is missing a required field 'name'", path) | ||
} | ||
if !nameRegex.MatchString(name) { | ||
return nil, usererr.New( | ||
"plugin %s has an invalid name %q. Name must match %s", | ||
path, name, nameRegex, | ||
) | ||
} | ||
return &localPlugin{ | ||
name: name, | ||
path: path, | ||
}, nil | ||
} | ||
|
||
func (l *localPlugin) CanonicalName() string { | ||
return l.name | ||
} | ||
|
||
func (l *localPlugin) IsLocal() bool { | ||
return true | ||
} | ||
|
||
func (l *localPlugin) Hash() string { | ||
h, _ := cachehash.Bytes([]byte(l.path)) | ||
return h | ||
} | ||
|
||
func (l *localPlugin) FileContent(subpath string) ([]byte, error) { | ||
return os.ReadFile(filepath.Join(filepath.Dir(l.path), subpath)) | ||
return parseReflike(include) | ||
} |
Oops, something went wrong.