parent
be6f189365
commit
18728fc993
@ -0,0 +1,72 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
clear
|
||||
. $(dirname ${BASH_SOURCE})/lib/util.sh
|
||||
|
||||
openim::util::desc "You can learn a lot about automation using make help"
|
||||
openim::util::run "make help"
|
||||
clear
|
||||
|
||||
openim::util::desc "You can learn a lot about automation using make help-all"
|
||||
openim::util::run "make help-all"
|
||||
clear
|
||||
|
||||
openim::util::desc "Run tidy"
|
||||
openim::util::run "make tidy"
|
||||
clear
|
||||
|
||||
openim::util::desc "Vendor go.mod"
|
||||
openim::util::run "make vendor"
|
||||
clear
|
||||
|
||||
openim::util::desc "Code style: fmt, vet, lint"
|
||||
openim::util::run "make style"
|
||||
clear
|
||||
|
||||
openim::util::desc "Run unit tests"
|
||||
openim::util::run "make test"
|
||||
clear
|
||||
|
||||
openim::util::desc "Run unit tests and get test coverage"
|
||||
openim::util::run "make cover"
|
||||
clear
|
||||
|
||||
openim::util::desc "Check for updates to go.mod dependencies"
|
||||
openim::util::run "make updates"
|
||||
clear
|
||||
|
||||
openim::util::desc "Clean"
|
||||
openim::util::run "make clean"
|
||||
clear
|
||||
|
||||
openim::util::desc "Generate all necessary files"
|
||||
openim::util::run "make gen"
|
||||
clear
|
||||
|
||||
openim::util::desc "Generate swagger document"
|
||||
openim::util::run "make swagger"
|
||||
clear
|
||||
|
||||
openim::util::desc "Serve swagger spec and docs"
|
||||
openim::util::run "make serve-swagger"
|
||||
clear
|
||||
|
||||
openim::util::desc "Verify the license headers for all files"
|
||||
openim::util::run "make verify-copyright"
|
||||
clear
|
||||
|
||||
openim::util::desc "Add copyright"
|
||||
openim::util::run "make add-copyright"
|
||||
clear
|
||||
|
||||
openim::util::desc "Project introduction, become a contributor"
|
||||
openim::util::run "make advertise"
|
||||
clear
|
||||
|
||||
openim::util::desc "Release the project"
|
||||
openim::util::run "make release"
|
||||
clear
|
||||
|
||||
openim::util::desc "Run demo"
|
||||
openim::util::run "make demo"
|
||||
clear
|
@ -0,0 +1,29 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -o errexit
|
||||
set -o nounset
|
||||
set -o pipefail
|
||||
|
||||
OPENIM_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
|
||||
source "${OPENIM_ROOT}/hack/lib/init.sh"
|
||||
|
||||
kube::golang::setup_env
|
||||
|
||||
cd "${OPENIM_ROOT}"
|
||||
|
||||
find_files() {
|
||||
find . -not \( \
|
||||
\( \
|
||||
-wholename './output' \
|
||||
-o -wholename './.git' \
|
||||
-o -wholename './_output' \
|
||||
-o -wholename './_gopath' \
|
||||
-o -wholename './release' \
|
||||
-o -wholename './target' \
|
||||
-o -wholename '*/vendor/*' \
|
||||
\) -prune \
|
||||
\) -name 'OWNERS*'
|
||||
}
|
||||
|
||||
export GO111MODULE=on
|
||||
find_files | xargs go run tools/yamlfmt/main.go
|
@ -0,0 +1,34 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# This script checks whether the OWNERS files need to be formatted or not by
|
||||
# `yamlfmt`. Run `scripts/update-yamlfmt.sh` to actually format sources.
|
||||
#
|
||||
# Usage: `scripts/verify-yamlfmt.sh`.
|
||||
|
||||
set -o errexit
|
||||
set -o nounset
|
||||
set -o pipefail
|
||||
|
||||
OPENIM_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
|
||||
source "${OPENIM_ROOT}/scripts/lib/init.sh"
|
||||
|
||||
openim::util::ensure_clean_working_dir
|
||||
# This sets up the environment, like GOCACHE, which keeps the worktree cleaner.
|
||||
openim::golang::setup_env
|
||||
|
||||
_tmpdir="$(openim::realpath "$(mktemp -d -t "$(basename "$0").XXXXXX")")"
|
||||
git worktree add -f -q "${_tmpdir}" HEAD
|
||||
openim::util::trap_add "git worktree remove -f ${_tmpdir}" EXIT
|
||||
cd "${_tmpdir}"
|
||||
|
||||
# Format YAML files
|
||||
hack/update-yamlfmt.sh
|
||||
|
||||
# Test for diffs
|
||||
diffs=$(git status --porcelain | wc -l)
|
||||
if [[ ${diffs} -gt 0 ]]; then
|
||||
echo "YAML files need to be formatted" >&2
|
||||
git diff
|
||||
echo "Please run 'hack/update-yamlfmt.sh'" >&2
|
||||
exit 1
|
||||
fi
|
@ -0,0 +1,35 @@
|
||||
// Copyright © 2023 OpenIM. All rights reserved.
|
||||
//
|
||||
// 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 "testing"
|
||||
|
||||
func Test_main(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
}{
|
||||
{
|
||||
name: "Test_main",
|
||||
},
|
||||
{
|
||||
name: "Test_main2",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
main()
|
||||
})
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
# See the OWNERS docs at https://go.k8s.io/owners
|
||||
|
||||
reviewers:
|
||||
- cubxxw
|
||||
- kubbot
|
||||
approvers:
|
||||
- cubxxw
|
||||
labels:
|
||||
- sig/testing
|
||||
- sig/contributor-experience
|
@ -0,0 +1,8 @@
|
||||
module github.com/OpenIMSDK/Open-IM-Server/tools/yamlfmt
|
||||
|
||||
go 1.20
|
||||
|
||||
require (
|
||||
github.com/likexian/gokit v0.25.13
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
)
|
@ -0,0 +1,6 @@
|
||||
github.com/likexian/gokit v0.25.13 h1:p2Uw3+6fGG53CwdU2Dz0T6bOycdb2+bAFAa3ymwWVkM=
|
||||
github.com/likexian/gokit v0.25.13/go.mod h1:qQhEWFBEfqLCO3/vOEo2EDKd+EycekVtUK4tex+l2H4=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
@ -0,0 +1,58 @@
|
||||
// OPENIM plan on prow tools
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Prow OWNERs file defines the default indent as 2 spaces.
|
||||
indent := flag.Int("indent", 2, "default indent")
|
||||
flag.Parse()
|
||||
for _, path := range flag.Args() {
|
||||
sourceYaml, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "%s: %v\n", path, err)
|
||||
continue
|
||||
}
|
||||
rootNode, err := fetchYaml(sourceYaml)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "%s: %v\n", path, err)
|
||||
continue
|
||||
}
|
||||
writer, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "%s: %v\n", path, err)
|
||||
continue
|
||||
}
|
||||
err = streamYaml(writer, indent, rootNode)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "%s: %v\n", path, err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func fetchYaml(sourceYaml []byte) (*yaml.Node, error) {
|
||||
rootNode := yaml.Node{}
|
||||
err := yaml.Unmarshal(sourceYaml, &rootNode)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &rootNode, nil
|
||||
}
|
||||
|
||||
func streamYaml(writer io.Writer, indent *int, in *yaml.Node) error {
|
||||
encoder := yaml.NewEncoder(writer)
|
||||
encoder.SetIndent(*indent)
|
||||
err := encoder.Encode(in)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return encoder.Close()
|
||||
}
|
@ -0,0 +1,144 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/likexian/gokit/assert"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func Test_main(t *testing.T) {
|
||||
sourceYaml := ` # See the OWNERS docs at https://go.k8s.io/owners
|
||||
approvers:
|
||||
- dep-approvers
|
||||
- thockin # Network
|
||||
- liggitt
|
||||
|
||||
labels:
|
||||
- sig/architecture
|
||||
`
|
||||
|
||||
outputYaml := `# See the OWNERS docs at https://go.k8s.io/owners
|
||||
approvers:
|
||||
- dep-approvers
|
||||
- thockin # Network
|
||||
- liggitt
|
||||
labels:
|
||||
- sig/architecture
|
||||
`
|
||||
node, _ := fetchYaml([]byte(sourceYaml))
|
||||
var output bytes.Buffer
|
||||
indent := 2
|
||||
writer := bufio.NewWriter(&output)
|
||||
_ = streamYaml(writer, &indent, node)
|
||||
_ = writer.Flush()
|
||||
assert.Equal(t, outputYaml, string(output.Bytes()), "yaml was not formatted correctly")
|
||||
}
|
||||
|
||||
func Test_fetchYaml(t *testing.T) {
|
||||
type args struct {
|
||||
sourceYaml []byte
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want *yaml.Node
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "Valid YAML",
|
||||
args: args{sourceYaml: []byte("key: value")},
|
||||
want: &yaml.Node{
|
||||
Kind: yaml.MappingNode,
|
||||
Tag: "!!map",
|
||||
Value: "",
|
||||
Content: []*yaml.Node{
|
||||
&yaml.Node{
|
||||
Kind: yaml.ScalarNode,
|
||||
Tag: "!!str",
|
||||
Value: "key",
|
||||
},
|
||||
&yaml.Node{
|
||||
Kind: yaml.ScalarNode,
|
||||
Tag: "!!str",
|
||||
Value: "value",
|
||||
},
|
||||
},
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "Invalid YAML",
|
||||
args: args{sourceYaml: []byte("key:")},
|
||||
want: nil,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := fetchYaml(tt.args.sourceYaml)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("fetchYaml() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("fetchYaml() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_streamYaml(t *testing.T) {
|
||||
type args struct {
|
||||
indent *int
|
||||
in *yaml.Node
|
||||
}
|
||||
defaultIndent := 2
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantWriter string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "Valid YAML node with default indent",
|
||||
args: args{
|
||||
indent: &defaultIndent,
|
||||
in: &yaml.Node{
|
||||
Kind: yaml.MappingNode,
|
||||
Tag: "!!map",
|
||||
Value: "",
|
||||
Content: []*yaml.Node{
|
||||
&yaml.Node{
|
||||
Kind: yaml.ScalarNode,
|
||||
Tag: "!!str",
|
||||
Value: "key",
|
||||
},
|
||||
&yaml.Node{
|
||||
Kind: yaml.ScalarNode,
|
||||
Tag: "!!str",
|
||||
Value: "value",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
wantWriter: "key: value\n",
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
writer := &bytes.Buffer{}
|
||||
if err := streamYaml(writer, tt.args.indent, tt.args.in); (err != nil) != tt.wantErr {
|
||||
t.Errorf("streamYaml() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if gotWriter := writer.String(); gotWriter != tt.wantWriter {
|
||||
t.Errorf("streamYaml() = %v, want %v", gotWriter, tt.wantWriter)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in new issue