/
aliases.go
128 lines (104 loc) · 2.87 KB
/
aliases.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* ZEUS - An Electrifying Build System
* Copyright (c) 2017 Philipp Mieden <dreadl0ck [at] protonmail [dot] ch>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"errors"
"strings"
"github.com/dreadl0ck/readline"
)
// ErrInvalidAlias means there is a name conflict with an existing command
var ErrInvalidAlias = errors.New("invalid alias")
func printAliasCommandErr() {
l.Println(ErrInvalidUsage)
l.Println("usage: alias [remove <name>] [set <name> <command>]")
}
// check if an alias name conflicts with builtin user defined command names
func validateAlias(name string) error {
// check for conflict with builtin
for _, b := range builtins {
if b == name {
Log.Error("alias ", name, " conflicts with builtin: ", b)
return ErrInvalidAlias
}
}
// check for conflict with user command
if command, ok := cmdMap.items[name]; ok {
Log.Error("alias ", name, " conflicts with command: ", command.path)
return ErrInvalidAlias
}
return nil
}
// add an alias to project data and shell completer
func addAlias(name, command string) {
err := validateAlias(name)
if err != nil {
Log.WithError(err).Error("failed to validate alias: ", name)
return
}
// add to project data
projectData.Lock()
projectData.fields.Aliases[name] = command
projectData.Unlock()
projectData.update()
// add to completer
completer.Lock()
completer.Children = append(completer.Children, readline.PcItem(name))
completer.Unlock()
}
func deleteAlias(name string) {
projectData.Lock()
delete(projectData.fields.Aliases, name)
projectData.Unlock()
}
// print alias names to stdout
func printAliases() {
var maxLen int
projectData.Lock()
for name := range projectData.fields.Aliases {
if len(name) > maxLen {
maxLen = len(name)
}
}
for name, command := range projectData.fields.Aliases {
l.Println(pad(name, maxLen+1), "=", command)
}
projectData.Unlock()
}
// handle alias shell command
func handleAliasCommand(args []string) {
if len(args) < 2 {
printAliases()
return
}
if len(args) < 3 {
printAliasCommandErr()
return
}
switch args[1] {
case "set":
if len(args) < 3 {
printAliasCommandErr()
return
}
addAlias(args[2], strings.Join(args[3:], " "))
case "remove":
deleteAlias(args[2])
default:
printAliasCommandErr()
}
}