Skip to content

Commit

Permalink
Merge pull request #259 from thaJeztah/fix_lint_step1
Browse files Browse the repository at this point in the history
assorted linting fixes and minor cleanups
  • Loading branch information
AkihiroSuda authored Oct 28, 2024
2 parents c156753 + 38f66a6 commit 0e83ada
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 37 deletions.
8 changes: 4 additions & 4 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
linters:
enable:
- staticcheck
- unconvert
- gofmt
- goimports
- govet
- ineffassign
- misspell
- revive
- vet
- staticcheck
- unconvert
- unused
- misspell
disable:
- errcheck

Expand Down
2 changes: 1 addition & 1 deletion cmd/continuity/commands/mount_unsupported.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ import (
"github.com/spf13/cobra"
)

var MountCmd *cobra.Command = nil
var MountCmd *cobra.Command
38 changes: 19 additions & 19 deletions manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,17 @@ func Unmarshal(p []byte) (*Manifest, error) {

func Marshal(m *Manifest) ([]byte, error) {
var bm pb.Manifest
for _, resource := range m.Resources {
bm.Resource = append(bm.Resource, toProto(resource))
for _, rsrc := range m.Resources {
bm.Resource = append(bm.Resource, toProto(rsrc))
}

return proto.Marshal(&bm)
}

func MarshalText(w io.Writer, m *Manifest) error {
var bm pb.Manifest
for _, resource := range m.Resources {
bm.Resource = append(bm.Resource, toProto(resource))
for _, rsrc := range m.Resources {
bm.Resource = append(bm.Resource, toProto(rsrc))
}

b, err := prototext.Marshal(&bm)
Expand All @@ -78,11 +78,11 @@ func MarshalText(w io.Writer, m *Manifest) error {
}

// BuildManifest creates the manifest for the given context
func BuildManifest(ctx Context) (*Manifest, error) {
func BuildManifest(fsContext Context) (*Manifest, error) {
resourcesByPath := map[string]Resource{}
hardLinks := newHardlinkManager()

if err := ctx.Walk(func(p string, fi os.FileInfo, err error) error {
if err := fsContext.Walk(func(p string, fi os.FileInfo, err error) error {
if err != nil {
return fmt.Errorf("error walking %s: %w", p, err)
}
Expand All @@ -92,7 +92,7 @@ func BuildManifest(ctx Context) (*Manifest, error) {
return nil
}

resource, err := ctx.Resource(p, fi)
rsrc, err := fsContext.Resource(p, fi)
if err != nil {
if err == ErrNotFound {
return nil
Expand All @@ -101,7 +101,7 @@ func BuildManifest(ctx Context) (*Manifest, error) {
}

// add to the hardlink manager
if err := hardLinks.Add(fi, resource); err == nil {
if err := hardLinks.Add(fi, rsrc); err == nil {
// Resource has been accepted by hardlink manager so we don't add
// it to the resourcesByPath until we merge at the end.
return nil
Expand All @@ -110,7 +110,7 @@ func BuildManifest(ctx Context) (*Manifest, error) {
return fmt.Errorf("adding hardlink %s: %w", p, err)
}

resourcesByPath[p] = resource
resourcesByPath[p] = rsrc

return nil
}); err != nil {
Expand All @@ -123,13 +123,13 @@ func BuildManifest(ctx Context) (*Manifest, error) {
return nil, err
}

for _, resource := range hardLinked {
resourcesByPath[resource.Path()] = resource
for _, rsrc := range hardLinked {
resourcesByPath[rsrc.Path()] = rsrc
}

var resources []Resource
for _, resource := range resourcesByPath {
resources = append(resources, resource)
for _, rsrc := range resourcesByPath {
resources = append(resources, rsrc)
}

sort.Stable(ByPath(resources))
Expand All @@ -141,9 +141,9 @@ func BuildManifest(ctx Context) (*Manifest, error) {

// VerifyManifest verifies all the resources in a manifest
// against files from the given context.
func VerifyManifest(ctx Context, manifest *Manifest) error {
for _, resource := range manifest.Resources {
if err := ctx.Verify(resource); err != nil {
func VerifyManifest(fsContext Context, manifest *Manifest) error {
for _, rsrc := range manifest.Resources {
if err := fsContext.Verify(rsrc); err != nil {
return err
}
}
Expand All @@ -153,9 +153,9 @@ func VerifyManifest(ctx Context, manifest *Manifest) error {

// ApplyManifest applies on the resources in a manifest to
// the given context.
func ApplyManifest(ctx Context, manifest *Manifest) error {
for _, resource := range manifest.Resources {
if err := ctx.Apply(resource); err != nil {
func ApplyManifest(fsContext Context, manifest *Manifest) error {
for _, rsrc := range manifest.Resources {
if err := fsContext.Apply(rsrc); err != nil {
return err
}
}
Expand Down
29 changes: 16 additions & 13 deletions manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,16 @@ func TestWalkFS(t *testing.T) {
}

var b bytes.Buffer
MarshalText(&b, m)
err = MarshalText(&b, m)
if err != nil {
t.Fatalf("error marshaling manifest: %v", err)
}
t.Log(b.String())

// TODO(dmcgowan): always verify, currently hard links not supported
//if err := VerifyManifest(ctx, m); err != nil {
// if err := VerifyManifest(ctx, m); err != nil {
// t.Fatalf("error verifying manifest: %v")
//}
// }

expectedResources, err := expectedResourceList(root, testResources)
if err != nil {
Expand Down Expand Up @@ -238,9 +241,9 @@ type dresource struct {
}

func generateTestFiles(t *testing.T, root string, resources []dresource) {
for i, resource := range resources {
p := filepath.Join(root, resource.path)
switch resource.kind {
for i, rsrc := range resources {
p := filepath.Join(root, rsrc.path)
switch rsrc.kind {
case rfile:
size := rng.Intn(4 << 20)
d := make([]byte, size)
Expand All @@ -250,35 +253,35 @@ func generateTestFiles(t *testing.T, root string, resources []dresource) {
resources[i].size = size

// this relies on the proper directory parent being defined.
if err := os.WriteFile(p, d, resource.mode); err != nil {
if err := os.WriteFile(p, d, rsrc.mode); err != nil {
t.Fatalf("error writing %q: %v", p, err)
}
case rdirectory:
if err := os.Mkdir(p, resource.mode); err != nil {
if err := os.Mkdir(p, rsrc.mode); err != nil {
t.Fatalf("error creating directory %q: %v", p, err)
}
case rhardlink:
target := filepath.Join(root, resource.target)
target := filepath.Join(root, rsrc.target)
if err := os.Link(target, p); err != nil {
t.Fatalf("error creating hardlink: %v", err)
}
case rrelsymlink:
if err := os.Symlink(resource.target, p); err != nil {
if err := os.Symlink(rsrc.target, p); err != nil {
t.Fatalf("error creating symlink: %v", err)
}
case rabssymlink:
// for absolute links, we join with root.
target := filepath.Join(root, resource.target)
target := filepath.Join(root, rsrc.target)

if err := os.Symlink(target, p); err != nil {
t.Fatalf("error creating symlink: %v", err)
}
case rchardev, rnamedpipe:
if err := devices.Mknod(p, resource.mode, resource.major, resource.minor); err != nil {
if err := devices.Mknod(p, rsrc.mode, rsrc.major, rsrc.minor); err != nil {
t.Fatalf("error creating device %q: %v", p, err)
}
default:
t.Fatalf("unknown resource type: %v", resource.kind)
t.Fatalf("unknown resource type: %v", rsrc.kind)
}

st, err := os.Lstat(p)
Expand Down

0 comments on commit 0e83ada

Please sign in to comment.