Skip to content
This repository has been archived by the owner on Jan 4, 2025. It is now read-only.

Commit

Permalink
feat(deactivate): concurrent deactivation of layers
Browse files Browse the repository at this point in the history
  • Loading branch information
tulilirockz committed Feb 26, 2024
1 parent 8f1915c commit 26e313a
Showing 1 changed file with 33 additions and 16 deletions.
49 changes: 33 additions & 16 deletions cmd/layer/deactivate/deactivate.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,61 @@
package deactivate

import (
"github.com/spf13/cobra"
"github.com/ublue-os/bext/internal"
"fmt"
"log/slog"
"os"
"path"
"path/filepath"
"strings"
"sync"

"github.com/spf13/cobra"
"github.com/ublue-os/bext/internal"
)

var DeactivateCmd = &cobra.Command{
Use: "deactivate [TARGET]",
Use: "deactivate [TARGET...]",
Short: "Deactivate a layer and refresh sysext",
Long: `Deativate a selected layer (unsymlink it from /var/lib/extensions) and refresh the system extensions store.`,
RunE: deactivateCmd,
Args: cobra.MinimumNArgs(1),
}

func deactivateCmd(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return internal.NewPositionalError("TARGET")
}

target_layer := args[0]

extensions_dir, err := filepath.Abs(path.Clean(internal.Config.ExtensionsDir))
if err != nil {
return err
}

target_layer_path := path.Join(extensions_dir, target_layer+internal.ValidSysextExtension)

if _, err := os.Stat(target_layer_path); err != nil {
return err
var (
errChan chan error
wg sync.WaitGroup
)

for _, target_layer := range args {
wg.Add(1)
go func(errChan chan<- error, target string) {
defer wg.Done()

if err := os.Remove(path.Join(extensions_dir, target+internal.ValidSysextExtension)); err != nil {
errChan <- err
return
}
}(errChan, target_layer)
}

if err := os.Remove(target_layer_path); err != nil {
return err
go func() {
wg.Wait()
close(errChan)
}()

for err := range errChan {
slog.Warn(fmt.Sprintf("Error encountered when deactivating layers: %s", err.Error()), slog.String("error", err.Error()))
}

slog.Info("Successfully deactivated " + target_layer)
if len(errChan) == 0 {
slog.Info("Successfully deactivated layers", slog.String("layers", strings.Join(args, " ")))
}

return nil
}

0 comments on commit 26e313a

Please sign in to comment.