forked from hwaf/hwaf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_pkg_add.go
165 lines (145 loc) · 4.05 KB
/
cmd_pkg_add.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"bufio"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/gonuts/commander"
"github.com/gonuts/flag"
"github.com/hwaf/hwaf/vcs"
//gocfg "github.com/gonuts/config"
)
func hwaf_make_cmd_pkg_add() *commander.Command {
cmd := &commander.Command{
Run: hwaf_run_cmd_pkg_add,
UsageLine: "co [options] <pkg-uri> [<local-pkg-name>]",
Short: "add a package to the current workarea",
Long: `
co adds a package to the current workarea.
ex:
$ hwaf pkg co /foo/pkg
$ hwaf pkg co Control/AthenaKernel
$ hwaf pkg co git://github.com/mana-fwk/mana-core-athenakernel
$ hwaf pkg co git://github.com/mana-fwk/mana-core-athenakernel Control/AthenaKernel
$ hwaf pkg co -b=rel/mana git://github.com/mana-fwk/mana-core-athenakernel Control/AthenaKernel
$ hwaf pkg co -b=AthenaKernel-00-00-01 svn+ssh://svn.cern.ch/reps/atlasoff/Control/AthenaKernel Control/AthenaKernel
$ hwaf pkg co -f=list.of.pkgs.txt
`,
Flag: *flag.NewFlagSet("hwaf-pkg-co", flag.ExitOnError),
}
cmd.Flag.Bool("v", false, "enable verbose output")
cmd.Flag.String("b", "", "branch to checkout (default=master)")
cmd.Flag.String("f", "", "path to a file holding a list of packages to retrieve")
return cmd
}
func hwaf_run_cmd_pkg_add(cmd *commander.Command, args []string) {
var err error
n := "hwaf-pkg-" + cmd.Name()
pkguri := ""
pkgname := ""
switch len(args) {
default:
err = fmt.Errorf("%s: expects 0, 1 or 2 arguments (got %d: %v)", n, len(args), args)
handle_err(err)
case 2:
pkguri = args[0]
pkgname = args[1]
case 1:
pkguri = args[0]
pkgname = ""
case 0:
fname := cmd.Flag.Lookup("f").Value.Get().(string)
if fname != "" {
f, err := os.Open(fname)
if err != nil {
handle_err(err)
}
pkgs := [][]string{}
scnr := bufio.NewScanner(f)
for scnr.Scan() {
line := strings.Trim(scnr.Text(), " \n")
if strings.HasPrefix(line, "#") {
continue
}
tokens := strings.Split(line, " ")
pkg := []string{}
for _, tok := range tokens {
tok = strings.Trim(tok, " \t")
if tok != "" {
pkg = append(pkg, tok)
}
}
if len(pkg) > 0 {
pkgs = append(pkgs, pkg)
}
}
err = scnr.Err()
if err != nil && err != io.EOF {
handle_err(err)
}
verbose := cmd.Flag.Lookup("v").Value.Get().(bool)
for _, pkg := range pkgs {
args := []string{"pkg", "co"}
if verbose {
args = append(args, "-v=1")
}
switch len(pkg) {
case 1:
args = append(args, pkg[0])
case 2:
args = append(args, "-b="+pkg[1], pkg[0])
case 3:
args = append(args, "-b="+pkg[1], pkg[0], pkg[2])
default:
err = fmt.Errorf("%s: invalid number of pkg-co arguments (expected [1-3], got=%d) args=%v", n, len(pkg), pkg)
handle_err(err)
}
cmd := exec.Command("hwaf", args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
err = cmd.Run()
handle_err(err)
}
return
}
err = fmt.Errorf("%s: you need to give a package URL", n)
handle_err(err)
}
verbose := cmd.Flag.Lookup("v").Value.Get().(bool)
bname := cmd.Flag.Lookup("b").Value.Get().(string)
if verbose {
fmt.Printf("%s: checkout package [%s]...\n", n, pkguri)
}
cfg, err := g_ctx.LocalCfg()
handle_err(err)
pkgdir := "src"
if cfg.HasOption("hwaf-cfg", "pkgdir") {
pkgdir, err = cfg.String("hwaf-cfg", "pkgdir")
handle_err(err)
}
//fmt.Printf(">>> helper(pkguri=%q, pkgname=%q, pkgid=%q)...\n", pkguri, pkgname, bname)
helper, err := vcs.NewHelper(pkguri, pkgname, bname, pkgdir)
handle_err(err)
defer helper.Delete()
dir := filepath.Join(pkgdir, helper.PkgName)
//fmt.Printf(">>> dir=%q\n", dir)
if g_ctx.PkgDb.HasPkg(dir) {
err = fmt.Errorf("%s: package [%s] already in db.\ndid you forget to run 'hwaf pkg rm %s' ?", n, dir, dir)
handle_err(err)
}
//fmt.Printf(">>> pkgname=%q\n", helper.PkgName)
err = helper.Checkout()
handle_err(err)
err = g_ctx.PkgDb.Add(helper.Type, helper.Repo, dir)
handle_err(err)
err = helper.Delete()
handle_err(err)
if verbose {
fmt.Printf("%s: checkout package [%s]... [ok]\n", n, pkguri)
}
}
// EOF