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

bundle: rearrange embed.FS setup #279

Merged
merged 1 commit into from
Aug 30, 2023
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
11 changes: 11 additions & 0 deletions bundle/bundle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package bundle

import (
"embed"
)

// Bundle FS will include the tests as well, but since that has negligible impact on the size of the binary,
// it's preferable to filter them out from the bundle than to e.g. create a separate directory for tests
//
//go:embed *
anderseknert marked this conversation as resolved.
Show resolved Hide resolved
var Bundle embed.FS
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error: ST1022: comment on exported var Bundle should be of the form "Bundle ..." (stylecheck)

Adding an ignore on package level should be fine here.

10 changes: 1 addition & 9 deletions cmd/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"io"
"io/fs"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -172,14 +171,7 @@ func lint(args []string, params lintCommandParams) (report.Report, error) {
}
}

// Create new fs from root of bundle, to avoid having to deal with
// "bundle" in paths (i.e. `data.bundle.regal`)
bfs, err := fs.Sub(embeds.EmbedBundleFS, "bundle")
if err != nil {
return report.Report{}, fmt.Errorf("failed reading embedded bundle %w", err)
}

regalRules := rio.MustLoadRegalBundleFS(bfs)
regalRules := rio.MustLoadRegalBundleFS(embeds.EmbedBundleFS)

var regalDir *os.File

Expand Down
12 changes: 1 addition & 11 deletions cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package cmd
import (
"context"
"fmt"
"io/fs"
"os"
"strings"
"time"
Expand Down Expand Up @@ -131,16 +130,7 @@ func opaTest(args []string) int {
return 1
}

// Create new fs from root of bundle, to avoid having to deal with
// "bundle" in paths (i.e. `data.bundle.regal`)
bfs, err := fs.Sub(embeds.EmbedBundleFS, "bundle")
if err != nil {
fmt.Fprintln(os.Stderr, fmt.Errorf("failed reading embedded bundle %w", err))

return 1
}

regalRules := rio.MustLoadRegalBundleFS(bfs)
regalRules := rio.MustLoadRegalBundleFS(embeds.EmbedBundleFS)

if bundles == nil {
bundles = make(map[string]*bundle.Bundle)
Expand Down
8 changes: 6 additions & 2 deletions internal/embeds/embeds.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
//nolint:gochecknoglobals
package embeds

import "embed"
import (
"embed"

var EmbedBundleFS embed.FS
"github.com/styrainc/regal/bundle"
)

var EmbedBundleFS = bundle.Bundle

//go:embed templates
var EmbedTemplatesFS embed.FS
Expand Down
2 changes: 1 addition & 1 deletion internal/io/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func CloseFileIgnore(file *os.File) {
}

func ExcludeTestFilter() filter.LoaderFilter {
return func(abspath string, info files.FileInfo, depth int) bool {
return func(_ string, info files.FileInfo, _ int) bool {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, unrelated change. I had just happened to look into how the bundle is loaded...

return strings.HasSuffix(info.Name(), "_test.rego") &&
// (anderseknert): This is an outlier, but not sure we need something
// more polished to deal with this for the time being.
Expand Down
13 changes: 0 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,17 @@
package main

import (
"embed"
"log"
"os"

"github.com/styrainc/regal/cmd"
"github.com/styrainc/regal/internal/embeds"
)

// Note: this will bundle the tests as well, but since that has negligible impact on the size of the binary,
// it's preferable to filter them out from the bundle than to e.g. create a separate directory for tests
//
//go:embed all:bundle
var bundle embed.FS

func main() {
// Remove date and time from any `log.*` calls, as that doesn't add much of value here
// Evaluate options for logging later..
log.SetFlags(0)

// Embedded files and directories can only traverse down in the tree, i.e. no parent paths,
// so while this isn't pretty, we'll use a separate package to carry state from here. If you
// know of a better way of doing this, don't hesitate to fix this.
embeds.EmbedBundleFS = bundle

if err := cmd.RootCommand.Execute(); err != nil {
os.Exit(1)
}
Expand Down
Loading