Fix configure hosts show emptying /etc/hosts and setpersist CLI variable in configure hosts
- Only open `/etc/hosts` when write intended - Correct `SetPersist` CLI variable
This commit is contained in:
@@ -83,13 +83,6 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
|
||||
|
||||
w.Close()
|
||||
|
||||
w, err = os.OpenFile("/etc/hosts", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
|
||||
if err != nil {
|
||||
wwlog.Printf(wwlog.ERROR, "%s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer w.Close()
|
||||
|
||||
nodes, _ := n.FindAllNodes()
|
||||
|
||||
replace.AllNodes = nodes
|
||||
@@ -97,6 +90,14 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
|
||||
replace.Fqdn = controller.Fqdn
|
||||
|
||||
if SetShow == false {
|
||||
// only open "/etc/hosts" when intended to write, as 'os.O_TRUNC' will empty the file otherwise.
|
||||
w, err = os.OpenFile("/etc/hosts", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
|
||||
if err != nil {
|
||||
wwlog.Printf(wwlog.ERROR, "%s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer w.Close()
|
||||
|
||||
err = tmpl.Execute(w, replace)
|
||||
if err != nil {
|
||||
wwlog.Printf(wwlog.ERROR, "%s\n", err)
|
||||
|
||||
@@ -16,7 +16,7 @@ var (
|
||||
|
||||
func init() {
|
||||
baseCmd.PersistentFlags().BoolVarP(&SetShow, "show", "s", false, "Show configuration (don't update)")
|
||||
baseCmd.PersistentFlags().BoolVar(&SetShow, "persist", false, "Persist the configuration and initialize the service")
|
||||
baseCmd.PersistentFlags().BoolVar(&SetPersist, "persist", false, "Persist the configuration and initialize the service")
|
||||
}
|
||||
|
||||
// GetRootCommand returns the root cobra.Command for the application.
|
||||
|
||||
Reference in New Issue
Block a user