Skip to content

Commit 0671f10

Browse files
committed
Also read config from RCON_ env vars
1 parent 2dfaca5 commit 0671f10

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

cli/entry.go

+3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ func Start(hostPort string, password string, in io.Reader, out io.Writer) {
4444

4545
resp, respReqId, err := remoteConsole.Read()
4646
if err != nil {
47+
if err == io.EOF {
48+
return
49+
}
4750
fmt.Fprintln(os.Stderr, "Failed to read command:", err.Error())
4851
continue
4952
}

cmd/root.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ import (
2727

2828
var (
2929
cfgFile string
30-
host string
31-
password string
32-
port int
3330
)
3431

3532
// RootCmd represents the base command when called without any subcommands
@@ -38,9 +35,8 @@ var RootCmd = &cobra.Command{
3835
Short: "A CLI for attaching to an RCON enabled game server",
3936
Long: ``,
4037
Run: func(cmd *cobra.Command, args []string) {
41-
42-
hostPort := net.JoinHostPort(host, strconv.Itoa(port))
43-
cli.Start(hostPort, password, os.Stdin, os.Stdout)
38+
hostPort := net.JoinHostPort(viper.GetString("host"), strconv.Itoa(viper.GetInt("port")))
39+
cli.Start(hostPort, viper.GetString("password"), os.Stdin, os.Stdout)
4440
},
4541
}
4642

@@ -57,9 +53,10 @@ func init() {
5753
cobra.OnInitialize(initConfig)
5854

5955
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.rcon-cli.yaml)")
60-
RootCmd.PersistentFlags().StringVar(&host, "host", "localhost", "RCON server's hostname")
61-
RootCmd.PersistentFlags().StringVar(&password, "password", "", "RCON server's password")
62-
RootCmd.PersistentFlags().IntVar(&port, "port", 27015, "Server's RCON port")
56+
RootCmd.PersistentFlags().String("host", "localhost", "RCON server's hostname")
57+
RootCmd.PersistentFlags().String("password", "", "RCON server's password")
58+
RootCmd.PersistentFlags().Int("port", 27015, "Server's RCON port")
59+
viper.BindPFlags(RootCmd.PersistentFlags())
6360
}
6461

6562
// initConfig reads in config file and ENV variables if set.
@@ -68,6 +65,9 @@ func initConfig() {
6865
viper.SetConfigFile(cfgFile)
6966
}
7067

68+
// This will allow for env vars like RCON_PORT
69+
viper.SetEnvPrefix("rcon")
70+
7171
viper.SetConfigName(".rcon-cli") // name of config file (without extension)
7272
viper.AddConfigPath("$HOME") // adding home directory as first search path
7373
viper.AutomaticEnv() // read in environment variables that match

0 commit comments

Comments
 (0)