Files
sealos/pkg/utils/flags/flags.go
T
fengxsong 6ab178bab8 feat: support netlink mode (#1424)
* feat: support netlink mode

Signed-off-by: fengxsong <fengxsong@outlook.com>

* fix: remove testing dir

Signed-off-by: fengxsong <fengxsong@outlook.com>

* replace modules with officials

Signed-off-by: fengxsong <fengxsong@outlook.com>

* fix: license header

Signed-off-by: fengxsong <fengxsong@outlook.com>

* fix: golanglint-ci warning

Signed-off-by: fengxsong <fengxsong@outlook.com>

* fix: bump k8s.io/* deps version

Signed-off-by: fengxsong <fengxsong@outlook.com>

* fix: errcheck, ignore it

Signed-off-by: fengxsong <fengxsong@outlook.com>

* docs: add test case

Signed-off-by: fengxsong <fengxsong@outlook.com>

* fix: add ipset into lvscare image

Signed-off-by: fengxsong <fengxsong@outlook.com>

* docs: implementation details

Signed-off-by: fengxsong <fengxsong@outlook.com>

* refactor: return fast

Signed-off-by: fengxsong <fengxsong@outlook.com>

* fix: add durationOrSecond flag type for compatibility with previous usage

Signed-off-by: fengxsong <fengxsong@outlook.com>
2022-07-31 22:49:08 +08:00

50 lines
1.3 KiB
Go

/*
Copyright 2022 cuisongliu@qq.com.
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 flags
import (
"os"
"strings"
"github.com/spf13/pflag"
"github.com/labring/sealos/pkg/utils/logger"
)
// PrintFlags logs the flags in the flagset
func PrintFlags(flags *pflag.FlagSet) {
flags.VisitAll(func(flag *pflag.Flag) {
logger.Debug("FLAG: --%s=%q", flag.Name, flag.Value)
})
}
// SetFlagsFromEnv set value of flag if not changed but has env key
func SetFlagsFromEnv(prefix string, flags *pflag.FlagSet) {
if prefix != "" && !strings.HasSuffix(prefix, "_") {
prefix += "_"
}
flags.VisitAll(func(flag *pflag.Flag) {
if flag.Changed {
return
}
envVar := strings.ToUpper(strings.Replace(prefix+flag.Name, "-", "_", -1))
if v := os.Getenv(envVar); v != "" {
_ = flags.Set(flag.Name, v)
}
})
}