Skip to content

Commit

Permalink
bundle: rearrange embed.FS setup
Browse files Browse the repository at this point in the history
No functional changes, I hope!

Signed-off-by: Stephan Renatus <stephan@styra.com>
  • Loading branch information
srenatus committed Aug 30, 2023
1 parent de1fc97 commit 88b0209
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 36 deletions.
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 *
var Bundle embed.FS
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 {
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

0 comments on commit 88b0209

Please sign in to comment.