refactor: replace a method to a function (#67)

This commit is contained in:
Teppei Fukuda 2021-01-04 14:44:37 +02:00 committed by GitHub
parent 6637c53214
commit 5206d8df5c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 32 additions and 72 deletions

View File

@ -6,7 +6,6 @@ import (
"encoding/xml"
"fmt"
"log"
"os"
"path/filepath"
"regexp"
"strings"
@ -124,14 +123,8 @@ func (c Config) saveCvrfPerYear(dirName string, cvrfID string, data interface{})
}
yearDir := filepath.Join(c.VulnListDir, dirName, year)
if err := c.AppFs.MkdirAll(yearDir, os.ModePerm); err != nil {
return xerrors.Errorf("failed to create directory: %w", err)
}
filePath := filepath.Join(yearDir, fmt.Sprintf("%s.json", strings.Replace(cvrfID, ":", "-", 1)))
fs := utils.NewFs(c.AppFs)
if err := fs.WriteJSON(filePath, data); err != nil {
fileName := fmt.Sprintf("%s.json", strings.Replace(cvrfID, ":", "-", 1))
if err := utils.WriteJSON(c.AppFs, yearDir, fileName, data); err != nil {
return xerrors.Errorf("failed to write file: %w", err)
}
return nil

View File

@ -169,13 +169,8 @@ func (c Config) fetchGithubSecurityAdvisories(ecosystem SecurityAdvisoryEcosyste
}
func (c Config) saveGSHA(dirName string, ghsaID string, data interface{}) error {
filePath := filepath.Join(dirName, fmt.Sprintf("%s.json", ghsaID))
if err := c.appFs.MkdirAll(dirName, os.ModePerm); err != nil {
return xerrors.Errorf("failed to create directory: %w", err)
}
fs := utils.NewFs(c.appFs)
if err := fs.WriteJSON(filePath, data); err != nil {
fileName := fmt.Sprintf("%s.json", ghsaID)
if err := utils.WriteJSON(c.appFs, dirName, fileName, data); err != nil {
return xerrors.Errorf("failed to write file: %w", err)
}
return nil

View File

@ -360,7 +360,7 @@ func TestConfig_Update(t *testing.T) {
},
},
},
expectedErrorMsg: "failed to save github security advisory: failed to create directory: operation not permitted",
expectedErrorMsg: "unable to create a directory: operation not permitted",
},
}
for _, tc := range testCases {

View File

@ -6,7 +6,6 @@ import (
"encoding/xml"
"fmt"
"log"
"os"
"path/filepath"
"strings"
@ -88,14 +87,7 @@ func (c Config) saveELSAPerYear(dirName string, elsaID string, data interface{})
}
yearDir := filepath.Join(c.VulnListDir, dirName, s[1])
if err := c.AppFs.MkdirAll(yearDir, os.ModePerm); err != nil {
return xerrors.Errorf("failed to create directory: %w", err)
}
filePath := filepath.Join(yearDir, fmt.Sprintf("%s.json", elsaID))
fs := utils.NewFs(c.AppFs)
if err := fs.WriteJSON(filePath, data); err != nil {
if err := utils.WriteJSON(c.AppFs, yearDir, fmt.Sprintf("%s.json", elsaID), data); err != nil {
return xerrors.Errorf("failed to write file: %w", err)
}
return nil

View File

@ -65,7 +65,7 @@ func TestConfig_Update(t *testing.T) {
"/oval/com.oracle.elsa-all.xml.bz2": "testdata/all-positive-data.xml.bz2",
},
goldenFiles: map[string]string{},
expectedErrorMsg: "failed to save ELSAPerYear: failed to create directory: operation not permitted",
expectedErrorMsg: "unable to create a directory: operation not permitted",
},
{
name: "invalid title format",

View File

@ -105,17 +105,17 @@ func (c Config) update(ovalFile string) error {
dirPath := filepath.Join(c.VulnListDir, ovalDir, redhatDir, release, platform)
// write tests/tests.json file
if err := c.writeJSON(filepath.Join(dirPath, testsDir), "tests.json", ovalroot.Tests); err != nil {
if err := utils.WriteJSON(c.AppFs, filepath.Join(dirPath, testsDir), "tests.json", ovalroot.Tests); err != nil {
return xerrors.Errorf("failed to write tests: %w", err)
}
// write objects/objects.json file
if err := c.writeJSON(filepath.Join(dirPath, objectsDir), "objects.json", ovalroot.Objects); err != nil {
if err := utils.WriteJSON(c.AppFs, filepath.Join(dirPath, objectsDir), "objects.json", ovalroot.Objects); err != nil {
return xerrors.Errorf("failed to write objects: %w", err)
}
// write states/states.json file
if err := c.writeJSON(filepath.Join(dirPath, statesDir), "states.json", ovalroot.States); err != nil {
if err := utils.WriteJSON(c.AppFs, filepath.Join(dirPath, statesDir), "states.json", ovalroot.States); err != nil {
return xerrors.Errorf("failed to write states: %w", err)
}
@ -197,22 +197,8 @@ func (c Config) saveAdvisoryPerYear(dirName string, id string, def Definition) e
}
yearDir := filepath.Join(dirName, year)
if err := c.writeJSON(yearDir, fmt.Sprintf(fileFmt, id), def); err != nil {
if err := utils.WriteJSON(c.AppFs, yearDir, fmt.Sprintf(fileFmt, id), def); err != nil {
return xerrors.Errorf("unable to write a JSON file: %w", err)
}
return nil
}
func (c Config) writeJSON(dirName, fileName string, data interface{}) error {
if err := c.AppFs.MkdirAll(dirName, os.ModePerm); err != nil {
return xerrors.Errorf("failed to create a year dir: %w", err)
}
fs := utils.NewFs(c.AppFs)
filePath := filepath.Join(dirName, fileName)
if err := fs.WriteJSON(filePath, data); err != nil {
return xerrors.Errorf("failed to write file: %w", err)
}
return nil
}

View File

@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"strings"
@ -95,14 +94,8 @@ func (c Config) saveCVEPerPkg(dirName, pkgName, cveID string, data interface{})
}
pkgDir := filepath.Join(c.VulnListDir, dirName, pkgName)
if err := c.AppFs.MkdirAll(pkgDir, os.ModePerm); err != nil {
return xerrors.Errorf("failed to create dir: %w", err)
}
filePath := filepath.Join(pkgDir, fmt.Sprintf("%s.json", cveID))
fs := utils.NewFs(c.AppFs)
if err := fs.WriteJSON(filePath, data); err != nil {
fileName := fmt.Sprintf("%s.json", cveID)
if err := utils.WriteJSON(c.AppFs, pkgDir, fileName, data); err != nil {
return xerrors.Errorf("failed to write file: %w", err)
}
return nil

View File

@ -70,7 +70,7 @@ func TestConfig_Update(t *testing.T) {
"/photon_cve_metadata/cve_data_photon3.0.json": "testdata/cve_data_photon3.0.json",
},
goldenFiles: map[string]string{},
expectedErrorMsg: "failed to create dir: operation not permitted",
expectedErrorMsg: "unable to create a directory: operation not permitted",
},
{
name: "404",

View File

@ -2,22 +2,20 @@ package utils
import (
"encoding/json"
"golang.org/x/xerrors"
"os"
"path/filepath"
"github.com/spf13/afero"
"golang.org/x/xerrors"
)
type Fs struct {
AppFs afero.Fs
}
func WriteJSON(fs afero.Fs, dir, fileName string, data interface{}) error {
if err := fs.MkdirAll(dir, os.ModePerm); err != nil {
return xerrors.Errorf("unable to create a directory: %w", err)
}
func NewFs(appFs afero.Fs) Fs {
return Fs{AppFs: appFs}
}
func (fs Fs) WriteJSON(filePath string, data interface{}) error {
f, err := fs.AppFs.Create(filePath)
filePath := filepath.Join(dir, fileName)
f, err := fs.Create(filePath)
if err != nil {
return xerrors.Errorf("unable to open a file: %w", err)
}

View File

@ -1,4 +1,4 @@
package utils
package utils_test
import (
"errors"
@ -7,9 +7,12 @@ import (
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/aquasecurity/vuln-list-update/utils"
)
func TestFs_WriteJSON(t *testing.T) {
func TestWriteJSON(t *testing.T) {
testCases := []struct {
name string
fs afero.Fs
@ -29,7 +32,7 @@ func TestFs_WriteJSON(t *testing.T) {
{
name: "sad path: fs.AppFs.Create returns an error",
fs: afero.NewReadOnlyFs(afero.NewMemMapFs()),
expectedError: errors.New("unable to open a file: operation not permitted"),
expectedError: errors.New("unable to create a directory: operation not permitted"),
},
{
name: "sad path: bad json input data",
@ -40,17 +43,17 @@ func TestFs_WriteJSON(t *testing.T) {
}
for _, tc := range testCases {
fs := NewFs(tc.fs)
err := fs.WriteJSON("foo", tc.inputData)
err := utils.WriteJSON(tc.fs, "dir", "file", tc.inputData)
switch {
case tc.expectedError != nil:
require.NotNil(t, err)
assert.Equal(t, tc.expectedError.Error(), err.Error(), tc.name)
return
default:
assert.NoError(t, err, tc.name)
}
actual, err := afero.ReadFile(tc.fs, "foo")
actual, err := afero.ReadFile(tc.fs, "dir/file")
assert.NoError(t, err, tc.name)
assert.Equal(t, tc.expectedData, string(actual), tc.name)
}