add alias use in 'run' and 'set' instructions (has priority over real path)
This commit is contained in:
parent
492835a67a
commit
b60b03539f
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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")
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue