Skip to content

Commit

Permalink
refactor: use std slices, maps package
Browse files Browse the repository at this point in the history
  • Loading branch information
MaineK00n committed Oct 4, 2024
1 parent d5982a2 commit df98009
Show file tree
Hide file tree
Showing 19 changed files with 43 additions and 38 deletions.
3 changes: 2 additions & 1 deletion contrib/future-vuls/pkg/cpe/cpe.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import (
"context"
"fmt"
"os"
"slices"
"time"

"github.com/BurntSushi/toml"

"github.com/future-architect/vuls/contrib/future-vuls/pkg/config"
"github.com/future-architect/vuls/contrib/future-vuls/pkg/fvuls"
"golang.org/x/exp/slices"
)

// AddCpeConfig ...
Expand Down
7 changes: 5 additions & 2 deletions contrib/snmp2cpe/pkg/util/util.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package util

import "golang.org/x/exp/maps"
import (
"maps"
"slices"
)

// Unique return unique elements
func Unique[T comparable](s []T) []T {
m := map[T]struct{}{}
for _, v := range s {
m[v] = struct{}{}
}
return maps.Keys(m)
return slices.Collect(maps.Keys(m))
}
2 changes: 1 addition & 1 deletion detector/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ package detector
import (
"fmt"
"os"
"slices"
"strings"
"time"

"golang.org/x/exp/slices"
"golang.org/x/xerrors"

"github.com/future-architect/vuls/config"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ require (
github.com/vulsio/gost v0.5.0
github.com/vulsio/goval-dictionary v0.11.0
go.etcd.io/bbolt v1.3.11
golang.org/x/exp v0.0.0-20240823005443-9b4947da3948
golang.org/x/oauth2 v0.23.0
golang.org/x/sync v0.8.0
golang.org/x/text v0.18.0
Expand Down Expand Up @@ -358,6 +357,7 @@ require (
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/exp v0.0.0-20240823005443-9b4947da3948 // indirect
golang.org/x/mod v0.20.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/sys v0.25.0 // indirect
Expand Down
12 changes: 6 additions & 6 deletions gost/debian.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"cmp"
"encoding/json"
"fmt"
"maps"
"slices"
"strings"

debver "github.com/knqyf263/go-deb-version"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
"golang.org/x/xerrors"

"github.com/future-architect/vuls/constant"
Expand Down Expand Up @@ -111,7 +111,7 @@ func (deb Debian) detectCVEsWithFixState(r *models.ScanResult, fixed bool) ([]st
for _, s := range append(strings.Split(content.cveContent.Cvss3Severity, "|"), strings.Split(c.cveContent.Cvss3Severity, "|")...) {
m[s] = struct{}{}
}
ss := maps.Keys(m)
ss := slices.Collect(maps.Keys(m))
slices.SortFunc(ss, deb.CompareSeverity)
severty := strings.Join(ss, "|")
content.cveContent.Cvss2Severity = severty
Expand Down Expand Up @@ -156,7 +156,7 @@ func (deb Debian) detectCVEsWithFixState(r *models.ScanResult, fixed bool) ([]st
for _, s := range append(strings.Split(content.cveContent.Cvss3Severity, "|"), strings.Split(c.cveContent.Cvss3Severity, "|")...) {
m[s] = struct{}{}
}
ss := maps.Keys(m)
ss := slices.Collect(maps.Keys(m))
slices.SortFunc(ss, deb.CompareSeverity)
severty := strings.Join(ss, "|")
content.cveContent.Cvss2Severity = severty
Expand Down Expand Up @@ -192,7 +192,7 @@ func (deb Debian) detectCVEsWithFixState(r *models.ScanResult, fixed bool) ([]st
r.ScannedCves[content.cveContent.CveID] = v
}

return maps.Keys(detects), nil
return slices.Collect(maps.Keys(detects)), nil
}

func (deb Debian) detect(cves map[string]gostmodels.DebianCVE, srcPkg models.SrcPackage, runningKernel models.Kernel) []cveContent {
Expand Down Expand Up @@ -268,7 +268,7 @@ func (deb Debian) ConvertToModel(cve *gostmodels.DebianCVE) *models.CveContent {
m[r.Urgency] = struct{}{}
}
}
ss := maps.Keys(m)
ss := slices.Collect(maps.Keys(m))
slices.SortFunc(ss, deb.CompareSeverity)
severity := strings.Join(ss, "|")

Expand Down
3 changes: 1 addition & 2 deletions gost/debian_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ package gost
import (
"cmp"
"reflect"
"slices"
"testing"

"golang.org/x/exp/slices"

"github.com/future-architect/vuls/models"
gostmodels "github.com/vulsio/gost/models"
)
Expand Down
8 changes: 4 additions & 4 deletions gost/microsoft.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ package gost
import (
"encoding/json"
"fmt"
"maps"
"net/http"
"slices"
"strconv"
"strings"
"time"

"github.com/cenkalti/backoff"
"github.com/hashicorp/go-version"
"github.com/parnurzeal/gorequest"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
"golang.org/x/xerrors"

"github.com/future-architect/vuls/logging"
Expand Down Expand Up @@ -137,7 +137,7 @@ func (ms Microsoft) DetectCVEs(r *models.ScanResult, _ bool) (nCVEs int, err err
default:
}
}
filtered = unique(append(filtered, maps.Keys(m)...))
filtered = unique(append(filtered, slices.Collect(maps.Keys(m))...))

var cves map[string]gostmodels.MicrosoftCVE
if ms.driver == nil {
Expand Down Expand Up @@ -294,7 +294,7 @@ func (ms Microsoft) DetectCVEs(r *models.ScanResult, _ bool) (nCVEs int, err err
CveContents: models.NewCveContents(*cveCont),
Mitigations: mitigations,
AffectedPackages: stats,
WindowsKBFixedIns: maps.Keys(uniqKB),
WindowsKBFixedIns: slices.Collect(maps.Keys(uniqKB)),
}
}
return nCVEs, nil
Expand Down
4 changes: 2 additions & 2 deletions gost/ubuntu.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ package gost
import (
"encoding/json"
"fmt"
"maps"
"slices"
"strings"

debver "github.com/knqyf263/go-deb-version"
"golang.org/x/exp/maps"
"golang.org/x/xerrors"

"github.com/future-architect/vuls/constant"
Expand Down Expand Up @@ -213,7 +213,7 @@ func (ubu Ubuntu) detectCVEsWithFixState(r *models.ScanResult, fixed bool) ([]st
r.ScannedCves[content.cveContent.CveID] = v
}

return maps.Keys(detects), nil
return slices.Collect(maps.Keys(detects)), nil
}

func (ubu Ubuntu) detect(cves map[string]gostmodels.UbuntuCVE, fixed bool, srcPkg models.SrcPackage) []cveContent {
Expand Down
5 changes: 3 additions & 2 deletions gost/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
package gost

import (
"maps"
"net/http"
"slices"
"strings"
"time"

"github.com/cenkalti/backoff"
"github.com/parnurzeal/gorequest"
"golang.org/x/exp/maps"
"golang.org/x/xerrors"

"github.com/future-architect/vuls/logging"
Expand Down Expand Up @@ -191,5 +192,5 @@ func unique[T comparable](s []T) []T {
for _, v := range s {
m[v] = struct{}{}
}
return maps.Keys(m)
return slices.Collect(maps.Keys(m))
}
5 changes: 2 additions & 3 deletions models/cvecontents.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package models
import (
"cmp"
"fmt"
"maps"
"slices"
"strings"
"time"

"golang.org/x/exp/maps"

"github.com/future-architect/vuls/constant"
)

Expand Down Expand Up @@ -218,7 +217,7 @@ func (v CveContents) UniqCweIDs(myFamily string) []CveContentStr {
for _, cwes := range v.CweIDs(myFamily) {
uniq[cwes.Value] = cwes
}
return maps.Values(uniq)
return slices.Collect(maps.Values(uniq))
}

// CveContentSSVC has CveContentType and SSVC
Expand Down
2 changes: 1 addition & 1 deletion models/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"bytes"
"fmt"
"regexp"
"slices"
"strconv"
"strings"

"golang.org/x/exp/slices"
"golang.org/x/xerrors"

"github.com/future-architect/vuls/constant"
Expand Down
2 changes: 1 addition & 1 deletion oval/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"net/http"
"regexp"
"slices"
"sort"
"strconv"
"strings"
Expand All @@ -18,7 +19,6 @@ import (
debver "github.com/knqyf263/go-deb-version"
rpmver "github.com/knqyf263/go-rpm-version"
"github.com/parnurzeal/gorequest"
"golang.org/x/exp/slices"
"golang.org/x/xerrors"

"github.com/future-architect/vuls/config"
Expand Down
5 changes: 3 additions & 2 deletions reporter/sbom/cyclonedx.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package sbom
import (
"bytes"
"fmt"
"maps"
"slices"
"strconv"
"strings"
"time"

cdx "github.com/CycloneDX/cyclonedx-go"
"github.com/google/uuid"
"github.com/package-url/packageurl-go"
"golang.org/x/exp/maps"
"golang.org/x/xerrors"

"github.com/future-architect/vuls/constant"
Expand Down Expand Up @@ -560,7 +561,7 @@ func cdxCWEs(cveContents models.CveContents) *[]int {
}
}
}
cweIDs := maps.Keys(m)
cweIDs := slices.Collect(maps.Keys(m))
return &cweIDs
}

Expand Down
2 changes: 1 addition & 1 deletion reporter/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"path/filepath"
"reflect"
"slices"
"sort"
"strings"
"time"
Expand All @@ -20,7 +21,6 @@ import (
"github.com/future-architect/vuls/models"
"github.com/gosuri/uitable"
"github.com/olekukonko/tablewriter"
"golang.org/x/exp/slices"
"golang.org/x/xerrors"
)

Expand Down
5 changes: 3 additions & 2 deletions scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package scanner

import (
"fmt"
"maps"
"math/rand"
"net/http"
"os"
ex "os/exec"
"path/filepath"
"runtime"
"slices"
"strings"
"time"

"golang.org/x/exp/maps"
"golang.org/x/xerrors"

"github.com/future-architect/vuls/cache"
Expand Down Expand Up @@ -207,7 +208,7 @@ func ViaHTTP(header http.Header, body string, toLocalFile bool) (models.ScanResu
RunningKernel: models.Kernel{
Version: kernelVersion,
},
WindowsKB: &models.WindowsKB{Applied: maps.Keys(applied), Unapplied: maps.Keys(unapplied)},
WindowsKB: &models.WindowsKB{Applied: slices.Collect(maps.Keys(applied)), Unapplied: slices.Collect(maps.Keys(unapplied))},
ScannedCves: models.VulnInfos{},
}, nil
default:
Expand Down
3 changes: 1 addition & 2 deletions scanner/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import (
"net/http"
"os"
"reflect"
"slices"
"testing"

"golang.org/x/exp/slices"

"github.com/future-architect/vuls/config"
"github.com/future-architect/vuls/constant"
"github.com/future-architect/vuls/models"
Expand Down
7 changes: 4 additions & 3 deletions scanner/windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package scanner
import (
"bufio"
"fmt"
"maps"
"net"
"regexp"
"slices"
"strconv"
"strings"

"golang.org/x/exp/maps"
"golang.org/x/xerrors"

"github.com/future-architect/vuls/config"
Expand Down Expand Up @@ -1199,7 +1200,7 @@ func (w *windows) scanKBs() (*models.WindowsKB, error) {
unapplied[kb] = struct{}{}
}

return &models.WindowsKB{Applied: maps.Keys(applied), Unapplied: maps.Keys(unapplied)}, nil
return &models.WindowsKB{Applied: slices.Collect(maps.Keys(applied)), Unapplied: slices.Collect(maps.Keys(unapplied))}, nil
}

func (w *windows) parseGetHotfix(stdout string) ([]string, error) {
Expand Down Expand Up @@ -1305,7 +1306,7 @@ func (w *windows) parseWindowsUpdateHistory(stdout string) ([]string, error) {
}
}

return maps.Keys(kbs), nil
return slices.Collect(maps.Keys(kbs)), nil
}

type windowsRelease struct {
Expand Down
2 changes: 1 addition & 1 deletion scanner/windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package scanner

import (
"reflect"
"slices"
"testing"

"github.com/future-architect/vuls/config"
"github.com/future-architect/vuls/models"
"golang.org/x/exp/slices"
)

func Test_parseSystemInfo(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"bytes"
"fmt"
"os"
"slices"
"sort"
"strings"
"text/template"
"time"

"golang.org/x/exp/slices"
"golang.org/x/xerrors"

"github.com/future-architect/vuls/config"
Expand Down

0 comments on commit df98009

Please sign in to comment.