-
Notifications
You must be signed in to change notification settings - Fork 307
/
parse_topology.go
executable file
·65 lines (54 loc) · 2.1 KB
/
parse_topology.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
// Copyright 2020 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package clusterutil
import (
"io/ioutil"
"github.com/joomcode/errorx"
"github.com/pingcap/tiup/pkg/cliutil"
"github.com/pingcap/tiup/pkg/errutil"
"go.uber.org/zap"
"gopkg.in/yaml.v2"
)
var (
errNSTopolohy = errorx.NewNamespace("topology")
// ErrTopologyReadFailed is ErrTopologyReadFailed
ErrTopologyReadFailed = errNSTopolohy.NewType("read_failed", errutil.ErrTraitPreCheck)
// ErrTopologyParseFailed is ErrTopologyParseFailed
ErrTopologyParseFailed = errNSTopolohy.NewType("parse_failed", errutil.ErrTraitPreCheck)
)
// ParseTopologyYaml read yaml content from `file` and unmarshal it to `out`
func ParseTopologyYaml(file string, out interface{}) error {
suggestionProps := map[string]string{
"File": file,
}
zap.L().Debug("Parse topology file", zap.String("file", file))
yamlFile, err := ioutil.ReadFile(file)
if err != nil {
return ErrTopologyReadFailed.
Wrap(err, "Failed to read topology file %s", file).
WithProperty(cliutil.SuggestionFromTemplate(`
Please check whether your topology file {{ColorKeyword}}{{.File}}{{ColorReset}} exists and try again.
To generate a sample topology file:
{{ColorCommand}}{{OsArgs0}} template topology > topo.yaml{{ColorReset}}
`, suggestionProps))
}
if err = yaml.UnmarshalStrict(yamlFile, out); err != nil {
return ErrTopologyParseFailed.
Wrap(err, "Failed to parse topology file %s", file).
WithProperty(cliutil.SuggestionFromTemplate(`
Please check the syntax of your topology file {{ColorKeyword}}{{.File}}{{ColorReset}} and try again.
`, suggestionProps))
}
zap.L().Debug("Parse topology file succeeded", zap.Any("topology", out))
return nil
}