Skip to content

Commit d7ddab8

Browse files
committed
Stdin and Stdout enhancements
- Added stdin source and stdout destination options
1 parent c6f0334 commit d7ddab8

File tree

3 files changed

+30
-17
lines changed

3 files changed

+30
-17
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2022 ScriptTiger
3+
Copyright (c) 2023 ScriptTiger
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ Argument | Description
1919
`-to_blackhole <IPv4>` | Black hole address in destination
2020
`-to_blackhole_v6 <IPv6>`| IPv6 Black hole address in destination
2121

22+
`-` can be used in place of `<file>` to designate standard input as the source and/or standard output as the destination. If standard input is used, standard output will be used by default if no destination file is given.
23+
2224
By default, dragging and dropping a hosts file over a hosts-bl executable will automatically pick out lines beginning with `0.0.0.0`, check for and remove any duplicates, and compress it to 9 domains per line in standard hosts file format with `0.0.0.0` as the black hole address.
2325

2426
In addition to removing duplicates by default, formats which support wild cards will automatically have wild card entries added and all child subdomains will be pruned from the list.

hosts-bl.go

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
//Import dependency packages
44
import (
55
"index/suffixarray"
6+
"io"
67
"os"
78
"path/filepath"
89
"strconv"
@@ -50,9 +51,6 @@ func reducible(format string) bool {
5051

5152
func main() {
5253

53-
//If no arguments given, display help
54-
if len(os.Args) == 1 {help(0)}
55-
5654
//Declare variables
5755
var (
5856
//Flag pointers
@@ -68,6 +66,7 @@ func main() {
6866

6967
//Common variables
7068
err error
69+
rawData []byte
7170
index *suffixarray.Index
7271
iData []string
7372
oData []string
@@ -96,6 +95,10 @@ func main() {
9695
*cmtsPtr = false
9796
dPtr = cmtsPtr
9897

98+
//Check if any data available from standard input and use it as default source if there is
99+
stdinStat, _ := os.Stdin.Stat()
100+
if stdinStat.Mode() & os.ModeNamedPipe != 0 {*ifilePtr = "-"}
101+
99102
//Push arguments to flag pointers
100103
for i := 1; i < len(os.Args); i++ {
101104
if strings.HasPrefix(os.Args[i], "-") {
@@ -144,18 +147,23 @@ func main() {
144147
} else {help(4)}
145148
}
146149

147-
//Set default input and output files if none given
148-
if *ifilePtr == "" {
149-
if os.Args[1] == "" {help(5)
150-
} else {*ifilePtr = os.Args[1]}
150+
//Print help if no input available
151+
if *ifilePtr == "" {help(0)}
152+
153+
//Set default output if none specified
154+
if *ofilePtr == "" {
155+
if *ifilePtr == "-" {*ofilePtr = "-"
156+
} else {*ofilePtr = *fmtPtr+"-"+filepath.Base(*ifilePtr)}
151157
}
152-
if *ofilePtr == "" {*ofilePtr = *fmtPtr+"-"+filepath.Base(*ifilePtr)}
153158

154159
//Initialize format string for quick reference
155160
format := strings.ToLower(*fmtPtr)
156161

157-
//Initialize data from file
158-
rawData, err := os.ReadFile(*ifilePtr)
162+
//Initialize data from either stdin or file
163+
if *ifilePtr == "-" {
164+
rawData, err = io.ReadAll(os.Stdin)
165+
if err != nil {help(5)}
166+
} else {rawData, err = os.ReadFile(*ifilePtr)}
159167
if err != nil {help(6)}
160168

161169
//Convert data to string and initialize variable for cleaning
@@ -216,9 +224,10 @@ func main() {
216224
oData = nil
217225
}
218226

219-
//If requested format is FQDN, just dump to file and exit
227+
//If requested format is FQDN, just dump to file or stdout and exit
220228
if format == "fqdn" {
221-
os.WriteFile(*ofilePtr, []byte(strings.Join(iData, eol)+eol), 0644)
229+
if *ofilePtr == "-" {os.Stdout.WriteString(strings.Join(iData, eol)+eol)
230+
} else {os.WriteFile(*ofilePtr, []byte(strings.Join(iData, eol)+eol), 0644)}
222231
os.Exit(0)
223232
}
224233

@@ -244,9 +253,10 @@ func main() {
244253
oData = nil
245254
}
246255

247-
//If requested format is reduced FQDN, just dump to file and exit
256+
//If requested format is reduced FQDN, just dump to file or stdout and exit
248257
if format == "rfqdn" {
249-
os.WriteFile(*ofilePtr, []byte(strings.Join(iData, eol)+eol), 0644)
258+
if *ofilePtr == "-" {os.Stdout.WriteString(strings.Join(iData, eol)+eol)
259+
} else {os.WriteFile(*ofilePtr, []byte(strings.Join(iData, eol)+eol), 0644)}
250260
os.Exit(0)
251261
}
252262

@@ -346,7 +356,8 @@ func main() {
346356
iData = oData
347357
oData = nil
348358

349-
//Write formatted data to output file and exit
350-
os.WriteFile(*ofilePtr, []byte(strings.Join(iData, eol)+eol), 0644)
359+
//Write formatted data to output file or stdout and exit
360+
if *ofilePtr == "-" {os.Stdout.WriteString(strings.Join(iData, eol)+eol)
361+
} else {os.WriteFile(*ofilePtr, []byte(strings.Join(iData, eol)+eol), 0644)}
351362
os.Exit(0)
352363
}

0 commit comments

Comments
 (0)