From 5a784f43a391cb601e9db80cbf0387ad88766ecc Mon Sep 17 00:00:00 2001 From: Mahmood Ali Date: Mon, 9 Nov 2020 15:01:31 -0500 Subject: [PATCH] Start using the new jobspec2 API --- command/agent/job_endpoint.go | 10 +++++---- command/helpers.go | 38 ++++++++++++----------------------- command/helpers_test.go | 11 ---------- 3 files changed, 19 insertions(+), 40 deletions(-) diff --git a/command/agent/job_endpoint.go b/command/agent/job_endpoint.go index c4e6d01fd..a37ddb981 100644 --- a/command/agent/job_endpoint.go +++ b/command/agent/job_endpoint.go @@ -675,14 +675,16 @@ func (s *HTTPServer) JobsParseRequest(resp http.ResponseWriter, req *http.Reques return nil, CodedError(400, "Job spec is empty") } - jobfile := strings.NewReader(args.JobHCL) - var jobStruct *api.Job var err error if args.HCLv1 { - jobStruct, err = jobspec.Parse(jobfile) + jobStruct, err = jobspec.Parse(strings.NewReader(args.JobHCL)) } else { - jobStruct, err = jobspec2.ParseWithArgs("input.hcl", jobfile, nil, false) + jobStruct, err = jobspec2.ParseWithConfig(&jobspec2.ParseConfig{ + Path: "input.hcl", + Body: []byte(args.JobHCL), + AllowFS: false, + }) } if err != nil { return nil, CodedError(400, err.Error()) diff --git a/command/helpers.go b/command/helpers.go index abd93fbf5..0f87cf140 100644 --- a/command/helpers.go +++ b/command/helpers.go @@ -388,10 +388,10 @@ type JobGetter struct { // StructJob returns the Job struct from jobfile. func (j *JobGetter) ApiJob(jpath string) (*api.Job, error) { - return j.ApiJobWithArgs(jpath, nil) + return j.ApiJobWithArgs(jpath, nil, nil) } -func (j *JobGetter) ApiJobWithArgs(jpath string, vars map[string]string) (*api.Job, error) { +func (j *JobGetter) ApiJobWithArgs(jpath string, vars []string, varfiles []string) (*api.Job, error) { var jobfile io.Reader pathName := filepath.Base(jpath) switch jpath { @@ -447,7 +447,17 @@ func (j *JobGetter) ApiJobWithArgs(jpath string, vars map[string]string) (*api.J if j.hcl1 { jobStruct, err = jobspec.Parse(jobfile) } else { - jobStruct, err = jobspec2.ParseWithArgs(pathName, jobfile, vars, true) + var buf bytes.Buffer + _, err = io.Copy(&buf, jobfile) + if err != nil { + return nil, fmt.Errorf("Error reading job file from %s: %v", jpath, err) + } + jobStruct, err = jobspec2.ParseWithConfig(&jobspec2.ParseConfig{ + Path: pathName, + Body: buf.Bytes(), + ArgVars: vars, + AllowFS: true, + }) } if err != nil { return nil, fmt.Errorf("Error parsing job file from %s:\n%v", jpath, err) @@ -523,25 +533,3 @@ func (w *uiErrorWriter) Close() error { } return nil } - -// parseVars decodes a slice of `=` or `` strings into a golang map. -// -// `` without corresponding value, is mapped to the `` environment variable. -func parseVars(vars []string) map[string]string { - if len(vars) == 0 { - return nil - } - - result := make(map[string]string, len(vars)) - for _, v := range vars { - parts := strings.SplitN(v, "=", 2) - k := parts[0] - if len(parts) == 2 { - result[k] = parts[1] - } else { - result[k] = os.Getenv(k) - } - } - - return result -} diff --git a/command/helpers_test.go b/command/helpers_test.go index dd2f5e113..dbead5b62 100644 --- a/command/helpers_test.go +++ b/command/helpers_test.go @@ -392,14 +392,3 @@ func TestUiErrorWriter(t *testing.T) { expectedErr += "and thensome more\n" require.Equal(t, expectedErr, errBuf.String()) } - -func TestParseVars(t *testing.T) { - input := []string{"key1=val1", "HOME", "key2=321"} - expected := map[string]string{ - "key1": "val1", - "HOME": os.Getenv("HOME"), - "key2": "321", - } - - require.Equal(t, expected, parseVars(input)) -}