Skip to content

Commit 0912aa9

Browse files
committed
fix: correct plugin behavior
1 parent fc17375 commit 0912aa9

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed

internal/base/interfaces.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,8 @@ func (p *Package) Clone() *Package {
251251
}
252252
}
253253

254+
var ErrNoResultProvide = errors.New("no result provided")
255+
254256
type Plugin interface {
255257
Available(ctx *AvailableHookCtx) ([]*AvailableHookResultItem, error)
256258

internal/plugin/luai/plugin.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ func (l *LuaPlugin) Available(ctx *base.AvailableHookCtx) ([]*base.AvailableHook
3838
return nil, err
3939
}
4040
if table == nil || table.Type() == lua.LTNil {
41-
return nil, errors.New("no result provided")
41+
return nil, base.ErrNoResultProvide
4242
}
4343

44-
hookResult := []*base.AvailableHookResultItem{}
44+
var hookResult []*base.AvailableHookResultItem
4545
err = Unmarshal(table, &hookResult)
4646
if err != nil {
4747
return nil, errors.New("failed to unmarshal the return value: " + err.Error())
@@ -60,7 +60,7 @@ func (l *LuaPlugin) PreInstall(ctx *base.PreInstallHookCtx) (*base.PreInstallHoo
6060
return nil, err
6161
}
6262
if table == nil || table.Type() == lua.LTNil {
63-
return nil, errors.New("no result provided")
63+
return nil, base.ErrNoResultProvide
6464
}
6565
hookResult := base.PreInstallHookResult{}
6666
err = Unmarshal(table, &hookResult)
@@ -81,7 +81,7 @@ func (l *LuaPlugin) EnvKeys(ctx *base.EnvKeysHookCtx) ([]*base.EnvKeysHookResult
8181
return nil, err
8282
}
8383
if table == nil || table.Type() == lua.LTNil || table.Len() == 0 {
84-
return nil, fmt.Errorf("no environment variables provided")
84+
return nil, base.ErrNoResultProvide
8585
}
8686

8787
var hookResult []*base.EnvKeysHookResultItem
@@ -103,7 +103,7 @@ func (l *LuaPlugin) PreUse(ctx *base.PreUseHookCtx) (*base.PreUseHookResult, err
103103
return nil, err
104104
}
105105
if table == nil || table.Type() == lua.LTNil {
106-
return nil, errors.New("no result provided")
106+
return nil, base.ErrNoResultProvide
107107
}
108108
hookResult := base.PreUseHookResult{}
109109
err = Unmarshal(table, &hookResult)
@@ -144,7 +144,7 @@ func (l *LuaPlugin) ParseLegacyFile(ctx *base.ParseLegacyFileHookCtx) (*base.Par
144144
return nil, err
145145
}
146146
if table == nil || table.Type() == lua.LTNil {
147-
return nil, errors.New("no result provided")
147+
return nil, base.ErrNoResultProvide
148148
}
149149
hookResult := base.ParseLegacyFileResult{}
150150
err = Unmarshal(table, &hookResult)

internal/plugin/plugin.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,13 @@ func (l *PluginWrapper) invokeAvailable(args []string) ([]*base.Package, error)
110110
Args: args,
111111
}
112112
hookResult, err := l.impl.Available(&ctx)
113+
if l.isNoResultProvided(err) {
114+
return []*base.Package{}, nil
115+
}
116+
113117
if err != nil {
114118
return nil, err
115119
}
116-
if hookResult == nil {
117-
return []*base.Package{}, nil
118-
}
119120

120121
var result []*base.Package
121122
for _, item := range hookResult {
@@ -200,12 +201,12 @@ func (l *PluginWrapper) PreInstall(version base.Version) (*base.Package, error)
200201
}
201202

202203
result, err := l.impl.PreInstall(&ctx)
204+
if l.isNoResultProvided(err) {
205+
return &base.Package{}, nil
206+
}
203207
if err != nil {
204208
return nil, err
205209
}
206-
if result == nil {
207-
return &base.Package{}, nil
208-
}
209210

210211
mainSdk, err := result.Info()
211212
if err != nil {
@@ -264,12 +265,12 @@ func (l *PluginWrapper) EnvKeys(sdkPackage *base.Package) (*env.Envs, error) {
264265

265266
logger.Debugf("EnvKeysHookCtx: %+v \n", ctx)
266267
items, err := l.impl.EnvKeys(ctx)
268+
if l.isNoResultProvided(err) {
269+
return nil, fmt.Errorf("no environment variables provided")
270+
}
267271
if err != nil {
268272
return nil, err
269273
}
270-
if len(items) == 0 {
271-
return nil, fmt.Errorf("no environment variables provided")
272-
}
273274

274275
envKeys := &env.Envs{
275276
Variables: make(env.Vars),
@@ -312,6 +313,11 @@ func (l *PluginWrapper) PreUse(version base.Version, previousVersion base.Versio
312313
logger.Debugf("PreUseHookCtx: %+v \n", ctx)
313314

314315
result, err := l.impl.PreUse(&ctx)
316+
317+
if l.isNoResultProvided(err) {
318+
return "", nil
319+
}
320+
315321
if err != nil {
316322
return "", err
317323
}
@@ -369,6 +375,10 @@ func (l *PluginWrapper) PreUninstall(p *base.Package) error {
369375
return l.impl.PreUninstall(ctx)
370376
}
371377

378+
func (l *PluginWrapper) isNoResultProvided(err error) bool {
379+
return errors.Is(err, base.ErrNoResultProvide)
380+
}
381+
372382
func IsLuaPluginDir(pluginDirPath string) bool {
373383
metadataPath := filepath.Join(pluginDirPath, "metadata.lua")
374384
if util.FileExists(metadataPath) {

0 commit comments

Comments
 (0)