@@ -3,26 +3,21 @@ package client
33import (
44 "fmt"
55 "os"
6- "regexp"
7- "strconv"
6+ "strings"
87
98 "github.com/coreos/fleet/job"
109 "github.com/coreos/fleet/unit"
1110)
1211
1312// Create schedules a new unit for the given component
1413// and blocks until the unit is loaded
15- func (c * FleetClient ) Create (component string , data bool ) (err error ) {
14+ func (c * FleetClient ) Create (target string ) (err error ) {
1615 var (
1716 unitName string
1817 unitPtr * unit.Unit
1918 )
2019 // create unit
21- if data == true {
22- unitName , unitPtr , err = c .createDataUnit (component )
23- } else {
24- unitName , unitPtr , err = c .createServiceUnit (component )
25- }
20+ unitName , unitPtr , err = c .createUnit (target )
2621 if err != nil {
2722 return err
2823 }
@@ -43,35 +38,36 @@ func (c *FleetClient) Create(component string, data bool) (err error) {
4338 return nil
4439}
4540
46- // Create normal service unit
47- func (c * FleetClient ) createServiceUnit (target string ) (unitName string , unitPtr * unit.Unit , err error ) {
48-
49- // see if we were provided a specific target
50- r := regexp .MustCompile (`([a-z-]+)\.([\d]+)` )
51- match := r .FindStringSubmatch (target )
52- var (
53- num int
54- component string
55- )
56- if len (match ) == 3 {
57- component = match [1 ]
58- num , err = strconv .Atoi (match [2 ])
59- if err != nil {
60- return "" , nil , err
61- }
62- unitName , err = formatUnitName (component , num )
41+ func (c * FleetClient ) createUnit (target string ) (unitName string , unitPtr * unit.Unit , err error ) {
42+ component , num , err := splitTarget (target )
43+ if err != nil {
44+ return
45+ }
46+ if strings .HasSuffix (component , "-data" ) {
47+ unitName , unitPtr , err = c .createDataUnit (component )
6348 } else {
64- component = target
65- num , err := c .nextUnit (component )
66- if err != nil {
67- return "" , nil , err
68- }
69- unitName , err = formatUnitName (component , num )
49+ unitName , unitPtr , err = c .createServiceUnit (component , num )
50+ }
51+ if err != nil {
52+ return unitName , unitPtr , err
53+ }
54+ return
55+ }
56+
57+ // Create normal service unit
58+ func (c * FleetClient ) createServiceUnit (component string , num int ) (unitName string , unitPtr * unit.Unit , err error ) {
59+ // if number wasn't provided get next unit number
60+ if num == 0 {
61+ num , err = c .nextUnit (component )
7062 if err != nil {
7163 return "" , nil , err
7264 }
7365 }
74-
66+ // build a fleet unit
67+ unitName , err = formatUnitName (component , num )
68+ if err != nil {
69+ return "" , nil , err
70+ }
7571 unitPtr , err = NewUnit (component )
7672 if err != nil {
7773 return
0 commit comments