/
flags.go
100 lines (87 loc) · 2.53 KB
/
flags.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
// Copyright 2022 Dhi Aurrahman
//
// 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 managed
import (
"os"
"strings"
"github.com/iancoleman/strcase"
"github.com/tetratelabs/run"
)
// Flags holds common flags that can be shared across services.
type Flags struct {
DefaultVersion string
Version string
Dir string
ConfigFile string
// Titleize allows to override the titleize. You want to use this, e.g. for preserving casing
// for xDS (the default titleize gives Xds).
Titleize func(string) string
disabled bool
g *run.Group
s run.Service
}
// Manage delegate disabled checking to this object.
func (m *Flags) Manage(flags *run.FlagSet, g *run.Group, s run.Service) {
if g == nil {
return
}
if m.Titleize == nil {
m.Titleize = titleize
}
title := m.Titleize(s.Name())
// When default version is not defined, we should not register the version flag, since it is
// probably hardcoded or not relevant (e.g. for internal implementation).
if m.DefaultVersion != "" {
// --<name>-version. For example: --proxy-version.
flags.StringVar(
&m.Version,
s.Name()+"-version",
m.DefaultVersion,
title+" version",
)
}
// --<name>-directory. For example: --proxy-directory.
flags.StringVar(
&m.Dir,
s.Name()+"-directory",
os.Getenv(strcase.ToScreamingSnake(s.Name())+"_HOME"),
"Path to the "+title+" work directory",
)
// --<name>-config. For example: --proxy-config.
flags.StringVar(
&m.ConfigFile,
s.Name()+"-config",
m.ConfigFile,
"Path to the "+title+" config file")
// --disable-<name>. For example: --disable-proxy.
flags.BoolVar(
&m.disabled,
"disable-"+s.Name(),
false,
"Disable "+title)
m.g = g
m.s = s
}
// IsDisabled returns true when a managed service is disabled.
func (m *Flags) IsDisabled() bool {
if m.g == nil || !m.disabled {
return false
}
m.g.Deregister(m.s)
return true
}
// titleize properly capitalize kebab case to title case.
func titleize(name string) string {
return strings.Title(strings.Join(strings.Split(name, "-"), " "))
}