blob: 3e46deaba672680cb990b44bd21d82ee44a7b3ce [file] [log] [blame]
package dkms
import (
"context"
"fmt"
"os"
"cos.googlesource.com/cos/tools.git/src/pkg/gcs"
"cos.googlesource.com/cos/tools.git/src/pkg/utils"
"github.com/golang/glog"
)
// Remove removes a package from the DKMS source tree.
// This will also unbuild and uninstall the package, if applicable.
func Remove(pkg *Package) error {
if isBuilt(pkg) {
if err := Unbuild(pkg); err != nil {
return err
}
}
if !IsAdded(pkg) {
glog.Info("module is not yet added; skipping removal from source tree")
return nil
}
// We must load the config before the package is removed in order to use
// the correct post-remove script, if applicable
config, err := LoadConfig(pkg)
if err != nil {
return err
}
sourceDir := pkg.SourceDir()
glog.Info("removing package sources", sourceDir)
if err := os.Remove(sourceDir); err != nil {
return fmt.Errorf("failed to remove package sources: %v", err)
}
if config.PostRemove != "" {
if err := utils.RunCommandString(sourceDir, config.PostRemove); err != nil {
return fmt.Errorf("failed to run post-remove script: %v", err)
}
}
return nil
}
// Remove removes a package from the local DKMS source tree and from the cache.
//
// This will also unbuild the package locally and in the cache, if applicable.
// If the package is installed, this will uninstall it.
func CachedRemove(ctx context.Context, pkg *Package, cache *gcs.GCSBucket) error {
if IsBuilt(pkg) {
if err := CachedUnbuild(ctx, pkg, cache); err != nil {
return err
}
}
if err := Remove(pkg); err != nil {
return err
}
if IsAddedInCache(ctx, pkg, cache) {
moduleSourceDir := pkg.SourceDir()
if err := cache.DeleteDir(ctx, moduleSourceDir); err != nil {
return err
}
}
return nil
}