From 3eced644537c1545c52eeb36d6ded7ad3337f745 Mon Sep 17 00:00:00 2001 From: Andrew Woodlee Date: Tue, 15 Jul 2025 20:24:30 -0500 Subject: [PATCH] CLI: Exec subcommand `host` --- cmd/hosts.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 cmd/hosts.go diff --git a/cmd/hosts.go b/cmd/hosts.go new file mode 100644 index 0000000..9f94765 --- /dev/null +++ b/cmd/hosts.go @@ -0,0 +1,55 @@ +package cmd + +import ( + "git.andrewnw.xyz/CyberShell/backy/pkg/backy" + "git.andrewnw.xyz/CyberShell/backy/pkg/logging" + "github.com/spf13/cobra" +) + +var ( + hostsExecCommand = &cobra.Command{ + Use: "hosts [--command=command1 --command=command2 ... | -c command1 -c command2 ...]", + Short: "Runs command defined in config file on the hosts in order specified.", + Long: "Hosts executes specified commands on all the hosts defined in config file.\nUse the --commands or -c flag to choose the commands.", + Run: Hosts, + } +) + +func init() { + + hostsExecCommand.Flags().StringArrayVarP(&cmdList, "command", "c", nil, "Accepts space-separated names of commands. Specify multiple times for multiple commands.") + parseS3Config() + +} + +// 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 Hosts(cmd *cobra.Command, args []string) { + backyConfOpts := backy.NewConfigOptions(configFile, + backy.SetLogFile(logFile), + backy.EnableCommandStdOut(cmdStdOut), + backy.SetHostsConfigFile(hostsConfigFile)) + backyConfOpts.InitConfig() + + backyConfOpts.ParseConfigurationFile() + + for _, h := range backyConfOpts.Hosts { + + hostsList = append(hostsList, h.Host) + } + + 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.ExecCmdsOnHosts(cmdList, hostsList) +}