[lxc-devel] [lxd/master] Build unix-like aliases directly into LXC
sean-jc on Github
lxc-bot at linuxcontainers.org
Mon Jun 13 20:44:08 UTC 2016
A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 581 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20160613/48091270/attachment.bin>
-------------- next part --------------
From 10a826bb9e0694be25771037271038e685907a0a Mon Sep 17 00:00:00 2001
From: Sean Christopherson <sean.j.christopherson at intel.com>
Date: Mon, 13 Jun 2016 10:19:37 -0700
Subject: [PATCH] Build unix-like aliases directly into LXC
Move default aliases into the alias checking code itself instead of
making them a property of the default config. Add unix-like aliases
to the map of default aliases, e.g. cp, ls, mv and rm.
Fixes #1615
Signed-off-by: Sean Christopherson <sean.j.christopherson at intel.com>
---
config.go | 6 +-----
lxc/main.go | 39 ++++++++++++++++++++++++++++++++++++---
test/suites/basic.sh | 30 +++++++++++++++++++++++++++---
3 files changed, 64 insertions(+), 11 deletions(-)
diff --git a/config.go b/config.go
index d553b06..f4da426 100644
--- a/config.go
+++ b/config.go
@@ -80,11 +80,7 @@ var DefaultRemotes = map[string]RemoteConfig{
var DefaultConfig = Config{
Remotes: DefaultRemotes,
- DefaultRemote: "local",
- Aliases: map[string]string{
- "shell": "exec @ARGS@ -- login -f root",
- },
-}
+ DefaultRemote: "local"}
// LoadConfig reads the configuration from the config path; if the path does
// not exist, it returns a default configuration.
diff --git a/lxc/main.go b/lxc/main.go
index 40f3d24..ff8b085 100644
--- a/lxc/main.go
+++ b/lxc/main.go
@@ -181,14 +181,39 @@ var commands = map[string]command{
"version": &versionCmd{},
}
+// defaultAliases contains LXC's built-in command line aliases. The built-in
+// aliases are checked only if no user-defined alias was found.
+var defaultAliases = map[string]string{
+ "shell": "exec @ARGS@ -- login -f root",
+
+ "cp": "copy",
+ "ls": "list",
+ "mv": "move",
+ "rm": "delete",
+
+ "image cp": "image copy",
+ "image ls": "image list",
+ "image rm": "image delete",
+
+ "image alias ls": "image alias list",
+ "image alias rm": "image alias delete",
+
+ "remote ls": "remote list",
+ "remote mv": "remote rename",
+ "remote rm": "remote remove",
+
+ "config device ls": "config device list",
+ "config device rm": "config device remove",
+}
+
var errArgs = fmt.Errorf(i18n.G("wrong number of subcommand arguments"))
-func expandAlias(config *lxd.Config, origArgs []string) ([]string, bool) {
+func findAlias(aliases map[string]string, origArgs []string) ([]string, []string, bool) {
foundAlias := false
aliasKey := []string{}
aliasValue := []string{}
- for k, v := range config.Aliases {
+ for k, v := range aliases {
foundAlias = true
for i, key := range strings.Split(k, " ") {
if len(origArgs) <= i+1 || origArgs[i+1] != key {
@@ -204,8 +229,16 @@ func expandAlias(config *lxd.Config, origArgs []string) ([]string, bool) {
}
}
+ return aliasKey, aliasValue, foundAlias
+}
+
+func expandAlias(config *lxd.Config, origArgs []string) ([]string, bool) {
+ aliasKey, aliasValue, foundAlias := findAlias(config.Aliases, origArgs)
if !foundAlias {
- return []string{}, false
+ aliasKey, aliasValue, foundAlias = findAlias(defaultAliases, origArgs)
+ if !foundAlias {
+ return []string{}, false
+ }
}
newArgs := []string{origArgs[0]}
diff --git a/test/suites/basic.sh b/test/suites/basic.sh
index 05dfd86..7e4f915 100644
--- a/test/suites/basic.sh
+++ b/test/suites/basic.sh
@@ -155,9 +155,33 @@ test_basic_usage() {
lxc delete bar2
lxc image delete foo
- # test basic alias support
- printf "aliases:\n ls: list" >> "${LXD_CONF}/config.yml"
- lxc ls
+ # Test alias support
+ cp "${LXD_CONF}/config.yml" "${LXD_CONF}/config.yml.bak"
+
+ # 1. Basic built-in alias functionality
+ [ "$(lxc ls)" = "$(lxc list)" ]
+ # 2. Basic user-defined alias functionality
+ printf "aliases:\n l: list\n" >> "${LXD_CONF}/config.yml"
+ [ "$(lxc l)" = "$(lxc list)" ]
+ # 3. Built-in aliases and user-defined aliases can coexist
+ [ "$(lxc ls)" = "$(lxc l)" ]
+ # 4. Multi-argument alias keys and values
+ printf " i ls: image list\n" >> "${LXD_CONF}/config.yml"
+ [ "$(lxc i ls)" = "$(lxc image list)" ]
+ # 5. Aliases where len(keys) != len(values) (expansion/contraction of number of arguments)
+ printf " ils: image list\n container ls: list\n" >> "${LXD_CONF}/config.yml"
+ [ "$(lxc ils)" = "$(lxc image list)" ]
+ [ "$(lxc container ls)" = "$(lxc list)" ]
+ # 6. User-defined aliases override built-in aliases
+ printf " cp: list\n" >> "${LXD_CONF}/config.yml"
+ [ "$(lxc ls)" = "$(lxc cp)" ]
+ # 7. User-defined aliases override commands and don't recurse
+ LXC_LIST_DEBUG=$(lxc list --debug 2>&1 | grep -o "Raw.*")
+ printf " list: list --debug\n" >> "${LXD_CONF}/config.yml"
+ [ "$(lxc list 2>&1 | grep -o 'Raw.*')" = "$LXC_LIST_DEBUG" ]
+
+ # Restore the config to remove the aliases
+ mv "${LXD_CONF}/config.yml.bak" "${LXD_CONF}/config.yml"
# Delete the bar container we've used for several tests
lxc delete bar
More information about the lxc-devel
mailing list