From 8cabae71340d8e2ba089829ec275d14bbed7ef4b Mon Sep 17 00:00:00 2001 From: yo Date: Sun, 22 Sep 2024 15:10:53 +0200 Subject: [PATCH] Add init subcommand --- cmd/root.go | 53 +++++++++++++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 2605bec..78a9411 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -14,7 +14,7 @@ import ( ) const ( - gVersion = "0.41" + gVersion = "0.42b" // TODO : Get from $jail_zpool/defaults.json MIN_DYN_DEVFS_RULESET = 1000 @@ -56,6 +56,9 @@ var ( gTimeZone string gSnapshotName string + gZPool string + gBridge string + gInterface string gMigrateDestDatastore string gYesToAll bool @@ -96,20 +99,14 @@ It support iocage jails and can coexist with iocage.`, fmt.Printf("GoCage v.%s on FreeBSD %d.%d-%s\n", gVersion, fv.major, fv.minor, fv.flavor) }, } - - /* TODO - Initialize datastore(s) /iocage, /iocage/jails - Put defaults.json, update it with hostid,interfaces, and maybe other necessary fields - Initialize bridge + initCmd = &cobra.Command{ Use: "init", Short: "Initialize GoCage", - //Long: `Let this show you how much fail I had to get this *cough* perfect`, Run: func(cmd *cobra.Command, args []string) { - fv, _ := getFreeBSDVersion() - fmt.Printf("GoCage v.%s on FreeBSD %d.%d-%s\n", gVersion, fv.major, fv.minor, fv.flavor) + InitGoCage(args) }, - }*/ + } listCmd = &cobra.Command{ Use: "list", @@ -379,6 +376,10 @@ func init() { rootCmd.PersistentFlags().BoolVar(&gDebug, "debug", false, "Debug mode") // Command dependant switches + initCmd.Flags().StringVarP(&gZPool, "pool", "p", "", "ZFS pool to create datastore on") + initCmd.Flags().StringVarP(&gBridge, "bridge", "b", "", "bridge to create for jails networking") + initCmd.Flags().StringVarP(&gInterface, "interface", "i", "", "interface to add as bridge member. This should be your main interface") + initCmd.MarkFlagsRequiredTogether("bridge", "interface") // We reuse these flags in "gocage snapshot list myjail" and 'gocage datastore list" commands listCmd.Flags().StringVarP(&gDisplayJColumns, "outcol", "o", "JID,Name,Config.Release,Config.Ip4_addr,Running", "Show these columns in output") @@ -424,6 +425,7 @@ func init() { createCmd.Flags().StringVarP(&gCreateArgs.Datastore, "datastore", "d", "", "Datastore to create the jail on. Defaults to first declared in config.") // Now declare commands + rootCmd.AddCommand(initCmd) rootCmd.AddCommand(versionCmd) rootCmd.AddCommand(listCmd) listCmd.AddCommand(listPropsCmd) @@ -441,7 +443,7 @@ func init() { rootCmd.AddCommand(updateCmd) rootCmd.AddCommand(upgradeCmd) rootCmd.AddCommand(createCmd) - + rootCmd.AddCommand(apiCmd) rootCmd.AddCommand(testCmd) snapshotCmd.AddCommand(snapshotListCmd) @@ -471,17 +473,6 @@ func initConfig() { fmt.Printf("ERROR reading config file %s : %s\n", gConfigFile, err.Error()) os.Exit(1) } - - // Load default configs from datastores - err := ListDatastores(viper.GetStringSlice("datastore"), false) - if err != nil { - fmt.Printf("ERROR: error checking datastores: %v\n", err) - os.Exit(1) - } - - // fmt.Println("Using config file:", viper.ConfigFileUsed()) - // fmt.Printf("datastore in config : %s\n", viper.GetStringSlice("datastore")) - // fmt.Printf("datastore.0 in config : %s\n", viper.GetStringSlice("datastore.0")) // Command line flags have priority on config file if rootCmd.Flags().Lookup("sudo") != nil && false == rootCmd.Flags().Lookup("sudo").Changed { @@ -515,12 +506,26 @@ func initConfig() { fmt.Printf("More than 3 sort criteria is not supported!\n") os.Exit(1) } - - + if gDebug { log.SetLevel(log.DebugLevel) log.Debugf("Debug mode enabled\n") } + + // no need to check prerequesites if we are initializing gocage + for _, rc := range rootCmd.Commands() { + //fmt.Printf("DEBUG: rootCmd subcommand: %v. Was it called? %s\n", rc.Use, rootCmd.Commands()[i].CalledAs()) + if len(rc.CalledAs()) > 0 && strings.EqualFold("init", rc.CalledAs()) { + return + } + } + + // Load default configs from datastores + err := ListDatastores(viper.GetStringSlice("datastore"), false) + if err != nil { + fmt.Printf("ERROR: error checking datastores: %v\n", err) + os.Exit(1) + } }