Compare commits

...

6 commits

Author SHA1 Message Date
Augusto Dwenger J. 8f3304b56f Fix prompt indicator 2023-07-28 19:10:04 +02:00
Augusto Dwenger J. f515849caa Remove color chars on windows
We only support Unix like systems for colors and windows gets the color
information removed to reduce the output clutter.
2023-07-28 18:20:11 +02:00
Augusto Dwenger J. 2608746df0 Add note about the tested support for minecraft in readme 2023-07-28 14:52:31 +02:00
Augusto Dwenger J. 017288f674 Add license section to the readme 2023-07-28 14:48:09 +02:00
Augusto Dwenger J. 54b2977dc5 Add craftbukkit color support
Except on Windows.

Taken from b39477abe1
2023-07-28 14:30:17 +02:00
Augusto Dwenger J. a532d2ef99 Acknowledge return values from printing functions
Most of the time we don't care about the return values of a print
function, but we should at leased visualize that there is a return
value that we don't care about or want to ignore with a "_".
2023-07-28 13:51:56 +02:00
2 changed files with 68 additions and 6 deletions

View file

@ -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
View file

@ -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
}