parent
62be691fea
commit
6304866953
@ -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
|
||||
|
||||
}
|
@ -0,0 +1 @@
|
||||
*.yaml
|
@ -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"
|
||||
|
||||
}
|
@ -1,4 +0,0 @@
|
||||
backup:
|
||||
mail-svr:
|
||||
backuptype: restic
|
||||
remote: email-svr
|
@ -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
|
||||
}
|
@ -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
|
||||
}
|
Loading…
Reference in new issue