First working version
This commit is contained in:
parent
b025416c25
commit
45fa38a4c2
9
.gitignore
vendored
9
.gitignore
vendored
@ -3,6 +3,7 @@
|
||||
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
|
||||
#
|
||||
# Binaries for programs and plugins
|
||||
cisgo
|
||||
*.exe
|
||||
*.exe~
|
||||
*.dll
|
||||
@ -21,3 +22,11 @@
|
||||
# Go workspace file
|
||||
go.work
|
||||
|
||||
# Config
|
||||
*.json
|
||||
|
||||
# outputdir
|
||||
output/
|
||||
|
||||
# template file
|
||||
template.txt
|
43
cmd/cisgo/main.go
Normal file
43
cmd/cisgo/main.go
Normal file
@ -0,0 +1,43 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"log"
|
||||
"text/template"
|
||||
|
||||
"git.vvoid.camp/cisgo/pkg/config"
|
||||
"git.vvoid.camp/cisgo/pkg/generator"
|
||||
)
|
||||
|
||||
var confpath string
|
||||
var templatepath string
|
||||
var outputdir string
|
||||
|
||||
func main() {
|
||||
flag.StringVar(&confpath, "c", "./config.json", "path to config file")
|
||||
flag.StringVar(&templatepath, "t", "./template.txt", "path to template file")
|
||||
flag.StringVar(&outputdir, "o", "./output", "path to output directory")
|
||||
flag.Parse()
|
||||
|
||||
// Load config
|
||||
config, err := config.LoadConfig(confpath)
|
||||
if err != nil {
|
||||
log.Fatal("Error loadin Config: " + err.Error())
|
||||
}
|
||||
|
||||
// Load template
|
||||
template, err := template.ParseFiles(templatepath)
|
||||
if err != nil {
|
||||
log.Fatal("Error loading template: " + err.Error())
|
||||
}
|
||||
|
||||
// Create generator
|
||||
generator := generator.NewGenerator(config, template)
|
||||
|
||||
// Generate
|
||||
err = generator.Generate(outputdir)
|
||||
if err != nil {
|
||||
log.Fatal("Error generating: " + err.Error())
|
||||
}
|
||||
log.Println("AP configs Generated")
|
||||
}
|
32
pkg/config/config.go
Normal file
32
pkg/config/config.go
Normal file
@ -0,0 +1,32 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
)
|
||||
|
||||
type AccessPoint struct {
|
||||
Name string
|
||||
Address string
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
AccessPoints []AccessPoint
|
||||
}
|
||||
|
||||
func LoadConfig(path string) (Config, error) {
|
||||
var result Config
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return result, errors.New("Could not open config file" + err.Error())
|
||||
}
|
||||
defer file.Close()
|
||||
decoder := json.NewDecoder(file)
|
||||
err = decoder.Decode(&result)
|
||||
if err != nil {
|
||||
return result, errors.New("Could not decode config file" + err.Error())
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
48
pkg/generator/generator.go
Normal file
48
pkg/generator/generator.go
Normal file
@ -0,0 +1,48 @@
|
||||
package generator
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
"os"
|
||||
"text/template"
|
||||
|
||||
"git.vvoid.camp/cisgo/pkg/config"
|
||||
)
|
||||
|
||||
type Generator struct {
|
||||
Config config.Config
|
||||
Template *template.Template
|
||||
}
|
||||
|
||||
func NewGenerator(config config.Config, template *template.Template) *Generator {
|
||||
return &Generator{
|
||||
Config: config,
|
||||
Template: template,
|
||||
}
|
||||
}
|
||||
|
||||
func (g *Generator) generateAP(ap config.AccessPoint, outputdir string) error {
|
||||
f, err := os.Create(outputdir + "/" + ap.Name + ".txt")
|
||||
if err != nil {
|
||||
return errors.New("Could not create file: " + err.Error())
|
||||
}
|
||||
defer f.Close()
|
||||
err = g.Template.Execute(f, ap)
|
||||
if err != nil {
|
||||
return errors.New("Could not execute template: " + err.Error())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Generator) Generate(outputdir string) error {
|
||||
|
||||
for _, ap := range g.Config.AccessPoints {
|
||||
log.Println("Generating AP: " + ap.Name)
|
||||
err := g.generateAP(ap, outputdir)
|
||||
if err != nil {
|
||||
return errors.New("Error generating AP " + ap.Name + ": " + err.Error())
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user