Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sundowndev
GitHub Repository: sundowndev/phoneinfoga
Path: blob/master/cmd/scan.go
988 views
1
package cmd
2
3
import (
4
"errors"
5
"fmt"
6
"github.com/fatih/color"
7
"github.com/joho/godotenv"
8
"github.com/sirupsen/logrus"
9
"github.com/spf13/cobra"
10
"github.com/sundowndev/phoneinfoga/v2/lib/filter"
11
"github.com/sundowndev/phoneinfoga/v2/lib/number"
12
"github.com/sundowndev/phoneinfoga/v2/lib/output"
13
"github.com/sundowndev/phoneinfoga/v2/lib/remote"
14
)
15
16
type ScanCmdOptions struct {
17
Number string
18
DisabledScanners []string
19
PluginPaths []string
20
EnvFiles []string
21
}
22
23
func init() {
24
// Register command
25
opts := &ScanCmdOptions{}
26
cmd := NewScanCmd(opts)
27
rootCmd.AddCommand(cmd)
28
29
// Register flags
30
cmd.PersistentFlags().StringVarP(&opts.Number, "number", "n", "", "The phone number to scan (E164 or international format)")
31
cmd.PersistentFlags().StringArrayVarP(&opts.DisabledScanners, "disable", "D", []string{}, "Scanner to skip for this scan")
32
cmd.PersistentFlags().StringArrayVar(&opts.PluginPaths, "plugin", []string{}, "Extra scanner plugin to use for the scan")
33
cmd.PersistentFlags().StringSliceVar(&opts.EnvFiles, "env-file", []string{}, "Env files to parse environment variables from (looks for .env by default)")
34
// scanCmd.PersistentFlags().StringVarP(&input, "input", "i", "", "Text file containing a list of phone numbers to scan (one per line)")
35
// scanCmd.PersistentFlags().StringVarP(&output, "output", "o", "", "Output to save scan results")
36
}
37
38
func NewScanCmd(opts *ScanCmdOptions) *cobra.Command {
39
return &cobra.Command{
40
Use: "scan",
41
Short: "Scan a phone number",
42
Args: cobra.NoArgs,
43
Run: func(cmd *cobra.Command, args []string) {
44
err := godotenv.Load(opts.EnvFiles...)
45
if err != nil {
46
logrus.WithField("error", err).Debug("Error loading .env file")
47
}
48
49
runScan(opts)
50
},
51
}
52
}
53
54
func runScan(opts *ScanCmdOptions) {
55
fmt.Fprintf(color.Output, color.WhiteString("Running scan for phone number %s...\n\n"), opts.Number)
56
57
if valid := number.IsValid(opts.Number); !valid {
58
logrus.WithFields(map[string]interface{}{
59
"input": opts.Number,
60
"valid": valid,
61
}).Debug("Input phone number is invalid")
62
exitWithError(errors.New("given phone number is not valid"))
63
}
64
65
num, err := number.NewNumber(opts.Number)
66
if err != nil {
67
exitWithError(err)
68
}
69
70
for _, p := range opts.PluginPaths {
71
err := remote.OpenPlugin(p)
72
if err != nil {
73
exitWithError(err)
74
}
75
}
76
77
f := filter.NewEngine()
78
f.AddRule(opts.DisabledScanners...)
79
80
remoteLibrary := remote.NewLibrary(f)
81
remote.InitScanners(remoteLibrary)
82
83
// Scanner options are currently not used in CLI
84
result, errs := remoteLibrary.Scan(num, remote.ScannerOptions{})
85
86
err = output.GetOutput(output.Console, color.Output).Write(result, errs)
87
if err != nil {
88
exitWithError(err)
89
}
90
}
91
92