Created
March 22, 2018 08:46
-
-
Save coding-yogi/272bdadae2f383029a4dc751431d5d68 to your computer and use it in GitHub Desktop.
go code to create node config for every android device dynamically
This file contains hidden or 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
package main | |
import ( | |
"encoding/json" | |
"io/ioutil" | |
"log" | |
"os" | |
"os/exec" | |
"strings" | |
) | |
type NodeConfig struct { | |
Capabilities []Capability | |
Configuration Configuration | |
} | |
type Capability struct { | |
ApplicationName string | |
DeviceName string | |
BrowserName string | |
PlatformName string | |
MaxInstances int32 | |
} | |
type Configuration struct { | |
CleanUpCycle int32 | |
Timeout int32 | |
Proxy string | |
Host string | |
Port int32 | |
Bootstrapport int32 | |
MaxSession int32 | |
Register bool | |
RegisterCycle int32 | |
HubPort int32 | |
HubHost string | |
Udid string | |
} | |
func main() { | |
devices := getConnectedDevices() | |
for _, device := range devices { | |
log.Println("Creating config file for device ", device) | |
writeConfigToFile(device) | |
} | |
} | |
func getConnectedDevices() []string { | |
path, err := exec.LookPath("adb") | |
if err != nil { | |
log.Fatal(err) | |
} | |
cmd := exec.Command(path, "devices") | |
out, err := cmd.Output() | |
if err != nil { | |
log.Fatal(err) | |
} | |
var devices []string | |
op := strings.Split(string(out), "\n") | |
for i := 1; i < len(op)-2; i++ { | |
if !strings.Contains(op[i], "device") { | |
continue | |
} | |
deviceDetails := strings.Split(op[i], "device") | |
devices = append(devices, strings.TrimSpace(deviceDetails[0])) | |
} | |
return devices | |
} | |
func getTemplateConfig() NodeConfig { | |
file, err := os.Open("template_config.json") | |
var template NodeConfig | |
data, err := ioutil.ReadAll(file) | |
if err != nil { | |
log.Fatal(err) | |
} | |
err = json.Unmarshal(data, &template) | |
if err != nil { | |
log.Fatal(err) | |
} | |
return template | |
} | |
func writeConfigToFile(deviceName string) { | |
file, err := os.Create(deviceName + "_config.json") | |
if err != nil { | |
log.Fatal(err) | |
} | |
nodeConfig := getTemplateConfig() | |
nodeConfig.Capabilities[0].DeviceName = deviceName | |
nodeConfig.Configuration.Udid = deviceName | |
nodeConfigData, err := json.Marshal(nodeConfig) | |
if err != nil { | |
log.Fatal(err) | |
} | |
file.Write(nodeConfigData) | |
if err != nil { | |
log.Fatal(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment