Skip to content
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: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
# Windows resource files
/cmd/gh/*.syso

# Third-party licenses
/internal/licenses/embed/*/*
!/internal/licenses/embed/*/PLACEHOLDER

# VS Code
.vscode

Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
1 change: 0 additions & 1 deletion internal/licenses/embed/report.txt

This file was deleted.

1 change: 0 additions & 1 deletion internal/licenses/embed/third-party/PLACEHOLDER

This file was deleted.

Empty file.
Empty file.
Empty file.
8 changes: 8 additions & 0 deletions internal/licenses/embed_darwin_amd64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package licenses

import "embed"

const rootDir = "embed/darwin-amd64"

//go:embed all:embed/darwin-amd64
var embedFS embed.FS
8 changes: 8 additions & 0 deletions internal/licenses/embed_darwin_arm64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package licenses

import "embed"

const rootDir = "embed/darwin-arm64"

//go:embed all:embed/darwin-arm64
var embedFS embed.FS
15 changes: 15 additions & 0 deletions internal/licenses/embed_default.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// This file is necessary to allow building on platforms that we do not have
// official release builds for. Without this, `go build` or `go install` calls
// would fail due to undefined symbols that are expected to be included in the
// build.

//go:build !(darwin && (amd64 || arm64)) && !(linux && (386 || amd64 || arm || arm64)) && !(windows && (386 || amd64 || arm64))

package licenses

import "embed"

const rootDir = ""

// embedFS is left empty to indicate there's no embedded content.
var embedFS embed.FS
8 changes: 8 additions & 0 deletions internal/licenses/embed_linux_386.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package licenses

import "embed"

const rootDir = "embed/linux-386"

//go:embed all:embed/linux-386
var embedFS embed.FS
8 changes: 8 additions & 0 deletions internal/licenses/embed_linux_amd64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package licenses

import "embed"

const rootDir = "embed/linux-amd64"

//go:embed all:embed/linux-amd64
var embedFS embed.FS
8 changes: 8 additions & 0 deletions internal/licenses/embed_linux_arm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package licenses

import "embed"

const rootDir = "embed/linux-arm"

//go:embed all:embed/linux-arm
var embedFS embed.FS
8 changes: 8 additions & 0 deletions internal/licenses/embed_linux_arm64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package licenses

import "embed"

const rootDir = "embed/linux-arm64"

//go:embed all:embed/linux-arm64
var embedFS embed.FS
8 changes: 8 additions & 0 deletions internal/licenses/embed_windows_386.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package licenses

import "embed"

const rootDir = "embed/windows-386"

//go:embed all:embed/windows-386
var embedFS embed.FS
8 changes: 8 additions & 0 deletions internal/licenses/embed_windows_amd64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package licenses

import "embed"

const rootDir = "embed/windows-amd64"

//go:embed all:embed/windows-amd64
var embedFS embed.FS
8 changes: 8 additions & 0 deletions internal/licenses/embed_windows_arm64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package licenses

import "embed"

const rootDir = "embed/windows-arm64"

//go:embed all:embed/windows-arm64
var embedFS embed.FS
49 changes: 25 additions & 24 deletions internal/licenses/licenses.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
package licenses

import (
"embed"
"fmt"
"io/fs"
"path/filepath"
"path"
"sort"
"strings"
)

//go:embed embed/report.txt
var report string

//go:embed all:embed/third-party
var thirdParty embed.FS

// Content returns the full license report, including the main report and all
// third-party licenses.
func Content() string {
return content(report, thirdParty, "embed/third-party")
return content(embedFS, rootDir)
}

func content(report string, thirdPartyFS fs.ReadFileFS, root string) string {
func content(embedFS fs.ReadFileFS, rootDir string) string {
var b strings.Builder

b.WriteString(report)
reportPath := path.Join(rootDir, "report.txt")
thirdPartyPath := path.Join(rootDir, "third-party")

report, err := fs.ReadFile(embedFS, reportPath)
if err != nil {
return "License information is only available in official release builds.\n"
}

b.Write(report)
b.WriteString("\n")

// Walk the third-party directory and output each license/notice file
Expand All @@ -32,8 +35,13 @@ func content(report string, thirdPartyFS fs.ReadFileFS, root string) string {
files []string
}

thirdPartyFS, err := fs.Sub(embedFS, thirdPartyPath)
if err != nil {
return b.String()
}

modules := map[string]*moduleFiles{}
fs.WalkDir(thirdPartyFS, root, func(filePath string, d fs.DirEntry, err error) error {
fs.WalkDir(thirdPartyFS, ".", func(filePath string, d fs.DirEntry, err error) error {
if err != nil {
return fmt.Errorf("failed to read embedded file %s: %w", filePath, err)
}
Expand All @@ -42,18 +50,11 @@ func content(report string, thirdPartyFS fs.ReadFileFS, root string) string {
return nil
}

name := d.Name()
if name == "PLACEHOLDER" {
return nil
}

// Module path is the directory relative to root
dir := filepath.Dir(filepath.FromSlash(filePath))
rel, _ := filepath.Rel(filepath.FromSlash(root), dir)
if _, ok := modules[rel]; !ok {
modules[rel] = &moduleFiles{path: rel}
dir := path.Dir(filePath)
if _, ok := modules[dir]; !ok {
modules[dir] = &moduleFiles{path: dir}
}
modules[rel].files = append(modules[rel].files, filePath)
modules[dir].files = append(modules[dir].files, filePath)
return nil
})

Expand All @@ -71,7 +72,7 @@ func content(report string, thirdPartyFS fs.ReadFileFS, root string) string {
b.WriteString("================================================================================\n\n")

for _, filePath := range mod.files {
data, err := thirdPartyFS.ReadFile(filePath)
data, err := fs.ReadFile(thirdPartyFS, filePath)
if err != nil {
continue
}
Expand Down
Loading
Loading