# Conflicts: # internal/msggateway/user_map.gopull/2148/head
commit
b9c62b20b8
@ -1,56 +0,0 @@
|
||||
# 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.
|
||||
|
||||
# OpenIM base image: https://github.com/openim-sigs/openim-base-image
|
||||
|
||||
# Set go mod installation source and proxy
|
||||
|
||||
FROM golang:1.20 AS builder
|
||||
|
||||
ARG GO111MODULE=on
|
||||
|
||||
WORKDIR /openim/openim-server
|
||||
|
||||
ENV GO111MODULE=$GO111MODULE
|
||||
ENV GOPROXY=$GOPROXY
|
||||
|
||||
RUN apt-get update && apt-get install -y curl unzip
|
||||
|
||||
RUN curl -LO https://app-1302656840.cos.ap-nanjing.myqcloud.com/dist.zip \
|
||||
&& unzip dist.zip -d ./ \
|
||||
&& rm dist.zip
|
||||
|
||||
COPY go.mod go.sum ./
|
||||
RUN go mod download
|
||||
|
||||
COPY . .
|
||||
|
||||
RUN make clean
|
||||
RUN make build BINS=openim-web
|
||||
|
||||
FROM ghcr.io/openim-sigs/openim-ubuntu-image:latest
|
||||
|
||||
WORKDIR /openim/openim-server
|
||||
|
||||
COPY --from=builder /openim/openim-server/_output/bin/tools /openim/openim-server/_output/bin/tools/
|
||||
COPY --from=builder /openim/openim-server/dist /openim/openim-server/dist
|
||||
|
||||
ENV PORT 11001
|
||||
ENV DISTPATH /openim/openim-server/dist
|
||||
|
||||
EXPOSE 11001
|
||||
|
||||
RUN mv ${OPENIM_SERVER_BINDIR}/tools/$(get_os)/$(get_arch)/openim-web /usr/bin/openim-web
|
||||
|
||||
ENTRYPOINT ["bash", "-c", "openim-web -port $PORT -distPath $DISTPATH"]
|
@ -1,102 +0,0 @@
|
||||
# Development of a Go-Based Conformity Checker for Project File and Directory Naming Standards
|
||||
|
||||
### 1. Project Overview
|
||||
|
||||
#### Project Name
|
||||
|
||||
- `GoConformityChecker`
|
||||
|
||||
#### Functionality Description
|
||||
|
||||
- Checks if the file and subdirectory names in a specified directory adhere to specific naming conventions.
|
||||
- Supports specific file types (e.g., `.go`, `.yml`, `.yaml`, `.md`, `.sh`, etc.).
|
||||
- Allows users to specify directories to be checked and directories to be ignored.
|
||||
- More read https://github.com/openimsdk/open-im-server/blob/main/docs/contrib/code-conventions.md
|
||||
|
||||
#### Naming Conventions
|
||||
|
||||
- Go files: Only underscores are allowed.
|
||||
- YAML, YML, and Markdown files: Only hyphens are allowed.
|
||||
- Directories: Only underscores are allowed.
|
||||
|
||||
### 2. File Structure
|
||||
|
||||
- `main.go`: Entry point of the program, handles command-line arguments.
|
||||
- `checker/checker.go`: Contains the core logic.
|
||||
- `config/config.go`: Parses and stores configuration information.
|
||||
|
||||
### 3. Core Code Design
|
||||
|
||||
#### main.go
|
||||
|
||||
- Parses command-line arguments, including the directory to be checked and directories to be ignored.
|
||||
- Calls the `checker` module for checking.
|
||||
|
||||
#### config.go
|
||||
|
||||
- Defines a configuration structure, such as directories to check and ignore.
|
||||
|
||||
#### checker.go
|
||||
|
||||
- Iterates through the specified directory.
|
||||
- Applies different naming rules based on file types and directory names.
|
||||
- Records files or directories that do not conform to the standards.
|
||||
|
||||
### 4. Pseudocode Example
|
||||
|
||||
#### main.go
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"GoConformityChecker/checker"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Parse command-line arguments
|
||||
var targetDir string
|
||||
var ignoreDirs string
|
||||
flag.StringVar(&targetDir, "target", ".", "Directory to check")
|
||||
flag.StringVar(&ignoreDirs, "ignore", "", "Directories to ignore")
|
||||
flag.Parse()
|
||||
|
||||
// Call the checker
|
||||
err := checker.CheckDirectory(targetDir, ignoreDirs)
|
||||
if err != nil {
|
||||
fmt.Println("Error:", err)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### checker.go
|
||||
|
||||
```go
|
||||
package checker
|
||||
|
||||
import (
|
||||
// Import necessary packages
|
||||
)
|
||||
|
||||
func CheckDirectory(targetDir, ignoreDirs string) error {
|
||||
// Iterate through the directory, applying rules to check file and directory names
|
||||
// Return any found errors or non-conformities
|
||||
return nil
|
||||
}
|
||||
```
|
||||
|
||||
### 5. Implementation Details
|
||||
|
||||
- **File and Directory Traversal**: Use Go's `path/filepath` package to traverse directories and subdirectories.
|
||||
- **Naming Rules Checking**: Apply different regex expressions for naming checks based on file extensions.
|
||||
- **Error Handling and Reporting**: Record files or directories that do not conform and report to the user.
|
||||
|
||||
### 6. Future Development and Extensions
|
||||
|
||||
- Support more file types and naming rules.
|
||||
- Provide more detailed error reports, such as showing line numbers and specific naming mistakes.
|
||||
- Add a graphical or web interface for non-command-line users.
|
||||
|
||||
The above is an overview of the entire project's design. Following this design, specific coding implementation can begin. Note that the actual implementation may need adjustments based on real-world conditions.
|
@ -1,150 +0,0 @@
|
||||
// Copyright © 2024 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 checker
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/openimsdk/open-im-server/tools/formitychecker/config"
|
||||
)
|
||||
|
||||
type Issue struct {
|
||||
Type string
|
||||
Path string
|
||||
Message string
|
||||
}
|
||||
|
||||
type Checker struct {
|
||||
Config *config.Config
|
||||
Summary struct {
|
||||
CheckedDirectories int
|
||||
CheckedFiles int
|
||||
Issues []Issue
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Checker) Check() error {
|
||||
return filepath.Walk(c.Config.BaseConfig.SearchDirectory, c.checkPath)
|
||||
}
|
||||
|
||||
func (c *Checker) checkPath(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
relativePath, err := filepath.Rel(c.Config.BaseConfig.SearchDirectory, path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if relativePath == "." {
|
||||
return nil
|
||||
}
|
||||
|
||||
if info.IsDir() {
|
||||
c.Summary.CheckedDirectories++
|
||||
if c.isIgnoredDirectory(relativePath) {
|
||||
c.Summary.Issues = append(c.Summary.Issues, Issue{
|
||||
Type: "ignoredDirectory",
|
||||
Path: path,
|
||||
Message: "This directory has been ignored",
|
||||
})
|
||||
return filepath.SkipDir
|
||||
}
|
||||
if !c.checkDirectoryName(relativePath) {
|
||||
c.Summary.Issues = append(c.Summary.Issues, Issue{
|
||||
Type: "directoryNaming",
|
||||
Path: path,
|
||||
Message: "The directory name is invalid",
|
||||
})
|
||||
}
|
||||
} else {
|
||||
if c.isIgnoredFile(path) {
|
||||
return nil
|
||||
}
|
||||
c.Summary.CheckedFiles++
|
||||
if !c.checkFileName(relativePath) {
|
||||
c.Summary.Issues = append(c.Summary.Issues, Issue{
|
||||
Type: "fileNaming",
|
||||
Path: path,
|
||||
Message: "The file name does not comply with the specification",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Checker) isIgnoredDirectory(path string) bool {
|
||||
for _, ignoredDir := range c.Config.IgnoreDirectories {
|
||||
if strings.Contains(path, ignoredDir) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (c *Checker) isIgnoredFile(path string) bool {
|
||||
ext := filepath.Ext(path)
|
||||
for _, format := range c.Config.IgnoreFormats {
|
||||
if ext == format {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (c *Checker) checkDirectoryName(path string) bool {
|
||||
dirName := filepath.Base(path)
|
||||
if c.Config.DirectoryNaming.MustBeLowercase && (dirName != strings.ToLower(dirName)) {
|
||||
return false
|
||||
}
|
||||
if !c.Config.DirectoryNaming.AllowHyphens && strings.Contains(dirName, "-") {
|
||||
return false
|
||||
}
|
||||
if !c.Config.DirectoryNaming.AllowUnderscores && strings.Contains(dirName, "_") {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (c *Checker) checkFileName(path string) bool {
|
||||
fileName := filepath.Base(path)
|
||||
ext := filepath.Ext(fileName)
|
||||
|
||||
allowHyphens := c.Config.FileNaming.AllowHyphens
|
||||
allowUnderscores := c.Config.FileNaming.AllowUnderscores
|
||||
mustBeLowercase := c.Config.FileNaming.MustBeLowercase
|
||||
|
||||
if specificNaming, ok := c.Config.FileTypeSpecificNaming[ext]; ok {
|
||||
allowHyphens = specificNaming.AllowHyphens
|
||||
allowUnderscores = specificNaming.AllowUnderscores
|
||||
mustBeLowercase = specificNaming.MustBeLowercase
|
||||
}
|
||||
|
||||
if mustBeLowercase && (fileName != strings.ToLower(fileName)) {
|
||||
return false
|
||||
}
|
||||
if !allowHyphens && strings.Contains(fileName, "-") {
|
||||
return false
|
||||
}
|
||||
if !allowUnderscores && strings.Contains(fileName, "_") {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
@ -1,80 +0,0 @@
|
||||
// Copyright © 2024 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 config
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/openimsdk/open-im-server/tools/codescan/config"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
BaseConfig struct {
|
||||
SearchDirectory string `yaml:"searchDirectory"`
|
||||
IgnoreCase bool `yaml:"ignoreCase"`
|
||||
} `yaml:"baseConfig"`
|
||||
DirectoryNaming struct {
|
||||
AllowHyphens bool `yaml:"allowHyphens"`
|
||||
AllowUnderscores bool `yaml:"allowUnderscores"`
|
||||
MustBeLowercase bool `yaml:"mustBeLowercase"`
|
||||
} `yaml:"directoryNaming"`
|
||||
FileNaming struct {
|
||||
AllowHyphens bool `yaml:"allowHyphens"`
|
||||
AllowUnderscores bool `yaml:"allowUnderscores"`
|
||||
MustBeLowercase bool `yaml:"mustBeLowercase"`
|
||||
} `yaml:"fileNaming"`
|
||||
IgnoreFormats []string `yaml:"ignoreFormats"`
|
||||
IgnoreDirectories []string `yaml:"ignoreDirectories"`
|
||||
FileTypeSpecificNaming map[string]FileTypeSpecificNaming `yaml:"fileTypeSpecificNaming"`
|
||||
}
|
||||
|
||||
type FileTypeSpecificNaming struct {
|
||||
AllowHyphens bool `yaml:"allowHyphens"`
|
||||
AllowUnderscores bool `yaml:"allowUnderscores"`
|
||||
MustBeLowercase bool `yaml:"mustBeLowercase"`
|
||||
}
|
||||
|
||||
type Issue struct {
|
||||
Type string
|
||||
Path string
|
||||
Message string
|
||||
}
|
||||
|
||||
type Checker struct {
|
||||
Config *config.Config
|
||||
Summary struct {
|
||||
CheckedDirectories int
|
||||
CheckedFiles int
|
||||
Issues []Issue
|
||||
}
|
||||
Errors []string
|
||||
}
|
||||
|
||||
func LoadConfig(configPath string) (*Config, error) {
|
||||
var config Config
|
||||
|
||||
file, err := os.ReadFile(configPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = yaml.Unmarshal(file, &config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &config, nil
|
||||
}
|
@ -1,71 +0,0 @@
|
||||
// Copyright © 2024 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 (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/openimsdk/open-im-server/tools/formitychecker/checker"
|
||||
"github.com/openimsdk/open-im-server/tools/formitychecker/config"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var configPath string
|
||||
flag.StringVar(&configPath, "config", "", "Path to the configuration file")
|
||||
flag.Parse()
|
||||
|
||||
if configPath == "" {
|
||||
configPath = os.Getenv("CONFIG_PATH")
|
||||
}
|
||||
|
||||
if configPath == "" {
|
||||
configPath = "config.yaml"
|
||||
if _, err := os.Stat(".github/formitychecker.yaml"); err == nil {
|
||||
configPath = ".github/formitychecker.yaml"
|
||||
}
|
||||
}
|
||||
|
||||
cfg, err := config.LoadConfig(configPath)
|
||||
if err != nil {
|
||||
fmt.Println("Error loading config:", err)
|
||||
return
|
||||
}
|
||||
|
||||
c := &checker.Checker{Config: cfg}
|
||||
err = c.Check()
|
||||
if err != nil {
|
||||
fmt.Println("Error during check:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// if len(c.Errors) > 0 {
|
||||
// fmt.Println("Found errors:")
|
||||
// for _, errMsg := range c.Errors {
|
||||
// fmt.Println("-", errMsg)
|
||||
// }
|
||||
// os.Exit(1)
|
||||
// }
|
||||
|
||||
summaryJSON, err := json.MarshalIndent(c.Summary, "", " ")
|
||||
if err != nil {
|
||||
fmt.Println("Error marshalling summary:", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(string(summaryJSON))
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
module github.com/openimsdk/open-im-server/tools/formitychecker
|
||||
|
||||
go 1.19
|
@ -1,47 +0,0 @@
|
||||
# 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.
|
||||
|
||||
# 使用官方Go镜像作为基础镜像
|
||||
FROM golang:1.21 AS build-env
|
||||
ENV CGO_ENABLED=0
|
||||
# 设置工作目录
|
||||
WORKDIR /app
|
||||
|
||||
# 安装curl和unzip工具
|
||||
#RUN apt-get update && apt-get install -y curl unzip
|
||||
|
||||
# 从GitHub下载并解压dist.zip
|
||||
#RUN curl -LO https://github.com/OpenIMSDK/dist.zip \
|
||||
# && unzip dist.zip -d ./ \
|
||||
# && rm dist.zip
|
||||
|
||||
# 复制Go代码到容器
|
||||
COPY . .
|
||||
|
||||
# 编译Go代码
|
||||
RUN go build -o openim-web
|
||||
|
||||
# 使用轻量级的基础镜像
|
||||
FROM debian:buster-slim
|
||||
|
||||
# 将编译好的二进制文件和dist资源复制到新的容器
|
||||
WORKDIR /app
|
||||
COPY --from=build-env /app/openim-web /app/openim-web
|
||||
COPY --from=build-env /app/dist /app/dist
|
||||
|
||||
# 开放容器的20001端口
|
||||
EXPOSE 20001
|
||||
|
||||
# 指定容器启动命令
|
||||
ENTRYPOINT ["/app/openim-web"]
|
@ -1,7 +0,0 @@
|
||||
module github.com/openimsdk/open-im-server/v3/tools/openim-web
|
||||
|
||||
go 1.19
|
||||
|
||||
require gopkg.in/yaml.v2 v2.4.0
|
||||
|
||||
require github.com/NYTimes/gziphandler v1.1.1 // indirect
|
@ -1,10 +0,0 @@
|
||||
github.com/NYTimes/gziphandler v1.1.1 h1:ZUDjpQae29j0ryrS0u/B8HZfJBtBQHjqw2rQ2cqUQ3I=
|
||||
github.com/NYTimes/gziphandler v1.1.1/go.mod h1:n/CVRwUEOgIxrgPvAQhUUr9oeUtvrhMomdKFjzJNB0c=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
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.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
@ -1,63 +0,0 @@
|
||||
// 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 (
|
||||
"flag"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/NYTimes/gziphandler"
|
||||
)
|
||||
|
||||
var (
|
||||
distPathFlag string
|
||||
portFlag string
|
||||
)
|
||||
|
||||
func init() {
|
||||
flag.StringVar(&distPathFlag, "distPath", "/app/dist", "Path to the distribution")
|
||||
flag.StringVar(&portFlag, "port", "11001", "Port to run the server on")
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
distPath := getConfigValue("DIST_PATH", distPathFlag, "/app/dist")
|
||||
fs := http.FileServer(http.Dir(distPath))
|
||||
|
||||
withGzip := gziphandler.GzipHandler(fs)
|
||||
|
||||
http.Handle("/", withGzip)
|
||||
|
||||
port := getConfigValue("PORT", portFlag, "11001")
|
||||
log.Printf("Server listening on port %s in %s...", port, distPath)
|
||||
err := http.ListenAndServe(":"+port, nil)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func getConfigValue(envKey, flagValue, fallback string) string {
|
||||
envVal := os.Getenv(envKey)
|
||||
if envVal != "" {
|
||||
return envVal
|
||||
}
|
||||
if flagValue != "" {
|
||||
return flagValue
|
||||
}
|
||||
return fallback
|
||||
}
|
@ -1,71 +0,0 @@
|
||||
// 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 (
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetConfigValue(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
envKey string
|
||||
envValue string
|
||||
flagValue string
|
||||
fallback string
|
||||
wantResult string
|
||||
}{
|
||||
{
|
||||
name: "environment variable set",
|
||||
envKey: "TEST_KEY",
|
||||
envValue: "envValue",
|
||||
flagValue: "",
|
||||
fallback: "default",
|
||||
wantResult: "envValue",
|
||||
},
|
||||
{
|
||||
name: "flag set and environment variable not set",
|
||||
envKey: "TEST_KEY",
|
||||
envValue: "",
|
||||
flagValue: "flagValue",
|
||||
fallback: "default",
|
||||
wantResult: "flagValue",
|
||||
},
|
||||
{
|
||||
name: "nothing set, use fallback",
|
||||
envKey: "TEST_KEY",
|
||||
envValue: "",
|
||||
flagValue: "",
|
||||
fallback: "default",
|
||||
wantResult: "default",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if tt.envValue != "" {
|
||||
os.Setenv(tt.envKey, tt.envValue)
|
||||
defer os.Unsetenv(tt.envKey)
|
||||
}
|
||||
|
||||
got := getConfigValue(tt.envKey, tt.flagValue, tt.fallback)
|
||||
|
||||
if got != tt.wantResult {
|
||||
t.Errorf("getConfigValue(%s, %s, %s) = %s; want %s", tt.envKey, tt.flagValue, tt.fallback, got, tt.wantResult)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in new issue