A runnable command
- Added backup sub-command - Added better parsing for config file - Basis for notifications, no running after a command yet - Updated docs and added License
This commit is contained in:
@ -2,6 +2,7 @@ package cmd
|
||||
|
||||
import (
|
||||
"git.andrewnw.xyz/CyberShell/backy/pkg/backy"
|
||||
"git.andrewnw.xyz/CyberShell/backy/pkg/notifications"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
@ -12,17 +13,20 @@ var (
|
||||
Short: "Runs commands defined in config file.",
|
||||
Long: `Backup executes commands defined in config file,
|
||||
use the -cmds flag to execute the specified commands.`,
|
||||
Run: Backup,
|
||||
}
|
||||
)
|
||||
var CmdList *[]string
|
||||
var CmdList []string
|
||||
|
||||
func init() {
|
||||
cobra.OnInitialize(initConfig)
|
||||
// cobra.OnInitialize(initConfig)
|
||||
|
||||
backupCmd.Flags().StringSliceVar(&CmdList, "cmds", nil, "Accepts a comma-separated list of command lists to execute.")
|
||||
|
||||
backupCmd.Flags().StringSliceVarP(CmdList, "commands", "cmds", nil, "Accepts a comma-separated list of command lists to execute.")
|
||||
}
|
||||
|
||||
func backup() {
|
||||
backyConfig := backy.NewOpts(cfgFile)
|
||||
backyConfig.GetConfig()
|
||||
func Backup(cmd *cobra.Command, args []string) {
|
||||
config := backy.ReadAndParseConfigFile(cfgFile)
|
||||
notifications.SetupNotify(*config)
|
||||
config.RunBackyConfig()
|
||||
}
|
||||
|
20
cmd/root.go
20
cmd/root.go
@ -1,9 +1,14 @@
|
||||
// root.go
|
||||
// Copyright (C) Andrew Woodlee 2023
|
||||
// License: Apache-2.0
|
||||
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
@ -17,14 +22,16 @@ var (
|
||||
rootCmd = &cobra.Command{
|
||||
Use: "backy",
|
||||
Short: "An easy-to-configure backup tool.",
|
||||
Long: `Backy is a command-line application useful
|
||||
for configuring backups, or any commands run in sequence.`,
|
||||
Long: `Backy is a command-line application useful for configuring backups, or any commands run in sequence.`,
|
||||
}
|
||||
)
|
||||
|
||||
// Execute executes the root command.
|
||||
func Execute() error {
|
||||
return rootCmd.Execute()
|
||||
func Execute() {
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
@ -33,11 +40,12 @@ func init() {
|
||||
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file to read from")
|
||||
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Sets verbose level")
|
||||
|
||||
rootCmd.AddCommand(backupCmd)
|
||||
}
|
||||
|
||||
func initConfig() {
|
||||
backyConfig := viper.New()
|
||||
if cfgFile != "" {
|
||||
if cfgFile != strings.TrimSpace("") {
|
||||
// Use config file from the flag.
|
||||
backyConfig.SetConfigFile(cfgFile)
|
||||
} else {
|
||||
@ -55,6 +63,6 @@ func initConfig() {
|
||||
backyConfig.AutomaticEnv()
|
||||
|
||||
if err := backyConfig.ReadInConfig(); err == nil {
|
||||
fmt.Println("Using config file:", backyConfig.ConfigFileUsed())
|
||||
// fmt.Println("Using config file:", backyConfig.ConfigFileUsed())
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user