added modular packages
This commit is contained in:
parent
62be691fea
commit
6304866953
32
backup.go
32
backup.go
@ -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
|
||||
|
||||
}
|
1
cmd/.gitignore
vendored
Normal file
1
cmd/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
*.yaml
|
18
cmd/backup.go
Normal file
18
cmd/backup.go
Normal file
@ -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
|
1
go.mod
1
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
|
||||
|
2
go.sum
2
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=
|
||||
|
@ -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
|
||||
*/
|
||||
|
126
pkg/config/config.go
Normal file
126
pkg/config/config.go
Normal file
@ -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
|
||||
}
|
33
pkg/ssh/ssh.go
Normal file
33
pkg/ssh/ssh.go
Normal file
@ -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
Block a user