Added inv-alias and hosto
This commit is contained in:
parent
dfe5db0360
commit
5a0f72b41a
31
scripts/bin/hosto
Executable file
31
scripts/bin/hosto
Executable file
@ -0,0 +1,31 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
POSITIONAL_ARGS=()
|
||||||
|
INVENTORY_FILE="$(pwd)/inventory"
|
||||||
|
|
||||||
|
while [[ $# -gt 0 ]]; do
|
||||||
|
case $1 in
|
||||||
|
-i|--inv-file)
|
||||||
|
INVENTORY_FILE="$2"
|
||||||
|
shift
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
-*|--*)
|
||||||
|
echo "hosto: Unknown option $1"
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
POSITIONAL_ARGS+=("$1")
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
if [ -f $INVENTORY_FILE ]; then
|
||||||
|
sudo inv-alias add $INVENTORY_FILE
|
||||||
|
eval "${POSITIONAL_ARGS[@]}"
|
||||||
|
sudo inv-alias rm $INVENTORY_FILE
|
||||||
|
else
|
||||||
|
echo "hosto: Could not find inventory file in $PWD"
|
||||||
|
eval "${POSITIONAL_ARGS[@]}"
|
||||||
|
fi
|
BIN
scripts/bin/inv-alias
Executable file
BIN
scripts/bin/inv-alias
Executable file
Binary file not shown.
143
scripts/inv-alias.go
Normal file
143
scripts/inv-alias.go
Normal file
@ -0,0 +1,143 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"io/fs"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type AliasMap map[string]string
|
||||||
|
|
||||||
|
const (
|
||||||
|
HostsFile string = "/etc/hosts"
|
||||||
|
)
|
||||||
|
|
||||||
|
func FixedSplit(s, sep string, parts int) []string {
|
||||||
|
n := make([]string, parts)
|
||||||
|
p := strings.SplitN(s, sep, parts)
|
||||||
|
copy(n, p)
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
|
func IsLegalLine(line string) bool {
|
||||||
|
c := line[0]
|
||||||
|
return c != '[' && c != '#'
|
||||||
|
}
|
||||||
|
|
||||||
|
func BuildRegionString(regionName string, aliases AliasMap) string {
|
||||||
|
b := strings.Builder{}
|
||||||
|
b.WriteString("#region ")
|
||||||
|
b.WriteString(regionName)
|
||||||
|
b.WriteString("\n")
|
||||||
|
|
||||||
|
for ip, alias := range aliases {
|
||||||
|
b.WriteString(ip)
|
||||||
|
b.WriteString("\t")
|
||||||
|
b.WriteString(alias)
|
||||||
|
b.WriteString("\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
b.WriteString("#endregion")
|
||||||
|
return b.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func BuildRegionRegexp(regionName string) *regexp.Regexp {
|
||||||
|
b := strings.Builder{}
|
||||||
|
b.WriteString("(?s)\n#region ")
|
||||||
|
b.WriteString(regexp.QuoteMeta(regionName))
|
||||||
|
b.WriteString(".*#endregion")
|
||||||
|
r := regexp.MustCompile(b.String())
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func ScanAliases(fileReader io.Reader) (AliasMap, error) {
|
||||||
|
aliasMap := AliasMap{}
|
||||||
|
scanner := bufio.NewScanner(fileReader)
|
||||||
|
for scanner.Scan() {
|
||||||
|
line := scanner.Text()
|
||||||
|
if IsLegalLine(line) {
|
||||||
|
s := FixedSplit(line, "#", 2)
|
||||||
|
ip, alias := strings.TrimSpace(s[0]), strings.TrimSpace(s[1])
|
||||||
|
if _, ok := aliasMap[ip]; !ok && alias != "" {
|
||||||
|
aliasMap[ip] = alias
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := scanner.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return aliasMap, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func AddAliases(fileName string) {
|
||||||
|
file, err := os.Open(fileName)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
h, err := ScanAliases(file)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
file.Close()
|
||||||
|
|
||||||
|
r := BuildRegionRegexp(fileName)
|
||||||
|
s := BuildRegionString(fileName, h)
|
||||||
|
|
||||||
|
content, err := ioutil.ReadFile(HostsFile)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
c := r.ReplaceAllString(string(content), s)
|
||||||
|
if !r.MatchString(c) {
|
||||||
|
c += ("\n" + s)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = os.WriteFile(HostsFile, []byte(c[:]), fs.FileMode(os.O_WRONLY|os.O_TRUNC))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func RemoveAliases(fileName string) {
|
||||||
|
regionReg := BuildRegionRegexp(fileName)
|
||||||
|
content, err := ioutil.ReadFile(HostsFile)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
c := regionReg.ReplaceAll(content, []byte(""))
|
||||||
|
err = os.WriteFile(HostsFile, c, fs.FileMode(os.O_WRONLY|os.O_TRUNC))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
u := fmt.Sprintf("Please use: %s <add|rm> <file:path>\n", os.Args[0])
|
||||||
|
|
||||||
|
if len(os.Args) < 3 {
|
||||||
|
fmt.Println(u)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
switch os.Args[1] {
|
||||||
|
case "add":
|
||||||
|
AddAliases(os.Args[2])
|
||||||
|
case "rm":
|
||||||
|
RemoveAliases(os.Args[2])
|
||||||
|
default:
|
||||||
|
fmt.Println(u)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user