Skip to content

Commit

Permalink
Merge pull request #15 from zluudg/leon/pop-issue-72/cli-refactor
Browse files Browse the repository at this point in the history
Leon/pop issue 72/cli refactor
  • Loading branch information
johanix authored Nov 12, 2024
2 parents 10cb1de + f15a5e3 commit 6aeac4b
Show file tree
Hide file tree
Showing 11 changed files with 2,430 additions and 0 deletions.
24 changes: 24 additions & 0 deletions cmd/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2024 Johan Stenstam, [email protected]
*/

package cmd

import (
"log"

"github.com/spf13/cobra"
"github.com/dnstapir/tapir"
)

var ApiCmd = &cobra.Command{
Use: "api",
Short: "request a TAPIR-POP api summary",
Long: `Query TAPIR-POP for the provided API endpoints and print that out in a (hopefully) comprehensible fashion.`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 0 {
log.Fatal("api must have no arguments")
}
tapir.GlobalCF.Api.ShowApi()
},
}
35 changes: 35 additions & 0 deletions cmd/bump.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (c) 2024 Johan Stenstam, [email protected]
*/

package cmd

import (
"fmt"

"github.com/dnstapir/tapir"
"github.com/miekg/dns"
"github.com/spf13/cobra"
)

var BumpCmd = &cobra.Command{
Use: "bump",
Short: "Instruct TAPIR-POP to bump the SOA serial of the RPZ zone",
Run: func(cmd *cobra.Command, args []string) {
resp := SendCommandCmd(tapir.CommandPost{
Command: "bump",
Zone: dns.Fqdn(tapir.GlobalCF.Zone),
})
if resp.Error {
fmt.Printf("%s\n", resp.ErrorMsg)
}

fmt.Printf("%s\n", resp.Msg)
},
}

func init() {
BumpCmd.Flags().StringVarP(&tapir.GlobalCF.Zone, "zone", "z", "", "Zone name")
}


46 changes: 46 additions & 0 deletions cmd/colourlists.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package cmd

import (
"fmt"
"strings"

"github.com/dnstapir/tapir"
"github.com/spf13/cobra"
)


var ColourlistsCmd = &cobra.Command{
Use: "colourlists",
Short: "Return the white/black/greylists from the current data structures",
Run: func(cmd *cobra.Command, args []string) {
resp := SendDebugCmd(tapir.DebugPost{
Command: "colourlists",
})
if resp.Error {
fmt.Printf("%s\n", resp.ErrorMsg)
}
fmtstring := "%-35s|%-20s|%-10s|%-10s\n"

// print the column headings
fmt.Printf(fmtstring, "Domain", "Source", "Src Fmt", "Colour")
fmt.Println(strings.Repeat("-", 78)) // A nice ruler over the data rows

for _, l := range resp.Lists["whitelist"] {
for _, n := range l.Names {
fmt.Printf(fmtstring, n.Name, l.Name, "-", "white")
}
}
for _, l := range resp.Lists["blacklist"] {
for _, n := range l.Names {
fmt.Printf(fmtstring, n.Name, l.Name, "-", "black")
}
}
for _, l := range resp.Lists["greylist"] {
for _, n := range l.Names {
fmt.Printf(fmtstring, n.Name, l.Name, l.SrcFormat, "grey")
}
}
},
}


203 changes: 203 additions & 0 deletions cmd/dawg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
/*
* Copyright (c) Johan Stenstam, [email protected]
*/

package cmd

import (
"errors"
"fmt"
"os"

"github.com/dnstapir/tapir"
"github.com/miekg/dns"
"github.com/smhanov/dawg"
"github.com/spf13/cobra"
)

var srcformat, srcfile, dawgfile, dawgname string

var DawgCmd = &cobra.Command{
Use: "dawg",
Short: "Generate or interact with data stored in a DAWG file; only useable via sub-commands",
}

var dawgCompileCmd = &cobra.Command{
Use: "compile",
Short: "Compile a new DAWG file from either a text or a CSV source file",
Run: func(cmd *cobra.Command, args []string) {
if srcfile == "" {
fmt.Printf("Error: source file not specified.\n")
os.Exit(1)
}

if dawgfile == "" {
fmt.Printf("Error: outfile not specified.\n")
os.Exit(1)
}

_, err := os.Stat(srcfile)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
fmt.Printf("Error: source file \"%s\" does not exist.\n", srcfile)
os.Exit(1)
} else {
fmt.Printf("Error: %v\n", err)
}
}

switch srcformat {
case "csv":
CompileDawgFromCSV(srcfile, dawgfile)
case "text":
CompileDawgFromText(srcfile, dawgfile)
default:
fmt.Printf("Error: format \"%s\" of source file \"%s\" unknown. Must be either \"csv\" or \"text\".\n",
srcformat, srcfile)
os.Exit(1)
}
},
}

