From b60b03539fe85bbb4f5c91377260d1b2e831de8c Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Tue, 13 Nov 2018 00:41:42 +0100 Subject: [PATCH] add alias use in 'run' and 'set' instructions (has priority over real path) --- README.md | 2 ++ internal/instruction/alias.go | 8 ++++++-- internal/instruction/errors.go | 3 +++ internal/instruction/run.go | 16 ++++++++++++---- internal/instruction/set.go | 23 ++++++++++++++++++----- 5 files changed, 41 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 690781f..f84b430 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,8 @@ alias name /path/to.file Create the alias `name` which resolves to the path `/path/to.file`. +> Alias value either for the `set` or `run` command have priority to real file paths. + #### 6) custom scripts diff --git a/internal/instruction/alias.go b/internal/instruction/alias.go index 0e3706c..2a50d06 100644 --- a/internal/instruction/alias.go +++ b/internal/instruction/alias.go @@ -24,8 +24,12 @@ func (d *alias) Build(_args string) error { return ErrInvalidSyntax } - d.Name = split[0] - d.Value = split[1] + if strings.Contains(split[0], "/") { + return ErrInvalidAlias + } + + d.Name = strings.Trim(split[0], " \t") + d.Value = strings.Trim(split[1], " \t") d.raw = _args return nil diff --git a/internal/instruction/errors.go b/internal/instruction/errors.go index 0eeb111..2448798 100644 --- a/internal/instruction/errors.go +++ b/internal/instruction/errors.go @@ -4,6 +4,9 @@ import ( "errors" ) +// ErrInvalidAlias is raised when encountering an invalid token in an alias name +var ErrInvalidAlias = errors.New("invalid alias name (contains '/')") + // ErrInvalidSyntax is raised when encountering an invalid token var ErrInvalidSyntax = errors.New("invalid instruction format") diff --git a/internal/instruction/run.go b/internal/instruction/run.go index c19df98..22c7dc0 100644 --- a/internal/instruction/run.go +++ b/internal/instruction/run.go @@ -19,14 +19,22 @@ func (d *run) Build(_args string) error { func (d run) Exec(ctx ExecutionContext) ([]byte, error) { + // 1. get file / alias + path := d.raw + if !strings.Contains(path, "/") { + if p, exists := ctx.Alias[path]; exists { + path = p + } + } + // 1. fail if file not found - if _, err := os.Stat(d.raw); os.IsNotExist(err) { - return nil, fmt.Errorf("cannot find script '%s'", d.raw) + if _, err := os.Stat(path); os.IsNotExist(err) { + return nil, fmt.Errorf("cannot find script '%s'", path) } // 2. execute script - if err := ctx.Executor.Command(d.raw).Run(); err != nil { - return nil, fmt.Errorf("cannot run '%s' | %s", d.raw, err) + if err := ctx.Executor.Command(path).Run(); err != nil { + return nil, fmt.Errorf("cannot run '%s' | %s", path, err) } return nil, nil diff --git a/internal/instruction/set.go b/internal/instruction/set.go index 48650f0..7f673af 100644 --- a/internal/instruction/set.go +++ b/internal/instruction/set.go @@ -57,19 +57,32 @@ func (d *set) Build(_args string) error { func (d set) Exec(ctx ExecutionContext) ([]byte, error) { - // 1. try to load format - format, err := cnf.Load(d.File) + // 1. get file / alias + path := d.File + if !strings.Contains(path, "/") { + if p, exists := ctx.Alias[path]; exists { + path = p + } + } + + // 2. fail if file not found + if _, err := os.Stat(path); os.IsNotExist(err) { + return nil, fmt.Errorf("cannot find file '%s'", path) + } + + // 3. try to load format + format, err := cnf.Load(path) if err != nil { return nil, err } - // 2. try to update value + // 4. try to update value if !format.Set(d.Path, d.Value) { return nil, ErrCannotSet } - // 3. Update file - file, err := os.OpenFile(d.File, os.O_WRONLY, 0) + // 5. Update file + file, err := os.OpenFile(path, os.O_WRONLY, 0) if err != nil { return nil, err }