-
Notifications
You must be signed in to change notification settings - Fork 37
/
config.go
133 lines (110 loc) · 2.76 KB
/
config.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
127
128
129
130
131
132
133
package smi
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/sleepinggenius2/gosmi/smi/internal"
"github.com/sleepinggenius2/gosmi/types"
)
const (
DefaultErrorLevel = 3
DefaultGlobalConfig = "/etc/smi.conf"
DefaultUserConfig = ".smirc"
)
var DefaultSmiPaths []string = []string{
"/usr/local/share/mibs/ietf",
"/usr/local/share/mibs/iana",
"/usr/local/share/mibs/irtf",
"/usr/local/share/mibs/site",
"/usr/local/share/mibs/jacobs",
"/usr/local/share/mibs/tubs",
}
type FS = internal.FS
type NamedFS = internal.NamedFS
func NewNamedFS(name string, fs FS) NamedFS { return NamedFS{Name: "[" + name + "]", FS: fs} }
func checkInit() {
if !internal.Initialized() {
Init()
}
}
// int smiInit(const char *tag)
func Init(tag ...string) bool {
var configTag, handleName string
if len(tag) > 0 {
configTag = tag[0]
handleName = strings.Join(tag, ":")
}
if !internal.Init(handleName) {
return false
}
// Set to built-in default path, if not Windows
if runtime.GOOS != "windows" {
internal.SetPath(DefaultSmiPaths...)
}
// Read global config file, if we can
_ = ReadConfig(DefaultGlobalConfig, configTag)
// Read user config file, if we can
if homedir, err := os.UserHomeDir(); err == nil {
_ = ReadConfig(filepath.Join(homedir, DefaultUserConfig), configTag)
}
// Use SMIPATH environment variable, if set
SetPath(os.Getenv("SMIPATH"))
return true
}
// void smiExit(void)
func Exit() {
internal.Exit()
}
// void smiSetErrorLevel(int level)
func SetErrorLevel(level int) {
checkInit()
internal.SetErrorLevel(level)
}
// int smiGetFlags(void)
func GetFlags() int {
checkInit()
return internal.GetFlags()
}
// void smiSetFlags(int userflags)
func SetFlags(userflags int) {
checkInit()
internal.SetFlags(userflags)
}
// char *smiGetPath(void)
func GetPath() string {
checkInit()
return internal.GetPath()
}
// int smiSetPath(const char *path)
func SetPath(path string) {
paths := filepath.SplitList(path)
if len(paths) == 0 {
return
}
internal.SetPath(paths...)
}
// void smiSetSeverity(char *pattern, int severity)
func SetSeverity(pattern string, severity int) {
checkInit()
internal.SetSeverity(pattern, severity)
}
// int smiReadConfig(const char *filename, const char *tag)
func ReadConfig(filename string, tag ...string) error {
f, err := os.Open(filename)
if err != nil {
return fmt.Errorf("Open file: %w", err)
}
defer f.Close()
// TODO: Parse file
return nil
}
// void smiSetErrorHandler(SmiErrorHandler smiErrorHandler)
func SetErrorHandler(smiErrorHandler types.SmiErrorHandler) {
checkInit()
internal.SetErrorHandler(smiErrorHandler)
}
func SetFS(fs ...NamedFS) { internal.SetFS(fs...) }
func AppendFS(fs ...NamedFS) { internal.AppendFS(fs...) }
func PrependFS(fs ...NamedFS) { internal.PrependFS(fs...) }