Skip to content

Commit 0f920b9

Browse files
authored
Merge pull request #33 from b4b4r07/fix/issue-32
Fix Installed() method behavior on plugin install
2 parents 588ae09 + 03f9d39 commit 0f920b9

File tree

1 file changed

+42
-15
lines changed

1 file changed

+42
-15
lines changed

pkg/config/plugin.go

+42-15
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os/exec"
1010
"path/filepath"
1111

12+
"github.com/goccy/go-yaml"
1213
"github.com/mattn/go-zglob"
1314
)
1415

@@ -21,18 +22,49 @@ type Plugin struct {
2122
If string `yaml:"if"`
2223
}
2324

24-
// Installed returns true ...
25-
func (p Plugin) Installed(pkg Package) bool {
26-
for _, source := range p.Sources {
27-
matches := glob(filepath.Join(pkg.GetHome(), source))
28-
if len(matches) == 0 {
29-
return false
30-
}
25+
func (p *Plugin) UnmarshalYAML(b []byte) error {
26+
type alias Plugin
27+
28+
// Unlike UnmarshalJSON, all of fields in struct should be listed here...
29+
// http://choly.ca/post/go-json-marshalling/
30+
// https://go.dev/play/p/rozEOsAYHPe // JSON works but replacing json with yaml then not working
31+
// https://stackoverflow.com/questions/48674624/unmarshal-a-yaml-to-a-struct-with-unexpected-fields-in-go
32+
// https://go.dev/play/p/XZg7tEPGXna // other YAML case
33+
tmp := struct {
34+
*alias
35+
Sources []string `yaml:"sources" validate:"required"`
36+
Env map[string]string `yaml:"env"`
37+
Snippet string `yaml:"snippet"`
38+
SnippetPrepare string `yaml:"snippet-prepare"`
39+
If string `yaml:"if"`
40+
}{
41+
alias: (*alias)(p),
42+
}
43+
44+
if err := yaml.Unmarshal(b, &tmp); err != nil {
45+
return err
3146
}
32-
return true
47+
48+
var sources []string
49+
for _, source := range tmp.Sources {
50+
sources = append(sources, expandTilda(os.ExpandEnv(source)))
51+
}
52+
53+
p.Sources = sources
54+
p.Env = tmp.Env
55+
p.Snippet = tmp.Snippet
56+
p.SnippetPrepare = tmp.SnippetPrepare
57+
p.If = tmp.If
58+
59+
return nil
3360
}
3461

35-
// Install is
62+
// Installed returns true if sources exist at least one or more
63+
func (p Plugin) Installed(pkg Package) bool {
64+
return len(p.GetSources(pkg)) > 0
65+
}
66+
67+
// Install runs nothing on plugin installation
3668
func (p Plugin) Install(pkg Package) error {
3769
return nil
3870
}
@@ -82,12 +114,7 @@ func (p Plugin) Init(pkg Package) error {
82114
fmt.Printf("%s\n", s)
83115
}
84116

85-
sources := p.GetSources(pkg)
86-
if len(sources) == 0 {
87-
return fmt.Errorf("%s: failed to get sources", pkg.GetName())
88-
}
89-
90-
for _, src := range sources {
117+
for _, src := range p.GetSources(pkg) {
91118
fmt.Printf("source %s\n", src)
92119
}
93120

0 commit comments

Comments
 (0)