Skip to content

Commit

Permalink
fix show file name only when is not the default value
Browse files Browse the repository at this point in the history
  • Loading branch information
guumaster committed Apr 8, 2020
1 parent d18dd8d commit 530ede5
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"github.com/spf13/cobra"
)

var defaultHostsFile = "/etc/hosts"

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "hostctl",
Expand All @@ -31,7 +29,8 @@ you need each time with a simple interface.
host, _ := cmd.Flags().GetString("host-file")
quiet, _ := cmd.Flags().GetBool("quiet")

if host != defaultHostsFile && !quiet {
defaultHostsFile := getDefaultHostFile()
if (host != defaultHostsFile || os.Getenv("HOSTCTL_FILE") != "") && !quiet {
fmt.Printf("Using hosts file: %s\n", host)
}

Expand All @@ -45,20 +44,22 @@ func Execute() {
}
}

func init() {
defaultHostsFile := "/etc/hosts"
func getDefaultHostFile() string {
envHostFile := os.Getenv("HOSTCTL_FILE")
if envHostFile != "" {
return envHostFile
}

if runtime.GOOS == "windows" {
defaultHostsFile = `C:/Windows/System32/Drivers/etc/hosts`
return `C:/Windows/System32/Drivers/etc/hosts`
}

envHostFile := os.Getenv("HOSTCTL_FILE")
if envHostFile != "" {
defaultHostsFile = envHostFile
}
return "/etc/hosts"
}

func init() {
rootCmd.PersistentFlags().StringP("profile", "p", "", "Choose a profile")
rootCmd.PersistentFlags().String("host-file", defaultHostsFile, "Hosts file path")
rootCmd.PersistentFlags().String("host-file", getDefaultHostFile(), "Hosts file path")
rootCmd.PersistentFlags().BoolP("quiet", "q", false, "Run command without output")
}

Expand Down

0 comments on commit 530ede5

Please sign in to comment.