Skip to content

Commit dbceb9b

Browse files
feat-pass-envvar implementation
1 parent e4ba732 commit dbceb9b

7 files changed

Lines changed: 31 additions & 14 deletions

File tree

‎AGENTS.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ type FDL struct {
362362
- `ListServicesWithContext(ctx, c) ([]*types.Service, error)`
363363
- `RemoveService(c, name) error` — DELETE `/system/services/{name}`
364364
- `ApplyService(svc, c, method) error` — POST or PUT `/system/services`
365-
- `RunService(c, name, token, endpoint, input) (io.ReadCloser, error)` — synchronous POST `/run/{name}`
365+
- `RunService(c, name, token, endpoint, input,header) (io.ReadCloser, error)` — synchronous POST `/run/{name}`
366366
- `JobService(c, name, token, endpoint, input) (io.ReadCloser, error)` — async POST `/job/{name}`
367367
- `ListLogs(c, name, page) (types.JobsResponse, error)` — GET `/system/logs/{name}`
368368
- `GetLogs(c, svcName, jobName, timestamps) (string, error)`

‎README.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ Flags:
360360
--decode-output decode the last base64-encoded line in the response and ignore logs
361361
-e, --endpoint string endpoint of a non registered cluster
362362
-f, --file-input string input file for the request
363+
-H, --header string header on service invocation (e.g. "Content-Type:application/json")
363364
-h, --help help for run
364365
-o, --output string file path to store the output
365366
-i, --text-input string text input string for the request
@@ -370,6 +371,11 @@ Global Flags:
370371
--config string set the location of the config file (YAML or JSON)
371372
```
372373
374+
Examples:
375+
```
376+
oscar-cli service run my-service -i '{"key":"value"}' -H "Content-Type:application/json"
377+
```
378+
373379
##### logs list
374380
375381
List the logs from a service.

‎cmd/hub_validate.go‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type hubValidateOptions struct {
3535
name string
3636
localPath string
3737
printAcceptanceCommands bool
38+
header string
3839
}
3940

4041
func (o *hubValidateOptions) applyToClient() []hub.Option {
@@ -98,7 +99,7 @@ func hubValidateFunc(cmd *cobra.Command, args []string, opts *hubValidateOptions
9899

99100
fmt.Fprintf(out, "Acceptance tests for %s\n", args[0])
100101

101-
results, err := client.ValidateService(cmd.Context(), args[0], conf.Oscar[clusterID], opts.name, opts.localPath)
102+
results, err := client.ValidateService(cmd.Context(), args[0], conf.Oscar[clusterID], opts.name, opts.localPath, opts.header)
102103
if err != nil {
103104
return err
104105
}
@@ -154,6 +155,7 @@ func makeHubValidateCmd() *cobra.Command {
154155
cmd.Flags().StringVar(&opts.apiBase, "api-base", "", "override the GitHub API base URL")
155156
cmd.Flags().StringVarP(&opts.name, "name", "n", "", "override the OSCAR service name during validation")
156157
cmd.Flags().StringVar(&opts.localPath, "local-path", "", "use a local directory containing the RO-Crate metadata instead of fetching it from GitHub")
158+
cmd.Flags().StringVarP(&opts.header, "header", "H", "", "header on service invocation (e.g. \"Content-Type:application/json\")")
157159
cmd.Flags().BoolVar(&opts.printAcceptanceCommands, "print-acceptance-commands", false, "print the acceptance test shell commands instead of executing them")
158160
if flag := cmd.Flags().Lookup("api-base"); flag != nil {
159161
flag.Hidden = true

‎cmd/service_run.go‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ func serviceRunFunc(cmd *cobra.Command, args []string) error {
6060
textInput, _ := cmd.Flags().GetString("text-input")
6161
outputFile, _ := cmd.Flags().GetString("output")
6262
decodeOutput, _ := cmd.Flags().GetBool("decode-output")
63+
header, _ := cmd.Flags().GetString("header")
6364
if inputFile == "" && textInput == "" {
6465
return errors.New("you must specify \"--file-input\" or \"--text-input\" flag")
6566
}
@@ -94,7 +95,7 @@ func serviceRunFunc(cmd *cobra.Command, args []string) error {
9495
writer.Close()
9596
}()
9697
// Make the request
97-
resBody, err := service.RunService(conf.Oscar[cluster], args[0], token, endpoint, reader)
98+
resBody, err := service.RunService(conf.Oscar[cluster], args[0], token, endpoint, reader, header)
9899
if err != nil {
99100
return err
100101
}
@@ -237,6 +238,7 @@ func makeServiceRunCmd() *cobra.Command {
237238
serviceRunCmd.Flags().StringP("file-input", "f", "", "input file for the request")
238239
serviceRunCmd.Flags().StringP("text-input", "i", "", "text input string for the request")
239240
serviceRunCmd.Flags().StringP("output", "o", "", "file path to store the output")
241+
serviceRunCmd.Flags().StringP("header", "H", "", "header on service invocation")
240242
serviceRunCmd.Flags().Bool("decode-output", false, "decode the last base64-encoded line in the response and ignore logs")
241243

242244
return serviceRunCmd

‎pkg/hub/validate.go‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ type AcceptanceCommandSet struct {
8989
}
9090

9191
// ValidateService downloads the RO-Crate metadata for the provided slug, runs its acceptance tests against the cluster and returns the aggregated results.
92-
func (c *Client) ValidateService(ctx context.Context, slug string, clusterCfg *cluster.Cluster, serviceNameOverride string, localRoot string) ([]AcceptanceResult, error) {
92+
func (c *Client) ValidateService(ctx context.Context, slug string, clusterCfg *cluster.Cluster, serviceNameOverride string, localRoot string, header string) ([]AcceptanceResult, error) {
9393
if strings.TrimSpace(slug) == "" {
9494
return nil, errors.New("service slug cannot be empty")
9595
}
@@ -110,7 +110,7 @@ func (c *Client) ValidateService(ctx context.Context, slug string, clusterCfg *c
110110
testName = test.ID
111111
}
112112
c.logf("Running acceptance test: %s\n", testName)
113-
res := c.runAcceptanceTest(ctx, repoPath, slug, test, clusterCfg, serviceNameOverride, localCratePath, serviceCache)
113+
res := c.runAcceptanceTest(ctx, repoPath, slug, test, clusterCfg, serviceNameOverride, localCratePath, serviceCache, header)
114114
c.logAcceptanceResult(res)
115115
results = append(results, res)
116116
}
@@ -178,7 +178,7 @@ func (c *Client) loadAcceptanceTests(ctx context.Context, slug string, localRoot
178178
return repoPath, localCratePath, tests, nil
179179
}
180180

181-
func (c *Client) runAcceptanceTest(ctx context.Context, repoPath, slug string, test AcceptanceTest, clusterCfg *cluster.Cluster, serviceNameOverride string, localCratePath string, svcCache map[string]*types.Service) AcceptanceResult {
181+
func (c *Client) runAcceptanceTest(ctx context.Context, repoPath, slug string, test AcceptanceTest, clusterCfg *cluster.Cluster, serviceNameOverride string, localCratePath string, svcCache map[string]*types.Service, header string) AcceptanceResult {
182182
result := AcceptanceResult{Test: test}
183183

184184
steps := test.Steps
@@ -201,7 +201,7 @@ func (c *Client) runAcceptanceTest(ctx context.Context, repoPath, slug string, t
201201
var lastOutput string
202202

203203
for _, step := range steps {
204-
stepRes := c.executeAcceptanceStep(ctx, repoPath, slug, test, step, supplyCache, clusterCfg, serviceNameOverride, localCratePath, svcCache, tempDir)
204+
stepRes := c.executeAcceptanceStep(ctx, repoPath, slug, test, step, supplyCache, clusterCfg, serviceNameOverride, localCratePath, svcCache, tempDir, header)
205205
result.StepResults = append(result.StepResults, stepRes)
206206

207207
if stepRes.Output != "" {
@@ -444,7 +444,7 @@ func shellQuote(value string) string {
444444
return value
445445
}
446446

447-
func (c *Client) executeAcceptanceStep(ctx context.Context, repoPath, slug string, test AcceptanceTest, step AcceptanceStep, baseSupply map[string]TestInput, clusterCfg *cluster.Cluster, serviceNameOverride string, localCratePath string, svcCache map[string]*types.Service, tempDir string) AcceptanceStepResult {
447+
func (c *Client) executeAcceptanceStep(ctx context.Context, repoPath, slug string, test AcceptanceTest, step AcceptanceStep, baseSupply map[string]TestInput, clusterCfg *cluster.Cluster, serviceNameOverride string, localCratePath string, svcCache map[string]*types.Service, tempDir string, header string) AcceptanceStepResult {
448448
result := AcceptanceStepResult{Step: step}
449449

450450
if strings.TrimSpace(step.Command) == "" {
@@ -480,7 +480,7 @@ func (c *Client) executeAcceptanceStep(ctx context.Context, repoPath, slug strin
480480
return result
481481
}
482482

483-
responseBytes, err := invokeServiceWithContent(clusterCfg, serviceName, payload)
483+
responseBytes, err := invokeServiceWithContent(clusterCfg, serviceName, payload, header)
484484
if err != nil {
485485
result.Err = err
486486
return result
@@ -1434,7 +1434,7 @@ func isWhitespace(r rune) bool {
14341434
return r == ' ' || r == '\t' || r == '\n' || r == '\r'
14351435
}
14361436

1437-
func invokeServiceWithContent(clusterCfg *cluster.Cluster, serviceName string, payload []byte) ([]byte, error) {
1437+
func invokeServiceWithContent(clusterCfg *cluster.Cluster, serviceName string, payload []byte, header string) ([]byte, error) {
14381438
reader, writer := io.Pipe()
14391439
go func() {
14401440
encoder := base64.NewEncoder(base64.StdEncoding, writer)
@@ -1447,7 +1447,7 @@ func invokeServiceWithContent(clusterCfg *cluster.Cluster, serviceName string, p
14471447
}
14481448
}()
14491449

1450-
response, err := service.RunService(clusterCfg, serviceName, "", "", reader)
1450+
response, err := service.RunService(clusterCfg, serviceName, "", "", reader, header)
14511451
if err != nil {
14521452
return nil, err
14531453
}

‎pkg/service/service.go‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ func ApplyService(svc *types.Service, c *cluster.Cluster, method string) error {
258258
}
259259

260260
// RunService invokes a service synchronously (a Serverless backend in the cluster is required)
261-
func RunService(c *cluster.Cluster, name string, token string, endpoint string, input io.Reader) (responseBody io.ReadCloser, err error) {
261+
func RunService(c *cluster.Cluster, name string, token string, endpoint string, input io.Reader, header string) (responseBody io.ReadCloser, err error) {
262262
client := &http.Client{Timeout: time.Second * runServiceTimeoutSeconds}
263263
if token == "" {
264264
client, _ = c.GetClientSafe(runServiceTimeoutSeconds)
@@ -288,6 +288,13 @@ func RunService(c *cluster.Cluster, name string, token string, endpoint string,
288288
if token != "" {
289289
req.Header.Set("Authorization", "Bearer "+token)
290290
}
291+
if header != "" {
292+
for _, lines := range strings.Split(header, ",") {
293+
chunck := strings.Split(lines, ":")
294+
req.Header.Add(chunck[0], chunck[1])
295+
}
296+
297+
}
291298
if err != nil {
292299
return nil, cluster.ErrMakingRequest
293300
}

‎pkg/service/service_test.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ func TestRunServiceUsesServiceToken(t *testing.T) {
189189
SSLVerify: true,
190190
}
191191

192-
resp, err := RunService(c, serviceName, "", "", bytes.NewBufferString(payload))
192+
resp, err := RunService(c, serviceName, "", "", bytes.NewBufferString(payload), "")
193193
if err != nil {
194194
t.Fatalf("RunService returned error: %v", err)
195195
}
@@ -224,7 +224,7 @@ func TestRunServiceWithProvidedToken(t *testing.T) {
224224
}))
225225
defer server.Close()
226226

227-
resp, err := RunService(&cluster.Cluster{Endpoint: server.URL, SSLVerify: true}, serviceName, token, server.URL, bytes.NewBufferString(payload))
227+
resp, err := RunService(&cluster.Cluster{Endpoint: server.URL, SSLVerify: true}, serviceName, token, server.URL, bytes.NewBufferString(payload), "")
228228
if err != nil {
229229
t.Fatalf("RunService returned error: %v", err)
230230
}

0 commit comments

Comments
 (0)