From 52d49e70f2f891625b22f924852dab2184cee963 Mon Sep 17 00:00:00 2001 From: Andrew Woodlee Date: Sun, 1 Jan 2023 23:39:19 -0600 Subject: [PATCH] redesign started --- cmd/root.go | 2 +- pkg/backy/backy.go | 41 ++++---------------------------------- pkg/backy/ssh.go | 2 +- pkg/config/backy/config.go | 36 +-------------------------------- 4 files changed, 7 insertions(+), 74 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 37dfe72..f0a2e99 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -30,7 +30,7 @@ func Execute() error { func init() { cobra.OnInitialize(initConfig) - rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $HOME/.config/backy/config.yaml)") + rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file to read from") rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Sets verbose level") rootCmd.PersistentFlags().Bool("viper", true, "use Viper for configuration") } diff --git a/pkg/backy/backy.go b/pkg/backy/backy.go index 441a671..57c29c9 100644 --- a/pkg/backy/backy.go +++ b/pkg/backy/backy.go @@ -10,10 +10,11 @@ import ( "git.andrewnw.xyz/CyberShell/backy/pkg/logging" ) +// Host defines a host to which to connect +// If not provided, the values will be looked up in the default ssh config file type Host struct { Empty bool Host string - UseSSHAgent bool HostName string Port uint16 PrivateKeyPath string @@ -22,17 +23,11 @@ type Host struct { } type Command struct { - Empty bool Remote bool RemoteHost Host Cmd string Args []string } -type Commands struct { - Before Command - Backup Command - After Command -} // BackupConfig is a configuration struct that is used to define backups type BackupConfig struct { @@ -40,40 +35,12 @@ type BackupConfig struct { BackupType string ConfigPath string - Cmds Commands + Cmd Command } /* * Runs a backup configuration */ -func (backup BackupConfig) Run() logging.Logging { - - beforeConfig := backup.Cmds.Before - beforeOutput := beforeConfig.runCmd() - if beforeOutput.Err != nil { - return logging.Logging{ - Output: beforeOutput.Output, - Err: beforeOutput.Err, - } - } - backupConfig := backup.Cmds.Backup - backupOutput := backupConfig.runCmd() - if backupOutput.Err != nil { - return logging.Logging{ - Output: beforeOutput.Output, - Err: beforeOutput.Err, - } - } - afterConfig := backup.Cmds.After - afterOutput := afterConfig.runCmd() - if afterOutput.Err != nil { - return afterOutput - } - return logging.Logging{ - Output: afterOutput.Output, - Err: nil, - } -} func (command Command) runCmd() logging.Logging { @@ -91,7 +58,7 @@ func (command Command) runCmd() logging.Logging { remoteHost.Port = 22 remoteHost.Host = command.RemoteHost.Host - sshc, err := remoteHost.connectToSSHHost() + sshc, err := remoteHost.ConnectToSSHHost() if err != nil { panic(fmt.Errorf("ssh dial: %w", err)) } diff --git a/pkg/backy/ssh.go b/pkg/backy/ssh.go index d1c7893..d396cd4 100644 --- a/pkg/backy/ssh.go +++ b/pkg/backy/ssh.go @@ -38,7 +38,7 @@ func GetSSHConfig(host string) (SshConfig, error) { return config, nil } -func (remoteConfig *Host) connectToSSHHost() (*ssh.Client, error) { +func (remoteConfig *Host) ConnectToSSHHost() (*ssh.Client, error) { var sshc *ssh.Client var connectErr error diff --git a/pkg/config/backy/config.go b/pkg/config/backy/config.go index 7a578b1..eae9252 100644 --- a/pkg/config/backy/config.go +++ b/pkg/config/backy/config.go @@ -81,39 +81,5 @@ func CreateConfig(backup backy.BackupConfig) backy.BackupConfig { ConfigPath: backup.ConfigPath, } - if !backup.Cmds.Before.Empty { - newBackupConfig.Cmds.Before.Cmd = backup.Cmds.Before.Cmd - newBackupConfig.Cmds.After.Args = backup.Cmds.Before.Args - if backup.Cmds.Before.Remote { - newBackupConfig.Cmds.Before.RemoteHost.Host = backup.Cmds.Before.RemoteHost.Host - newBackupConfig.Cmds.Before.RemoteHost.Port = backup.Cmds.Before.RemoteHost.Port - newBackupConfig.Cmds.Before.RemoteHost.PrivateKeyPath = backup.Cmds.Before.RemoteHost.PrivateKeyPath - } else { - newBackupConfig.Cmds.Before.RemoteHost.Empty = true - } - } - if !backup.Cmds.Backup.Empty { - newBackupConfig.Cmds.Backup.Cmd = backup.Cmds.Backup.Cmd - newBackupConfig.Cmds.Backup.Args = backup.Cmds.Backup.Args - if backup.Cmds.Backup.Remote { - newBackupConfig.Cmds.Backup.RemoteHost.Host = backup.Cmds.Backup.RemoteHost.Host - newBackupConfig.Cmds.Backup.RemoteHost.Port = backup.Cmds.Backup.RemoteHost.Port - newBackupConfig.Cmds.Backup.RemoteHost.PrivateKeyPath = backup.Cmds.Backup.RemoteHost.PrivateKeyPath - } else { - newBackupConfig.Cmds.Backup.RemoteHost.Empty = true - } - } - if !backup.Cmds.After.Empty { - newBackupConfig.Cmds.After.Cmd = backup.Cmds.After.Cmd - newBackupConfig.Cmds.After.Args = backup.Cmds.After.Args - if backup.Cmds.After.Remote { - newBackupConfig.Cmds.After.RemoteHost.Host = backup.Cmds.After.RemoteHost.Host - newBackupConfig.Cmds.After.RemoteHost.Port = backup.Cmds.After.RemoteHost.Port - newBackupConfig.Cmds.After.RemoteHost.PrivateKeyPath = backup.Cmds.After.RemoteHost.PrivateKeyPath - } else { - newBackupConfig.Cmds.Before.RemoteHost.Empty = true - } - } - - return backup + return newBackupConfig }