From 6304866953ea3dc958135948f65b13b57831d784 Mon Sep 17 00:00:00 2001 From: Andrew Woodlee Date: Tue, 22 Nov 2022 15:09:39 -0600 Subject: [PATCH] added modular packages --- backup.go | 32 ----------- cmd/.gitignore | 1 + cmd/backup.go | 18 +++++++ config.yaml | 4 -- go.mod | 1 + go.sum | 2 + pkg/backy/backy.go | 90 ------------------------------- pkg/config/config.go | 126 +++++++++++++++++++++++++++++++++++++++++++ pkg/ssh/ssh.go | 33 ++++++++++++ 9 files changed, 181 insertions(+), 126 deletions(-) delete mode 100644 backup.go create mode 100644 cmd/.gitignore create mode 100644 cmd/backup.go delete mode 100644 config.yaml create mode 100644 pkg/config/config.go create mode 100644 pkg/ssh/ssh.go diff --git a/backup.go b/backup.go deleted file mode 100644 index 1e152d1..0000000 --- a/backup.go +++ /dev/null @@ -1,32 +0,0 @@ -package main - -import ( - "github.com/spf13/viper" -) - -type commandBackup struct { - cmd string - args []string -} - -type directory struct { - dst string - src string -} - -type backup struct { - backupType string - local bool - commandToRunBefore commandBackup - commandToRunAfter commandBackup - directories directory - name string -} - -func main() { - - viper.AddConfigPath(".") - viper.SetConfigName("config") // name of config file (without extension) - viper.SetConfigType("yaml") // REQUIRED if the config file does not have the extension in the name - -} diff --git a/cmd/.gitignore b/cmd/.gitignore new file mode 100644 index 0000000..2a61605 --- /dev/null +++ b/cmd/.gitignore @@ -0,0 +1 @@ +*.yaml \ No newline at end of file diff --git a/cmd/backup.go b/cmd/backup.go new file mode 100644 index 0000000..0a2593e --- /dev/null +++ b/cmd/backup.go @@ -0,0 +1,18 @@ +package main + +import ( + "git.andrewnw.xyz/CyberShell/backy/pkg/backy" +) + +func main() { + + // config := backy.BackupConfig{ + // BackupType: "restic", + // Name: "mail-svr", + // } + // home, err := homedir.Dir() + host := backy.Host{} + + host.Host = "email-svr" + +} diff --git a/config.yaml b/config.yaml deleted file mode 100644 index 4469b2b..0000000 --- a/config.yaml +++ /dev/null @@ -1,4 +0,0 @@ -backup: - mail-svr: - backuptype: restic - remote: email-svr diff --git a/go.mod b/go.mod index 3ae416f..cf9ba66 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( github.com/hashicorp/hcl v1.0.0 // indirect github.com/helloyi/go-sshclient v1.1.1 // indirect github.com/inconshreveable/mousetrap v1.0.1 // indirect + github.com/kevinburke/ssh_config v1.2.0 // indirect github.com/kr/fs v0.1.0 // indirect github.com/magiconair/properties v1.8.6 // indirect github.com/melbahja/goph v1.3.0 // indirect diff --git a/go.sum b/go.sum index 1142138..cdad352 100644 --- a/go.sum +++ b/go.sum @@ -132,6 +132,8 @@ github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7P github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= +github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4= +github.com/kevinburke/ssh_config v1.2.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/kr/fs v0.1.0 h1:Jskdu9ieNAYnjxsi0LbQp1ulIKZV1LAFgK1tWhpZgl8= github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg= diff --git a/pkg/backy/backy.go b/pkg/backy/backy.go index 9879ec4..9ea4e93 100644 --- a/pkg/backy/backy.go +++ b/pkg/backy/backy.go @@ -6,7 +6,6 @@ import ( "github.com/melbahja/goph" "git.andrewnw.xyz/CyberShell/backy/pkg/logging" - "github.com/spf13/viper" ) type Host struct { @@ -42,95 +41,6 @@ type BackupConfig struct { SrcDir string } -func ReadConfig(backup string, config viper.Viper) (*viper.Viper, error) { - err := viper.ReadInConfig() - if err != nil { - return &viper.Viper{}, err - } - - conf := config.Sub("backup." + backup) - - backupConfig := config.Unmarshal(&conf) - if backupConfig == nil { - return &viper.Viper{}, backupConfig - } - - return conf, nil -} - -// pass Name-level (i.e. "backups."+configName) to function -func CreateConfig(backup BackupConfig) BackupConfig { - newBackupConfig := BackupConfig{ - Name: backup.Name, - BackupType: backup.BackupType, - - DstDir: backup.DstDir, - SrcDir: backup.SrcDir, - - 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 -} - -// writes config to file -func WriteConfig(config viper.Viper, backup BackupConfig) error { - configName := "backup." + backup.Name - config.Set(configName+".BackupType", backup.BackupType) - if !backup.Cmds.After.Empty { - config.Set(configName+".Cmds.After.Cmd", backup.Cmds.After.Cmd) - config.Set(configName+".Cmds.After.Args", backup.Cmds.After.Args) - if backup.Cmds.Before.Remote { - config.Set(configName+".Cmds.After.RemoteHost.Host", backup.Cmds.After.RemoteHost.Host) - } - } - - config.Set(configName+"..Cmds.backup.Cmd", backup.Cmds.Backup.Cmd) - if !backup.Cmds.Before.Empty { - config.Set(configName+"Cmds.Before.Cmd", backup.Cmds.Before.Cmd) - config.Set(configName+"Cmds.Before.Args", backup.Cmds.Before.Args) - } - err := config.WriteConfig() - if err != nil { - return err - } - return nil -} - /* * Runs a backup configuration */ diff --git a/pkg/config/config.go b/pkg/config/config.go new file mode 100644 index 0000000..e655f2c --- /dev/null +++ b/pkg/config/config.go @@ -0,0 +1,126 @@ +package config + +import ( + "git.andrewnw.xyz/CyberShell/backy/pkg/backy" + "github.com/spf13/viper" +) + +type BackyViperOpts struct { + ConfigFilePath string +} + +func ReadConfig(Config *viper.Viper) (*viper.Viper, error) { + + backyViper := viper.New() + + // Check for existing config, if none exists, return new one + if Config == nil { + + backyViper.AddConfigPath(".") + // name of config file (without extension) + backyViper.SetConfigName("backy") + // REQUIRED if the config file does not have the extension in the name + backyViper.SetConfigType("yaml") + + if err := backyViper.ReadInConfig(); err != nil { + if configFileNotFound, ok := err.(viper.ConfigFileNotFoundError); ok { + return nil, configFileNotFound + // Config file not found; ignore error if desired + } else { + // Config file was found but another error was produced + } + } + } else { + // Config exists, try to read config file + if err := Config.ReadInConfig(); err != nil { + if _, ok := err.(viper.ConfigFileNotFoundError); ok { + + backyViper.AddConfigPath(".") + // name of config file (without extension) + backyViper.SetConfigName("backy") + // REQUIRED if the config file does not have the extension in the name + backyViper.SetConfigType("yaml") + + if err := backyViper.ReadInConfig(); err != nil { + if configFileNotFound, ok := err.(viper.ConfigFileNotFoundError); ok { + return nil, configFileNotFound + } else { + // Config file was found but another error was produced + } + } + + } else { + // Config file was found but another error was produced + } + } + } + + return backyViper, nil +} + +func unmarshallConfig(backup string, config *viper.Viper) (*viper.Viper, error) { + err := viper.ReadInConfig() + if err != nil { + return nil, err + } + + yamlConfigPath := "backup." + backup + conf := config.Sub(yamlConfigPath) + + backupConfig := config.Unmarshal(&conf) + if backupConfig == nil { + return nil, backupConfig + } + + return conf, nil +} + +// CreateConfig creates a configuration +// pass Name-level (i.e. "backups."+configName) to function +func CreateConfig(backup backy.BackupConfig) backy.BackupConfig { + newBackupConfig := backy.BackupConfig{ + Name: backup.Name, + BackupType: backup.BackupType, + + DstDir: backup.DstDir, + SrcDir: backup.SrcDir, + + 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 +} diff --git a/pkg/ssh/ssh.go b/pkg/ssh/ssh.go new file mode 100644 index 0000000..f79db0d --- /dev/null +++ b/pkg/ssh/ssh.go @@ -0,0 +1,33 @@ +package ssh + +import ( + "errors" + + "github.com/kevinburke/ssh_config" +) + +type SshConfig struct { + PrivateKey string + Port uint + HostName string + User string +} + +func GetSSHConfig(host string) (SshConfig, error) { + var config SshConfig + hostName := ssh_config.Get(host, "HostName") + if hostName == "" { + return SshConfig{}, errors.New("hostname not found") + } + config.HostName = hostName + privKey, err := ssh_config.GetStrict(host, "IdentityFile") + if err != nil { + return SshConfig{}, err + } + config.PrivateKey = privKey + User := ssh_config.Get(host, "User") + if User == "" { + return SshConfig{}, errors.New("user not found") + } + return config, nil +}