Skip to content

Commit a4c1f90

Browse files
committed
fix: input.txt file not found
1 parent 8f81390 commit a4c1f90

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

.github/workflows/ci.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,20 @@ jobs:
1313
steps:
1414
- uses: actions/checkout@v4
1515

16+
- name: Create input file
17+
run: echo "This is the original file content." > input.txt
18+
1619
- name: Run Comprehensive Action
1720
id: my_action
1821
uses: ./
1922

20-
- name: Check Outputs
23+
- name: Display outputs
2124
run: |
2225
echo "processed_text: ${{ steps.my_action.outputs.processed_text }}"
2326
echo "word_count: ${{ steps.my_action.outputs.word_count }}"
2427
echo "sum: ${{ steps.my_action.outputs.sum }}"
2528
echo "average: ${{ steps.my_action.outputs.average }}"
2629
echo "response_field: ${{ steps.my_action.outputs.response_field }}"
30+
31+
- name: Display modified file content
32+
run: cat output.txt

main.go

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,24 @@ func callAPIAndExtractField(ctx context.Context, apiURL string) (string, error)
125125
return dataField, nil
126126
}
127127

128-
func setOutput(name, value string) {
129-
fmt.Printf("::set-output name=%s::%s\n", name, value)
128+
func setOutput(name, value string) error {
129+
envFile := os.Getenv("GITHUB_OUTPUT")
130+
f, err := os.OpenFile(envFile, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
131+
if err != nil {
132+
return fmt.Errorf("failed to open GITHUB_OUTPUT file: %w", err)
133+
}
134+
defer func(f *os.File) {
135+
err := f.Close()
136+
if err != nil {
137+
log.Printf("Failed to close GITHUB_OUTPUT file: %v", err)
138+
}
139+
}(f)
140+
141+
_, err = fmt.Fprintf(f, "%s=%s\n", name, value)
142+
if err != nil {
143+
return fmt.Errorf("failed to write to GITHUB_OUTPUT file: %w", err)
144+
}
145+
return nil
130146
}
131147

132148
func main() {
@@ -138,13 +154,21 @@ func main() {
138154

139155
// 1. 文本处理
140156
processedText, wordCount := processText(config.InputText, config.FindWord, config.ReplaceWord)
141-
setOutput("processed_text", processedText)
142-
setOutput("word_count", strconv.Itoa(wordCount))
157+
if err := setOutput("processed_text", processedText); err != nil {
158+
log.Fatalf("Failed to set output: %v", err)
159+
}
160+
if err := setOutput("word_count", strconv.Itoa(wordCount)); err != nil {
161+
log.Fatalf("Failed to set output: %v", err)
162+
}
143163

144164
// 2. 列表处理
145165
sum, average := calculateSumAndAverage(config.NumberList)
146-
setOutput("sum", strconv.FormatFloat(sum, 'f', -1, 64))
147-
setOutput("average", strconv.FormatFloat(average, 'f', -1, 64))
166+
if err := setOutput("sum", strconv.FormatFloat(sum, 'f', -1, 64)); err != nil {
167+
log.Fatalf("Failed to set output: %v", err)
168+
}
169+
if err := setOutput("average", strconv.FormatFloat(average, 'f', -1, 64)); err != nil {
170+
log.Fatalf("Failed to set output: %v", err)
171+
}
148172

149173
// 3. 文件处理
150174
if err := readAndAppendToFile(config.InputFile, config.OutputFile, config.AppendText); err != nil {
@@ -159,5 +183,7 @@ func main() {
159183
if err != nil {
160184
log.Fatalf("API request error: %v", err)
161185
}
162-
setOutput("response_field", responseField)
186+
if err := setOutput("response_field", responseField); err != nil {
187+
log.Fatalf("Failed to set output: %v", err)
188+
}
163189
}

0 commit comments

Comments
 (0)