This is an automated email from the ASF dual-hosted git repository.

albumenj pushed a commit to branch refactor-with-go
in repository https://gitbox.apache.org/repos/asf/dubbo-admin.git


The following commit(s) were added to refs/heads/refactor-with-go by this push:
     new 7079d88d read options from env (#1017)
7079d88d is described below

commit 7079d88d08171e4226375a234cf7dbcd4f875a1e
Author: xin gu <[email protected]>
AuthorDate: Tue Mar 7 15:02:51 2023 +0800

    read options from env (#1017)
    
    read options from env
---
 cmd/authority/main.go           | 26 +----------
 pkg/authority/config/envflag.go | 97 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 99 insertions(+), 24 deletions(-)

diff --git a/cmd/authority/main.go b/cmd/authority/main.go
index b220c4a8..3129051c 100644
--- a/cmd/authority/main.go
+++ b/cmd/authority/main.go
@@ -25,33 +25,11 @@ import (
        "github.com/apache/dubbo-admin/pkg/logger"
 )
 
-// TODO read namespace from env
-const (
-       namespace   = "dubbo-system"
-       serviceName = "dubbo-ca"
-)
-
 func main() {
        logger.Init()
-       // TODO read options from env
-       options := &config.Options{
-               Namespace:   namespace,
-               ServiceName: serviceName,
-
-               PlainServerPort:  30060,
-               SecureServerPort: 30062,
-               DebugPort:        30070,
 
-               WebhookPort:       30080,
-               WebhookAllowOnErr: true,
-
-               CaValidity:   30 * 24 * 60 * 60 * 1000, // 30 day
-               CertValidity: 1 * 60 * 60 * 1000,       // 1 hour
-
-               InPodEnv:              false,
-               IsKubernetesConnected: false,
-               EnableOIDCCheck:       true,
-       }
+       // TODO read options from env
+       options := config.GetOptions()
 
        s := security.NewServer(options)
 
diff --git a/pkg/authority/config/envflag.go b/pkg/authority/config/envflag.go
new file mode 100644
index 00000000..d2edf91d
--- /dev/null
+++ b/pkg/authority/config/envflag.go
@@ -0,0 +1,97 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You 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 config
+
+import (
+       "flag"
+       "os"
+       "strconv"
+)
+
+func GetOptions() *Options {
+       // TODO read options from env
+       options := Options{
+               Namespace:   GetStringEnv("namespace", "dubbo-system"),
+               ServiceName: GetStringEnv("servicename", "dubbo-ca"),
+
+               PlainServerPort:  GetIntEnv("plainserverport", 30060),
+               SecureServerPort: GetIntEnv("secureserverport", 30062),
+               DebugPort:        GetIntEnv("debugport", 30070),
+
+               WebhookPort:       30080,
+               WebhookAllowOnErr: GetBoolEnv("webhookallowonerr", true),
+
+               CaValidity:   30 * 24 * 60 * 60 * 1000, // 30 day
+               CertValidity: 1 * 60 * 60 * 1000,       // 1 hour
+
+               InPodEnv:              GetBoolEnv("inpodenv", false),
+               IsKubernetesConnected: GetBoolEnv("iskubernetesconnected", 
false),
+               EnableOIDCCheck:       GetBoolEnv("enableoidccheck", true),
+       }
+
+       flag.StringVar(&options.Namespace, "namespace", options.Namespace, 
"dubbo namespace")
+       flag.StringVar(&options.ServiceName, "servicename", 
options.ServiceName, "dubbo serviceName")
+       flag.IntVar(&options.PlainServerPort, "plainserverport", 
options.PlainServerPort, "dubbo plainServerPort")
+       flag.IntVar(&options.SecureServerPort, "secureserverport", 
options.SecureServerPort, "dubbo secureServerPort")
+       flag.IntVar(&options.DebugPort, "debugport", options.DebugPort, "dubbo 
debugPort")
+       webhookport := GetIntEnv("webhookport", 30080)
+       flag.IntVar(&webhookport, "webhookport", webhookport, "dubbo 
webhookPort")
+       flag.BoolVar(&options.WebhookAllowOnErr, "webhookallowonerr", 
options.WebhookAllowOnErr, "dubbo webhookAllowOnErr")
+       flag.BoolVar(&options.InPodEnv, "inpodenv", options.InPodEnv, "dubbo 
inPodEnv")
+       flag.BoolVar(&options.IsKubernetesConnected, "iskubernetesconnected", 
options.IsKubernetesConnected, "dubbo isKubernetesConnected")
+       flag.BoolVar(&options.EnableOIDCCheck, "enableoidccheck", 
options.EnableOIDCCheck, "dubbo enableOIDCCheck")
+       flag.Parse()
+
+       options.WebhookPort = int32(webhookport)
+       return &options
+}
+
+func GetStringEnv(name string, defvalue string) string {
+       val, ex := os.LookupEnv(name)
+       if ex {
+               return val
+       } else {
+               return defvalue
+       }
+}
+
+func GetIntEnv(name string, defvalue int) int {
+       val, ex := os.LookupEnv(name)
+       if ex {
+               num, err := strconv.Atoi(val)
+               if err != nil {
+                       return defvalue
+               } else {
+                       return num
+               }
+       } else {
+               return defvalue
+       }
+}
+
+func GetBoolEnv(name string, defvalue bool) bool {
+       val, ex := os.LookupEnv(name)
+       if ex {
+               boolVal, err := strconv.ParseBool(val)
+               if err != nil {
+                       return defvalue
+               } else {
+                       return boolVal
+               }
+       } else {
+               return defvalue
+       }
+}

Reply via email to