Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error and exit when initialization fails #1674

Merged
merged 2 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/guaccollect/cmd/deps_dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ you have access to read and write to the respective blob store.`,
// Register collector
depsDevCollector, err := deps_dev.NewDepsCollector(ctx, opts.dataSource, opts.poll, opts.retrieveDependencies, 30*time.Second)
if err != nil {
logger.Errorf("unable to register oci collector: %v", err)
logger.Fatalf("unable to register oci collector: %v", err)
}
err = collector.RegisterDocumentCollector(depsDevCollector, deps_dev.DepsCollector)
if err != nil {
logger.Errorf("unable to register oci collector: %v", err)
logger.Fatalf("unable to register oci collector: %v", err)
}
if opts.enablePrometheus {
go func() {
Expand Down
10 changes: 4 additions & 6 deletions cmd/guaccollect/cmd/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ you have access to read and write to the respective blob store.`,
fileCollector := file.NewFileCollector(ctx, opts.path, opts.poll, 30*time.Second)
err = collector.RegisterDocumentCollector(fileCollector, file.FileCollector)
if err != nil {
logger.Errorf("unable to register file collector: %v", err)
logger.Fatalf("unable to register file collector: %v", err)
}

initializeNATsandCollector(ctx, opts.pubsubAddr, opts.blobAddr)
Expand Down Expand Up @@ -121,16 +121,15 @@ func initializeNATsandCollector(ctx context.Context, pubsubAddr string, blobAddr
// TODO: pass in credentials file for NATS secure login
jetStream := emitter.NewJetStream(pubsubAddr, "", "")
if err := jetStream.JetStreamInit(ctx); err != nil {
logger.Errorf("jetStream initialization failed with error: %v", err)
os.Exit(1)
logger.Fatalf("jetStream initialization failed with error: %v", err)
}
defer jetStream.Close()
}

// initialize blob store
blobStore, err := blob.NewBlobStore(ctx, blobAddr)
if err != nil {
logger.Errorf("unable to connect to blog store: %v", err)
logger.Fatalf("unable to connect to blog store: %v", err)
}

