fix: improve error messages related to plugins

This commit is contained in:
Ludovic Fernandez 2023-06-02 11:34:06 +02:00 committed by GitHub
parent 6977b68b72
commit 9aa57f362b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 17 deletions

View file

@ -35,12 +35,12 @@ func initPlugins(staticCfg *static.Configuration) (*plugins.Client, map[string]p
var err error
client, err = plugins.NewClient(opts)
if err != nil {
return nil, nil, nil, err
return nil, nil, nil, fmt.Errorf("unable to create plugins client: %w", err)
}
err = plugins.SetupRemotePlugins(client, staticCfg.Experimental.Plugins)
if err != nil {
return nil, nil, nil, err
return nil, nil, nil, fmt.Errorf("unable to set up plugins environment: %w", err)
}
plgs = staticCfg.Experimental.Plugins

View file

@ -264,7 +264,7 @@ func (c *Client) unzipArchive(pName, pVersion string) error {
for _, f := range archive.File {
err = unzipFile(f, dest)
if err != nil {
return err
return fmt.Errorf("unable to unzip %s: %w", f.Name, err)
}
}
@ -283,12 +283,17 @@ func unzipFile(f *zipa.File, dest string) error {
p := filepath.Join(dest, pathParts[1])
if f.FileInfo().IsDir() {
return os.MkdirAll(p, f.Mode())
err = os.MkdirAll(p, f.Mode())
if err != nil {
return fmt.Errorf("unable to create archive directory %s: %w", p, err)
}
return nil
}
err = os.MkdirAll(filepath.Dir(p), 0o750)
if err != nil {
return err
return fmt.Errorf("unable to create archive directory %s for file %s: %w", filepath.Dir(p), p, err)
}
elt, err := os.OpenFile(p, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
@ -347,7 +352,7 @@ func (c *Client) WriteState(plugins map[string]Descriptor) error {
mp, err := json.MarshalIndent(m, "", " ")
if err != nil {
return err
return fmt.Errorf("unable to marshal plugin state: %w", err)
}
return os.WriteFile(c.stateFile, mp, 0o600)
@ -361,10 +366,15 @@ func (c *Client) ResetAll() error {
err := resetDirectory(filepath.Join(c.goPath, ".."))
if err != nil {
return err
return fmt.Errorf("unable to reset plugins GoPath directory %s: %w", c.goPath, err)
}
return resetDirectory(c.archives)
err = resetDirectory(c.archives)
if err != nil {
return fmt.Errorf("unable to reset plugins archives directory: %w", err)
}
return nil
}
func (c *Client) buildArchivePath(pName, pVersion string) string {
@ -374,12 +384,12 @@ func (c *Client) buildArchivePath(pName, pVersion string) string {
func resetDirectory(dir string) error {
dirPath, err := filepath.Abs(dir)
if err != nil {
return err
return fmt.Errorf("unable to get absolute path of %s: %w", dir, err)
}
currentPath, err := os.Getwd()
if err != nil {
return err
return fmt.Errorf("unable to get the current directory: %w", err)
}
if strings.HasPrefix(currentPath, dirPath) {
@ -388,10 +398,15 @@ func resetDirectory(dir string) error {
err = os.RemoveAll(dir)
if err != nil {
return err
return fmt.Errorf("unable to remove directory %s: %w", dirPath, err)
}
return os.MkdirAll(dir, 0o755)
err = os.MkdirAll(dir, 0o755)
if err != nil {
return fmt.Errorf("unable to create directory %s: %w", dirPath, err)
}
return nil
}
func computeHash(filepath string) (string, error) {

View file

@ -21,7 +21,7 @@ func SetupRemotePlugins(client *Client, plugins map[string]Descriptor) error {
err = client.CleanArchives(plugins)
if err != nil {
return fmt.Errorf("failed to clean archives: %w", err)
return fmt.Errorf("unable to clean archives: %w", err)
}
ctx := context.Background()
@ -32,27 +32,27 @@ func SetupRemotePlugins(client *Client, plugins map[string]Descriptor) error {
hash, err := client.Download(ctx, desc.ModuleName, desc.Version)
if err != nil {
_ = client.ResetAll()
return fmt.Errorf("failed to download plugin %s: %w", desc.ModuleName, err)
return fmt.Errorf("unable to download plugin %s: %w", desc.ModuleName, err)
}
err = client.Check(ctx, desc.ModuleName, desc.Version, hash)
if err != nil {
_ = client.ResetAll()
return fmt.Errorf("failed to check archive integrity of the plugin %s: %w", desc.ModuleName, err)
return fmt.Errorf("unable to check archive integrity of the plugin %s: %w", desc.ModuleName, err)
}
}
err = client.WriteState(plugins)
if err != nil {
_ = client.ResetAll()
return fmt.Errorf("failed to write plugins state: %w", err)
return fmt.Errorf("unable to write plugins state: %w", err)
}
for _, desc := range plugins {
err = client.Unzip(desc.ModuleName, desc.Version)
if err != nil {
_ = client.ResetAll()
return fmt.Errorf("failed to unzip archive: %w", err)
return fmt.Errorf("unable to unzip archive: %w", err)
}
}