github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/client/allocrunner/taskrunner/getter/replacer_test.go (about)

     1  package getter
     2  
     3  import (
     4  	"path/filepath"
     5  	"strings"
     6  
     7  	"github.com/hashicorp/nomad/client/interfaces"
     8  	"github.com/hashicorp/nomad/helper/escapingfs"
     9  )
    10  
    11  // noopReplacer is a noop version of taskenv.TaskEnv.ReplaceEnv.
    12  type noopReplacer struct {
    13  	taskDir string
    14  }
    15  
    16  // noopTaskEnv creates a new noopReplacer with the given taskDir.
    17  func noopTaskEnv(taskDir string) interfaces.EnvReplacer {
    18  	return &noopReplacer{taskDir: taskDir}
    19  }
    20  
    21  func (*noopReplacer) ReplaceEnv(s string) string {
    22  	return s
    23  }
    24  
    25  func (r *noopReplacer) ClientPath(p string, join bool) (string, bool) {
    26  	path, escapes := clientPath(r.taskDir, r.ReplaceEnv(p), join)
    27  	return path, escapes
    28  }
    29  
    30  // type upReplacer is a version of taskenv.TaskEnv.ReplaceEnv
    31  // that uppercases all the things.
    32  type upReplacer struct {
    33  	taskDir string
    34  }
    35  
    36  // upTaskEnv creates a new noopReplacer with the given taskDir.
    37  func upTaskEnv(taskDir string) interfaces.EnvReplacer {
    38  	return &upReplacer{taskDir: taskDir}
    39  }
    40  
    41  func (*upReplacer) ReplaceEnv(s string) string {
    42  	return strings.ToUpper(s)
    43  }
    44  
    45  func (r *upReplacer) ClientPath(p string, join bool) (string, bool) {
    46  	path, escapes := clientPath(r.taskDir, r.ReplaceEnv(p), join)
    47  	return path, escapes
    48  }
    49  
    50  func clientPath(taskDir, path string, join bool) (string, bool) {
    51  	if !filepath.IsAbs(path) || (escapingfs.PathEscapesSandbox(taskDir, path) && join) {
    52  		path = filepath.Join(taskDir, path)
    53  	}
    54  	path = filepath.Clean(path)
    55  	if taskDir != "" && !escapingfs.PathEscapesSandbox(taskDir, path) {
    56  		return path, false
    57  	}
    58  	return path, true
    59  }