github.com/drycc/workflow-cli@v1.5.3-0.20240322092846-d4ee25983af9/cmd/services.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 "regexp" 6 "strconv" 7 8 "github.com/drycc/controller-sdk-go/services" 9 ) 10 11 // ServicesList lists extra services for the app 12 func (d *DryccCmd) ServicesList(appID string) error { 13 s, appID, err := load(d.ConfigFile, appID) 14 15 if err != nil { 16 return err 17 } 18 19 services, err := services.List(s.Client, appID) 20 if d.checkAPICompatibility(s.Client, err) != nil { 21 return err 22 } 23 if len(services) > 0 { 24 table := d.getDefaultFormatTable([]string{"PTYPE", "NAME", "PORT", "PROTOCOL", "TARGET-PORT"}) 25 for _, service := range services { 26 for _, port := range service.Ports { 27 table.Append([]string{ 28 service.ProcfileType, 29 port.Name, 30 fmt.Sprint(port.Port), 31 port.Protocol, 32 fmt.Sprint(port.TargetPort), 33 }) 34 } 35 } 36 table.Render() 37 } else { 38 d.Println(fmt.Sprintf("No services found in %s app.", appID)) 39 } 40 return nil 41 } 42 43 // ServicesAdd adds a service to an app. 44 func (d *DryccCmd) ServicesAdd(appID, procfileType string, ports string, protocol string) error { 45 s, appID, err := load(d.ConfigFile, appID) 46 47 if err != nil { 48 return err 49 } 50 portArray, err := parsePorts(ports) 51 if err != nil { 52 return err 53 } 54 d.Printf("Adding %s (%d) to %s... ", procfileType, portArray[0], appID) 55 56 quit := progress(d.WOut) 57 err = services.New(s.Client, appID, procfileType, portArray[0], protocol, portArray[1]) 58 quit <- true 59 <-quit 60 if d.checkAPICompatibility(s.Client, err) != nil { 61 return err 62 } 63 64 d.Println("done") 65 return nil 66 } 67 68 // ServicesRemove removes a service for procfileType registered with an app. 69 func (d *DryccCmd) ServicesRemove(appID, procfileType string, protocol string, port int) error { 70 s, appID, err := load(d.ConfigFile, appID) 71 72 if err != nil { 73 return err 74 } 75 76 d.Printf("Removing %s from %s... ", procfileType, appID) 77 78 quit := progress(d.WOut) 79 err = services.Delete(s.Client, appID, procfileType, protocol, port) 80 quit <- true 81 <-quit 82 if d.checkAPICompatibility(s.Client, err) != nil { 83 return err 84 } 85 86 d.Println("done") 87 return nil 88 } 89 90 // parsePorts transfer ports to [2]int 91 func parsePorts(param string) ([2]int, error) { 92 var ports [2]int 93 var err error 94 regex := regexp.MustCompile(`(^[1-9]+[0-9_]+):([1-9]+[0-9_]+)$`) 95 96 if regex.MatchString(param) { 97 captures := regex.FindStringSubmatch(param) 98 ports[0], _ = strconv.Atoi(captures[1]) 99 ports[1], _ = strconv.Atoi(captures[2]) 100 } else { 101 err = fmt.Errorf("'%s' does not match the pattern 'port:targatPort', ex: 80:8000", param) 102 } 103 104 return ports, err 105 }