diff --git a/cmd/nopfs/main.go b/cmd/nopfs/main.go index 1d751a1..183819f 100644 --- a/cmd/nopfs/main.go +++ b/cmd/nopfs/main.go @@ -72,7 +72,7 @@ func main() { fmt.Println(err) } else { status := blocker.IsCidBlocked(c) - fmt.Printf("%s: line %d. %s\n", status.Status, status.Entry.Line, status.Entry) + fmt.Println(status) } default: fmt.Println("Usage:") diff --git a/status.go b/status.go index 9992b3f..911a687 100644 --- a/status.go +++ b/status.go @@ -1,6 +1,8 @@ package nopfs import ( + "fmt" + "github.com/ipfs/boxo/path" "github.com/ipfs/go-cid" ) @@ -42,6 +44,35 @@ type StatusResponse struct { Error error } +// String provides a string with the details of a StatusResponse. +func (r StatusResponse) String() string { + if err := r.Error; err != nil { + return err.Error() + } + + path := "" + if c := r.Cid; c.Defined() { + path = c.String() + } else { + path = r.Path.String() + } + + return fmt.Sprintf("%s: %s (%s:%d)", + path, r.Status, + r.Filename, r.Entry.Line, + ) +} + +// ToError returns nil if the Status of the StatusResponse is Allowed or Not Found. +// When the status is Blocked or Errored, it returns a StatusError. +func (r StatusResponse) ToError() *StatusError { + if r.Status != StatusBlocked && r.Status != StatusErrored { + return nil + } + + return &StatusError{Response: r} +} + // StatusError implements the error interface and can be used to provide // information about a blocked-status in the form of an error. type StatusError struct { @@ -57,13 +88,3 @@ func (err *StatusError) Error() string { } return err.Response.Path.String() + " is blocked and cannot be provided" } - -// ToError returns nil if the Status of the StatusResponse is Allowed or Not Found. -// When the status is Blocked or Errored, it returns a StatusError. -func (r StatusResponse) ToError() *StatusError { - if r.Status != StatusBlocked && r.Status != StatusErrored { - return nil - } - - return &StatusError{Response: r} -}