cleanAfterRun renamed to WriteConfigToDisk, dont overwrite "auto" values by default

This commit is contained in:
yo 2022-04-18 13:50:20 +02:00
parent 6821b14407
commit 77a2e9dabf

View File

@ -38,8 +38,8 @@ var (
Long: `GoCage is a jail management tool. It support VNET, host-only, NAT networks. Provides snapshots and cloning.
It support iocage jails and can coexist with iocage.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Here we are in the Run")
cleanAfterRun()
fmt.Printf("GoCage v.%s on FreeBSD %.1f\n", gVersion, gHostVersion)
fmt.Printf("Use -h flag to display help\n")
},
}
@ -49,7 +49,6 @@ It support iocage jails and can coexist with iocage.`,
Long: `Let this show you how much fail I had to get this *cough* perfect`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("GoCage v.%s on FreeBSD %.1f\n", gVersion, gHostVersion)
cleanAfterRun()
},
}
@ -61,7 +60,6 @@ Jail list can be restricted by adding name on command line
ex: gocage list srv-db srv-web`,
Run: func(cmd *cobra.Command, args []string) {
ListJails(args, true)
cleanAfterRun()
},
}
@ -71,7 +69,6 @@ ex: gocage list srv-db srv-web`,
Long: "Display jails properties. You can use properties to filter, get or set them.",
Run: func(cmd *cobra.Command, args []string) {
ListJailsProps(args)
cleanAfterRun()
},
}
@ -83,7 +80,6 @@ ex: gocage list srv-db srv-web`,
// Load inventory
ListJails(args, false)
StopJail(args)
cleanAfterRun()
},
}
@ -94,7 +90,7 @@ ex: gocage list srv-db srv-web`,
// Load inventory
ListJails(args, false)
StartJail(args)
cleanAfterRun()
WriteConfigToDisk(false)
},
}
@ -117,7 +113,7 @@ Multiples properties can be specified, separated with space (Ex: gocage set allo
// Load inventory
ListJails(args, false)
SetJailProperties(args)
cleanAfterRun()
WriteConfigToDisk(true)
},
}
@ -131,7 +127,6 @@ For all properties specify "all" (Ex: gocage get all myjail)`,
// Load inventory
ListJails(args, false)
GetJailProperties(args)
cleanAfterRun()
},
}
@ -292,16 +287,53 @@ func initConfig() {
gSortFields = viper.GetString("sort")
}
if len(strings.Split(gSortFields, ",")) > 3 {
fmt.Printf("More than 3 sort criteria, this is not supported!\n")
fmt.Printf("More than 3 sort criteria is not supported!\n")
os.Exit(1)
}
}
// Called after execution
func cleanAfterRun() {
/********************************************************************************
* Write jails config which been updated to disk.
* If changeauto not set, values which are in "auto" mode on disk
* won't be overwritten (p.ex defaultrouter wont be overwritten with current
* default route, so if route change on jailhost this will reflect on jail next
* start)
*******************************************************************************/
func WriteConfigToDisk(changeauto bool) {
for _, j := range gJails {
if j.ConfigUpdated {
marshaled, err := json.MarshalIndent(j.Config, "", " ")
// we will manipulate properties so get a copy
jc := j.Config
if changeauto == false {
// Overwrite "auto" properties
ondiskjc, err := getJailConfig(j.ConfigPath)
if err != nil {
panic(err)
}
// TODO : List all fields, then call getStructFieldValue to compare value with "auto"
// If "auto" then keep it that way before writing ondiskjc to disk
var properties []string
properties = getStructFieldNames(ondiskjc, properties, "")
for _, p := range properties {
v, _, err := getStructFieldValue(ondiskjc, p)
if err != nil {
panic(err)
}
if v.String() == "auto" {
err = setStructFieldValue(&jc, p, "auto")
if err != nil {
fmt.Printf("ERROR sanitizing config: %s\n", err.Error())
os.Exit(1)
}
}
}
}
marshaled, err := json.MarshalIndent(jc, "", " ")
if err != nil {
fmt.Printf("ERROR marshaling config: %s\n", err.Error())
}