-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
126 lines (114 loc) · 2.91 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright © 2019 Moises P. Sena <moisespsena@gmail.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package logrotate
import (
"fmt"
"math"
"os"
"strconv"
"strings"
)
const (
Monthly RotationDuration = 'M'
Weekly RotationDuration = 'W'
Daily RotationDuration = 'D'
Hourly RotationDuration = 'h'
Minutely RotationDuration = 'm'
Yearly RotationDuration = 'Y'
)
type RotationDuration byte
func (this RotationDuration) Valid() bool {
switch this {
case 0, Monthly, Weekly, Daily, Hourly, Minutely, Yearly:
return true
}
return false
}
type Options struct {
HistoryPath string // path inside root
MaxSize int64 // -1 disables limitter
Duration RotationDuration
FileMode os.FileMode
DirMode os.FileMode
HistoryDir string
HistoryCount int
}
type Config struct {
MaxSize string `yaml:"max_size" mapstructure:"max_size"`
Duration string
FileMode os.FileMode `yaml:"file_mode" mapstructure:"file_mode"`
DirMode os.FileMode `yaml:"dir_mode" mapstructure:"dir_mode"`
HistoryDir string `yaml:"history_dir" mapstructure:"history_dir"`
HistoryPath string `yaml:"history_path" mapstructure:"history_path"`
HistoryCount int `yaml:"history_count" mapstructure:"history_count"`
}
func (this Config) Yaml() string {
return fmt.Sprintf(`max_size: %s
duration: %s
file_mode: 0%o
dir_mode: 0%o
history_dir: %q
history_path: %q
history_count: %d`,
this.MaxSize,
this.Duration,
this.FileMode,
this.DirMode,
this.HistoryDir,
this.HistoryPath,
this.HistoryCount)
}
func (this Config) Options() (opt Options, err error) {
var d RotationDuration
if this.Duration != "" {
d = RotationDuration(this.Duration[0])
if !d.Valid() {
err = fmt.Errorf("bad duration %q", string(d))
return
}
}
var s int
if this.MaxSize != "" {
var (
m float64
c = strings.ToUpper(string(this.MaxSize[len(this.MaxSize)-1]))[0]
maxSize = this.MaxSize[0 : len(this.MaxSize)-1]
)
switch c {
case 'K':
m = 3
case 'M':
m = 6
case 'G':
m = 9
case 'T':
m = 12
default:
maxSize += string(c)
}
if s, err = strconv.Atoi(maxSize); err != nil {
return
}
s *= int(math.Pow(10, m))
}
return Options{
HistoryDir: this.HistoryDir,
HistoryPath: this.HistoryPath,
MaxSize: int64(s),
HistoryCount: this.HistoryCount,
Duration: d,
FileMode: this.FileMode,
DirMode: this.DirMode,
}, nil
}