@@ -27,9 +27,6 @@ import (
2727
2828var (
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