2022-06-15 15:23:00 +03:00
|
|
|
package module
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
|
|
"github.com/google/go-containerregistry/pkg/name"
|
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
|
2023-03-30 05:53:24 +03:00
|
|
|
"github.com/aquasecurity/trivy/pkg/fanal/types"
|
2022-06-15 15:23:00 +03:00
|
|
|
"github.com/aquasecurity/trivy/pkg/log"
|
|
|
|
|
"github.com/aquasecurity/trivy/pkg/oci"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const mediaType = "application/vnd.module.wasm.content.layer.v1+wasm"
|
|
|
|
|
|
|
|
|
|
// Install installs a module
|
2023-04-21 00:40:51 +05:30
|
|
|
func Install(ctx context.Context, dir, repo string, quiet bool, opt types.RegistryOptions) error {
|
2022-06-15 15:23:00 +03:00
|
|
|
ref, err := name.ParseReference(repo)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return xerrors.Errorf("repository parse error: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-11 22:59:09 +04:00
|
|
|
log.Info("Installing the module from the repository...", log.String("repo", repo))
|
2024-10-01 19:16:06 +06:00
|
|
|
art := oci.NewArtifact(repo, opt)
|
2022-06-15 15:23:00 +03:00
|
|
|
|
2023-03-01 15:39:53 +05:30
|
|
|
dst := filepath.Join(dir, ref.Context().Name())
|
2024-04-11 22:59:09 +04:00
|
|
|
log.Debug("Installing the module...", log.String("dst", dst))
|
2024-10-01 19:16:06 +06:00
|
|
|
if err = art.Download(ctx, dst, oci.DownloadOption{MediaType: mediaType, Quiet: quiet}); err != nil {
|
2022-06-15 15:23:00 +03:00
|
|
|
return xerrors.Errorf("module download error: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Uninstall uninstalls a module
|
2023-03-01 15:39:53 +05:30
|
|
|
func Uninstall(_ context.Context, dir, repo string) error {
|
2022-06-15 15:23:00 +03:00
|
|
|
ref, err := name.ParseReference(repo)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return xerrors.Errorf("repository parse error: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-11 22:59:09 +04:00
|
|
|
log.Info("Uninstalling the module ...", log.String("module", repo))
|
2023-03-01 15:39:53 +05:30
|
|
|
dst := filepath.Join(dir, ref.Context().Name())
|
2022-06-15 15:23:00 +03:00
|
|
|
if err = os.RemoveAll(dst); err != nil {
|
|
|
|
|
return xerrors.Errorf("remove error: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|