// initialize pubsub
Expand All @@ -147,8 +146,7 @@ func initializeNATsandCollector(ctx context.Context, pubsubAddr string, blobAddr
emit := func(d *processor.Document) error {
err = collectorPubFunc(d)
if err != nil {
logger.Errorf("error publishing document from collector: %v", err)
os.Exit(1)
logger.Fatalf("error publishing document from collector: %v", err)
}
return nil
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/guaccollect/cmd/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ you have access to read and write to the respective blob store.`,
// GITHUB_TOKEN is the default token name
ghc, err := githubclient.NewGithubClient(ctx, os.Getenv("GITHUB_TOKEN"))
if err != nil {
logger.Errorf("unable to create github client: %v", err)
logger.Fatalf("unable to create github client: %v", err)
}

// Register collector
Expand All @@ -129,11 +129,11 @@ you have access to read and write to the respective blob store.`,

if opts.ownerRepoName != "" {
if !strings.Contains(opts.ownerRepoName, "/") {
logger.Errorf("owner-repo flag must be in the format <owner>/<repo>")
logger.Fatalf("owner-repo flag must be in the format <owner>/<repo>")
} else {
ownerRepoName := strings.Split(opts.ownerRepoName, "/")
if len(ownerRepoName) != 2 {
logger.Errorf("owner-repo flag must be in the format <owner>/<repo>")
logger.Fatalf("owner-repo flag must be in the format <owner>/<repo>")
}
collectorOpts = append(collectorOpts, github.WithOwner(ownerRepoName[0]))
collectorOpts = append(collectorOpts, github.WithRepo(ownerRepoName[1]))
Expand All @@ -142,11 +142,11 @@ you have access to read and write to the respective blob store.`,

githubCollector, err := github.NewGithubCollector(collectorOpts...)
if err != nil {
logger.Errorf("unable to create Github collector: %v", err)
logger.Fatalf("unable to create Github collector: %v", err)
}
err = collector.RegisterDocumentCollector(githubCollector, github.GithubCollector)
if err != nil {
logger.Errorf("unable to register Github collector: %v", err)
logger.Fatalf("unable to register Github collector: %v", err)
}

initializeNATsandCollector(ctx, opts.pubsubAddr, opts.blobAddr)
Expand Down
2 changes: 1 addition & 1 deletion cmd/guaccollect/cmd/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ you have access to read and write to the respective blob store.`,
ociCollector := oci.NewOCICollector(ctx, opts.dataSource, opts.poll, 30*time.Second)
err = collector.RegisterDocumentCollector(ociCollector, oci.OCICollector)
if err != nil {
logger.Errorf("unable to register oci collector: %v", err)
logger.Fatalf("unable to register oci collector: %v", err)
}

initializeNATsandCollector(ctx, opts.pubsubAddr, opts.blobAddr)
Expand Down
5 changes: 2 additions & 3 deletions cmd/guacingest/cmd/ingest.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,15 @@ func ingest(cmd *cobra.Command, args []string) {
// TODO: pass in credentials file for NATS secure login
jetStream := emitter.NewJetStream(opts.pubsubAddr, "", "")
if err := jetStream.JetStreamInit(ctx); err != nil {
logger.Errorf("jetStream initialization failed with error: %v", err)
os.Exit(1)
logger.Fatalf("jetStream initialization failed with error: %v", err)
}
defer jetStream.Close()
}

// initialize blob store
blobStore, err := blob.NewBlobStore(ctx, opts.blobAddr)
if err != nil {
logger.Errorf("unable to connect to blog store: %v", err)
logger.Fatalf("unable to connect to blog store: %v", err)
}

// initialize pubsub
Expand Down
4 changes: 2 additions & 2 deletions cmd/guacone/cmd/deps_dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ var depsDevCmd = &cobra.Command{
// Register collector
depsDevCollector, err := deps_dev.NewDepsCollector(ctx, opts.dataSource, opts.poll, opts.retrieveDependencies, 30*time.Second)
if err != nil {
logger.Errorf("unable to register depsdev collector: %v", err)
logger.Fatalf("unable to register depsdev collector: %v", err)
}
if err := collector.RegisterDocumentCollector(depsDevCollector, deps_dev.DepsCollector); err != nil {
logger.Errorf("unable to register depsdev collector: %v", err)
logger.Fatalf("unable to register depsdev collector: %v", err)
}

totalNum := 0
Expand Down
12 changes: 5 additions & 7 deletions cmd/guacone/cmd/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,34 +81,32 @@ var filesCmd = &cobra.Command{
inmemory := inmemory.NewInmemoryProvider()
err = key.RegisterKeyProvider(inmemory, inmemory.Type())
if err != nil {
logger.Errorf("unable to register key provider: %v", err)
logger.Fatalf("unable to register key provider: %v", err)
}

if opts.keyPath != "" && opts.keyID != "" {
keyRaw, err := os.ReadFile(opts.keyPath)
if err != nil {
logger.Errorf("error: %v", err)
os.Exit(1)
logger.Fatalf("error: %v", err)
}
err = key.Store(ctx, opts.keyID, keyRaw, inmemory.Type())
if err != nil {
logger.Errorf("error: %v", err)
os.Exit(1)
logger.Fatalf("error: %v", err)
}
}

// Register Verifier
sigstoreAndKeyVerifier := sigstore_verifier.NewSigstoreAndKeyVerifier()
err = verifier.RegisterVerifier(sigstoreAndKeyVerifier, sigstoreAndKeyVerifier.Type())
if err != nil {
logger.Errorf("unable to register key provider: %v", err)
logger.Fatalf("unable to register key provider: %v", err)
}

// Register collector
fileCollector := file.NewFileCollector(ctx, opts.path, false, time.Second)
err = collector.RegisterDocumentCollector(fileCollector, file.FileCollector)
if err != nil {
logger.Errorf("unable to register file collector: %v", err)
logger.Fatalf("unable to register file collector: %v", err)
}

// initialize collectsub client
Expand Down
2 changes: 1 addition & 1 deletion cmd/guacone/cmd/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ var ociCmd = &cobra.Command{
ociCollector := oci.NewOCICollector(ctx, opts.dataSource, false, 10*time.Minute)
err = collector.RegisterDocumentCollector(ociCollector, oci.OCICollector)
if err != nil {
logger.Errorf("unable to register oci collector: %v", err)
logger.Fatalf("unable to register oci collector: %v", err)
}

// initialize collectsub client
Expand Down
3 changes: 1 addition & 2 deletions cmd/guacone/cmd/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ $ guacone collect s3 --s3-url http://localhost:9000 --s3-bucket guac-test --poll
})

if err := collector.RegisterDocumentCollector(s3Collector, s3.S3CollectorType); err != nil {
logger.Errorf("unable to register s3 collector: %v\n", err)
os.Exit(1)
logger.Fatalf("unable to register s3 collector: %v\n", err)
}

csubClient, err := csub_client.NewClient(s3Opts.csubClientOptions)
Expand Down
3 changes: 1 addition & 2 deletions internal/testing/cmd/ingest/cmd/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ func ingestExample(cmd *cobra.Command, args []string) {
// This is a test example, so we will ignore calling out to a collectsub service
input, _, err := parser.ParseDocumentTree(ctx, doc)
if err != nil {
logger.Errorf("unable to parse document: %v", err)
os.Exit(1)
logger.Fatalf("unable to parse document: %v", err)
}

// TODO(bulldozer): collate inputs
Expand Down
8 changes: 3 additions & 5 deletions internal/testing/cmd/pubsub_test/cmd/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,14 @@ func initializeNATsandCollector(ctx context.Context, pubsubAddr string, blobAddr
// TODO: pass in credentials file for NATS secure login
jetStream := emitter.NewJetStream(pubsubAddr, "", "")
if err := jetStream.JetStreamInit(ctx); err != nil {
logger.Errorf("jetStream initialization failed with error: %v", err)
os.Exit(1)
logger.Fatalf("jetStream initialization failed with error: %v", err)
}
defer jetStream.Close()
}

blobStore, err := blob.NewBlobStore(ctx, blobAddr)
if err != nil {
logger.Errorf("unable to connect to blog store: %v", err)
logger.Fatalf("unable to connect to blog store: %v", err)
}

pubsub := emitter.NewEmitterPubSub(ctx, pubsubAddr)
Expand All @@ -145,8 +144,7 @@ func initializeNATsandCollector(ctx context.Context, pubsubAddr string, blobAddr
emit := func(d *processor.Document) error {
err = collectorPubFunc(d)
if err != nil {
logger.Errorf("error publishing document from collector: %v", err)
os.Exit(1)
logger.Fatalf("error publishing document from collector: %v", err)
}
return nil
}
Expand Down
8 changes: 3 additions & 5 deletions internal/testing/cmd/pubsub_test/cmd/osv.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,14 @@ func initializeNATsandCertifier(ctx context.Context, opts options) {
// TODO: pass in credentials file for NATS secure login
jetStream := emitter.NewJetStream(opts.pubsubAddr, "", "")
if err := jetStream.JetStreamInit(ctx); err != nil {
logger.Errorf("jetStream initialization failed with error: %v", err)
os.Exit(1)
logger.Fatalf("jetStream initialization failed with error: %v", err)
}
defer jetStream.Close()
}

blobStore, err := blob.NewBlobStore(ctx, opts.blobAddr)
if err != nil {
logger.Errorf("unable to connect to blog store: %v", err)
logger.Fatalf("unable to connect to blog store: %v", err)
}

pubsub := emitter.NewEmitterPubSub(ctx, opts.pubsubAddr)
Expand All @@ -136,8 +135,7 @@ func initializeNATsandCertifier(ctx context.Context, opts options) {
emit := func(d *processor.Document) error {
err = certifierPubFunc(d)
if err != nil {
logger.Errorf("error publishing document from collector: %v", err)
os.Exit(1)
logger.Fatalf("error publishing document from collector: %v", err)
}
return nil
}
Expand Down
Loading