|
| 1 | +# 基本解析 |
| 2 | + |
| 3 | +此示例演示了 GitHub Action Parser 库的基本解析功能。 |
| 4 | + |
| 5 | +## 解析 Action 文件 |
| 6 | + |
| 7 | +最基本的操作是解析 GitHub Action 文件: |
| 8 | + |
| 9 | +```go |
| 10 | +package main |
| 11 | + |
| 12 | +import ( |
| 13 | + "fmt" |
| 14 | + "log" |
| 15 | + "github.com/scagogogo/github-action-parser/pkg/parser" |
| 16 | +) |
| 17 | + |
| 18 | +func main() { |
| 19 | + // 解析 action.yml 文件 |
| 20 | + action, err := parser.ParseFile("action.yml") |
| 21 | + if err != nil { |
| 22 | + log.Fatalf("解析 action 失败: %v", err) |
| 23 | + } |
| 24 | + |
| 25 | + // 显示基本信息 |
| 26 | + fmt.Printf("Action 名称: %s\n", action.Name) |
| 27 | + fmt.Printf("描述: %s\n", action.Description) |
| 28 | + fmt.Printf("作者: %s\n", action.Author) |
| 29 | +} |
| 30 | +``` |
| 31 | + |
| 32 | +## 访问输入参数 |
| 33 | + |
| 34 | +```go |
| 35 | +// 访问输入参数 |
| 36 | +fmt.Println("\n输入:") |
| 37 | +for name, input := range action.Inputs { |
| 38 | + fmt.Printf(" %s:\n", name) |
| 39 | + fmt.Printf(" 描述: %s\n", input.Description) |
| 40 | + fmt.Printf(" 必需: %t\n", input.Required) |
| 41 | + if input.Default != "" { |
| 42 | + fmt.Printf(" 默认值: %s\n", input.Default) |
| 43 | + } |
| 44 | + if input.Deprecated { |
| 45 | + fmt.Printf(" 已弃用: true\n") |
| 46 | + } |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +## 访问输出参数 |
| 51 | + |
| 52 | +```go |
| 53 | +// 访问输出参数 |
| 54 | +fmt.Println("\n输出:") |
| 55 | +for name, output := range action.Outputs { |
| 56 | + fmt.Printf(" %s:\n", name) |
| 57 | + fmt.Printf(" 描述: %s\n", output.Description) |
| 58 | + if output.Value != "" { |
| 59 | + fmt.Printf(" 值: %s\n", output.Value) |
| 60 | + } |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +## 检查 Action 类型 |
| 65 | + |
| 66 | +```go |
| 67 | +// 检查这是什么类型的 action |
| 68 | +fmt.Printf("\nAction 类型: %s\n", action.Runs.Using) |
| 69 | + |
| 70 | +switch action.Runs.Using { |
| 71 | +case "composite": |
| 72 | + fmt.Printf("复合 action,包含 %d 个步骤\n", len(action.Runs.Steps)) |
| 73 | +case "docker": |
| 74 | + fmt.Printf("Docker action,使用镜像: %s\n", action.Runs.Image) |
| 75 | +case "node16", "node20": |
| 76 | + fmt.Printf("JavaScript action,主文件: %s\n", action.Runs.Main) |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +## 访问品牌信息 |
| 81 | + |
| 82 | +```go |
| 83 | +// 访问品牌信息 |
| 84 | +if action.Branding.Icon != "" || action.Branding.Color != "" { |
| 85 | + fmt.Println("\n品牌:") |
| 86 | + if action.Branding.Icon != "" { |
| 87 | + fmt.Printf(" 图标: %s\n", action.Branding.Icon) |
| 88 | + } |
| 89 | + if action.Branding.Color != "" { |
| 90 | + fmt.Printf(" 颜色: %s\n", action.Branding.Color) |
| 91 | + } |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +## 完整示例 |
| 96 | + |
| 97 | +这是一个演示所有基本解析功能的完整示例: |
| 98 | + |
| 99 | +```go |
| 100 | +package main |
| 101 | + |
| 102 | +import ( |
| 103 | + "fmt" |
| 104 | + "log" |
| 105 | + "os" |
| 106 | + "github.com/scagogogo/github-action-parser/pkg/parser" |
| 107 | +) |
| 108 | + |
| 109 | +func main() { |
| 110 | + if len(os.Args) < 2 { |
| 111 | + log.Fatal("用法: go run main.go <action.yml>") |
| 112 | + } |
| 113 | + |
| 114 | + actionFile := os.Args[1] |
| 115 | + |
| 116 | + // 解析 action 文件 |
| 117 | + action, err := parser.ParseFile(actionFile) |
| 118 | + if err != nil { |
| 119 | + log.Fatalf("解析 %s 失败: %v", actionFile, err) |
| 120 | + } |
| 121 | + |
| 122 | + // 显示全面信息 |
| 123 | + displayActionInfo(action) |
| 124 | +} |
| 125 | + |
| 126 | +func displayActionInfo(action *parser.ActionFile) { |
| 127 | + fmt.Printf("=== Action 信息 ===\n") |
| 128 | + fmt.Printf("名称: %s\n", action.Name) |
| 129 | + fmt.Printf("描述: %s\n", action.Description) |
| 130 | + |
| 131 | + if action.Author != "" { |
| 132 | + fmt.Printf("作者: %s\n", action.Author) |
| 133 | + } |
| 134 | + |
| 135 | + // 显示输入 |
| 136 | + if len(action.Inputs) > 0 { |
| 137 | + fmt.Printf("\n=== 输入 (%d) ===\n", len(action.Inputs)) |
| 138 | + for name, input := range action.Inputs { |
| 139 | + fmt.Printf("• %s", name) |
| 140 | + if input.Required { |
| 141 | + fmt.Printf(" (必需)") |
| 142 | + } |
| 143 | + fmt.Printf("\n %s\n", input.Description) |
| 144 | + if input.Default != "" { |
| 145 | + fmt.Printf(" 默认值: %s\n", input.Default) |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + // 显示输出 |
| 151 | + if len(action.Outputs) > 0 { |
| 152 | + fmt.Printf("\n=== 输出 (%d) ===\n", len(action.Outputs)) |
| 153 | + for name, output := range action.Outputs { |
| 154 | + fmt.Printf("• %s\n", name) |
| 155 | + fmt.Printf(" %s\n", output.Description) |
| 156 | + if output.Value != "" { |
| 157 | + fmt.Printf(" 值: %s\n", output.Value) |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + // 显示运行时信息 |
| 163 | + fmt.Printf("\n=== 运行时 ===\n") |
| 164 | + fmt.Printf("使用: %s\n", action.Runs.Using) |
| 165 | + |
| 166 | + switch action.Runs.Using { |
| 167 | + case "composite": |
| 168 | + fmt.Printf("步骤: %d\n", len(action.Runs.Steps)) |
| 169 | + for i, step := range action.Runs.Steps { |
| 170 | + fmt.Printf(" %d. %s\n", i+1, step.Name) |
| 171 | + } |
| 172 | + case "docker": |
| 173 | + fmt.Printf("镜像: %s\n", action.Runs.Image) |
| 174 | + if action.Runs.Entrypoint != "" { |
| 175 | + fmt.Printf("入口点: %s\n", action.Runs.Entrypoint) |
| 176 | + } |
| 177 | + case "node16", "node20": |
| 178 | + fmt.Printf("主文件: %s\n", action.Runs.Main) |
| 179 | + if action.Runs.Pre != "" { |
| 180 | + fmt.Printf("预执行: %s\n", action.Runs.Pre) |
| 181 | + } |
| 182 | + if action.Runs.Post != "" { |
| 183 | + fmt.Printf("后执行: %s\n", action.Runs.Post) |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + // 显示品牌 |
| 188 | + if action.Branding.Icon != "" || action.Branding.Color != "" { |
| 189 | + fmt.Printf("\n=== 品牌 ===\n") |
| 190 | + if action.Branding.Icon != "" { |
| 191 | + fmt.Printf("图标: %s\n", action.Branding.Icon) |
| 192 | + } |
| 193 | + if action.Branding.Color != "" { |
| 194 | + fmt.Printf("颜色: %s\n", action.Branding.Color) |
| 195 | + } |
| 196 | + } |
| 197 | +} |
| 198 | +``` |
| 199 | + |
| 200 | +## 错误处理 |
| 201 | + |
| 202 | +解析文件时始终适当处理错误: |
| 203 | + |
| 204 | +```go |
| 205 | +action, err := parser.ParseFile("action.yml") |
| 206 | +if err != nil { |
| 207 | + // 检查特定错误类型 |
| 208 | + if os.IsNotExist(err) { |
| 209 | + log.Fatal("Action 文件不存在") |
| 210 | + } else if strings.Contains(err.Error(), "yaml") { |
| 211 | + log.Fatal("Action 文件中的 YAML 语法无效") |
| 212 | + } else { |
| 213 | + log.Fatalf("解析 action 失败: %v", err) |
| 214 | + } |
| 215 | +} |
| 216 | +``` |
| 217 | + |
| 218 | +## 下一步 |
| 219 | + |
| 220 | +- 了解 [工作流解析](/zh/examples/workflow-parsing) |
| 221 | +- 探索 [验证](/zh/examples/validation) 功能 |
| 222 | +- 查看 [工具函数](/zh/examples/utilities) |
0 commit comments