github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/lib/log.go (about)

     1  package lib
     2  
     3  import (
     4  	"context"
     5  
     6  	qhttp "github.com/qri-io/qri/lib/http"
     7  	"github.com/qri-io/qri/logbook"
     8  )
     9  
    10  // LogMethods extends a lib.Instance with business logic for working with lists
    11  // of dataset versions. think "git log".
    12  type LogMethods struct {
    13  	d dispatcher
    14  }
    15  
    16  // Name returns the name of this method group
    17  func (m LogMethods) Name() string {
    18  	return "log"
    19  }
    20  
    21  // Attributes defines attributes for each method
    22  func (m LogMethods) Attributes() map[string]AttributeSet {
    23  	return map[string]AttributeSet{
    24  		"log":            {Endpoint: qhttp.DenyHTTP},
    25  		"rawlogbook":     {Endpoint: qhttp.DenyHTTP},
    26  		"logbooksummary": {Endpoint: qhttp.DenyHTTP},
    27  	}
    28  }
    29  
    30  // RefListParams encapsulates parameters for requests to a single reference
    31  // that will produce a paginated result
    32  type RefListParams struct {
    33  	// String value of a reference
    34  	Ref string
    35  	// Pagination Parameters
    36  	Offset, Limit int
    37  }
    38  
    39  // LogEntry is a record in a log of operations on a dataset
    40  type LogEntry = logbook.LogEntry
    41  
    42  // Log lists log entries for actions taken on a given dataset
    43  func (m LogMethods) Log(ctx context.Context, p *RefListParams) ([]LogEntry, error) {
    44  	got, _, err := m.d.Dispatch(ctx, dispatchMethodName(m, "log"), p)
    45  	if res, ok := got.([]LogEntry); ok {
    46  		return res, err
    47  	}
    48  	return nil, dispatchReturnError(got, err)
    49  }
    50  
    51  // RawLogbookParams enapsulates parameters for the RawLogbook methods
    52  type RawLogbookParams struct {
    53  	// no options yet
    54  }
    55  
    56  // RawLogs is an alias for a human representation of a plain-old-data logbook
    57  type RawLogs = []logbook.PlainLog
    58  
    59  // RawLogbook encodes the full logbook as human-oriented json
    60  func (m LogMethods) RawLogbook(ctx context.Context, p *RawLogbookParams) (*RawLogs, error) {
    61  	got, _, err := m.d.Dispatch(ctx, dispatchMethodName(m, "rawlogbook"), p)
    62  	if res, ok := got.(*RawLogs); ok {
    63  		return res, err
    64  	}
    65  	return nil, dispatchReturnError(got, err)
    66  }
    67  
    68  // LogbookSummary returns a string overview of the logbook
    69  func (m LogMethods) LogbookSummary(ctx context.Context, p *struct{}) (*string, error) {
    70  	got, _, err := m.d.Dispatch(ctx, dispatchMethodName(m, "logbooksummary"), p)
    71  	if res, ok := got.(*string); ok {
    72  		return res, err
    73  	}
    74  	return nil, dispatchReturnError(got, err)
    75  }
    76  
    77  // logImpl holds the method implementations for LogMethods
    78  type logImpl struct{}
    79  
    80  // Entries lists log entries for actions taken on a given dataset
    81  func (logImpl) Log(scope scope, p *RefListParams) ([]LogEntry, error) {
    82  	res := []LogEntry{}
    83  	var err error
    84  
    85  	ref, _, err := scope.ParseAndResolveRef(scope.Context(), p.Ref)
    86  	if err != nil {
    87  		return nil, err
    88  	}
    89  
    90  	book := scope.Logbook()
    91  	res, err = book.LogEntries(scope.Context(), ref, p.Offset, p.Limit)
    92  	if err != nil {
    93  		return nil, err
    94  	}
    95  	return res, nil
    96  }
    97  
    98  // RawLogbook encodes the full logbook as human-oriented json
    99  func (logImpl) RawLogbook(scope scope, p *RawLogbookParams) (*RawLogs, error) {
   100  	res := &RawLogs{}
   101  	var err error
   102  
   103  	*res, err = scope.Logbook().PlainLogs(scope.Context())
   104  	if err != nil {
   105  		return nil, err
   106  	}
   107  	return res, nil
   108  }
   109  
   110  // LogbookSummary returns a string overview of the logbook
   111  func (logImpl) LogbookSummary(scope scope, p *struct{}) (*string, error) {
   112  	res := ""
   113  	res = scope.Logbook().SummaryString(scope.Context())
   114  	return &res, nil
   115  }