-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create console and json output formats
- Loading branch information
1 parent
2f1a8e1
commit 224ca01
Showing
4 changed files
with
151 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |