MCPcopy
hub / github.com/crowdsecurity/crowdsec / import_

Method import_

cmd/crowdsec-cli/clidecision/import.go:84–257  ·  view source on GitHub ↗
(ctx context.Context, input string, duration string, scope string, reason string, type_ string, batch int, format string)

Source from the content-addressed store, hash-verified

82}
83
84func (cli *cliDecisions) import_(ctx context.Context, input string, duration string, scope string, reason string, type_ string, batch int, format string) error {
85 var (
86 content []byte
87 fin *os.File
88 err error
89 )
90
91 if duration == "" {
92 return errors.New("default duration cannot be empty")
93 }
94
95 if scope == "" {
96 return errors.New("default scope cannot be empty")
97 }
98
99 if reason == "" {
100 return errors.New("default reason cannot be empty")
101 }
102
103 if type_ == "" {
104 return errors.New("default type cannot be empty")
105 }
106
107 // set format if the file has a json or csv extension
108 if format == "" {
109 if strings.HasSuffix(input, ".json") {
110 format = "json"
111 } else if strings.HasSuffix(input, ".csv") {
112 format = "csv"
113 }
114 }
115
116 if format == "" {
117 return errors.New("unable to guess format from file extension, please provide a format with --format flag")
118 }
119
120 if input == "-" {
121 fin = os.Stdin
122 input = "stdin"
123 } else {
124 fin, err = os.Open(input)
125 if err != nil {
126 return fmt.Errorf("unable to open %s: %w", input, err)
127 }
128 }
129
130 content, err = io.ReadAll(fin)
131 if err != nil {
132 return fmt.Errorf("unable to read from %s: %w", input, err)
133 }
134
135 decisionsListRaw, err := parseDecisionList(content, format)
136 if err != nil {
137 return err
138 }
139
140 if len(decisionsListRaw) == 0 {
141 return errors.New("no decisions found")

Callers 1

newImportCmdMethod · 0.95

Calls 8

NormalizeScopeFunction · 0.92
parseDecisionListFunction · 0.85
OpenMethod · 0.80
FormatMethod · 0.45
StringMethod · 0.45
AddMethod · 0.45

Tested by

no test coverage detected