This repository was archived by the owner on Oct 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
83 lines (77 loc) · 2.3 KB
/
main.go
File metadata and controls
83 lines (77 loc) · 2.3 KB
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
package main
import (
"fmt"
"os"
"path"
"runtime"
"strings"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/zeusro/common-bandwidth-auto-switch/manager"
"github.com/zeusro/common-bandwidth-auto-switch/model"
"github.com/zeusro/common-bandwidth-auto-switch/sdk/aliyun"
)
const (
defaultConfig = "config.yaml"
exampleConfig = "config-example.yaml"
myName = `
✄╔════╗
✄╚══╗═║
✄──╔╝╔╝╔══╗╔╗╔╗╔══╗╔═╗╔══╗
✄─╔╝╔╝─║║═╣║║║║║══╣║╔╝║╔╗║
✄╔╝═╚═╗║║═╣║╚╝║╠══║║║─║╚╝║
✄╚════╝╚══╝╚══╝╚══╝╚╝─╚══╝
`
LINE = "----------------------------------------"
)
func init() {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
}
func main() {
fmt.Println(LINE)
fmt.Print("Power by")
fmt.Println(myName)
fmt.Println(LINE)
setMaxProcs()
config := model.NewProjectConfig()
homeDir, err := os.Getwd()
if err != nil {
panic(err)
}
configDir := path.Join(homeDir, "config")
configPath := path.Join(configDir, defaultConfig)
err = config.LoadYAML(configPath)
if err != nil {
log.Warn().Msg(err.Error())
configPath = path.Join(configDir, exampleConfig)
err := config.LoadYAML(configPath)
if err != nil {
panic(err)
}
}
if strings.EqualFold("debug", config.LogLevel) {
log.Info().Msgf("config.LogLevel:%s", config.LogLevel)
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
aliyunSDKConfig := config.AliyunConfig
useDingTalkNotification := len(config.DingTalkConfig.NotificationToken) > 0
sdk := aliyun.NewAliyunSDK(&aliyunSDKConfig)
for _, cbp := range config.CommonBandwidthPackages {
manager := manager.NewManager(sdk, &cbp)
if useDingTalkNotification {
manager.UseDingTalkNotification(config.DingTalkConfig.NotificationToken)
}
manager.Run()
}
}
func setMaxProcs() {
// Allow as many threads as we have cores unless the user specified a value.
numProcs := runtime.NumCPU()
runtime.GOMAXPROCS(numProcs)
// Check if the setting was successful.
actualNumProcs := runtime.GOMAXPROCS(0)
if actualNumProcs != numProcs {
log.Info().Msgf("Specified max procs of %d but using %d", numProcs, actualNumProcs)
}
}