mirror of
https://github.com/hamburghammer/rcon-cli.git
synced 2024-05-08 14:55:27 +02:00
Compare commits
6 commits
00a1fd3f0e
...
8f3304b56f
Author | SHA1 | Date | |
---|---|---|---|
Augusto Dwenger J. | 8f3304b56f | ||
Augusto Dwenger J. | f515849caa | ||
Augusto Dwenger J. | 2608746df0 | ||
Augusto Dwenger J. | 017288f674 | ||
Augusto Dwenger J. | 54b2977dc5 | ||
Augusto Dwenger J. | a532d2ef99 |
|
@ -7,6 +7,8 @@ This is a fork from [itzg/rcon-cli](https://github.com/itzg/rcon-cli) with follo
|
|||
- Remove `config.yml` support.
|
||||
- Change the `Dockerfile` to have build support.
|
||||
|
||||
Note: *This tool is primarily being maintained to support minecraft RCON implementation.*
|
||||
|
||||
## Installation
|
||||
### From Source
|
||||
Clone the repository and install it with `go install` (requires working `go` installation)
|
||||
|
@ -40,3 +42,7 @@ EXAMPLES:
|
|||
rcon-cli --password admin123 stop
|
||||
RCON_CLI_PORT=25575 rcon-cli stop
|
||||
```
|
||||
|
||||
## LICENSE
|
||||
|
||||
The software is licensed under the [Apache-2.0](LICENSE) license.
|
||||
|
|
68
main.go
68
main.go
|
@ -7,6 +7,7 @@ import (
|
|||
"io"
|
||||
"net"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/hamburghammer/rcon"
|
||||
|
@ -21,6 +22,35 @@ const (
|
|||
envVarPrefix = "RCON_CLI_"
|
||||
)
|
||||
|
||||
// resetColor is the ASCII code for resetting the color.
|
||||
const resetColor = "\u001B[0m"
|
||||
|
||||
// colors a map with the ASCII color codes for Unix terms.
|
||||
var colors = map[string]string{
|
||||
"0": "\u001B[30m", // black
|
||||
"1": "\u001B[34m", // dark blue
|
||||
"2": "\u001B[32m", // dark green
|
||||
"3": "\u001B[36m", // dark aqua
|
||||
"4": "\u001B[31m", // dark red
|
||||
"5": "\u001B[35m", // dark purple
|
||||
"6": "\u001B[33m", // gold
|
||||
"7": "\u001B[37m", // gray
|
||||
"8": "\u001B[30m", // dark gray
|
||||
"9": "\u001B[34m", // blue
|
||||
"a": "\u001B[32m", // green
|
||||
"b": "\u001B[32m", // aqua
|
||||
"c": "\u001B[31m", // red
|
||||
"d": "\u001B[35m", // light purple
|
||||
"e": "\u001B[33m", // yellow
|
||||
"f": "\u001B[37m", // white
|
||||
"k": "", // random
|
||||
"m": "\u001B[9m", // strikethrough
|
||||
"o": "\u001B[3m", // italic
|
||||
"l": "\u001B[1m", // bold
|
||||
"n": "\u001B[4m", // underline
|
||||
"r": resetColor, // reset
|
||||
}
|
||||
|
||||
// vars holding the parsed configuration
|
||||
var (
|
||||
host string
|
||||
|
@ -40,7 +70,10 @@ func main() {
|
|||
|
||||
err := run(strings.Join(commands, " "))
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err.Error())
|
||||
_, err = fmt.Fprintln(os.Stderr, err.Error())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
@ -93,14 +126,14 @@ func run(cmd string) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(resp)
|
||||
_, err = fmt.Println(resp)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func printHelp() {
|
||||
fmt.Println(`rcon-cli is a CLI to interact with a RCON server.
|
||||
_, _ = fmt.Println(`rcon-cli is a CLI to interact with a RCON server.
|
||||
It can be run in an interactive mode or to execute a single command.
|
||||
|
||||
USAGE:
|
||||
|
@ -122,9 +155,9 @@ EXAMPLES:
|
|||
|
||||
func executeInteractive(remoteConsole *rcon.RemoteConsole) error {
|
||||
scanner := bufio.NewScanner(os.Stdin)
|
||||
fmt.Println("To quit the session type 'exit'.")
|
||||
_, _ = fmt.Println("To quit the session type 'exit'.")
|
||||
_, _ = fmt.Print("> ")
|
||||
for scanner.Scan() {
|
||||
fmt.Print("> ")
|
||||
cmd := scanner.Text()
|
||||
if cmd == "exit" {
|
||||
return nil
|
||||
|
@ -134,7 +167,8 @@ func executeInteractive(remoteConsole *rcon.RemoteConsole) error {
|
|||
return err
|
||||
}
|
||||
|
||||
fmt.Println(resp)
|
||||
_, _ = fmt.Println(resp)
|
||||
_, _ = fmt.Print("> ")
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
|
@ -163,5 +197,27 @@ func execute(cmd string, remoteConsole *rcon.RemoteConsole) (string, error) {
|
|||
return resp, errors.New("the response id didn't match the request id")
|
||||
}
|
||||
|
||||
resp = colorize(resp)
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// colorize tries to add the color codes for the terminal.
|
||||
// works on none windows machines.
|
||||
func colorize(str string) string {
|
||||
const sectionSign = "§"
|
||||
|
||||
for code := range colors {
|
||||
if runtime.GOOS == "windows" {
|
||||
str = strings.ReplaceAll(str, sectionSign+code, "")
|
||||
} else {
|
||||
str = strings.ReplaceAll(str, sectionSign+code, colors[code])
|
||||
}
|
||||
}
|
||||
// reset color after each new line
|
||||
if runtime.GOOS != "windows" {
|
||||
return strings.ReplaceAll(str, "\n", "\n"+resetColor)
|
||||
}
|
||||
|
||||
return str
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue