-
Notifications
You must be signed in to change notification settings - Fork 622
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merge command can merge plaintext IP & CIDR from standard input, then print the merged result to standard output usage example: curl https://example.com/path/to/your/plaintext/cidr.txt | geoip merge cat ./path/to/your/plaintext/cidr.txt | geoip merge
- Loading branch information
1 parent
18e9f1a
commit 130d27a
Showing
3 changed files
with
145 additions
and
0 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
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,117 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"strings" | ||
|
||
"github.com/Loyalsoldier/geoip/lib" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
const tempConfig = ` | ||
{ | ||
"input": [ | ||
{ | ||
"type": "stdin", | ||
"action": "add", | ||
"args": { | ||
"name": "temp" | ||
} | ||
} | ||
], | ||
"output": [ | ||
{ | ||
"type": "stdout", | ||
"action": "output" | ||
} | ||
] | ||
} | ||
` | ||
|
||
const tempConfigWithIPv4 = ` | ||
{ | ||
"input": [ | ||
{ | ||
"type": "stdin", | ||
"action": "add", | ||
"args": { | ||
"name": "temp" | ||
} | ||
} | ||
], | ||
"output": [ | ||
{ | ||
"type": "stdout", | ||
"action": "output", | ||
"args": { | ||
"onlyIPType": "ipv4" | ||
} | ||
} | ||
] | ||
} | ||
` | ||
|
||
const tempConfigWithIPv6 = ` | ||
{ | ||
"input": [ | ||
{ | ||
"type": "stdin", | ||
"action": "add", | ||
"args": { | ||
"name": "temp" | ||
} | ||
} | ||
], | ||
"output": [ | ||
{ | ||
"type": "stdout", | ||
"action": "output", | ||
"args": { | ||
"onlyIPType": "ipv6" | ||
} | ||
} | ||
] | ||
} | ||
` | ||
|
||
func init() { | ||
rootCmd.AddCommand(mergeCmd) | ||
mergeCmd.PersistentFlags().StringP("onlyiptype", "t", "", "The only IP type to output, available options: \"ipv4\", \"ipv6\"") | ||
} | ||
|
||
var mergeCmd = &cobra.Command{ | ||
Use: "merge", | ||
Aliases: []string{"m"}, | ||
Short: "Merge plaintext IP & CIDR from standard input, then print to standard output", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
otype, _ := cmd.Flags().GetString("onlyiptype") | ||
otype = strings.ToLower(strings.TrimSpace(otype)) | ||
|
||
if otype != "" && otype != "ipv4" && otype != "ipv6" { | ||
log.Fatal("invalid argument onlyiptype: ", otype) | ||
} | ||
|
||
var configBytes []byte | ||
switch lib.IPType(otype) { | ||
case lib.IPv4: | ||
configBytes = []byte(tempConfigWithIPv4) | ||
case lib.IPv6: | ||
configBytes = []byte(tempConfigWithIPv6) | ||
default: | ||
configBytes = []byte(tempConfig) | ||
} | ||
|
||
instance, err := lib.NewInstance() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
if err := instance.InitFromBytes(configBytes); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
if err := instance.Run(); err != nil { | ||
log.Fatal(err) | ||
} | ||
}, | ||
} |