@@ -3,33 +3,34 @@ package snmp
33import (
44 "fmt"
55 "io"
6+ "maps"
67 "os"
78
89 "github.com/gosnmp/gosnmp"
910)
1011
11- // Provides a GoSNMP like data interface, but with data from a snmpwalk output
12+ // FileHandler provides a GoSNMP like data interface, but with data from a snmpwalk output
1213//
1314// Anyone can generate an output by running:
1415//
1516// snmpwalk -c public -v2c -On HOST .1.3.6.1 >snmp-data.txt
1617//
1718// Warning: This does not implement all functions of gosnmp.Handler
1819type FileHandler struct {
19- Data WalkData
20-
2120 gosnmp.Handler
21+
22+ Data WalkData
2223}
2324
24- // Create a new file handler and initialize it with data from an io.Reader
25+ // NewFileHandler creates a new file handler and initialize it with data from an io.Reader
2526func NewFileHandler (r io.Reader ) (h * FileHandler , err error ) {
2627 h = & FileHandler {}
2728 err = h .ReadFromWalk (r )
2829
2930 return
3031}
3132
32- // Create a new file handler and initialize it with data by reading from a file
33+ // NewFileHandlerFromFile creates a new file handler and initialize it with data by reading from a file
3334func NewFileHandlerFromFile (filePath string ) (h * FileHandler , err error ) {
3435 fh , err := os .Open (filePath )
3536 if err != nil {
@@ -42,7 +43,7 @@ func NewFileHandlerFromFile(filePath string) (h *FileHandler, err error) {
4243 return NewFileHandler (fh )
4344}
4445
45- // Read data from a io.Reader and parse it for PDUs
46+ // ReadFromWalk reads data from a io.Reader and parse it for PDUs
4647//
4748// They will be stored in Data for later use.
4849func (h * FileHandler ) ReadFromWalk (r io.Reader ) error {
@@ -55,34 +56,32 @@ func (h *FileHandler) ReadFromWalk(r io.Reader) error {
5556 return err
5657 }
5758
58- for k , v := range pduList {
59- h .Data [k ] = v
60- }
59+ maps .Copy (h .Data , pduList )
6160
6261 return nil
6362}
6463
65- // Simulate Connect behavior by returning no error
64+ // Connect simulates Connect behavior by returning no error
6665func (h * FileHandler ) Connect () error {
6766 return nil
6867}
6968
70- // Simulate ConnectIPv4 behavior by returning no error
69+ // ConnectIPv4 simulates ConnectIPv4 behavior by returning no error
7170func (h * FileHandler ) ConnectIPv4 () error {
7271 return nil
7372}
7473
75- // Simulate ConnectIPv6 behavior by returning no error
74+ // ConnectIPv6 simulates ConnectIPv6 behavior by returning no error
7675func (h * FileHandler ) ConnectIPv6 () error {
7776 return nil
7877}
7978
80- // Simulate Close behavior by returning no error
79+ // Close simulates Close behavior by returning no error
8180func (h * FileHandler ) Close () error {
8281 return nil
8382}
8483
85- // Simulating Get() behavior by searching read in data
84+ // Get simulates Get() behavior by searching read in data
8685func (h * FileHandler ) Get (oids []string ) (result * gosnmp.SnmpPacket , err error ) {
8786 result = & gosnmp.SnmpPacket {
8887 Version : gosnmp .Version2c ,
@@ -102,18 +101,18 @@ func (h *FileHandler) Get(oids []string) (result *gosnmp.SnmpPacket, err error)
102101 return
103102}
104103
105- // Not yet implemented
104+ // GetBulk is not yet implemented
106105func (h * FileHandler ) GetBulk (oids []string , nonRepeaters uint8 ,
107106 maxRepetitions uint32 ) (result * gosnmp.SnmpPacket , err error ) {
108107 panic ("not implemented" )
109108}
110109
111- // Not yet implemented
110+ // GetNext is not yet implemented
112111func (h * FileHandler ) GetNext (oids []string ) (result * gosnmp.SnmpPacket , err error ) {
113112 panic ("not implemented" )
114113}
115114
116- // Simulating Walk() behavior by searching read in data
115+ // Walk simulates Walk() behavior by searching read in data
117116func (h * FileHandler ) Walk (rootOid string , walkFn gosnmp.WalkFunc ) (err error ) {
118117 rootOid , err = EnsureValidOid (rootOid )
119118 if err != nil {
@@ -134,17 +133,17 @@ func (h *FileHandler) Walk(rootOid string, walkFn gosnmp.WalkFunc) (err error) {
134133 return
135134}
136135
137- // Not yet implemented
136+ // WalkAll is not yet implemented
138137func (h * FileHandler ) WalkAll (rootOid string ) (results []gosnmp.SnmpPDU , err error ) {
139138 panic ("not implemented" )
140139}
141140
142- // Not yet implemented
141+ // BulkWalk is not yet implemented
143142func (h * FileHandler ) BulkWalk (rootOid string , walkFn gosnmp.WalkFunc ) error {
144143 panic ("not implemented" )
145144}
146145
147- // Not yet implemented
146+ // BulkWalkAll is not yet implemented
148147func (h * FileHandler ) BulkWalkAll (rootOid string ) (results []gosnmp.SnmpPDU , err error ) {
149148 panic ("not implemented" )
150149}
0 commit comments