From 065a7238fd99defa1a18d755edbf1c80b5d12b13 Mon Sep 17 00:00:00 2001 From: "Xinwei Xiong (cubxxw)" <3293172751nss@gmail.com> Date: Sat, 16 Mar 2024 18:03:12 +0800 Subject: [PATCH] fix: fix cicd build project --- Dockerfile | 3 +-- README.md | 50 +++++++++++++++++++++++++++++++++++++++ config/config.go | 40 +++++++++++++++++++++---------- deploy/cld/Dockerfile.cld | 2 +- go.mod | 7 +++++- go.sum | 4 ++++ 6 files changed, 90 insertions(+), 16 deletions(-) diff --git a/Dockerfile b/Dockerfile index e7c90ea..cbbafc4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -18,6 +18,5 @@ FROM alpine:latest WORKDIR /app COPY --from=builder /build/_output/bin/platforms/linux/amd64/cld . -COPY --from=builder /build/config.yaml . -ENTRYPOINT ["./cld", "-config", "config.yaml"] +ENTRYPOINT ["./cld"] diff --git a/README.md b/README.md index 16d60aa..1780f75 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,53 @@ # comment-lang-detector A GitHub Action for detecting specified languages (e.g., Chinese or Japanese) in comments within code files across multiple programming languages (YAML, Go, Java, Rust). Ideal for projects aiming to adhere to internationalization standards or maintain language-specific coding guidelines. + +## Use + + + + + +## Configuration Strategy for Code Language Detector + +The Code Language Detector (CLD) Action offers a flexible approach to configuration, allowing users to specify the path to their configuration file in several ways. This ensures ease of use and adaptability to various workflows. Below is the hierarchy in which the CLD Action searches for the configuration file: + +1. **Command-Line Argument**: + - The Action first checks if the configuration file path is provided as a command-line argument (`--config`). This method is ideal for users who wish to specify a custom configuration path directly in their workflow file. + + ```yaml + steps: + - name: Detect specified languages in comments + uses: kubecub/comment-lang-detector@v1 + with: + args: --config path/to/your/config.yaml + ``` + +2. **Environment Variable**: + - If the configuration file path is not specified as a command-line argument, the Action then looks for an environment variable named `CLD_CONFIG_PATH`. This method provides a flexible way to set the configuration path without modifying the workflow file. + + ```yaml + steps: + - name: Detect specified languages in comments + uses: kubecub/comment-lang-detector@v1 + env: + CLD_CONFIG_PATH: path/to/your/config.yaml + ``` + +3. **Default Configuration Paths**: + - In the absence of both a command-line argument and an environment variable, the Action attempts to locate the configuration file at default paths: + - `config.yaml`: The root directory of the repository. + - `.github/code-language-detector.yml`: Inside the `.github` folder, allowing for a clean repository structure. + + The Action will search for these files in the specified order. If `config.yaml` is found, it will be used; if not, the Action will look for `.github/code-language-detector.yml`. + +4. **Error on Missing Configuration**: + - If the Action cannot locate a configuration file through any of the aforementioned methods, it will terminate with an error message. This ensures that the Action only runs when it is properly configured, preventing unexpected behavior. + +### Important Notes + +- **Configuration File Format**: The configuration file must be in YAML format. Ensure that your configuration adheres to the required schema, which includes the languages and programming languages to be detected, among other settings. + +- **Customizing the Configuration Path**: We recommend using the command-line argument or environment variable methods for specifying the configuration path when you have multiple workflows or need to adhere to different coding standards across projects. + +This configuration strategy is designed to maximize flexibility and ease of use, ensuring that users can integrate the CLD Action into their workflows efficiently and effectively. diff --git a/config/config.go b/config/config.go index aebf0d4..b10ad16 100644 --- a/config/config.go +++ b/config/config.go @@ -16,7 +16,7 @@ package config import ( "flag" - "log" + "fmt" "os" "gopkg.in/yaml.v2" @@ -30,20 +30,36 @@ type Config struct { func ParseConfig() (Config, error) { var configPath string - flag.StringVar(&configPath, "config", "./", "Path to config file") + flag.StringVar(&configPath, "config", "", "Path to config file") flag.Parse() - var config Config - if configPath != "" { - configFile, err := os.ReadFile(configPath) - if err != nil { - return Config{}, err - } - if err := yaml.Unmarshal(configFile, &config); err != nil { - return Config{}, err + if configPath == "" { + configPath = os.Getenv("CLD_CONFIG_PATH") + } + + if configPath == "" { + defaultPaths := []string{"config.yaml", ".github/code-language-detector.yml"} + for _, path := range defaultPaths { + if _, err := os.Stat(path); err == nil { + configPath = path + break + } } - } else { - log.Fatal("Config file must be provided") } + + if configPath == "" { + return Config{}, fmt.Errorf("config file not found") + } + + configFile, err := os.ReadFile(configPath) + if err != nil { + return Config{}, err + } + + var config Config + if err := yaml.Unmarshal(configFile, &config); err != nil { + return Config{}, err + } + return config, nil } diff --git a/deploy/cld/Dockerfile.cld b/deploy/cld/Dockerfile.cld index bbe96c3..e163593 100644 --- a/deploy/cld/Dockerfile.cld +++ b/deploy/cld/Dockerfile.cld @@ -10,4 +10,4 @@ RUN cd /app; go build -o _output/bin/cld cmd/cld/main.go FROM scratch COPY --from=builder /app/_output/bin/cld /app -ENTRYPOINT ["/app/cld --help"] \ No newline at end of file +ENTRYPOINT ["/app/cld"] \ No newline at end of file diff --git a/go.mod b/go.mod index aa75f88..72078f0 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,9 @@ module github.com/kubecub/comment-lang-detector go 1.19 -require gopkg.in/yaml.v2 v2.4.0 +require ( + github.com/bugsnag/bugsnag-go v2.3.0+incompatible + gopkg.in/yaml.v2 v2.4.0 +) + +require github.com/pkg/errors v0.9.1 // indirect diff --git a/go.sum b/go.sum index dd0bc19..3dffa63 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,7 @@ +github.com/bugsnag/bugsnag-go v2.3.0+incompatible h1:NobKC6g8bl3hbZ0XHSfb/Y9Ut4ynhJMh0uEhHaLingU= +github.com/bugsnag/bugsnag-go v2.3.0+incompatible/go.mod h1:2oa8nejYd4cQ/b0hMIopN0lCRxU0bueqREvZLWFrtK8= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= 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=