added some features
- Added `cron` command to run lists with `cron` time specifed - Changed `-c` flag to `-f` flag for passing config file - Modified some config keys - cmdArgs -> Args - Got rid of `hosts.config` - better SSH handling - respects values in config file
This commit is contained in:
@ -1,8 +1,11 @@
|
||||
// backup.go
|
||||
// Copyright (C) Andrew Woodlee 2023
|
||||
// License: Apache-2.0
|
||||
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"git.andrewnw.xyz/CyberShell/backy/pkg/backy"
|
||||
"git.andrewnw.xyz/CyberShell/backy/pkg/notification"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
@ -18,17 +21,17 @@ var (
|
||||
)
|
||||
|
||||
// Holds command list to run
|
||||
var cmdList []string
|
||||
var cmdLists []string
|
||||
|
||||
func init() {
|
||||
|
||||
backupCmd.Flags().StringSliceVarP(&cmdList, "lists", "l", nil, "Accepts a comma-separated names of command lists to execute.")
|
||||
backupCmd.Flags().StringSliceVarP(&cmdLists, "lists", "l", nil, "Accepts a comma-separated names of command lists to execute.")
|
||||
|
||||
}
|
||||
|
||||
func Backup(cmd *cobra.Command, args []string) {
|
||||
|
||||
config := backy.ReadAndParseConfigFile(cfgFile, cmdList)
|
||||
notification.SetupNotify(*config)
|
||||
config.RunBackyConfig()
|
||||
backyConfOpts := backy.NewOpts(cfgFile, backy.AddCommandLists(cmdLists))
|
||||
backyConfOpts.InitConfig()
|
||||
config := backy.ReadConfig(backyConfOpts)
|
||||
config.RunBackyConfig("")
|
||||
}
|
||||
|
24
cmd/cron.go
Normal file
24
cmd/cron.go
Normal file
@ -0,0 +1,24 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"git.andrewnw.xyz/CyberShell/backy/pkg/backy"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
cronCmd = &cobra.Command{
|
||||
Use: "cron command ...",
|
||||
Short: "Runs commands defined in config file.",
|
||||
Long: `Cron executes commands at the time defined in config file.`,
|
||||
Run: cron,
|
||||
}
|
||||
)
|
||||
|
||||
func cron(cmd *cobra.Command, args []string) {
|
||||
|
||||
opts := backy.NewOpts(cfgFile, backy.UseCron())
|
||||
opts.InitConfig()
|
||||
|
||||
backy.ReadConfig(opts).Cron()
|
||||
}
|
13
cmd/exec.go
13
cmd/exec.go
@ -1,3 +1,7 @@
|
||||
// exec.go
|
||||
// Copyright (C) Andrew Woodlee 2023
|
||||
// License: Apache-2.0
|
||||
|
||||
package cmd
|
||||
|
||||
import (
|
||||
@ -9,7 +13,7 @@ import (
|
||||
|
||||
var (
|
||||
execCmd = &cobra.Command{
|
||||
Use: "exec command1 command2",
|
||||
Use: "exec command ...",
|
||||
Short: "Runs commands defined in config file.",
|
||||
Long: `Exec executes commands defined in config file.`,
|
||||
Run: execute,
|
||||
@ -23,9 +27,8 @@ func execute(cmd *cobra.Command, args []string) {
|
||||
}
|
||||
|
||||
opts := backy.NewOpts(cfgFile, backy.AddCommands(args))
|
||||
|
||||
commands := opts.GetCmdsInConfigFile()
|
||||
|
||||
commands.ExecuteCmds()
|
||||
opts.InitConfig()
|
||||
// opts.InitMongo()
|
||||
backy.ReadConfig(opts).ExecuteCmds()
|
||||
|
||||
}
|
||||
|
33
cmd/root.go
33
cmd/root.go
@ -7,11 +7,8 @@ package cmd
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -35,35 +32,9 @@ func Execute() {
|
||||
}
|
||||
|
||||
func init() {
|
||||
cobra.OnInitialize(initConfig)
|
||||
|
||||
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file to read from")
|
||||
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "f", "", "config file to read from")
|
||||
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Sets verbose level")
|
||||
|
||||
rootCmd.AddCommand(backupCmd)
|
||||
rootCmd.AddCommand(execCmd)
|
||||
}
|
||||
|
||||
func initConfig() {
|
||||
backyConfig := viper.New()
|
||||
if cfgFile != strings.TrimSpace("") {
|
||||
// Use config file from the flag.
|
||||
backyConfig.SetConfigFile(cfgFile)
|
||||
} else {
|
||||
// Find home directory.
|
||||
home, err := os.UserHomeDir()
|
||||
cobra.CheckErr(err)
|
||||
|
||||
configPath := path.Join(home, ".config", "backy")
|
||||
// Search config in config directory with name "backy" (without extension).
|
||||
backyConfig.AddConfigPath(configPath)
|
||||
backyConfig.SetConfigType("yaml")
|
||||
backyConfig.SetConfigName("backy")
|
||||
}
|
||||
|
||||
backyConfig.AutomaticEnv()
|
||||
|
||||
if err := backyConfig.ReadInConfig(); err == nil {
|
||||
// fmt.Println("Using config file:", backyConfig.ConfigFileUsed())
|
||||
}
|
||||
rootCmd.AddCommand(backupCmd, execCmd, cronCmd)
|
||||
}
|
||||
|
Reference in New Issue
Block a user