You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by ke...@apache.org on 2022/08/11 08:56:26 UTC

[skywalking-cli] 01/01: Trim license headers in query string

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

kezhenxu94 pushed a commit to branch trim/licenseheader
in repository https://gitbox.apache.org/repos/asf/skywalking-cli.git

commit 674523392ba044f388443feec2ddd171c0904491
Author: kezhenxu94 <ke...@apache.org>
AuthorDate: Thu Aug 11 16:56:14 2022 +0800

    Trim license headers in query string
---
 assets/assets.go                | 16 ++++++++--
 assets/assets_test.go           | 65 +++++++++++++++++++++++++++++++++++++++++
 go.mod                          |  1 +
 pkg/graphql/dashboard/global.go |  4 ++-
 4 files changed, 83 insertions(+), 3 deletions(-)

diff --git a/assets/assets.go b/assets/assets.go
index 5265f03..a10fbed 100644
--- a/assets/assets.go
+++ b/assets/assets.go
@@ -19,6 +19,7 @@ package assets
 
 import (
 	"embed"
+	"strings"
 
 	"github.com/apache/skywalking-cli/internal/logger"
 )
@@ -26,11 +27,22 @@ import (
 //go:embed *
 var assets embed.FS
 
-// Read reads all content from a file under assets, which is packed in to the binary
+// Read reads all content from a file under assets, which is packed in to the binary.
+// It will also trim the consecutive lines start with #.
 func Read(filename string) string {
 	content, err := assets.ReadFile(filename)
 	if err != nil {
 		logger.Log.Fatalln("failed to read asset: ", filename, err)
 	}
-	return string(content)
+	var lines []string
+	stop := false
+	for _, line := range strings.Split(string(content), "\n") {
+		if !stop && strings.HasPrefix(line, "#") {
+			continue
+		}
+		stop = true
+		lines = append(lines, line)
+	}
+
+	return strings.Join(lines, "\n")
 }
diff --git a/assets/assets_test.go b/assets/assets_test.go
new file mode 100644
index 0000000..6b87025
--- /dev/null
+++ b/assets/assets_test.go
@@ -0,0 +1,65 @@
+// Licensed to 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. Apache Software Foundation (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 assets
+
+import "testing"
+
+func TestRead(t *testing.T) {
+	type args struct {
+		filename string
+	}
+	tests := []struct {
+		name string
+		args args
+		want string
+	}{
+		{
+			name: "Trim license header",
+			args: args{
+				filename: "graphqls/dependency/EndpointDependency.graphql",
+			},
+			want: `
+query ($endpointId:ID!, $duration: Duration!) {
+    result: getEndpointDependencies(duration: $duration, endpointId: $endpointId) {
+        nodes {
+            id
+            name
+            serviceId
+            serviceName
+            type
+            isReal
+        }
+        calls {
+            id
+            source
+            target
+            detectPoints
+        }
+    }
+}
+`,
+		},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			if got := Read(tt.args.filename); got != tt.want {
+				t.Errorf("Read() = %v, want %v", got, tt.want)
+			}
+		})
+	}
+}
diff --git a/go.mod b/go.mod
index 89b1960..5267337 100644
--- a/go.mod
+++ b/go.mod
@@ -20,6 +20,7 @@ require (
 	golang.org/x/net v0.0.0-20220421235706-1d1ef9303861 // indirect
 	golang.org/x/sys v0.0.0-20220422013727-9388b58f7150 // indirect
 	golang.org/x/term v0.0.0-20220411215600-e5f449aeb171 // indirect
+	golang.org/x/text v0.3.7
 	google.golang.org/grpc v1.40.0
 	gopkg.in/yaml.v2 v2.4.0
 	k8s.io/apimachinery v0.21.1
diff --git a/pkg/graphql/dashboard/global.go b/pkg/graphql/dashboard/global.go
index 13b1aad..cb3327f 100644
--- a/pkg/graphql/dashboard/global.go
+++ b/pkg/graphql/dashboard/global.go
@@ -23,6 +23,8 @@ import (
 	"strings"
 	"sync"
 
+	"golang.org/x/text/cases"
+	"golang.org/x/text/language"
 	api "skywalking.apache.org/repo/goapi/query"
 
 	"github.com/spf13/viper"
@@ -134,7 +136,7 @@ func getButtonTexts(byteValue []byte) ([]string, error) {
 
 	for s := range c {
 		if s != "style" {
-			ret = append(ret, strings.Title(s))
+			ret = append(ret, cases.Title(language.Und).String(s))
 		}
 	}
 	return ret, nil