[lxc-devel] [lxd/master] lxc: Allow aliases to external commands

monstermunchkin on Github lxc-bot at linuxcontainers.org
Tue Aug 7 08:10:20 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 372 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180807/4979d970/attachment.bin>
-------------- next part --------------
From 42c494e6fa7bc1371577ab7a999f918c03c3675b Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Tue, 7 Aug 2018 10:07:46 +0200
Subject: [PATCH] lxc: Allow aliases to external commands

Signed-off-by: Thomas Hipp <thomas.hipp at canonical.com>
---
 lxc/main_aliases.go | 7 +++++--
 lxc/main_test.go    | 5 +++++
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/lxc/main_aliases.go b/lxc/main_aliases.go
index 83c8e76faf..3865b3a6fa 100644
--- a/lxc/main_aliases.go
+++ b/lxc/main_aliases.go
@@ -54,7 +54,10 @@ func expandAlias(conf *config.Config, origArgs []string) ([]string, bool) {
 		}
 	}
 
-	newArgs := []string{origArgs[0]}
+	var newArgs []string
+	if !strings.HasPrefix(aliasValue[0], "/") {
+		newArgs = append(newArgs, origArgs[0])
+	}
 	hasReplacedArgsVar := false
 
 	for i, aliasArg := range aliasValue {
@@ -118,7 +121,7 @@ func execIfAliases() {
 	}
 
 	// Look for the executable
-	path, err := exec.LookPath(args[0])
+	path, err := exec.LookPath(newArgs[0])
 	if err != nil {
 		fmt.Fprintf(os.Stderr, i18n.G("Processing aliases failed: %s\n"), err)
 		os.Exit(1)
diff --git a/lxc/main_test.go b/lxc/main_test.go
index 4e42e01be7..9165bc26a3 100644
--- a/lxc/main_test.go
+++ b/lxc/main_test.go
@@ -37,6 +37,7 @@ func TestExpandAliases(t *testing.T) {
 	aliases := map[string]string{
 		"tester 12": "list",
 		"foo":       "list @ARGS@ -c n",
+		"ssh":       "/usr/bin/ssh @ARGS@",
 	}
 
 	testcases := []aliasTestcase{
@@ -52,6 +53,10 @@ func TestExpandAliases(t *testing.T) {
 			input:    []string{"lxc", "foo", "asdf"},
 			expected: []string{"lxc", "list", "asdf", "-c", "n"},
 		},
+		{
+			input:    []string{"lxc", "ssh", "c1"},
+			expected: []string{"/usr/bin/ssh", "c1"},
+		},
 	}
 
 	conf := &config.Config{Aliases: aliases}


More information about the lxc-devel mailing list