var dawgLookupCmd = &cobra.Command{
Use: "lookup",
Short: "Look up a name in an existing DAWG file",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command.`,
Run: func(cmd *cobra.Command, args []string) {
if dawgfile == "" {
fmt.Printf("Error: DAWG file not specified.\n")
os.Exit(1)
}

if dawgname == "" {
fmt.Printf("Error: Name to look up not specified.\n")
os.Exit(1)
}

fmt.Printf("Loading DAWG: %s\n", dawgfile)
dawgf, err := dawg.Load(dawgfile)
if err != nil {
fmt.Printf("Error from dawg.Load(%s): %v", dawgfile, err)
os.Exit(1)
}

dawgname = dns.Fqdn(dawgname)
idx := dawgf.IndexOf(dawgname)

switch idx {
case -1:
fmt.Printf("Name not found\n")
default:
fmt.Printf("Name %s found, index: %d\n", dawgname, idx)
}
},
}

var dawgListCmd = &cobra.Command{
Use: "list",
Short: "List all names in an existing DAWG file",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command.`,
Run: func(cmd *cobra.Command, args []string) {
if dawgfile == "" {
fmt.Printf("Error: DAWG file not specified.\n")
os.Exit(1)
}

fmt.Printf("Loading DAWG: %s\n", dawgfile)
dawgf, err := dawg.Load(dawgfile)
if err != nil {
fmt.Printf("Error from dawg.Load(%s): %v", dawgfile, err)
os.Exit(1)
}

if tapir.GlobalCF.Debug {
fmt.Printf("DAWG has %d nodes, %d added and %d edges.\n", dawgf.NumNodes(),
dawgf.NumAdded(), dawgf.NumEdges())
}

count, result := tapir.ListDawg(dawgf)
fmt.Printf("%v\n", result)
if tapir.GlobalCF.Verbose {
fmt.Printf("Enumeration func was called %d times\n", count)
}
},
}

func init() {
DawgCmd.AddCommand(dawgCompileCmd, dawgLookupCmd, dawgListCmd)

DawgCmd.PersistentFlags().StringVarP(&dawgfile, "dawg", "", "",
"Name of DAWG file, must end in \".dawg\"")
dawgCompileCmd.Flags().StringVarP(&srcformat, "format", "", "",
"Format of text file, either csv or text")
dawgCompileCmd.Flags().StringVarP(&srcfile, "src", "", "",
"Name of source text file")
// dawgCompileCmd.Flags().StringVarP(&outfile, "outfile", "", "",
// "Name of outfile, must end in \".dawg\"")
// dawgLookupCmd.Flags().StringVarP(&dawgfile, "dawg", "", "",
// "Name of DAWG file")
dawgLookupCmd.Flags().StringVarP(&dawgname, "name", "", "",
"Name to look up")
dawgListCmd.Flags().StringVarP(&dawgname, "name", "", "",
"Name to find prefixes of")
}

func CompileDawgFromCSV(srcfile, outfile string) {
ofd, err := os.Create(outfile)
if err != nil {
fmt.Printf("Error creating \"%s\": %v\n", outfile, err)
os.Exit(1)
}

sortednames, err := tapir.ParseCSV(srcfile, map[string]tapir.TapirName{}, false)
if err != nil {
fmt.Printf("Error parsing CSV source \"%s\": %v\n", srcfile, err)
os.Exit(1)
}

if tapir.GlobalCF.Debug {
fmt.Print("Sorted list of names:\n")
for _, n := range sortednames {
fmt.Printf("%s\n", n)
}
}

err = tapir.CreateDawg(sortednames, outfile)
if err != nil {
fmt.Printf("Error creating DAWG \"%s\" from sorted list of names: %v\n", outfile, err)
os.Exit(1)
}

ofd.Close()
}

func CompileDawgFromText(srcfile, outfile string) {
ofd, err := os.Create(outfile)
if err != nil {
fmt.Printf("Error creating \"%s\": %v\n", outfile, err)
os.Exit(1)
}

sortednames, err := tapir.ParseText(srcfile, map[string]tapir.TapirName{}, false)
if err != nil {
fmt.Printf("Error parsing text source \"%s\": %v\n", srcfile, err)
os.Exit(1)
}

if tapir.GlobalCF.Debug {
fmt.Print("Sorted list of names:\n")
for _, n := range sortednames {
fmt.Printf("%s\n", n)
}
}

err = tapir.CreateDawg(sortednames, outfile)
if err != nil {
fmt.Printf("Error creating DAWG \"%s\" from sorted list of names: %v\n", outfile, err)
os.Exit(1)
}

ofd.Close()
}
Loading

0 comments on commit 6aeac4b

Please sign in to comment.