-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
111 lines (94 loc) · 3.33 KB
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"fmt"
"path/filepath"
"strings"
)
// Options is the main options struct
type Options struct {
profileName string
sources []string
target string
targetHost string
targetUser string
targetPort uint
rsyncOptions []string
sshOptions []string
maxMain uint
maxDaily uint
maxWeekly uint
maxMonthly uint
ReportOptions ReportOptions
Verbose bool
}
// ReportOptions is the options struct for report mail-related options
type ReportOptions struct {
enabled bool
recipients []string
from string
smtpHost string
smtpPort uint
smtpUsername string
smtpPassword string
smtpInsecure bool
}
// SSHOptions constructs and returns a string slice containing all SSH options, including
// the target user as -l and the port as -p
func (options *Options) SSHOptions() []string {
sshOptions := append(options.sshOptions)
if options.IsRemoteTarget() {
if strings.TrimSpace(options.targetUser) != "" {
sshOptions = append(sshOptions, "-l", strings.TrimSpace(options.targetUser))
}
if options.targetPort != 22 {
sshOptions = append(sshOptions, "-p", fmt.Sprintf("%d", options.targetPort))
}
}
return sshOptions
}
// TargetPath returns a well-formed target path with trailing slash
func (options *Options) TargetPath() string {
return NormalizeFolderPath(options.target)
}
// DailyFolderPath Returns the full path to the "daily" folder based on the target path
func (options *Options) DailyFolderPath() string {
return NormalizeFolderPath(filepath.Join(options.target, DailyFolderName))
}
// DailyRelativeFolderPath Returns the relative path to the "daily" folder (relative to target folder)
func (options *Options) DailyRelativeFolderPath() string {
return options.TargetRelativePath(options.DailyFolderPath())
}
// WeeklyFolderPath Returns the full path to the "weekly" folder based on the target path
func (options *Options) WeeklyFolderPath() string {
return NormalizeFolderPath(filepath.Join(options.target, WeeklyFolderName))
}
// WeeklyRelativeFolderPath Returns the relative path to the "weekly" folder (relative to target folder)
func (options *Options) WeeklyRelativeFolderPath() string {
return options.TargetRelativePath(options.WeeklyFolderPath())
}
// MonthlyFolderPath Returns the full path to the "monthly" folder based on the target path
func (options *Options) MonthlyFolderPath() string {
return NormalizeFolderPath(filepath.Join(options.target, MonthlyFolderName))
}
// MonthlyRelativeFolderPath Returns the relative path to the "monthly" folder (relative to target folder)
func (options *Options) MonthlyRelativeFolderPath() string {
return options.TargetRelativePath(options.MonthlyFolderPath())
}
// TargetRelativePath Returns the relative path from the target path to the passed path
func (options *Options) TargetRelativePath(fullPath string) string {
relPath, err := filepath.Rel(options.TargetPath(), fullPath)
if err != nil {
panic(err)
}
// Log.Info.Printf("TargetRelativePath: from %s to %s = %s", options.TargetPath(), fullPath, relPath)
return NormalizeFolderPath(relPath)
}
// IsRemoteTarget is a helper function to check if the options indicate the target folder
// is to be on a remote host
func (options *Options) IsRemoteTarget() bool {
if options.targetHost != "" {
// TODO Validate user/port
return true
}
return false
}