forked from sourcenetwork/defradb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollection.go
90 lines (81 loc) · 2.7 KB
/
collection.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright 2023 Democratized Data Foundation
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package cli
import (
"context"
"github.com/sourcenetwork/immutable"
"github.com/spf13/cobra"
"github.com/sourcenetwork/defradb/client"
)
func MakeCollectionCommand() *cobra.Command {
var txID uint64
var identity string
var name string
var schemaRoot string
var versionID string
var getInactive bool
var cmd = &cobra.Command{
Use: "collection [--name <name> --schema <schemaRoot> --version <versionID>]",
Short: "Interact with a collection.",
Long: `Create, read, update, and delete documents within a collection.`,
PersistentPreRunE: func(cmd *cobra.Command, args []string) (err error) {
// cobra does not chain pre run calls so we have to run them again here
if err := setContextRootDir(cmd); err != nil {
return err
}
if err := setContextConfig(cmd); err != nil {
return err
}
if err := setContextIdentity(cmd, identity); err != nil {
return err
}
if err := setContextTransaction(cmd, txID); err != nil {
return err
}
if err := setContextDB(cmd); err != nil {
return err
}
store := mustGetContextStore(cmd)
options := client.CollectionFetchOptions{}
if versionID != "" {
options.SchemaVersionID = immutable.Some(versionID)
}
if schemaRoot != "" {
options.SchemaRoot = immutable.Some(schemaRoot)
}
if name != "" {
options.Name = immutable.Some(name)
}
if getInactive {
options.IncludeInactive = immutable.Some(getInactive)
}
cols, err := store.GetCollections(cmd.Context(), options)
if err != nil {
return err
}
if len(cols) != 1 {
// If more than one collection matches the given criteria we cannot set the context collection
return nil
}
col := cols[0]
ctx := context.WithValue(cmd.Context(), colContextKey, col)
cmd.SetContext(ctx)
return nil
},
}
cmd.PersistentFlags().Uint64Var(&txID, "tx", 0, "Transaction ID")
cmd.PersistentFlags().StringVarP(&identity, "identity", "i", "",
"Hex formatted private key used to authenticate with ACP")
cmd.PersistentFlags().StringVar(&name, "name", "", "Collection name")
cmd.PersistentFlags().StringVar(&schemaRoot, "schema", "", "Collection schema Root")
cmd.PersistentFlags().StringVar(&versionID, "version", "", "Collection version ID")
cmd.PersistentFlags().BoolVar(&getInactive, "get-inactive", false, "Get inactive collections as well as active")
return cmd
}