-
-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add launch and kill endpoints (#191)
- Loading branch information
Showing
3 changed files
with
127 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,140 @@ | ||
package api | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/danielpaulus/go-ios/ios" | ||
"github.com/danielpaulus/go-ios/ios/installationproxy" | ||
"github.com/danielpaulus/go-ios/ios/instruments" | ||
"github.com/gin-gonic/gin" | ||
"net/http" | ||
) | ||
|
||
// List apps on a device | ||
// @Summary List apps on a device | ||
// @Description List the installed apps on a device | ||
// @Tags apps | ||
// @Produce json | ||
// @Success 200 {object} []installationproxy.AppInfo | ||
// @Failure 500 {object} GenericResponse | ||
// @Router /device/{udid}/apps [post] | ||
func ListApps(c *gin.Context) { | ||
device := c.MustGet(IOS_KEY).(ios.DeviceEntry) | ||
svc, _ := installationproxy.New(device) | ||
var err error | ||
var response []installationproxy.AppInfo | ||
response, err = svc.BrowseAllApps() | ||
if err != nil { | ||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err}) | ||
c.JSON(http.StatusInternalServerError, GenericResponse{Error: err.Error()}) | ||
} | ||
c.IndentedJSON(http.StatusOK, response) | ||
} | ||
|
||
// Launch app on a device | ||
// @Summary Launch app on a device | ||
// @Description Launch app on a device by provided bundleID | ||
// @Tags apps | ||
// @Produce json | ||
// @Param bundleID query string true "bundle identifier of the targeted app" | ||
// @Success 200 {object} GenericResponse | ||
// @Failure 500 {object} GenericResponse | ||
// @Router /device/{udid}/apps/launch [post] | ||
func LaunchApp(c *gin.Context) { | ||
device := c.MustGet(IOS_KEY).(ios.DeviceEntry) | ||
|
||
bundleID := c.Query("bundleID") | ||
if bundleID == "" { | ||
c.JSON(http.StatusUnprocessableEntity, GenericResponse{Error: "bundleID query param is missing"}) | ||
return | ||
} | ||
|
||
pControl, err := instruments.NewProcessControl(device) | ||
if err != nil { | ||
c.JSON(http.StatusInternalServerError, GenericResponse{Error: err.Error()}) | ||
return | ||
} | ||
|
||
_, err = pControl.LaunchApp(bundleID) | ||
if err != nil { | ||
c.JSON(http.StatusInternalServerError, GenericResponse{Error: err.Error()}) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, GenericResponse{Message: bundleID + " launched successfully"}) | ||
} | ||
|
||
// Kill running app on a device | ||
// @Summary Kill running app on a device | ||
// @Description Kill running app on a device by provided bundleID | ||
// @Tags apps | ||
// @Produce json | ||
// @Param bundleID query string true "bundle identifier of the targeted app" | ||
// @Success 200 {object} GenericResponse | ||
// @Failure 500 {object} GenericResponse | ||
// @Router /device/{udid}/apps/kill [post] | ||
func KillApp(c *gin.Context) { | ||
device := c.MustGet(IOS_KEY).(ios.DeviceEntry) | ||
var processName = "" | ||
|
||
bundleID := c.Query("bundleID") | ||
if bundleID == "" { | ||
c.JSON(http.StatusUnprocessableEntity, GenericResponse{Error: "bundleID query param is missing"}) | ||
return | ||
} | ||
|
||
pControl, err := instruments.NewProcessControl(device) | ||
if err != nil { | ||
c.JSON(http.StatusInternalServerError, GenericResponse{Error: err.Error()}) | ||
return | ||
} | ||
|
||
svc, err := installationproxy.New(device) | ||
if err != nil { | ||
c.JSON(http.StatusInternalServerError, GenericResponse{Error: err.Error()}) | ||
return | ||
} | ||
|
||
response, err := svc.BrowseAllApps() | ||
if err != nil { | ||
c.JSON(http.StatusInternalServerError, GenericResponse{Error: err.Error()}) | ||
return | ||
} | ||
|
||
for _, app := range response { | ||
if app.CFBundleIdentifier == bundleID { | ||
processName = app.CFBundleExecutable | ||
break | ||
} | ||
} | ||
|
||
if processName == "" { | ||
c.JSON(http.StatusNotFound, GenericResponse{Message: bundleID + " is not installed"}) | ||
return | ||
} | ||
|
||
service, err := instruments.NewDeviceInfoService(device) | ||
if err != nil { | ||
c.JSON(http.StatusInternalServerError, GenericResponse{Error: err.Error()}) | ||
return | ||
} | ||
defer service.Close() | ||
|
||
processList, err := service.ProcessList() | ||
if err != nil { | ||
c.JSON(http.StatusInternalServerError, GenericResponse{Error: err.Error()}) | ||
return | ||
} | ||
|
||
for _, p := range processList { | ||
if p.Name == processName { | ||
err = pControl.KillProcess(p.Pid) | ||
if err != nil { | ||
c.JSON(http.StatusInternalServerError, GenericResponse{Error: err.Error()}) | ||
return | ||
} | ||
c.JSON(http.StatusOK, GenericResponse{Message: bundleID + " successfully killed"}) | ||
return | ||
} | ||
} | ||
|
||
c.JSON(http.StatusOK, GenericResponse{Message: bundleID + " is not running"}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters