Skip to content

Commit

Permalink
feat(dagInfo): add ability to get a subDag at a specific label
Browse files Browse the repository at this point in the history
added `lib.SubDAGInfo`, `lib.SubDAGParams`, `actions.NewSubDAGInfo`, `base.NewSubDAGInfo`
  • Loading branch information
ramfox committed Mar 15, 2019
1 parent e4d9e27 commit 7a07ee8
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
10 changes: 10 additions & 0 deletions actions/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ func NewDAGInfo(node *p2p.QriNode, path string) (*dag.Info, error) {
return base.NewDAGInfo(node.Context(), node.Repo.Store(), ng, path)
}

// NewSubDAGInfo generates a SubDAGInfo for a given node at a given label
func NewSubDAGInfo(node *p2p.QriNode, path, label string) (*dag.Info, error) {
ng, err := newNodeGetter(node)
if err != nil {
return nil, err
}

return base.NewSubDAGInfo(node.Context(), node.Repo.Store(), ng, path, label)
}

// newNodeGetter generates an ipld.NodeGetter from a QriNode
func newNodeGetter(node *p2p.QriNode) (ng ipld.NodeGetter, err error) {
ipfsn, err := node.IPFSNode()
Expand Down
9 changes: 9 additions & 0 deletions base/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,12 @@ func NewDAGInfo(ctx context.Context, store cafs.Filestore, ng ipld.NodeGetter, p
}
return info, nil
}

// NewSubDAGInfo generates a SubDAGInfo for a given node at a given label
func NewSubDAGInfo(ctx context.Context, store cafs.Filestore, ng ipld.NodeGetter, path, label string) (*dag.Info, error) {
info, err := NewDAGInfo(ctx, store, ng, path)
if err != nil {
return nil, err
}
return info.InfoAtLabel(label)
}
18 changes: 16 additions & 2 deletions cmd/DAGInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,19 @@ type DAGInfoOptions struct {
Pretty bool
Hex bool
File string
Label string

DatasetRequests *lib.DatasetRequests
}

// Complete adds any missing configuration that can only be added just before calling Run
func (o *DAGInfoOptions) Complete(f Factory, args []string) (err error) {
if len(args) > 0 {
if isDatasetField.MatchString(args[0]) {
o.Label = args[0]
args = args[1:]
}
}
o.Refs = args
o.DatasetRequests, err = f.DatasetRequests()
return
Expand All @@ -86,8 +93,15 @@ func (o *DAGInfoOptions) Complete(f Factory, args []string) (err error) {
func (o *DAGInfoOptions) Get() (err error) {
info := &dag.Info{}
for _, refstr := range o.Refs {
if err = o.DatasetRequests.DAGInfo(&refstr, info); err != nil {
return err
if o.Label != "" {
s := &lib.SubDAGParams{RefStr: refstr, Label: o.Label}
if err = o.DatasetRequests.SubDAGInfo(s, info); err != nil {
return err
}
} else {
if err = o.DatasetRequests.DAGInfo(&refstr, info); err != nil {
return err
}
}

var buffer []byte
Expand Down
30 changes: 29 additions & 1 deletion lib/datasets.go
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ func (r *DatasetRequests) ManifestMissing(a, b *dag.Manifest) (err error) {
return
}

// DAGInfo generates a manifest for a dataset path
// DAGInfo generates a dag.Info for a dataset path
func (r *DatasetRequests) DAGInfo(refstr *string, i *dag.Info) (err error) {
if r.cli != nil {
return r.cli.Call("DatasetRequests.DAGInfo", refstr, i)
Expand All @@ -648,3 +648,31 @@ func (r *DatasetRequests) DAGInfo(refstr *string, i *dag.Info) (err error) {
*i = *info
return
}

// SubDAGParams defines parameters for the SubDAGInfo method
type SubDAGParams struct {
RefStr, Label string
}

// SubDAGInfo generates a dag.Info for a sub DAG at a label in the original dag.Info
func (r *DatasetRequests) SubDAGInfo(s *SubDAGParams, i *dag.Info) error {
if r.cli != nil {
return r.cli.Call("DatasetRequests.SubDAGInfo", s, i)
}

ref, err := repo.ParseDatasetRef(s.RefStr)
if err != nil {
return err
}
if err = repo.CanonicalizeDatasetRef(r.node.Repo, &ref); err != nil {
return err
}

var info *dag.Info
info, err = actions.NewSubDAGInfo(r.node, ref.Path, s.Label)
if err != nil {
return err
}
*i = *info
return nil
}

0 comments on commit 7a07ee8

Please sign in to comment.