v0.6.0
This commit is contained in:
@ -20,8 +20,15 @@ var (
|
||||
}
|
||||
)
|
||||
|
||||
func execute(cmd *cobra.Command, args []string) {
|
||||
func init() {
|
||||
execCmd.AddCommand(hostExecCommand)
|
||||
|
||||
hostExecCommand.Flags().StringSliceVarP(&hostsList, "hosts", "m", nil, "Accepts comma-separated names of hosts.")
|
||||
hostExecCommand.Flags().StringSliceVarP(&cmdList, "commands", "c", nil, "Accepts comma-separated names of commands.")
|
||||
|
||||
}
|
||||
|
||||
func execute(cmd *cobra.Command, args []string) {
|
||||
if len(args) < 1 {
|
||||
logging.ExitWithMSG("Please provide a command to run. Pass --help to see options.", 1, nil)
|
||||
}
|
||||
|
60
cmd/host.go
Normal file
60
cmd/host.go
Normal file
@ -0,0 +1,60 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"git.andrewnw.xyz/CyberShell/backy/pkg/backy"
|
||||
"git.andrewnw.xyz/CyberShell/backy/pkg/logging"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
hostExecCommand = &cobra.Command{
|
||||
Use: "host [--commands=command1,command2, ... | -c command1,command2, ...] [--hosts=host1,hosts2, ... | -m host1,host2, ...] ",
|
||||
Short: "Runs command defined in config file on the hosts in order specified.",
|
||||
Long: "Host executes specified commands on the hosts defined in config file.\nUse the --commands or -c flag to choose the commands.",
|
||||
Run: Host,
|
||||
}
|
||||
)
|
||||
|
||||
// Holds command list to run
|
||||
var hostsList []string
|
||||
var cmdList []string
|
||||
|
||||
func init() {
|
||||
|
||||
}
|
||||
|
||||
// cli input should be hosts and commands. Hosts are defined in config files.
|
||||
// commands can be passed by the following mutually exclusive options:
|
||||
// 1. as a list of commands defined in the config file
|
||||
// 2. stdin (on command line) (TODO)
|
||||
|
||||
func Host(cmd *cobra.Command, args []string) {
|
||||
backyConfOpts := backy.NewOpts(cfgFile)
|
||||
backyConfOpts.InitConfig()
|
||||
|
||||
backy.ReadConfig(backyConfOpts)
|
||||
|
||||
// check CLI input
|
||||
if hostsList == nil {
|
||||
logging.ExitWithMSG("error: hosts must be specified", 1, &backyConfOpts.Logger)
|
||||
}
|
||||
// host is only checked when we read the SSH File
|
||||
// so a check may not be needed here
|
||||
for _, h := range hostsList {
|
||||
_, hostFound := backyConfOpts.Hosts[h]
|
||||
if !hostFound {
|
||||
logging.ExitWithMSG("host "+h+" not found", 1, &backyConfOpts.Logger)
|
||||
}
|
||||
}
|
||||
if cmdList == nil {
|
||||
logging.ExitWithMSG("error: commands must be specified", 1, &backyConfOpts.Logger)
|
||||
}
|
||||
for _, c := range cmdList {
|
||||
_, cmdFound := backyConfOpts.Cmds[c]
|
||||
if !cmdFound {
|
||||
logging.ExitWithMSG("cmd "+c+" not found", 1, &backyConfOpts.Logger)
|
||||
}
|
||||
}
|
||||
|
||||
backyConfOpts.ExecCmdsSSH(cmdList, hostsList)
|
||||
}
|
Reference in New Issue
Block a user