Skip to content

Commit

Permalink
refactor: unexport db types (Packae, Slice...)
Browse files Browse the repository at this point in the history
  • Loading branch information
rebornplusplus committed Apr 18, 2024
1 parent 7193681 commit 06cc866
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 29 deletions.
40 changes: 20 additions & 20 deletions cmd/chisel/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ func generateDB(opts *generateDBOptions) ([]string, error) {
dbw := jsonwall.NewDBWriter(&jsonwall.DBWriterOptions{
Schema: dbSchema,
})
dbPaths := []string{}
genPaths := []string{}

// Add packages to the DB.
for _, info := range opts.PackageInfo {
err := dbw.Add(&Package{
err := dbw.Add(&dbPackage{
Kind: "package",
Name: info.Name,
Version: info.Version,
Expand All @@ -55,7 +55,7 @@ func generateDB(opts *generateDBOptions) ([]string, error) {
}
// Add slices to the DB.
for _, s := range opts.Slices {
err := dbw.Add(&Slice{
err := dbw.Add(&dbSlice{
Kind: "slice",
Name: s.String(),
})
Expand All @@ -70,7 +70,7 @@ func generateDB(opts *generateDBOptions) ([]string, error) {
for s := range entry.Slices {
name := s.String()
// Add contents to the DB.
err := dbw.Add(&Content{
err := dbw.Add(&dbContent{
Kind: "content",
Slice: name,
Path: entry.Path,
Expand All @@ -80,7 +80,7 @@ func generateDB(opts *generateDBOptions) ([]string, error) {
}
sliceNames = append(sliceNames, name)
}
err := dbw.Add(&Path{
err := dbw.Add(&dbPath{
Kind: "path",
Path: entry.Path,
Mode: mode,
Expand All @@ -95,24 +95,24 @@ func generateDB(opts *generateDBOptions) ([]string, error) {
}
// Add the DB path and content entries.
for path, slices := range opts.ManifestSlices {
dbPath := filepath.Join(strings.TrimRight(path, "*"), dbFile)
dbPaths = append(dbPaths, dbPath)
fPath := filepath.Join(strings.TrimRight(path, "*"), dbFile)
genPaths = append(genPaths, fPath)
sliceNames := []string{}
for _, s := range slices {
name := s.String()
err := dbw.Add(&Content{
err := dbw.Add(&dbContent{
Kind: "content",
Slice: name,
Path: dbPath,
Path: fPath,
})
if err != nil {
return nil, err
}
sliceNames = append(sliceNames, name)
}
err := dbw.Add(&Path{
err := dbw.Add(&dbPath{
Kind: "path",
Path: dbPath,
Path: fPath,
Mode: fmt.Sprintf("0%o", dbMode&fs.ModePerm),
Slices: sliceNames,
})
Expand All @@ -122,30 +122,30 @@ func generateDB(opts *generateDBOptions) ([]string, error) {
}

filePaths := []string{}
for _, path := range dbPaths {
for _, path := range genPaths {
filePaths = append(filePaths, filepath.Join(opts.RootDir, path))
}
err := WriteDB(dbw, filePaths)
err := writeDB(dbw, filePaths)
if err != nil {
return nil, err
}
return dbPaths, nil
return genPaths, nil
}

type Package struct {
type dbPackage struct {
Kind string `json:"kind"`
Name string `json:"name"`
Version string `json:"version"`
Digest string `json:"sha256"`
Arch string `json:"arch"`
}

type Slice struct {
type dbSlice struct {
Kind string `json:"kind"`
Name string `json:"name"`
}

type Path struct {
type dbPath struct {
Kind string `json:"kind"`
Path string `json:"path"`
Mode string `json:"mode"`
Expand All @@ -156,14 +156,14 @@ type Path struct {
Link string `json:"link,omitempty"`
}

type Content struct {
type dbContent struct {
Kind string `json:"kind"`
Slice string `json:"slice"`
Path string `json:"path"`
}

// WriteDB writes all added entries and generates the Chisel DB file.
func WriteDB(writer *jsonwall.DBWriter, paths []string) (err error) {
// writeDB writes all added entries and generates the Chisel DB file.
func writeDB(writer *jsonwall.DBWriter, paths []string) (err error) {
files := []io.Writer{}
for _, path := range paths {
if err = os.MkdirAll(filepath.Dir(path), 0755); err != nil {
Expand Down
16 changes: 8 additions & 8 deletions cmd/chisel/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ import (

type writeDBTest struct {
summary string
packages []*chisel.Package
slices []*chisel.Slice
paths []*chisel.Path
contents []*chisel.Content
packages []*chisel.DBPackage
slices []*chisel.DBSlice
paths []*chisel.DBPath
contents []*chisel.DBContent
expectedDB string
err string
}

var writeDBTests = []writeDBTest{{
summary: "Write Chisel DB",
packages: []*chisel.Package{{
packages: []*chisel.DBPackage{{
Kind: "package",
Name: "mypkg",
Version: "12ubuntu4.6",
Expand All @@ -38,7 +38,7 @@ var writeDBTests = []writeDBTest{{
Digest: "b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c",
Arch: "amd64",
}},
slices: []*chisel.Slice{{
slices: []*chisel.DBSlice{{
Kind: "slice",
Name: "mypkg_myslice",
}, {
Expand All @@ -48,7 +48,7 @@ var writeDBTests = []writeDBTest{{
Kind: "slice",
Name: "foo_bar",
}},
paths: []*chisel.Path{{
paths: []*chisel.DBPath{{
Kind: "path",
Path: "/usr/bin/foo",
Mode: "0644",
Expand Down Expand Up @@ -82,7 +82,7 @@ var writeDBTests = []writeDBTest{{
FinalHash: "71f28b05f5b0a3af1776ae55d578c16a11f10aef7dd408421c35dac17ca7cbad",
Size: 489,
}},
contents: []*chisel.Content{{
contents: []*chisel.DBContent{{
Kind: "content",
Slice: "foo_bar",
Path: "/usr/bin/foo",
Expand Down
9 changes: 8 additions & 1 deletion cmd/chisel/export_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package main

var RunMain = run
var (
WriteDB = writeDB
)

type DBPackage dbPackage
type DBSlice dbSlice
type DBPath dbPath
type DBContent dbContent

func FakeIsStdoutTTY(t bool) (restore func()) {
oldIsStdoutTTY := isStdoutTTY
Expand Down

0 comments on commit 06cc866

Please sign in to comment.