fixes for logging

This commit is contained in:
Andrew Woodlee 2022-10-13 12:22:02 -05:00
parent c39330f3a0
commit efbc7619f1

View File

@ -5,7 +5,7 @@ import (
"github.com/melbahja/goph" "github.com/melbahja/goph"
// "git.andrewnw.xyz/CyberShell/backy/cmd/logging" "git.andrewnw.xyz/CyberShell/backy/cmd/logging"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -134,55 +134,55 @@ func WriteConfig(config viper.Viper, backup BackupConfig) error {
/* /*
* Runs a backup configuration * Runs a backup configuration
*/ */
func Run(backup BackupConfig) logging { func Run(backup BackupConfig) logging.Logging {
beforeConfig := backup.Cmds.Before beforeConfig := backup.Cmds.Before
beforeOutput := runCmd(beforeConfig) beforeOutput := runCmd(beforeConfig)
if beforeOutput.err != nil { if beforeOutput.Err != nil {
return logging{ return logging.Logging{
output: beforeOutput.output, Output: beforeOutput.Output,
err: beforeOutput.err, Err: beforeOutput.Err,
} }
} }
backupConfig := backup.Cmds.Backup backupConfig := backup.Cmds.Backup
backupOutput := runCmd(backupConfig) backupOutput := runCmd(backupConfig)
if backupOutput.err != nil { if backupOutput.Err != nil {
return logging{ return logging.Logging{
output: backupOutput.output, Output: beforeOutput.Output,
err: beforeOutput.err, Err: beforeOutput.Err,
} }
} }
afterConfig := backup.Cmds.After afterConfig := backup.Cmds.After
afterOutput := runCmd(afterConfig) afterOutput := runCmd(afterConfig)
if afterOutput.err != nil { if afterOutput.Err != nil {
return logging{ return logging.Logging{
output: afterOutput.output, Output: beforeOutput.Output,
err: afterOutput.err, Err: beforeOutput.Err,
} }
} }
return logging{ return logging.Logging{
output: afterOutput.output, Output: afterOutput.Output,
err: nil, Err: nil,
} }
} }
func runCmd(cmd Command) logging { func runCmd(cmd Command) logging.Logging {
if !cmd.Empty { if !cmd.Empty {
if cmd.Remote { if cmd.Remote {
// Start new ssh connection with private key. // Start new ssh connection with private key.
auth, err := goph.Key(cmd.RemoteHost.PrivateKeyPath, cmd.RemoteHost.PrivateKeyPassword) auth, err := goph.Key(cmd.RemoteHost.PrivateKeyPath, cmd.RemoteHost.PrivateKeyPassword)
if err != nil { if err != nil {
return logging{ return logging.Logging{
output: err.Error(), Output: err.Error(),
err: err, Err: err,
} }
} }
client, err := goph.New(cmd.RemoteHost.User, cmd.RemoteHost.Host, auth) client, err := goph.New(cmd.RemoteHost.User, cmd.RemoteHost.Host, auth)
if err != nil { if err != nil {
return logging{ return logging.Logging{
output: err.Error(), Output: err.Error(),
err: err, Err: err,
} }
} }
@ -197,23 +197,23 @@ func runCmd(cmd Command) logging {
// Execute your command. // Execute your command.
out, err := client.Run(command) out, err := client.Run(command)
if err != nil { if err != nil {
return logging{ return logging.Logging{
output: string(out), Output: string(out),
err: err, Err: err,
} }
} }
} }
cmdOut := exec.Command(cmd.Cmd, cmd.Args...) cmdOut := exec.Command(cmd.Cmd, cmd.Args...)
output, err := cmdOut.Output() output, err := cmdOut.Output()
if err != nil { if err != nil {
return logging{ return logging.Logging{
output: string(output), Output: string(output),
err: err, Err: err,
} }
} }
} }
return logging{ return logging.Logging{
output: "", Output: "",
err: nil, Err: nil,
} }
} }