Skip to content

Commit

Permalink
Create console and json output formats
Browse files Browse the repository at this point in the history
  • Loading branch information
Themimitoof committed Aug 17, 2022
1 parent 2f1a8e1 commit 224ca01
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 31 deletions.
81 changes: 50 additions & 31 deletions ipg.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ import (
"github.com/alecthomas/kong"
"github.com/c-robinson/iplib"
"github.com/themimitoof/ipg/src"
"github.com/themimitoof/ipg/src/output"
)

var cliOutputSettings output.OutputInformationSettings
var cli struct {
Subnet string `arg:"" help:"IPv6 Subnet"`
Random bool `short:"r" xor:"Name,Random" required:"" help:"Generate a random IPv6 address on the given subnet."`
Name string `short:"n" xor:"Name,Random" required:"" help:"Specify the hostname of a machine, an IPv6 address will be generated based on it."`
Random bool `short:"r" required:"" help:"Generate a random IPv6 address on the given subnet."`
Name string `short:"n" required:"" default:"hostname" help:"Specify the hostname of a machine, an IPv6 address will be generated based on it."`
Silent bool `short:"s" help:"Only display values without their labels."`
Format string `name:"format" short:"f" enum:"console,json" default:"console" help:"Specify the type of output. Possible values: console, json"`
Address bool `short:"a" help:"Display the generated IP address."`
Reverse bool `short:"R" help:"Display the ARPA version of the IP address."`
DNSRecord bool `name:"dns" short:"d" help:"Returns a DNS record ready to paste to a DNS zone."`
ReverseRecord bool `name:"rrecord" short:"x" help:"Returns a rDNS record ready to paste to a DNS zone."`
Expand All @@ -27,19 +31,34 @@ func main() {
kong.Description("A simple IPv6 generator for lazy netadmins."),
)

cliOutputSettings = output.OutputInformationSettings{
Address: cli.Address,
Reverse: cli.Reverse,
DNSRecord: cli.DNSRecord,
ReverseRecord: cli.ReverseRecord,
Format: cli.Format,
Silent: cli.Silent,
}

if !cli.Address && !cli.Reverse && !cli.DNSRecord && !cli.ReverseRecord {
cliOutputSettings.AllData = true
}

var ipNetwork iplib.Net6 = iplib.Net6FromStr(cli.Subnet)

// Check if the Subnet is valid or not.
if len(ipNetwork.IP()) == 0 {
fmt.Println("The given subnet is not valid.")
os.Stderr.WriteString("The given subnet is not valid.\n")
os.Exit(1)
}

// Check if the subnet is not too small to be used
var cidr, _ = ipNetwork.Mask().Size()

if cidr > 126 {
fmt.Printf("The given subnet is too small (/%d) to be used with ipg.\n", cidr)
os.Stderr.WriteString(
fmt.Sprintf("The given subnet (/%d) is too small to be used with ipg.\n", cidr),
)
os.Exit(1)
}

Expand All @@ -54,34 +73,34 @@ func main() {

var reverseIpAddr string = iplib.IPToARPA(generatedIp)
var ipAddr string = generatedIp.String()
var dnsRecord string = src.GenerateDNSRecord(ipAddr, cli.DNSTTL, cli.Name)
var reverseDnsRecord string = src.GenerateReverseDNSRecord(reverseIpAddr, cli.DNSTTL, cli.Name)
var cmdOutput []byte

// Always display the generated IP address
if !cli.Silent {
fmt.Printf("IP address: ")
}
color.Yellow(ipAddr)

// Display the ARPA version of the IP address if the flag is set
if cli.Reverse {
if !cli.Silent {
fmt.Printf("Reverse IP address: ")
}
color.Yellow(reverseIpAddr)
}

// Display the BIND DNS record if the flag is set
if cli.DNSRecord {
if !cli.Silent {
fmt.Println("DNS record:")
}
color.Yellow(GenerateDNSRecord(ipAddr, cli.DNSTTL, cli.Name))
// Render the output
if cli.Format == "json" {
cmdOutput = output.IpgJsonOutput{
Config: &cliOutputSettings,
Data: output.IpgOutputData{
Hostname: cli.Name,
IPAddress: generatedIp.String(),
IPReverse: reverseIpAddr,
DNSRecord: dnsRecord,
ReverseRecord: reverseDnsRecord,
},
}.Render()
} else {
cmdOutput = output.IpgConsoleOutput{
Config: &cliOutputSettings,
Data: output.IpgOutputData{
Hostname: cli.Name,
IPAddress: generatedIp.String(),
IPReverse: iplib.IPToARPA(generatedIp),
DNSRecord: dnsRecord,
ReverseRecord: reverseDnsRecord,
},
}.Render()
}

// Display the ARPA DNS record if the flag is set
if cli.ReverseRecord {
if !cli.Silent {
fmt.Println("ARPA DNS record:")
}
color.Yellow(GenerateReverseDNSRecord(reverseIpAddr, cli.DNSTTL, cli.Name))
}
os.Stdout.Write(cmdOutput)
}
59 changes: 59 additions & 0 deletions src/output/console.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package output

import (
"bytes"
"fmt"

"github.com/fatih/color"
)

type IpgConsoleOutput struct {
Config *OutputInformationSettings
Data IpgOutputData
}

func (c IpgConsoleOutput) Render() []byte {
output := bytes.Buffer{}

// Display the generated IP address
if c.Config.AllData || c.Config.Address {
if c.Config.Silent {
output.WriteString(fmt.Sprintf("%s\n", c.Data.IPAddress))
} else {
output.WriteString("IP address: ")
output.WriteString(color.YellowString("%s\n", c.Data.IPAddress))
}
}

// Display the ARPA version of the IP address if the flag is set
if c.Config.AllData || c.Config.Address {
if c.Config.Silent {
output.WriteString(fmt.Sprintf("%s\n", c.Data.IPReverse))
} else {
output.WriteString("Reverse IP address: ")
output.WriteString(color.YellowString("%s\n", c.Data.IPReverse))
}
}

// Display the BIND DNS record if the flag is set
if c.Config.AllData || c.Config.DNSRecord {
if c.Config.Silent {
output.WriteString(fmt.Sprintf("%s\n", c.Data.DNSRecord))
} else {
output.WriteString("DNS record: ")
output.WriteString(color.YellowString("%s\n", c.Data.DNSRecord))
}
}

// Display the ARPA DNS record if the flag is set
if c.Config.AllData || c.Config.ReverseRecord {
if c.Config.Silent {
output.WriteString(fmt.Sprintf("%s\n", c.Data.ReverseRecord))
} else {
output.WriteString("ARPA DNS record: ")
output.WriteString(color.YellowString("%s\n", c.Data.ReverseRecord))
}
}

return output.Bytes()
}
22 changes: 22 additions & 0 deletions src/output/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package output

import (
"encoding/json"
"os"
)

type IpgJsonOutput struct {
Config *OutputInformationSettings
Data IpgOutputData
}

func (c IpgJsonOutput) Render() []byte {
b, err := json.Marshal(c.Data)

if err != nil {
os.Stderr.WriteString("Unable to output the JSON.")
os.Exit(2)
}

return append(b, '\n')
}
20 changes: 20 additions & 0 deletions src/output/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package output

type OutputInformationSettings struct {
AllData bool
Address bool
Reverse bool
DNSRecord bool
ReverseRecord bool

Format string
Silent bool
}

type IpgOutputData struct {
Hostname string
IPAddress string
IPReverse string
DNSRecord string
ReverseRecord string
}

0 comments on commit 224ca01

Please sign in to comment.