Skip to content

Commit 192450b

Browse files
committed
Don't use config file searching when explicitly passed
For #2
1 parent 4705ae1 commit 192450b

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

cmd/root.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ package cmd
1616

1717
import (
1818
"fmt"
19+
"log"
1920
"os"
2021

22+
"github.com/itzg/rcon-cli/cli"
2123
"github.com/spf13/cobra"
2224
"github.com/spf13/viper"
2325
"net"
24-
"github.com/itzg/rcon-cli/cli"
2526
"strconv"
2627
)
2728

@@ -38,7 +39,7 @@ rcon-cli --host mc1 --port 25575
3839
rcon-cli --port 25575 stop
3940
RCON_PORT=25575 rcon-cli stop
4041
`,
41-
Long: `
42+
Long: `
4243
rcon-cli is a CLI for attaching to an RCON enabled game server, such as Minecraft.
4344
Without any additional arguments, the CLI will start an interactive session with
4445
the RCON server.
@@ -47,7 +48,7 @@ If arguments are passed into the CLI, then the arguments are sent
4748
as a single command (joined by spaces), the response is displayed,
4849
and the CLI will exit.
4950
`,
50-
Run: func(cmd *cobra.Command, args []string) {
51+
Run: func(cmd *cobra.Command, args []string) {
5152

5253
hostPort := net.JoinHostPort(viper.GetString("host"), strconv.Itoa(viper.GetInt("port")))
5354
password := viper.GetString("password")
@@ -76,21 +77,24 @@ func init() {
7677
RootCmd.PersistentFlags().String("host", "localhost", "RCON server's hostname")
7778
RootCmd.PersistentFlags().String("password", "", "RCON server's password")
7879
RootCmd.PersistentFlags().Int("port", 27015, "Server's RCON port")
79-
viper.BindPFlags(RootCmd.PersistentFlags())
80+
err := viper.BindPFlags(RootCmd.PersistentFlags())
81+
if err != nil {
82+
log.Fatal(err)
83+
}
8084
}
8185

8286
// initConfig reads in config file and ENV variables if set.
8387
func initConfig() {
8488
if cfgFile != "" { // enable ability to specify config file via flag
8589
viper.SetConfigFile(cfgFile)
90+
} else {
91+
viper.SetConfigName(".rcon-cli") // name of config file (without extension)
92+
viper.AddConfigPath("$HOME") // adding home directory as first search path
8693
}
8794

8895
// This will allow for env vars like RCON_PORT
8996
viper.SetEnvPrefix("rcon")
90-
91-
viper.SetConfigName(".rcon-cli") // name of config file (without extension)
92-
viper.AddConfigPath("$HOME") // adding home directory as first search path
93-
viper.AutomaticEnv() // read in environment variables that match
97+
viper.AutomaticEnv() // read in environment variables that match
9498

9599
// If a config file is found, read it in.
96100
if err := viper.ReadInConfig(); err == nil {

0 commit comments

Comments
 (0)