redesign started

This commit is contained in:
Andrew Woodlee 2023-01-01 23:39:19 -06:00
parent 2daae5cf9e
commit 52d49e70f2
4 changed files with 7 additions and 74 deletions

View File

@ -30,7 +30,7 @@ func Execute() error {
func init() { func init() {
cobra.OnInitialize(initConfig) 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().BoolVarP(&verbose, "verbose", "v", false, "Sets verbose level")
rootCmd.PersistentFlags().Bool("viper", true, "use Viper for configuration") rootCmd.PersistentFlags().Bool("viper", true, "use Viper for configuration")
} }

View File

@ -10,10 +10,11 @@ import (
"git.andrewnw.xyz/CyberShell/backy/pkg/logging" "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 { type Host struct {
Empty bool Empty bool
Host string Host string
UseSSHAgent bool
HostName string HostName string
Port uint16 Port uint16
PrivateKeyPath string PrivateKeyPath string
@ -22,17 +23,11 @@ type Host struct {
} }
type Command struct { type Command struct {
Empty bool
Remote bool Remote bool
RemoteHost Host RemoteHost Host
Cmd string Cmd string
Args []string Args []string
} }
type Commands struct {
Before Command
Backup Command
After Command
}
// BackupConfig is a configuration struct that is used to define backups // BackupConfig is a configuration struct that is used to define backups
type BackupConfig struct { type BackupConfig struct {
@ -40,40 +35,12 @@ type BackupConfig struct {
BackupType string BackupType string
ConfigPath string ConfigPath string
Cmds Commands Cmd Command
} }
/* /*
* Runs a backup configuration * 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 { func (command Command) runCmd() logging.Logging {
@ -91,7 +58,7 @@ func (command Command) runCmd() logging.Logging {
remoteHost.Port = 22 remoteHost.Port = 22
remoteHost.Host = command.RemoteHost.Host remoteHost.Host = command.RemoteHost.Host
sshc, err := remoteHost.connectToSSHHost() sshc, err := remoteHost.ConnectToSSHHost()
if err != nil { if err != nil {
panic(fmt.Errorf("ssh dial: %w", err)) panic(fmt.Errorf("ssh dial: %w", err))
} }

View File

@ -38,7 +38,7 @@ func GetSSHConfig(host string) (SshConfig, error) {
return config, nil return config, nil
} }
func (remoteConfig *Host) connectToSSHHost() (*ssh.Client, error) { func (remoteConfig *Host) ConnectToSSHHost() (*ssh.Client, error) {
var sshc *ssh.Client var sshc *ssh.Client
var connectErr error var connectErr error

View File

@ -81,39 +81,5 @@ func CreateConfig(backup backy.BackupConfig) backy.BackupConfig {
ConfigPath: backup.ConfigPath, ConfigPath: backup.ConfigPath,
} }
if !backup.Cmds.Before.Empty { return newBackupConfig
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
} }