forked from lhcb-org/lbpkr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_remove.go
89 lines (78 loc) · 2.02 KB
/
cmd_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
84
85
86
87
88
89
package main
import (
"fmt"
"regexp"
"strings"
"github.com/gonuts/commander"
"github.com/gonuts/flag"
)
func lbpkr_make_cmd_remove() *commander.Command {
cmd := &commander.Command{
Run: lbpkr_run_cmd_remove,
UsageLine: "remove [options] <rpmname> [<rpmname> [<rpmname>]]",
Short: "remove a RPM from the yum repository",
Long: `
remove removes a RPM from the yum repository.
ex:
$ lbpkr remove gcc_4.8.1_x86_64_slc6-1.0.0-1
$ lbpkr remove gcc_4.8.1_x86_64_slc6-1.0.0-1 xrootd-3a806_3.2.7_x86_64_slc6_gcc48_opt-1.0.0-4
`,
Flag: *flag.NewFlagSet("lbpkr-remove", flag.ExitOnError),
}
add_default_options(cmd)
cmd.Flag.Bool("force", false, "force removal of RPM")
return cmd
}
func lbpkr_run_cmd_remove(cmd *commander.Command, args []string) error {
var err error
siteroot := cmd.Flag.Lookup("siteroot").Value.Get().(string)
cfgtype := cmd.Flag.Lookup("type").Value.Get().(string)
debug := cmd.Flag.Lookup("v").Value.Get().(bool)
force := cmd.Flag.Lookup("force").Value.Get().(bool)
rpms := make([][3]string, 0)
switch len(args) {
case 0:
cmd.Usage()
return fmt.Errorf("lbpkr: invalid number of arguments (got=%d)", len(args))
default:
re := regexp.MustCompile(`(.*)-([\d\.]+)-(\d)$`)
for _, name := range args {
rpmname := name
version := ""
release := ""
match := re.FindAllStringSubmatch(rpmname, -1)
if len(match) == 1 {
m := match[0]
switch len(m) {
case 2:
rpmname = m[1]
case 3:
rpmname = m[1]
version = m[2]
case 4:
rpmname = m[1]
version = m[2]
release = m[3]
}
}
rpms = append(rpms, [3]string{rpmname, version, release})
}
}
cfg := NewConfig(cfgtype, siteroot)
ctx, err := New(cfg, debug)
if err != nil {
return err
}
defer ctx.Close()
str := []string{}
for _, s := range rpms {
str = append(str, fmt.Sprintf("%s %s %s", s[0], s[1], s[2]))
}
plural := ""
if len(rpms) > 1 {
plural = "s"
}
ctx.msg.Infof("removing RPM%s:\n%v\n", plural, strings.Join(str, "\n"))
err = ctx.RemoveRPM(rpms, force)
return err
}