vet/pkg/analyzer/json_dump.go
Abhisek Datta a18c204b5d
Sync Develop to Main (#4)
* Update Insight service API and client

* Add cli banner

* Show API errors from insight API

* Use standard error model

* Add reporting interface

* Update markdown template

* Add trials registration client

* Add trials registration support

* Add supported ecosystem filter to parsers

* Update OSV scanner

* Use table renderer for CEL filter output

* Rename filter opt to filter

* Add an opinionated console summary reporter

* Update README

* Update README

* Update README

* Add filter spec

* Update spec driven CEL filtering

* Add query workflow with docs

* Add secrets scan workflow
2023-02-03 12:30:48 +05:30

59 lines
1.2 KiB
Go

package analyzer
import (
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"time"
"github.com/safedep/vet/pkg/common/logger"
"github.com/safedep/vet/pkg/models"
)
type jsonDumperAnalyzer struct {
dir string
}
func NewJsonDumperAnalyzer(dir string) (Analyzer, error) {
fi, err := os.Stat(dir)
if err != nil {
if os.IsNotExist(err) {
err = os.MkdirAll(dir, os.ModePerm)
if err != nil {
return nil, fmt.Errorf("cannot create dir: %w", err)
}
} else {
return nil, fmt.Errorf("cannot stat dir: %w", err)
}
} else if !fi.IsDir() {
return nil, fmt.Errorf("%s is not a dir", dir)
}
return &jsonDumperAnalyzer{dir: dir}, nil
}
func (j *jsonDumperAnalyzer) Name() string {
return "JSON Dump Generator"
}
func (j *jsonDumperAnalyzer) Analyze(manifest *models.PackageManifest,
handler AnalyzerEventHandler) error {
logger.Infof("Running analyzer: %s", j.Name())
data, err := json.MarshalIndent(manifest, "", " ")
if err != nil {
return fmt.Errorf("Failed to JSON serialize manifest: %w", err)
}
rand.Seed(time.Now().UnixNano())
path := filepath.Join(j.dir, fmt.Sprintf("%s-%s--%d-dump.json",
manifest.Ecosystem,
filepath.Base(manifest.Path),
rand.Intn(2<<15)))
return ioutil.WriteFile(path, data, 0600)
}