-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.go
More file actions
215 lines (179 loc) · 5.05 KB
/
main.go
File metadata and controls
215 lines (179 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Copyright 2024 Harness Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"log/slog"
"os"
"path/filepath"
"github.com/drone/go-task/task"
"github.com/drone/go-task/task/cloner"
"github.com/drone/go-task/task/common"
download "github.com/drone/go-task/task/downloader"
"github.com/drone/go-task/task/drivers/cgi"
"github.com/drone/go-task/task/expression"
"github.com/drone/go-task/task/packaged"
)
var (
// path to the task file
path = flag.String("path", "", "")
// pretty print the task output
pretty = flag.Bool("pretty", false, "")
// runs with verbose output if true
verbose = flag.Bool("verbose", false, "")
// displays the help / usage if true
help = flag.Bool("help", false, "")
// resolve mode flags
resolveExpr = flag.String("resolve", "", "")
secretsJSON = flag.String("secrets", "", "")
)
func main() {
// parse the input parameters
flag.BoolVar(help, "h", false, "")
flag.BoolVar(verbose, "v", false, "")
flag.Usage = usage
flag.Parse()
if *help {
flag.Usage()
os.Exit(0)
}
// handle resolve mode
if *resolveExpr != "" {
handleResolve(*resolveExpr, *secretsJSON)
return
}
// set the default log level
level := slog.LevelInfo
if *verbose {
level = slog.LevelDebug
}
// set the default logger (used by handlers)
slog.SetDefault(
slog.New(
slog.NewJSONHandler(
os.Stdout,
&slog.HandlerOptions{Level: level},
),
),
)
// user may specify the path as a non-flag variable
if flag.NArg() > 0 {
path = &flag.Args()[0]
}
// parse the task file
data, err := os.ReadFile(*path)
if err != nil {
log.Fatalln(err)
}
// unmarshal the task file into a request
req := new(task.Request)
if err := json.Unmarshal(data, req); err != nil {
log.Fatalln(err)
}
cache, err := os.UserCacheDir()
if err != nil {
log.Fatalln(err)
}
// create the task downloader which downloads and
// caches tasks at ~/.cache/harness/task
downloader := download.New(
// use the built-in cloner which uses
// os/exec to clone the repository.
//
// this avoids any external dependencies
// in this module, however, a production
// installation may want to provide a
// custom implementation that uses a native
// go git module to avoid os/exec.
cloner.Default(),
// top-level directory where the downloading should happen
filepath.Join(cache, "download"),
)
packageLoader := packaged.New(filepath.Join(cache, "default"))
// create the task router
router := task.NewRouter()
router.RegisterFunc("sample/exec", execHandler) // sample bult-in handler
router.RegisterFunc("sample/file", fileHandler) // sample bult-in handler
router.NotFound(
// default to cgi handler when no built-in
// task handler is found.
cgi.New(
// use the default downloader which
// caches tasks at ~/.cache/harness/task
downloader,
packageLoader,
),
)
// handle the request
res := router.Handle(context.Background(), req)
if err := res.Error(); err != nil {
log.Fatalln(err)
}
// if the response is an error, print the error
// message and exit with failure.
if err := res.Error(); err != nil {
log.Fatalln(err)
}
if *pretty {
// write the task details to stdout
fmt.Fprintf(os.Stdout, "id: %s", req.Task.ID)
fmt.Fprintln(os.Stdout, "")
fmt.Fprintf(os.Stdout, "type: %s", req.Task.Type)
fmt.Fprintln(os.Stdout, "")
fmt.Fprintln(os.Stdout, "")
// decode the response body into a temporary
// data structure.
var temp any
json.Unmarshal(res.Body(), &temp)
// re-encode the response body as json with
// indentation.
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
enc.Encode(temp)
return
}
// write the logs to stdout
os.Stdout.Write(res.Body())
}
func handleResolve(inputJson, secretsJSON string) {
// parse secrets from JSON
var secrets []*common.Secret
if secretsJSON != "" {
if err := json.Unmarshal([]byte(secretsJSON), &secrets); err != nil {
log.Fatalf("Failed to parse secrets JSON: %v", err)
}
}
// validate input is valid JSON
var testJSON interface{}
if err := json.Unmarshal([]byte(inputJson), &testJSON); err != nil {
log.Fatalf("Input must be valid JSON: %v", err)
}
// create resolver
resolver := expression.New(secrets)
// resolve the expression
resolvedData, _, err := resolver.Resolve([]byte(inputJson))
if err != nil {
log.Fatalf("Failed to resolve expression: %v", err)
}
// print resolved result
fmt.Println(string(resolvedData))
}
var usage = func() {
println(`Usage: go-task [OPTION]... [PATH]
--path path to the task file
--pretty pretty print the task output
-v, --verbose execute the task with verbose output
-h, --help display this help and exit
Expression Resolver Mode:
--resolve expression string to resolve
--secrets JSON array of secrets [{"id":"key","value":"val"}]
Examples:
go-task path/to/task.json
go-task --resolve "Hello \${{secrets.name}}" --secrets '[{"id":"name","value":"World"}]'
`)
}