Skip to content

Commit

Permalink
Add CLI cmdline args + auth support
Browse files Browse the repository at this point in the history
  • Loading branch information
AlinaNova21 committed Dec 7, 2020
1 parent e72a150 commit 3daad4e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 11 deletions.
11 changes: 9 additions & 2 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@ type ScreepsCLI struct {
WelcomeText string
}

func NewScreepsCLI(host string, port int16) *ScreepsCLI {
func NewScreepsCLI(host string, port int16, username string, password string) *ScreepsCLI {
client := resty.New()
client.SetHostURL(fmt.Sprintf("http://%s:%d", host, port))
protocol := "http"
if port == 443 {
protocol = "https"
}
client.SetHostURL(fmt.Sprintf("%s://%s:%d", protocol, host, port))
if username != "" && password != "" {
client.SetBasicAuth(username, password)
}
s := &ScreepsCLI{
client: client,
}
Expand Down
30 changes: 26 additions & 4 deletions launcher/cli.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package launcher

import (
"flag"
"fmt"
"log"
"os"
Expand All @@ -11,18 +12,39 @@ import (
"github.com/screepers/screeps-launcher/v1/cli"
)

var (
hostFlag = flag.String("host", "", "CLI Host")
portFlag = flag.Int("port", 0, "CLI Port")
userFlag = flag.String("username", "", "CLI Username")
passFlag = flag.String("password", "", "CLI Password")
)

func runCli(config *Config) error {
host := "localhost"
port := 21026
host := config.Cli.Host
port := config.Cli.Port
user := config.Cli.Username
pass := config.Cli.Password
if v, ok := config.Env.Backend["CLI_HOST"]; ok {
host = v
}
if v, ok := config.Env.Backend["CLI_PORT"]; ok {
if i, err := strconv.Atoi(v); err == nil {
port = i
port = int16(i)
}
}
c := cli.NewScreepsCLI(host, int16(port))
if *hostFlag != "" {
host = *hostFlag
}
if *portFlag != 0 {
port = int16(*portFlag)
}
if *userFlag != "" {
user = *userFlag
}
if *passFlag != "" {
pass = *passFlag
}
c := cli.NewScreepsCLI(host, port, user, pass)

if err := c.Start(); err != nil {
log.Fatalf("Error! %v", err)
Expand Down
25 changes: 20 additions & 5 deletions launcher/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,18 @@ type ConfigBackup struct {
Files []string `yaml:"files" json:"files"`
}

// ConfigCli CLI section of config
type ConfigCli struct {
Username string `yaml:"username" json:"username"`
Password string `yaml:"password" json:"password"`
Host string `yaml:"host" json:"host"`
Port int16 `yaml:"port" json:"port"`
}

// Config server config structure
type Config struct {
SteamKey string `yaml:"steamKey" json:"steamKey"`
Cli *ConfigCli `yaml:"cli" json:"cli"`
Env *ConfigEnv `yaml:"env" json:"env"`
Processors int `yaml:"processors" json:"processors"`
RunnerThreads int `yaml:"runnerThreads" json:"runnerThreads"`
Expand All @@ -38,7 +47,7 @@ type Config struct {
ExtraPackages map[string]string `yaml:"extraPackages" json:"extraPackages"`
LocalMods string `yaml:"localMods" json:"localMods"`
Backup *ConfigBackup `yaml:"backup" json:"backup"`
Modules map[string]bool `yaml:"modules" json:"modules"`
Modules map[string]bool `yaml:"modules" json:"modules"`
}

// NewConfig Create a new Config
Expand All @@ -50,6 +59,12 @@ func NewConfig() *Config {
RunnerThreads: int(runners),
Version: "latest",
NodeVersion: "Dubnium",
Cli: &ConfigCli{
Username: "",
Password: "",
Host: "127.0.0.1",
Port: 21026,
},
Env: &ConfigEnv{
Shared: map[string]string{
"MODFILE": "mods.json",
Expand Down Expand Up @@ -79,11 +94,11 @@ func NewConfig() *Config {
Files: make([]string, 0),
},
Modules: map[string]bool{
"backend": true,
"main": true,
"backend": true,
"main": true,
"processor": true,
"runner": true,
"storage": true,
"runner": true,
"storage": true,
},
}
}
Expand Down

0 comments on commit 3daad4e

Please sign in to comment.