Mount local FS; get struct pointer so we can modify values

This commit is contained in:
yo
2022-04-02 14:18:50 +02:00
parent 349ea12979
commit eacc165481
3 changed files with 257 additions and 40 deletions

View File

@ -3,14 +3,16 @@ package cmd
import (
"os"
"fmt"
"strconv"
"strings"
"encoding/json"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
const (
version = "0.02"
gVersion = "0.022a"
)
var (
@ -24,6 +26,8 @@ var (
gSortFields string
gNoLineSep bool
gHostVersion float64
rootCmd = & cobra.Command{
Use: "gocage",
@ -33,6 +37,7 @@ 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()
},
}
@ -41,7 +46,8 @@ It support iocage jails and can coexist with iocage.`,
Short: "Print the version number of GoCage",
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\n", version)
fmt.Printf("GoCage v.%s on FreeBSD %.1f\n", gVersion, gHostVersion)
cleanAfterRun()
},
}
@ -53,6 +59,7 @@ 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()
},
}
@ -64,6 +71,7 @@ ex: gocage list srv-db srv-web`,
// Get the inventory
ListJails(args, false)
StopJail(args)
cleanAfterRun()
},
}
@ -74,6 +82,7 @@ ex: gocage list srv-db srv-web`,
// Get the inventory
ListJails(args, false)
StartJail(args)
cleanAfterRun()
},
}
)
@ -98,6 +107,15 @@ func init() {
rootCmd.AddCommand(listCmd)
rootCmd.AddCommand(stopCmd)
rootCmd.AddCommand(startCmd)
// Get FreeBSD version
out, err := executeCommand("freebsd-version")
if err != nil {
fmt.Printf("Error running \"freebsd-version\": %s", err.Error())
os.Exit(1)
}
gHostVersion, _ = strconv.ParseFloat(strings.Split(out, "-")[0], 32)
}
func initConfig() {
@ -141,6 +159,21 @@ func initConfig() {
}
}
// Called after execution
func cleanAfterRun() {
for _, j := range gJails {
if j.ConfigUpdated {
// TODO : Marshall to disk
fmt.Printf("Config for jail %s will be updated\n", j.Name)
marshaled, err := json.MarshalIndent(j.Config, "", " ")
if err != nil {
fmt.Printf("ERROR marshaling config: %s\n", err.Error())
}
fmt.Printf(string(marshaled))
}
}
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)