From 427251e6f320849022e55510051797a10d4e9d83 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Thu, 4 Jun 2020 00:26:16 +0700 Subject: os/exec: new package to simplify the standard "os/exec" New extension to standard package is function ParseCommandArgs() that receive input as string and return itas command and list of arguments. Unlike strings.Fields() which only separated the field by space, ParseCommandArgs can detect possible single, double, or back quotes. Another extension is Run() function that accept the string command to be executed and their standard output and error. --- lib/os/exec/exec_test.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 lib/os/exec/exec_test.go (limited to 'lib/os/exec/exec_test.go') diff --git a/lib/os/exec/exec_test.go b/lib/os/exec/exec_test.go new file mode 100644 index 00000000..ad0e82b0 --- /dev/null +++ b/lib/os/exec/exec_test.go @@ -0,0 +1,52 @@ +// Copyright 2020, Shulhan . All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package exec + +import ( + "testing" + + "github.com/shuLhan/share/lib/test" +) + +func TestParseCommandArg(t *testing.T) { + cases := []struct { + in string + expCmd string + expArgs []string + }{{ + in: ``, + }, { + in: `a `, + expCmd: `a`, + expArgs: nil, + }, { + in: `a "b c"`, + expCmd: `a`, + expArgs: []string{`b c`}, + }, { + in: `a "b'c"`, + expCmd: `a`, + expArgs: []string{`b'c`}, + }, { + in: `'a "b'c"`, + expCmd: `a "b`, + expArgs: []string{`c`}, + }, { + in: "a `b c`", + expCmd: `a`, + expArgs: []string{`b c`}, + }, { + in: "a `b'c`", + expCmd: `a`, + expArgs: []string{`b'c`}, + }} + + for _, c := range cases { + t.Logf(c.in) + gotCmd, gotArgs := ParseCommandArgs(c.in) + test.Assert(t, "cmd", c.expCmd, gotCmd, true) + test.Assert(t, "args", c.expArgs, gotArgs, true) + } +} -- cgit v1.3-6-g1900