Skip to content

Commit

Permalink
fix: cleanup repo cache on delete (#90)
Browse files Browse the repository at this point in the history
Fixes #80
  • Loading branch information
martinohmann committed Apr 8, 2021
1 parent 3270739 commit d7491df
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions internal/cmd/repository/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package repository

import (
"fmt"
"os"
"strings"

"github.com/martinohmann/kickoff/internal/cli"
"github.com/martinohmann/kickoff/internal/cmdutil"
Expand Down Expand Up @@ -76,15 +78,33 @@ func (o *RemoveOptions) Validate() error {

// Run removes a skeleton repository from the config.
func (o *RemoveOptions) Run() error {
repoRef, err := kickoff.ParseRepoRef(o.Repositories[o.RepoName])
if err != nil {
return err
}

if repoRef.IsRemote() {
// Prevent removal of anything outside of the local user cache dir.
// Remote repos should never ever reside outside of the user cache dir.
// If they do this is a programmer error.
if !strings.HasPrefix(repoRef.Path, kickoff.LocalRepositoryCacheDir) {
log.WithField("path", repoRef.Path).Fatal("found remote repository cache outside of user cache dir, refusing to delete")
}

log.WithField("path", repoRef.Path).Debug("deleting repository cache dir")

if err := os.RemoveAll(repoRef.Path); err != nil {
return fmt.Errorf("failed to delete repository cache dir: %w", err)
}
}

delete(o.Repositories, o.RepoName)

err := kickoff.SaveConfig(o.ConfigPath, &o.Config)
err = kickoff.SaveConfig(o.ConfigPath, &o.Config)
if err != nil {
return err
}

log.WithField("name", o.RepoName).Info("repository removed")

fmt.Fprintln(o.Out, "Repository removed")

return nil
Expand Down

0 comments on commit d7491df

Please sign in to comment.