gofmt
This commit is contained in:
@@ -1,25 +1,25 @@
|
||||
package environmentchecks
|
||||
|
||||
import (
|
||||
"os"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func HandleDefaults(envDefaults map[string]string){
|
||||
for env, defaultValue := range envDefaults{
|
||||
func HandleDefaults(envDefaults map[string]string) {
|
||||
for env, defaultValue := range envDefaults {
|
||||
// Check if env is set
|
||||
_, isSet := os.LookupEnv(env)
|
||||
if(!isSet){
|
||||
if !isSet {
|
||||
os.Setenv(env, defaultValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func HandleRequired(envRequired []string) error {
|
||||
for _, env := range envRequired{
|
||||
for _, env := range envRequired {
|
||||
// Check if env is set
|
||||
_, isSet := os.LookupEnv(env)
|
||||
if(!isSet){
|
||||
if !isSet {
|
||||
return fmt.Errorf("env '%s' required, but not set", env)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
package iproute2mapping
|
||||
|
||||
import (
|
||||
"os"
|
||||
"bufio"
|
||||
"strings"
|
||||
"strconv"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Storage for mapping
|
||||
var ByName = make(map[int]map[string]int)
|
||||
var ById = make(map[int]map[int]string)
|
||||
var ByName = make(map[int]map[string]int)
|
||||
var ById = make(map[int]map[int]string)
|
||||
|
||||
// Mapping types
|
||||
const (
|
||||
@@ -20,49 +20,49 @@ const (
|
||||
|
||||
// Paths
|
||||
var filePaths = map[int]string{
|
||||
PROTOCOL: "/etc/iproute2/rt_protos",
|
||||
TABLE: "/etc/iproute2/rt_tables",
|
||||
PROTOCOL: "/etc/iproute2/rt_protos",
|
||||
TABLE: "/etc/iproute2/rt_tables",
|
||||
}
|
||||
|
||||
// Export error-check
|
||||
var Errors []error;
|
||||
var Errors []error
|
||||
|
||||
func init() {
|
||||
var err error
|
||||
for mapType, filePath := range filePaths{
|
||||
for mapType, filePath := range filePaths {
|
||||
ByName[mapType], ById[mapType], err = readFromFile(filePath)
|
||||
if(err != nil){
|
||||
if err != nil {
|
||||
Errors = append(Errors,
|
||||
fmt.Errorf("failed reading mapping-file '%s': %s", filePath, err),
|
||||
);
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func readFromFile(filePath string) (mapByName map[string]int, mapById map[int]string, err error){
|
||||
func readFromFile(filePath string) (mapByName map[string]int, mapById map[int]string, err error) {
|
||||
file, err := os.Open(filePath)
|
||||
if(err != nil){
|
||||
return nil, nil, err;
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
scanner.Split(bufio.ScanLines)
|
||||
|
||||
scanner.Split(bufio.ScanLines)
|
||||
|
||||
mapByName = make(map[string]int)
|
||||
mapById = make(map[int]string)
|
||||
mapById = make(map[int]string)
|
||||
|
||||
// Go through file line-by-line
|
||||
for scanner.Scan() {
|
||||
for scanner.Scan() {
|
||||
text := scanner.Text()
|
||||
if(text == "" || strings.HasPrefix(text, "#")){
|
||||
if text == "" || strings.HasPrefix(text, "#") {
|
||||
continue
|
||||
}
|
||||
|
||||
id, name, err := getMappingFromText(text)
|
||||
if(err != nil){
|
||||
if err != nil {
|
||||
// Only warn as we can continue processing the file
|
||||
Errors = append(Errors,
|
||||
Errors = append(Errors,
|
||||
fmt.Errorf("mappig-data invalid '%s': %s", text, err),
|
||||
)
|
||||
continue
|
||||
@@ -70,7 +70,7 @@ func readFromFile(filePath string) (mapByName map[string]int, mapById map[int]st
|
||||
|
||||
mapByName[name] = id
|
||||
mapById[id] = name
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
@@ -80,7 +80,7 @@ func getMappingFromText(text string) (int, string, error) {
|
||||
data := strings.Split(text, "\t")
|
||||
|
||||
id, err := strconv.Atoi(data[0])
|
||||
if(err != nil){
|
||||
if err != nil {
|
||||
return 0, "", err
|
||||
}
|
||||
|
||||
@@ -88,13 +88,13 @@ func getMappingFromText(text string) (int, string, error) {
|
||||
}
|
||||
|
||||
// Try getting an id from a name (or a string containing an id) with a specified type
|
||||
func TryGetId(mappingType int, name string) (int, error){
|
||||
func TryGetId(mappingType int, name string) (int, error) {
|
||||
// Try to convert name to id
|
||||
id, err := strconv.Atoi(name)
|
||||
if(err != nil){ // name given -> Convert to id
|
||||
id, err := strconv.Atoi(name)
|
||||
if err != nil { // name given -> Convert to id
|
||||
var found bool
|
||||
id, found = ByName[mappingType][name]
|
||||
if(!found){
|
||||
if !found {
|
||||
return 0, fmt.Errorf("no id found from name")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,2 +1 @@
|
||||
package iproute2mapping
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ func PeerIndexByIP(peers *[]wgtypes.Peer, ip *net.IP) (int, int, error) {
|
||||
}
|
||||
func PeerByIP(peers *[]wgtypes.Peer, ip *net.IP) (*wgtypes.Peer, error) {
|
||||
index, _, err := PeerIndexByIP(peers, ip)
|
||||
if(err != nil) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &(*peers)[index], nil
|
||||
@@ -35,7 +35,7 @@ func PeerIndexByIPNet(peers *[]wgtypes.Peer, ipNet *net.IPNet) (int, int, error)
|
||||
}
|
||||
func PeerByIPNet(peers *[]wgtypes.Peer, ipNet *net.IPNet) (*wgtypes.Peer, error) {
|
||||
index, _, err := PeerIndexByIPNet(peers, ipNet)
|
||||
if(err != nil) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &(*peers)[index], nil
|
||||
|
||||
Reference in New Issue
Block a user