357afc54d8
* initial commit * chore(mod): update * test(gemnasium): add tests * feat(main): add target * change(GitLab Advisories Database) gemnasium to advisories community * fix(GitLab Advisory Database) Identifer to upper case * test(GitLab Advisory Database) add lower case identifer test * fix(GitLab Advisory Database) no newline at end of file * fix(GitLab Advisory Database) fix test use JSONEq * fix(GitLab Advisory Database) fix clone community advisory branch name * change(git) CloneOrPull use default branch * refactor(glad) Co-authored-by: masahiro331 <mur4m4s4.331@gmail.com>
34 lines
732 B
Go
34 lines
732 B
Go
package utils
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/spf13/afero"
|
|
"golang.org/x/xerrors"
|
|
)
|
|
|
|
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)
|
|
}
|
|
|
|
filePath := filepath.Join(dir, fileName)
|
|
f, err := fs.Create(filePath)
|
|
if err != nil {
|
|
return xerrors.Errorf("unable to open %s: %w", filePath, err)
|
|
}
|
|
defer f.Close()
|
|
|
|
b, err := json.MarshalIndent(data, "", " ")
|
|
if err != nil {
|
|
return xerrors.Errorf("failed to marshal JSON: %w", err)
|
|
}
|
|
|
|
if _, err = f.Write(b); err != nil {
|
|
return xerrors.Errorf("failed to save a file: %w", err)
|
|
}
|
|
return nil
|
|
}
|