forked from attic-labs/noms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolver.go
127 lines (115 loc) · 3.69 KB
/
resolver.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright 2016 Attic Labs, Inc. All rights reserved.
// Licensed under the Apache License, version 2.0:
// http://www.apache.org/licenses/LICENSE-2.0
package config
import (
"fmt"
"strings"
"github.com/ndau/noms/go/chunks"
"github.com/ndau/noms/go/datas"
"github.com/ndau/noms/go/spec"
"github.com/ndau/noms/go/types"
"github.com/ndau/noms/go/util/verbose"
)
type Resolver struct {
config *Config
dotDatapath string // set to the first datapath that was resolved
}
// A Resolver enables using db defaults, db aliases and dataset '.' replacement in command
// line arguments when a .nomsconfig file is present. To use it, create a config resolver
// before command line processing and use it to resolve each dataspec argument in
// succession.
func NewResolver() *Resolver {
c, err := FindNomsConfig()
if err != nil {
if err != NoConfig {
panic(fmt.Errorf("Failed to read .nomsconfig due to: %v", err))
}
return &Resolver{}
}
return &Resolver{c, ""}
}
// Print replacement if one occurred
func (r *Resolver) verbose(orig string, replacement string) string {
if orig != replacement {
if orig == "" {
orig = `""`
}
verbose.Log("\tresolving %s -> %s\n", orig, replacement)
}
return replacement
}
// Resolve string to database name. If config is defined:
// - replace the empty string with the default db url
// - replace any db alias with it's url
func (r *Resolver) ResolveDbSpec(str string) string {
if r.config != nil {
if str == "" {
return r.config.Db[DefaultDbAlias].Url
}
if val, ok := r.config.Db[str]; ok {
return val.Url
}
}
return str
}
// Resolve string to dataset or path name.
// - replace database name as described in ResolveDatabase
// - if this is the first call to ResolvePath, remember the
// datapath part for subsequent calls.
// - if this is not the first call and a "." is used, replace
// it with the first datapath.
func (r *Resolver) ResolvePathSpec(str string) string {
if r.config != nil {
split := strings.SplitN(str, spec.Separator, 2)
db, rest := "", split[0]
if len(split) > 1 {
db, rest = split[0], split[1]
}
if r.dotDatapath == "" {
r.dotDatapath = rest
} else if rest == "." {
rest = r.dotDatapath
}
return r.ResolveDbSpec(db) + spec.Separator + rest
}
return str
}
// Resolve string to database spec. If a config is present,
// - resolve a db alias to its db spec
// - resolve "" to the default db spec
func (r *Resolver) GetDatabase(str string) (datas.Database, error) {
sp, err := spec.ForDatabase(r.verbose(str, r.ResolveDbSpec(str)))
if err != nil {
return nil, err
}
return sp.GetDatabase(), nil
}
// Resolve string to a chunkstore. Like ResolveDatabase, but returns the underlying ChunkStore
func (r *Resolver) GetChunkStore(str string) (chunks.ChunkStore, error) {
sp, err := spec.ForDatabase(r.verbose(str, r.ResolveDbSpec(str)))
if err != nil {
return nil, err
}
return sp.NewChunkStore(), nil
}
// Resolve string to a dataset. If a config is present,
// - if no db prefix is present, assume the default db
// - if the db prefix is an alias, replace it
func (r *Resolver) GetDataset(str string) (datas.Database, datas.Dataset, error) {
sp, err := spec.ForDataset(r.verbose(str, r.ResolvePathSpec(str)))
if err != nil {
return nil, datas.Dataset{}, err
}
return sp.GetDatabase(), sp.GetDataset(), nil
}
// Resolve string to a value path. If a config is present,
// - if no db spec is present, assume the default db
// - if the db spec is an alias, replace it
func (r *Resolver) GetPath(str string) (datas.Database, types.Value, error) {
sp, err := spec.ForPath(r.verbose(str, r.ResolvePathSpec(str)))
if err != nil {
return nil, nil, err
}
return sp.GetDatabase(), sp.GetValue(), nil
}