|
1 | 1 | package main
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "context" |
| 5 | + "encoding/json" |
4 | 6 | "fmt"
|
| 7 | + "io" |
| 8 | + "log" |
| 9 | + "net/http" |
5 | 10 | "os"
|
| 11 | + "strconv" |
| 12 | + "strings" |
| 13 | + "time" |
| 14 | + |
| 15 | + "github.com/BurntSushi/toml" |
| 16 | +) |
| 17 | + |
| 18 | +type Config struct { |
| 19 | + InputText string `toml:"input_text"` |
| 20 | + FindWord string `toml:"find_word"` |
| 21 | + ReplaceWord string `toml:"replace_word"` |
| 22 | + NumberList []float64 `toml:"number_list"` |
| 23 | + InputFile string `toml:"input_file"` |
| 24 | + OutputFile string `toml:"output_file"` |
| 25 | + AppendText string `toml:"append_text"` |
| 26 | + ApiURL string `toml:"api_url"` |
| 27 | +} |
| 28 | + |
| 29 | +const ( |
| 30 | + defaultConfigPath = ".github/configs/setup-my-action.toml" |
| 31 | + httpTimeout = 10 * time.Second |
| 32 | + contextTimeout = 15 * time.Second |
6 | 33 | )
|
7 | 34 |
|
| 35 | +func loadConfig(path string) (*Config, error) { |
| 36 | + if path == "" { |
| 37 | + path = defaultConfigPath |
| 38 | + } |
| 39 | + |
| 40 | + var config Config |
| 41 | + if _, err := toml.DecodeFile(path, &config); err != nil { |
| 42 | + return nil, fmt.Errorf("failed to decode TOML file: %w", err) |
| 43 | + } |
| 44 | + |
| 45 | + // Validate config |
| 46 | + if err := validateConfig(&config); err != nil { |
| 47 | + return nil, fmt.Errorf("invalid configuration: %w", err) |
| 48 | + } |
| 49 | + |
| 50 | + return &config, nil |
| 51 | +} |
| 52 | + |
| 53 | +func validateConfig(config *Config) error { |
| 54 | + if config.InputText == "" { |
| 55 | + return fmt.Errorf("input_text is required") |
| 56 | + } |
| 57 | + if config.InputFile == "" || config.OutputFile == "" { |
| 58 | + return fmt.Errorf("input_file and output_file are required") |
| 59 | + } |
| 60 | + if config.ApiURL == "" { |
| 61 | + return fmt.Errorf("api_url is required") |
| 62 | + } |
| 63 | + return nil |
| 64 | +} |
| 65 | + |
| 66 | +func processText(inputText, findWord, replaceWord string) (string, int) { |
| 67 | + processedText := strings.ReplaceAll(inputText, findWord, replaceWord) |
| 68 | + wordCount := len(strings.Fields(inputText)) |
| 69 | + return processedText, wordCount |
| 70 | +} |
| 71 | + |
| 72 | +func calculateSumAndAverage(numbers []float64) (float64, float64) { |
| 73 | + sum := 0.0 |
| 74 | + for _, num := range numbers { |
| 75 | + sum += num |
| 76 | + } |
| 77 | + average := sum / float64(len(numbers)) |
| 78 | + return sum, average |
| 79 | +} |
| 80 | + |
| 81 | +func readAndAppendToFile(inputFile, outputFile, appendText string) error { |
| 82 | + content, err := os.ReadFile(inputFile) |
| 83 | + if err != nil { |
| 84 | + return fmt.Errorf("failed to read input file: %w", err) |
| 85 | + } |
| 86 | + |
| 87 | + modifiedContent := string(content) + "\n" + appendText |
| 88 | + |
| 89 | + return os.WriteFile(outputFile, []byte(modifiedContent), 0644) |
| 90 | +} |
| 91 | + |
| 92 | +func callAPIAndExtractField(ctx context.Context, apiURL string) (string, error) { |
| 93 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, apiURL, nil) |
| 94 | + if err != nil { |
| 95 | + return "", fmt.Errorf("failed to create request: %w", err) |
| 96 | + } |
| 97 | + |
| 98 | + client := &http.Client{Timeout: httpTimeout} |
| 99 | + resp, err := client.Do(req) |
| 100 | + if err != nil { |
| 101 | + return "", fmt.Errorf("failed to make API request: %w", err) |
| 102 | + } |
| 103 | + defer func(Body io.ReadCloser) { |
| 104 | + err := Body.Close() |
| 105 | + if err != nil { |
| 106 | + log.Printf("Failed to close response body: %v", err) |
| 107 | + } |
| 108 | + }(resp.Body) |
| 109 | + |
| 110 | + body, err := io.ReadAll(resp.Body) |
| 111 | + if err != nil { |
| 112 | + return "", fmt.Errorf("failed to read API response: %w", err) |
| 113 | + } |
| 114 | + |
| 115 | + var result map[string]interface{} |
| 116 | + if err := json.Unmarshal(body, &result); err != nil { |
| 117 | + return "", fmt.Errorf("failed to parse JSON response: %w", err) |
| 118 | + } |
| 119 | + |
| 120 | + dataField, ok := result["data"].(string) |
| 121 | + if !ok { |
| 122 | + return "", fmt.Errorf("field 'data' not found in API response or is not a string") |
| 123 | + } |
| 124 | + |
| 125 | + return dataField, nil |
| 126 | +} |
| 127 | + |
| 128 | +func setOutput(name, value string) { |
| 129 | + fmt.Printf("::set-output name=%s::%s\n", name, value) |
| 130 | +} |
| 131 | + |
8 | 132 | func main() {
|
9 |
| - // 从环境变量中读取 GitHub Action 的输入 |
10 |
| - input := os.Getenv("INPUT_MY_INPUT") |
11 |
| - if input == "" { |
12 |
| - fmt.Println("No input provided") |
13 |
| - return |
| 133 | + configPath := os.Getenv("INPUT_CONFIG_PATH") |
| 134 | + config, err := loadConfig(configPath) |
| 135 | + if err != nil { |
| 136 | + log.Fatalf("Failed to load configuration: %v", err) |
14 | 137 | }
|
15 | 138 |
|
16 |
| - // 打印输出以便在 GitHub Action 日志中查看 |
17 |
| - fmt.Printf("Hello, %s!\n", input) |
| 139 | + // 1. 文本处理 |
| 140 | + processedText, wordCount := processText(config.InputText, config.FindWord, config.ReplaceWord) |
| 141 | + setOutput("processed_text", processedText) |
| 142 | + setOutput("word_count", strconv.Itoa(wordCount)) |
| 143 | + |
| 144 | + // 2. 列表处理 |
| 145 | + sum, average := calculateSumAndAverage(config.NumberList) |
| 146 | + setOutput("sum", strconv.FormatFloat(sum, 'f', -1, 64)) |
| 147 | + setOutput("average", strconv.FormatFloat(average, 'f', -1, 64)) |
| 148 | + |
| 149 | + // 3. 文件处理 |
| 150 | + if err := readAndAppendToFile(config.InputFile, config.OutputFile, config.AppendText); err != nil { |
| 151 | + log.Fatalf("File processing error: %v", err) |
| 152 | + } |
| 153 | + |
| 154 | + // 4. API 请求处理 |
| 155 | + ctx, cancel := context.WithTimeout(context.Background(), contextTimeout) |
| 156 | + defer cancel() |
| 157 | + |
| 158 | + responseField, err := callAPIAndExtractField(ctx, config.ApiURL) |
| 159 | + if err != nil { |
| 160 | + log.Fatalf("API request error: %v", err) |
| 161 | + } |
| 162 | + setOutput("response_field", responseField) |
18 | 163 | }
|
0 commit comments