9
9
"os/exec"
10
10
"path/filepath"
11
11
12
+ "github.com/goccy/go-yaml"
12
13
"github.com/mattn/go-zglob"
13
14
)
14
15
@@ -21,18 +22,49 @@ type Plugin struct {
21
22
If string `yaml:"if"`
22
23
}
23
24
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
31
46
}
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
33
60
}
34
61
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
36
68
func (p Plugin ) Install (pkg Package ) error {
37
69
return nil
38
70
}
@@ -82,12 +114,7 @@ func (p Plugin) Init(pkg Package) error {
82
114
fmt .Printf ("%s\n " , s )
83
115
}
84
116
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 ) {
91
118
fmt .Printf ("source %s\n " , src )
92
119
}
93
120
0 commit comments