-
Notifications
You must be signed in to change notification settings - Fork 275
/
hostdoc.go
85 lines (74 loc) · 2.1 KB
/
hostdoc.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
//
// Copyright The Athenz Authors
//
// 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,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package main
import (
"errors"
"fmt"
"os"
"strings"
"github.com/AthenZ/athenz/libs/go/sia/host/hostdoc"
"github.com/jawher/mow.cli"
)
func main() {
app := cli.App(os.Args[0], "hostdoc parser program")
// Global Options
docFile := app.String(cli.StringOpt{
Name: "f file",
Value: "/var/lib/sia/host_document",
Desc: "path to the host document file",
})
app.Command("show", "show entries from host document", func(cmd *cli.Cmd) {
var (
domain = cmd.BoolOpt("d domain", false, "show domain")
service = cmd.BoolOpt("s service", false, "show services, comma separated")
profile = cmd.BoolOpt("p profile", false, "show profile")
primary = cmd.BoolOpt("primary-service", false, "show primary service")
)
cmd.Action = func() {
result, err := Process(*docFile, *domain, *service, *profile, *primary)
if err != nil {
fmt.Fprintln(os.Stderr, err)
} else {
fmt.Printf("%s\n", result)
}
}
})
app.Run(os.Args)
}
func Process(docFile string, domain, service, profile, primary bool) (string, error) {
b, err := os.ReadFile(docFile)
if err != nil {
return "", err
}
doc, _, err := hostdoc.NewPlainDoc(b)
if err != nil {
return "", err
}
// the first non-false flag wins the precedence
switch {
case domain:
return doc.Domain, nil
case service:
return strings.Join(doc.Services, ","), nil
case profile:
return doc.Profile, nil
case primary:
return doc.Services[0], nil
default:
return "", errors.New("no selection made to print from host document")
}
return "", nil
}