-
Notifications
You must be signed in to change notification settings - Fork 204
Bulk ASN #159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Bulk ASN #159
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6c32c96
Exploring
Harisabdullah e34255c
testing and exploring
Harisabdullah 55de7b7
Added a bunch of things
Harisabdullah 039e117
Bulk lookup for ASN
Harisabdullah ee80d94
Fixes not reading token from saved config
Harisabdullah 345cbce
minor fixes
Harisabdullah 91986b1
Refactor: Generalize Input Source Handling
Harisabdullah 20ed7ee
Incomplete. small fixes need to be made
Harisabdullah 83d89fd
Making `op` work per input instead of creating arrays
Harisabdullah bbcbdb4
Refactored the default `piped` case
Harisabdullah 10fd258
Apply suggestions from code review
UmanShahzad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,113 +1,65 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "net/http" | ||
|
|
||
| "github.com/fatih/color" | ||
| "github.com/ipinfo/cli/lib/complete" | ||
| "github.com/ipinfo/cli/lib/complete/predict" | ||
| "github.com/ipinfo/go/v2/ipinfo" | ||
| "github.com/spf13/pflag" | ||
| "os" | ||
| ) | ||
|
|
||
| var completionsASN = &complete.Command{ | ||
| Sub: map[string]*complete.Command{ | ||
| "bulk": completionsASNBulk, | ||
| }, | ||
| Flags: map[string]complete.Predictor{ | ||
| "-t": predict.Nothing, | ||
| "--token": predict.Nothing, | ||
| "--nocache": predict.Nothing, | ||
| "-h": predict.Nothing, | ||
| "--help": predict.Nothing, | ||
| "-f": predict.Set(asnFields), | ||
| "--field": predict.Set(asnFields), | ||
| "--nocolor": predict.Nothing, | ||
| "-p": predict.Nothing, | ||
| "--pretty": predict.Nothing, | ||
| "-j": predict.Nothing, | ||
| "--json": predict.Nothing, | ||
| "-c": predict.Nothing, | ||
| "--csv": predict.Nothing, | ||
| "-h": predict.Nothing, | ||
| "--help": predict.Nothing, | ||
| }, | ||
| } | ||
|
|
||
| func printHelpASN(asn string) { | ||
| func printHelpASN() { | ||
| fmt.Printf( | ||
| `Usage: %s %s [<opts>] | ||
| `Usage: %s asn <cmd> [<opts>] | ||
|
|
||
| Commands: | ||
| bulk lookup ASNs in bulk | ||
|
|
||
| Options: | ||
| General: | ||
| --token <tok>, -t <tok> | ||
| use <tok> as API token. | ||
| --nocache | ||
| do not use the cache. | ||
| --help, -h | ||
| show help. | ||
|
|
||
| Outputs: | ||
| --field <field>, -f <field> | ||
| lookup only specific fields in the output. | ||
| field names correspond to JSON keys, e.g. 'registry' or 'allocated'. | ||
| multiple field names must be separated by commas. | ||
| --nocolor | ||
| disable colored output. | ||
|
|
||
| Formats: | ||
| --json, -j | ||
| output JSON format. (default) | ||
| --yaml, -y | ||
| output YAML format. | ||
| `, progBase, asn) | ||
| `, progBase) | ||
| } | ||
|
|
||
| func cmdASN(asn string) error { | ||
| var fTok string | ||
| var fField []string | ||
| var fJSON bool | ||
| var fYAML bool | ||
|
|
||
| pflag.StringVarP(&fTok, "token", "t", "", "the token to use.") | ||
| pflag.BoolVar(&fNoCache, "nocache", false, "disable the cache.") | ||
| pflag.BoolVarP(&fHelp, "help", "h", false, "show help.") | ||
| pflag.StringSliceVarP(&fField, "field", "f", nil, "specific field to lookup.") | ||
| pflag.BoolVarP(&fJSON, "json", "j", true, "output JSON format. (default)") | ||
| pflag.BoolVarP(&fYAML, "yaml", "y", false, "output YAML format.") | ||
| pflag.BoolVar(&fNoColor, "nocolor", false, "disable color output.") | ||
| pflag.Parse() | ||
|
|
||
| if fNoColor { | ||
| color.NoColor = true | ||
| } | ||
|
|
||
| if fHelp { | ||
| printHelpASN(asn) | ||
| func cmdASNDefault() error { | ||
| // check whether the standard input (stdin) is being piped | ||
| // or redirected from another source or whether it's being read from the terminal (interactive mode). | ||
| stat, _ := os.Stdin.Stat() | ||
| if (stat.Mode() & os.ModeCharDevice) != 0 { | ||
| printHelpASN() | ||
| return nil | ||
| } | ||
| return cmdASNBulk(true) | ||
| } | ||
|
|
||
| ii = prepareIpinfoClient(fTok) | ||
|
|
||
| // require token for ASN API. | ||
| if ii.Token == "" { | ||
| return errors.New("ASN lookups require a token; login via `ipinfo init`.") | ||
| // cmdASN is the handler for the "asn" command. | ||
| func cmdASN() error { | ||
| var err error | ||
| cmd := "" | ||
| if len(os.Args) > 2 { | ||
| cmd = os.Args[2] | ||
| } | ||
|
|
||
| data, err := ii.GetASNDetails(asn) | ||
| if err != nil { | ||
| iiErr, ok := err.(*ipinfo.ErrorResponse) | ||
| if ok && (iiErr.Response.StatusCode == http.StatusUnauthorized) { | ||
| return errors.New("Token does not have access to ASN API") | ||
| } | ||
| return err | ||
| switch { | ||
| case cmd == "bulk": | ||
| err = cmdASNBulk(false) | ||
| default: | ||
| err = cmdASNDefault() | ||
| } | ||
|
|
||
| if len(fField) > 0 { | ||
| d := make(ipinfo.BatchASNDetails, 1) | ||
| d[data.ASN] = data | ||
| return outputFieldBatchASNDetails(d, fField, false, false) | ||
| } | ||
| if fYAML { | ||
| return outputYAML(data) | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "err: %v\n", err) | ||
| } | ||
|
|
||
| return outputJSON(data) | ||
| return nil | ||
| } | ||
This file contains hidden or 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,95 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "github.com/ipinfo/cli/lib" | ||
| "github.com/ipinfo/cli/lib/complete" | ||
| "github.com/ipinfo/cli/lib/complete/predict" | ||
| "github.com/spf13/pflag" | ||
| ) | ||
|
|
||
| var completionsASNBulk = &complete.Command{ | ||
| Flags: map[string]complete.Predictor{ | ||
| "-t": predict.Nothing, | ||
| "--token": predict.Nothing, | ||
| "--nocache": predict.Nothing, | ||
| "-h": predict.Nothing, | ||
| "--help": predict.Nothing, | ||
| "-f": predict.Set(asnFields), | ||
| "--field": predict.Set(asnFields), | ||
| "-j": predict.Nothing, | ||
| "--json": predict.Nothing, | ||
| }, | ||
| } | ||
|
|
||
| // printHelpASNBulk prints the help message for the asn bulk command. | ||
| func printHelpASNBulk() { | ||
| fmt.Printf( | ||
| `Usage: %s asn bulk [<opts>] <ASNs | filepath> | ||
|
|
||
| Description: | ||
| Accepts ASNs and file paths. | ||
|
|
||
| Examples: | ||
| # Lookup all ASNs in multiple files. | ||
| $ %[1]s asn bulk /path/to/asnlist1.txt /path/to/asnlist2.txt | ||
|
|
||
| # Lookup multiple ASNs. | ||
| $ %[1]s asn bulk AS123 AS456 AS789 | ||
|
|
||
| # Lookup ASNs from multiple sources simultaneously. | ||
| $ %[1]s asn bulk AS123 AS456 AS789 asns.txt | ||
|
|
||
| Options: | ||
| General: | ||
| --token <tok>, -t <tok> | ||
| use <tok> as API token. | ||
| --nocache | ||
| do not use the cache. | ||
| --help, -h | ||
| show help. | ||
|
|
||
| Outputs: | ||
| --field <field>, -f <field> | ||
| lookup only specific fields in the output. | ||
| field names correspond to JSON keys, e.g. 'name' or 'registry'. | ||
| multiple field names must be separated by commas. | ||
|
|
||
| Formats: | ||
| --json, -j | ||
| output JSON format. (default) | ||
| --yaml, -y | ||
| output YAML format. | ||
| `, progBase) | ||
| } | ||
|
|
||
| // cmdASNBulk is the asn bulk command. | ||
| func cmdASNBulk(piped bool) error { | ||
| f := lib.CmdASNBulkFlags{} | ||
| f.Init() | ||
| pflag.Parse() | ||
|
|
||
| ii = prepareIpinfoClient(f.Token) | ||
| var args []string | ||
| if !piped { | ||
| args = pflag.Args()[2:] | ||
| } | ||
|
|
||
| data, err := lib.CmdASNBulk(f, ii, args, printHelpASNBulk) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if (data) == nil { | ||
| return nil | ||
| } | ||
|
|
||
| if len(f.Field) > 0 { | ||
| return outputFieldBatchASNDetails(data, f.Field, false, false) | ||
| } | ||
|
|
||
| if f.Yaml { | ||
| return outputYAML(data) | ||
| } | ||
|
|
||
| return outputJSON(data) | ||
| } |
This file contains hidden or 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,111 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "net/http" | ||
|
|
||
| "github.com/fatih/color" | ||
| "github.com/ipinfo/cli/lib/complete" | ||
| "github.com/ipinfo/cli/lib/complete/predict" | ||
| "github.com/ipinfo/go/v2/ipinfo" | ||
| "github.com/spf13/pflag" | ||
| ) | ||
|
|
||
| var completionsASNSingle = &complete.Command{ | ||
| Flags: map[string]complete.Predictor{ | ||
| "-t": predict.Nothing, | ||
| "--token": predict.Nothing, | ||
| "--nocache": predict.Nothing, | ||
| "-h": predict.Nothing, | ||
| "--help": predict.Nothing, | ||
| "-f": predict.Set(asnFields), | ||
| "--field": predict.Set(asnFields), | ||
| "--nocolor": predict.Nothing, | ||
| "-p": predict.Nothing, | ||
| "--pretty": predict.Nothing, | ||
| "-j": predict.Nothing, | ||
| "--json": predict.Nothing, | ||
| }, | ||
| } | ||
|
|
||
| func printHelpASNSingle(asn string) { | ||
| fmt.Printf( | ||
| `Usage: %s %s [<opts>] | ||
|
|
||
| Options: | ||
| General: | ||
| --token <tok>, -t <tok> | ||
| use <tok> as API token. | ||
| --nocache | ||
| do not use the cache. | ||
| --help, -h | ||
| show help. | ||
|
|
||
| Outputs: | ||
| --field <field>, -f <field> | ||
| lookup only specific fields in the output. | ||
| field names correspond to JSON keys, e.g. 'registry' or 'allocated'. | ||
| multiple field names must be separated by commas. | ||
| --nocolor | ||
| disable colored output. | ||
|
|
||
| Formats: | ||
| --json, -j | ||
| output JSON format. (default) | ||
| --yaml, -y | ||
| output YAML format. | ||
| `, progBase, asn) | ||
| } | ||
|
|
||
| func cmdASNSingle(asn string) error { | ||
| var fTok string | ||
| var fField []string | ||
| var fJSON bool | ||
| var fYAML bool | ||
|
|
||
| pflag.StringVarP(&fTok, "token", "t", "", "the token to use.") | ||
| pflag.BoolVar(&fNoCache, "nocache", false, "disable the cache.") | ||
| pflag.BoolVarP(&fHelp, "help", "h", false, "show help.") | ||
| pflag.StringSliceVarP(&fField, "field", "f", nil, "specific field to lookup.") | ||
| pflag.BoolVarP(&fJSON, "json", "j", true, "output JSON format. (default)") | ||
| pflag.BoolVarP(&fYAML, "yaml", "y", false, "output YAML format.") | ||
| pflag.BoolVar(&fNoColor, "nocolor", false, "disable color output.") | ||
| pflag.Parse() | ||
|
|
||
| if fNoColor { | ||
| color.NoColor = true | ||
| } | ||
|
|
||
| if fHelp { | ||
| printHelpASNSingle(asn) | ||
| return nil | ||
| } | ||
|
|
||
| ii = prepareIpinfoClient(fTok) | ||
|
|
||
| // require token for ASN API. | ||
| if ii.Token == "" { | ||
| return errors.New("ASN lookups require a token; login via `ipinfo init`.") | ||
| } | ||
|
|
||
| data, err := ii.GetASNDetails(asn) | ||
| if err != nil { | ||
| iiErr, ok := err.(*ipinfo.ErrorResponse) | ||
| if ok && (iiErr.Response.StatusCode == http.StatusUnauthorized) { | ||
| return errors.New("Token does not have access to ASN API") | ||
| } | ||
| return err | ||
| } | ||
|
|
||
| if len(fField) > 0 { | ||
| d := make(ipinfo.BatchASNDetails, 1) | ||
| d[data.ASN] = data | ||
| return outputFieldBatchASNDetails(d, fField, false, false) | ||
| } | ||
| if fYAML { | ||
| return outputYAML(data) | ||
| } | ||
|
|
||
| return outputJSON(data) | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.