Skip to content

Commit

Permalink
Merge pull request #73 from sam80180/main
Browse files Browse the repository at this point in the history
fix: launch app with environment variables and arguments
  • Loading branch information
ZhouYixun committed Dec 26, 2023
2 parents 86d96be + a5affd3 commit e639c1c
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 33 deletions.
40 changes: 38 additions & 2 deletions cmd/app/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,68 @@
package app

import (
"github.com/SonicCloudOrg/sonic-ios-bridge/src/util"
"bytes"
"fmt"
"os"
"strings"

giDevice "github.com/SonicCloudOrg/sonic-gidevice"
"github.com/SonicCloudOrg/sonic-ios-bridge/src/util"
envparse "github.com/hashicorp/go-envparse"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var bKillExisting bool
var envKVs []string

var launchCmd = &cobra.Command{
Use: "launch",
Args: cobra.ArbitraryArgs,
Short: "Launch App",
Long: "Launch App",
RunE: func(cmd *cobra.Command, args []string) error {
device := util.GetDeviceByUdId(udid)
if device == nil {
os.Exit(0)
}
_, errLaunch := device.AppLaunch(bundleId)
argv := []any{}
for _, arg := range args {
argv = append(argv, arg)
}
inputEnvVars, errParseEnv := envparse.Parse(bytes.NewReader([]byte(strings.Join(envKVs, "\n"))))
if errParseEnv != nil {
logrus.Warnf("Failed to parse env vars: %+v", errParseEnv)
}
envVars := map[string]any{}
for k, v := range inputEnvVars {
envVars[k] = v
}
_, errLaunch := device.AppLaunch(bundleId, giDevice.WithArguments(argv), giDevice.WithKillExisting(bKillExisting), giDevice.WithEnvironment(envVars))
if errLaunch != nil {
return util.NewErrorPrint(util.ErrSendCommand, "launch", errLaunch)
}
return nil
},
}

func myHelpFunc(cmd *cobra.Command, args []string) {
fmt.Printf(`%s
Usage:
%s -- [arguments [arguments ...]]
Flags:
%s`, cmd.Long, cmd.UseLine(), cmd.Flags().FlagUsages())
}

func initAppLaunch() {
appRootCMD.AddCommand(launchCmd)
launchCmd.Flags().StringVarP(&udid, "udid", "u", "", "device's serialNumber")
launchCmd.Flags().StringVarP(&bundleId, "bundleId", "b", "", "target bundleId")
launchCmd.MarkFlagRequired("bundleId")
launchCmd.Flags().StringSliceVarP(&envKVs, "env", "e", []string{}, "environment variables; format: KEY=VALUE")
launchCmd.Flags().BoolVar(&bKillExisting, "kill-existing", false, "kill the application if it is already running")
launchCmd.SetHelpFunc(myHelpFunc)
launchCmd.UseLine()
}
7 changes: 4 additions & 3 deletions cmd/crash.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ package cmd

import (
"fmt"
"os"
"path/filepath"

giDevice "github.com/SonicCloudOrg/sonic-gidevice"
"github.com/SonicCloudOrg/sonic-ios-bridge/src/util"
"github.com/spf13/cobra"
"os"
"path/filepath"
)

var crashCmd = &cobra.Command{
Expand All @@ -38,7 +39,7 @@ var crashCmd = &cobra.Command{
if !filepath.IsAbs(crashOutputPath) {
var err error
if crashOutputPath, err = filepath.Abs(crashOutputPath); err != nil {
fmt.Println("path no found!")
fmt.Println("path not found!")
os.Exit(0)
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ var devicesCmd = &cobra.Command{
data := util.ResultData(device)
fmt.Println(util.Format(data, isFormat, isDetail))
} else {
fmt.Println("device no found")
fmt.Println("device not found")
os.Exit(0)
}
} else {
Expand Down
2 changes: 0 additions & 2 deletions cmd/devmode/arm.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"net/http"

"github.com/SonicCloudOrg/sonic-ios-bridge/src/util"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"golang.org/x/xerrors"
Expand All @@ -15,7 +14,6 @@ var devmodeArmCmd = &cobra.Command{
Short: "Arm the Developer Mode (device will reboot)",
Long: "Arm the Developer Mode (device will reboot)",
RunE: func(cmd *cobra.Command, args []string) error {
util.InitLogger()
if bCan, eCan := canToggleDevMode(udid); eCan != nil {
strErrMsg := fmt.Sprintf("Failed to check device %s iOS version", udid)
logrus.Warn(strErrMsg)
Expand Down
2 changes: 0 additions & 2 deletions cmd/devmode/confirm.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"net/http"

"github.com/SonicCloudOrg/sonic-ios-bridge/src/util"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"golang.org/x/xerrors"
Expand All @@ -15,7 +14,6 @@ var devmodeConfirmCmd = &cobra.Command{
Short: "Confirm enabling of Developer Mode",
Long: "Confirm enabling of Developer Mode",
RunE: func(cmd *cobra.Command, args []string) error {
util.InitLogger()
if bPreCheckIOSVer {
if bCan, eCan := canToggleDevMode(udid); eCan != nil {
strErrMsg := fmt.Sprintf("Failed to check device %s iOS version", udid)
Expand Down
1 change: 0 additions & 1 deletion cmd/devmode/enable.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ var devmodeEnableCmd = &cobra.Command{
Short: "Enable Developer Mode (device will reboot)",
Long: "Enable Developer Mode (device will reboot)",
RunE: func(cmd *cobra.Command, args []string) error {
//util.InitLogger()
errArm := devmodeArmCmd.RunE(cmd, args)
if errArm != nil {
return errArm
Expand Down
1 change: 0 additions & 1 deletion cmd/devmode/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ var devmodeListCmd = &cobra.Command{
Short: "Print the Developer Mode status of connected devices",
Long: "Print the Developer Mode status of connected devices",
RunE: func(cmd *cobra.Command, args []string) error {
util.InitLogger()
usbMuxClient, err := giDevice.NewUsbmux()
if err != nil {
return util.NewErrorPrint(util.ErrConnect, "usbMux", err)
Expand Down
2 changes: 0 additions & 2 deletions cmd/devmode/reveal.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"net/http"

"github.com/SonicCloudOrg/sonic-ios-bridge/src/util"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"golang.org/x/xerrors"
Expand All @@ -15,7 +14,6 @@ var devmodeRevealCmd = &cobra.Command{
Short: "Reveal the Developer Mode menu on the device",
Long: "Reveal the Developer Mode menu on the device",
RunE: func(cmd *cobra.Command, args []string) error {
util.InitLogger()
if bCan, eCan := canToggleDevMode(udid); eCan != nil {
strErrMsg := fmt.Sprintf("Failed to check device %s iOS version", udid)
logrus.Warn(strErrMsg)
Expand Down
26 changes: 15 additions & 11 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package cmd
import (
"os"

"github.com/SonicCloudOrg/sonic-ios-bridge/src/util"
"github.com/spf13/cobra"
)

Expand All @@ -34,21 +35,24 @@ var rootCmd = &cobra.Command{
Use: strExeccutable,
Short: "Bridge of iOS Devices",
Long: `
▄▄▄▄ ▄▄▄▄ ▄▄▄ ▄▄ ▄▄▄▄▄▄ ▄▄▄▄
▄█▀▀▀▀█ ██▀▀██ ███ ██ ▀▀██▀▀ ██▀▀▀▀█
██▄ ██ ██ ██▀█ ██ ██ ██▀
▀████▄ ██ ██ ██ ██ ██ ██ ██
▀██ ██ ██ ██ █▄██ ██ ██▄
█▄▄▄▄▄█▀ ██▄▄██ ██ ███ ▄▄██▄▄ ██▄▄▄▄█
▀▀▀▀▀ ▀▀▀▀ ▀▀ ▀▀▀ ▀▀▀▀▀▀ ▀▀▀▀
Copyright (C) 2022 SonicCloudOrg AGPLv3
https://github.com/SonicCloudOrg/sonic-ios-bridge
`,
▄▄▄▄ ▄▄▄▄ ▄▄▄ ▄▄ ▄▄▄▄▄▄ ▄▄▄▄
▄█▀▀▀▀█ ██▀▀██ ███ ██ ▀▀██▀▀ ██▀▀▀▀█
██▄ ██ ██ ██▀█ ██ ██ ██▀
▀████▄ ██ ██ ██ ██ ██ ██ ██
▀██ ██ ██ ██ █▄██ ██ ██▄
█▄▄▄▄▄█▀ ██▄▄██ ██ ███ ▄▄██▄▄ ██▄▄▄▄█
▀▀▀▀▀ ▀▀▀▀ ▀▀ ▀▀▀ ▀▀▀▀▀▀ ▀▀▀▀
Copyright (C) 2022 SonicCloudOrg AGPLv3
https://github.com/SonicCloudOrg/sonic-ios-bridge
`,
}

// Execute error
func Execute() {
rootCmd.PersistentFlags().String("log-level", "info", "Valid values: [panic, fatal, error, warn, info, debug, trace]")
rootCmd.ParseFlags(os.Args) // parse flags and set log level
util.InitLogger(rootCmd.PersistentFlags().Lookup("log-level").Value.String())
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/google/gopacket v1.1.19
github.com/google/uuid v1.3.0
github.com/gorilla/websocket v1.5.0
github.com/hashicorp/go-envparse v0.1.0
github.com/mitchellh/mapstructure v1.5.0
github.com/satori/go.uuid v1.2.0
github.com/sirupsen/logrus v1.9.3
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/SonicCloudOrg/sonic-gidevice v0.7.6 h1:zSHY1aCrMEQNtnsKsfsOUp7erxf5ocl/eoxjfvKSpNg=
github.com/SonicCloudOrg/sonic-gidevice v0.7.6/go.mod h1:SEquP5doc1Xa9Y0556SJBpFg7Pex+jXk+y4XQ4i0yxo=
github.com/SonicCloudOrg/sonic-gidevice v0.7.7 h1:oWNtc5j0ke/VmDVaXYZOR5JAQ8Cr12Fo1UrMq51lVM4=
github.com/SonicCloudOrg/sonic-gidevice v0.7.7/go.mod h1:SEquP5doc1Xa9Y0556SJBpFg7Pex+jXk+y4XQ4i0yxo=
github.com/SonicCloudOrg/sonic-ios-webkit-adapter v0.0.7 h1:s4OTcJ0VG4mg3501Ec+aSR6gQ8eTWz26fdgCUjxlmJQ=
Expand Down Expand Up @@ -43,6 +41,8 @@ github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGa
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/hashicorp/go-envparse v0.1.0 h1:bE++6bhIsNCPLvgDZkYqo3nA+/PFI51pkrHdmPSDFPY=
github.com/hashicorp/go-envparse v0.1.0/go.mod h1:OHheN1GoygLlAkTlXLXvAdnXdZxy8JUweQ1rAXx1xnc=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
Expand Down Expand Up @@ -182,5 +182,3 @@ gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C
howett.net/plist v0.0.0-20201203080718-1454fab16a06/go.mod h1:vMygbs4qMhSZSc4lCUl2OEE+rDiIIJAIdR4m7MiMcm0=
howett.net/plist v1.0.0 h1:7CrbWYbPPO/PyNy38b2EB/+gYbjCe2DXBxgtOOZbSQM=
howett.net/plist v1.0.0/go.mod h1:lqaXoTrLY4hg8tnEzNru53gicrbv7rrk+2xJA/7hw9g=
howett.net/plist v1.0.1 h1:37GdZ8tP09Q35o9ych3ehygcsL+HqKSwzctveSlarvM=
howett.net/plist v1.0.1/go.mod h1:lqaXoTrLY4hg8tnEzNru53gicrbv7rrk+2xJA/7hw9g=
7 changes: 4 additions & 3 deletions src/util/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import (
"encoding/json"
"errors"
"fmt"
giDevice "github.com/SonicCloudOrg/sonic-gidevice"
"github.com/SonicCloudOrg/sonic-ios-bridge/src/entity"
"io"
"io/ioutil"
"log"
Expand All @@ -36,6 +34,9 @@ import (
"strings"
"sync"
"time"

giDevice "github.com/SonicCloudOrg/sonic-gidevice"
"github.com/SonicCloudOrg/sonic-ios-bridge/src/entity"
)

const (
Expand Down Expand Up @@ -82,7 +83,7 @@ func GetDeviceByUdId(udId string) (device giDevice.Device) {
device = list[0]
}
if device == nil || device.Properties().SerialNumber == "" {
fmt.Println("device no found")
fmt.Println("device not found")
return nil
}
} else {
Expand Down
46 changes: 45 additions & 1 deletion src/util/log.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,60 @@
package util

import (
stdlog "log"
"os"
"regexp"
"strings"

"github.com/sirupsen/logrus"
easy "github.com/t-tomalak/logrus-easy-formatter"
)

func InitLogger() {
func InitLogger(strIntLevel string) {
logrus.SetOutput(os.Stderr)
logrus.SetFormatter(&easy.Formatter{
TimestampFormat: "2006-01-02 15:04:05",
LogFormat: "[%lvl%]: %time% - %msg%\n",
})
SetLogLevel(strIntLevel)
stdlog.SetOutput(new(LogrusWriter))
}

func SetLogLevel(strIntLevel string) {
loglevel := logrus.InfoLevel
switch strings.ToLower(strIntLevel) {
case "panic":
loglevel = logrus.PanicLevel
case "fatal":
loglevel = logrus.FatalLevel
case "error":
loglevel = logrus.ErrorLevel
case "warn":
loglevel = logrus.WarnLevel
case "info":
loglevel = logrus.InfoLevel
case "debug":
loglevel = logrus.DebugLevel
case "trace":
loglevel = logrus.TraceLevel
}
logrus.SetLevel(loglevel)
}

/********** work around to this problem **********/
// https://github.com/google/gousb/issues/87#issuecomment-1100956460
type LogrusWriter int

var reStdGoLogFormat = regexp.MustCompile(`(?s)[0-9]{4}/[0-9]{2}/[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2} (?P<msg>.+)`)

func (LogrusWriter) Write(data []byte) (int, error) {
logmessage := string(data)
if reStdGoLogFormat.MatchString(logmessage) {
logmessage = logmessage[20:]
}
if strings.HasSuffix(logmessage, "\n") {
logmessage = logmessage[:len(logmessage)-1]
}
logrus.Infof("[gousb] %s", logmessage)
return len(logmessage), nil
}

0 comments on commit e639c1c

Please sign in to comment.