2021-12-18 13:13:25 +01:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"fmt"
|
|
|
|
// "strings"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
version = "0.001"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-12-18 21:34:11 +01:00
|
|
|
gJails []Jail
|
|
|
|
|
|
|
|
gUseSudo bool
|
|
|
|
|
2021-12-18 13:13:25 +01:00
|
|
|
gConfigFile string
|
|
|
|
|
|
|
|
rootCmd = & cobra.Command{
|
|
|
|
Use: "gocage",
|
|
|
|
Short: "GoCage is a FreeBSD Jail management tool",
|
|
|
|
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")
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
versionCmd = &cobra.Command{
|
|
|
|
Use: "version",
|
|
|
|
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)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
listCmd = &cobra.Command{
|
|
|
|
Use: "list",
|
|
|
|
Short: "Print jails",
|
2021-12-18 21:34:11 +01:00
|
|
|
Long: `Display jails, their IP and OS.
|
|
|
|
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)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
stopCmd = &cobra.Command{
|
|
|
|
Use: "stop",
|
|
|
|
Short: "stop jail",
|
|
|
|
Long: `shutdown jail`,
|
2021-12-18 13:13:25 +01:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2021-12-18 21:34:11 +01:00
|
|
|
// Get the inventory
|
|
|
|
listJails(args, false)
|
|
|
|
stopJail(args)
|
2021-12-18 13:13:25 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
cobra.OnInitialize(initConfig)
|
|
|
|
|
|
|
|
// Global switches
|
2021-12-18 21:34:11 +01:00
|
|
|
rootCmd.PersistentFlags().StringVarP(&gConfigFile, "config", "c", "/usr/local/etc/gocage.conf.yml", "GoCage configuration file")
|
|
|
|
rootCmd.PersistentFlags().BoolVarP(&gUseSudo, "sudo", "s", false, "Use sudo to run commands")
|
2021-12-18 13:13:25 +01:00
|
|
|
|
|
|
|
// Command dependant switches
|
|
|
|
/* listComputerCmd.PersistentFlags().BoolVarP(&gDisplayAsCSV, "csv-format", "v", false, "Show results in CSV (separator = ';')")
|
|
|
|
searchComputerCmd.PersistentFlags().BoolVarP(&gShowAccount, "show-account", "c", false, "Show account when listing computer")
|
|
|
|
searchComputerCmd.PersistentFlags().BoolVarP(&gDisplayAsCSV, "csv-format", "v", false, "Show results in CSV (separator = ';')")
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Now declare commands
|
|
|
|
rootCmd.AddCommand(versionCmd)
|
|
|
|
rootCmd.AddCommand(listCmd)
|
2021-12-18 21:34:11 +01:00
|
|
|
rootCmd.AddCommand(stopCmd)
|
2021-12-18 13:13:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func initConfig() {
|
|
|
|
if gConfigFile == "" {
|
|
|
|
fmt.Println("No config file!")
|
2021-12-18 21:34:11 +01:00
|
|
|
os.Exit(1)
|
2021-12-18 13:13:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// fmt.Printf("We are in initConfig(), with config file %s\n", gConfigFile)
|
|
|
|
|
|
|
|
viper.SetConfigFile(gConfigFile)
|
|
|
|
|
|
|
|
if err := viper.ReadInConfig(); err != nil {
|
|
|
|
fmt.Printf("ERROR reading config file %s : %s\n", gConfigFile, err.Error())
|
2021-12-18 21:34:11 +01:00
|
|
|
os.Exit(1)
|
2021-12-18 13:13:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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"))
|
2021-12-18 21:34:11 +01:00
|
|
|
gUseSudo = viper.GetBool("sudo")
|
2021-12-18 13:13:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func Execute() {
|
|
|
|
if err := rootCmd.Execute(); err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|