-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathremove.go
83 lines (73 loc) · 1.74 KB
/
remove.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package shortener
import (
"context"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"github.com/google/go-github/github"
)
// Remover implements Runner for the remove operation.
type Remover struct {
Repos []string
Verbose bool
}
func (r *Remover) run(ctx context.Context, client *github.Client) error {
for _, repo := range r.Repos {
i, ent := r.findRepo(repo)
if i < 0 {
return fmt.Errorf("could not find entry %s", repo)
}
var err error
if ent.IsSubdir {
err = r.removeSubdir(ctx, client, ent)
} else {
_, err = client.Repositories.Delete(ctx, ent.Owner, ent.Repo)
}
if err != nil {
return err
}
state.Log.Entries = append(state.Log.Entries[:i], state.Log.Entries[i+1:]...)
if err := saveLog(); err != nil {
return err
}
fmt.Printf("Removed entry %s.\n", ent.Name)
}
return nil
}
func (r *Remover) removeSubdir(ctx context.Context, client *github.Client, ent *entry) error {
repo, _, err := client.Repositories.Get(ctx, ent.Owner, ent.Repo)
if err != nil {
return err
}
dir, err := ioutil.TempDir("", fmt.Sprintf("%s-", packageName))
if err != nil {
return err
}
defer os.RemoveAll(dir)
for _, cmd := range [][]string{
{"clone", repo.GetCloneURL(), dir},
{"-C", dir, "rm", "-r", ent.Name[strings.LastIndex(ent.Name, "/")+1:]},
{"-C", dir, "commit", "-m", fmt.Sprintf("%s: remove entry (%s)", packageName, ent.Name)},
{"-C", dir, "push"},
} {
gitCmd := exec.Command("git", cmd...)
if r.Verbose {
gitCmd.Stdout = os.Stdout
gitCmd.Stderr = os.Stderr
}
if err := gitCmd.Run(); err != nil {
return err
}
}
return nil
}
func (r *Remover) findRepo(repo string) (int, *entry) {
for i, ent := range state.Log.Entries {
if ent.Name == repo {
return i, &ent
}
}
return -1, nil
}