@@ -27,9 +27,6 @@ import (
27
27
28
28
var (
29
29
cfgFile string
30
- host string
31
- password string
32
- port int
33
30
)
34
31
35
32
// RootCmd represents the base command when called without any subcommands
@@ -38,9 +35,8 @@ var RootCmd = &cobra.Command{
38
35
Short : "A CLI for attaching to an RCON enabled game server" ,
39
36
Long : `` ,
40
37
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 )
44
40
},
45
41
}
46
42
@@ -57,9 +53,10 @@ func init() {
57
53
cobra .OnInitialize (initConfig )
58
54
59
55
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 ())
63
60
}
64
61
65
62
// initConfig reads in config file and ENV variables if set.
@@ -68,6 +65,9 @@ func initConfig() {
68
65
viper .SetConfigFile (cfgFile )
69
66
}
70
67
68
+ // This will allow for env vars like RCON_PORT
69
+ viper .SetEnvPrefix ("rcon" )
70
+
71
71
viper .SetConfigName (".rcon-cli" ) // name of config file (without extension)
72
72
viper .AddConfigPath ("$HOME" ) // adding home directory as first search path
73
73
viper .AutomaticEnv () // read in environment variables that match
0 commit comments