Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions cli/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,28 @@ import (
// ExecLine executes a line of command
func ExecLine(line string) error {
config.Debug("ExecLine line:", line)
trimmed := strings.TrimSpace(line)
if strings.HasPrefix(trimmed, "!") {
shellCommand := strings.TrimSpace(trimmed[1:])
if len(shellCommand) == 0 {
return nil
}
return cmd.RunShellCommand(shellCommand)
}

args, err := shlex.Split(line)
if err != nil {
return err
}

if len(args) > 0 && args[0] == "shell" {
shellCommand := strings.TrimSpace(strings.TrimPrefix(trimmed, "shell"))
if len(shellCommand) == 0 {
return cmd.FindCommand("shell").Handle(cmd.NewRequest(cmd.FindCommand("shell"), cfg, nil, false))
}
return cmd.RunShellCommand(shellCommand)
}

if runtime.GOOS != "windows" {
for _, arg := range args {
if arg == "|" {
Expand Down
80 changes: 80 additions & 0 deletions cli/exec_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package cli

import (
"io/ioutil"
"os"
"strings"
"testing"
)

func captureStdout(t *testing.T, fn func() error) (string, error) {
t.Helper()
origStdout := os.Stdout
r, w, err := os.Pipe()
if err != nil {
t.Fatalf("failed to create pipe: %v", err)
}
os.Stdout = w

runErr := fn()

w.Close()
os.Stdout = origStdout

out, readErr := ioutil.ReadAll(r)
r.Close()
if readErr != nil {
t.Fatalf("failed to read stdout pipe: %v", readErr)
}
return string(out), runErr
}

func TestExecLineBangShellCommand(t *testing.T) {
out, err := captureStdout(t, func() error {
return ExecLine("!echo cmk-bang-test")
})
if err != nil {
t.Fatalf("ExecLine(!echo) returned unexpected error: %v", err)
}
if !strings.Contains(out, "cmk-bang-test") {
t.Fatalf("expected stdout to contain %q, got %q", "cmk-bang-test", out)
}
}

func TestExecLineShellCommand(t *testing.T) {
out, err := captureStdout(t, func() error {
return ExecLine("shell echo cmk-shell-test")
})
if err != nil {
t.Fatalf("ExecLine(shell echo) returned unexpected error: %v", err)
}
if !strings.Contains(out, "cmk-shell-test") {
t.Fatalf("expected stdout to contain %q, got %q", "cmk-shell-test", out)
}
}

func TestExecLineEmptyShellCommandReturnsError(t *testing.T) {
if err := ExecLine("shell"); err == nil {
t.Fatal("expected empty 'shell' command to return an error")
}
if err := ExecLine("!"); err != nil {
t.Fatalf("expected bare '!' to be a no-op, got error: %v", err)
}
}
68 changes: 68 additions & 0 deletions cmd/shell.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package cmd

import (
"errors"
"fmt"
"os"
"os/exec"
"runtime"
"strings"
)

// RunShellCommand executes a local OS shell command and streams output to stdout/stderr
func RunShellCommand(command string) error {
command = strings.TrimSpace(command)
if len(command) == 0 {
return errors.New("please provide a shell command to execute")
}

var shellCmd *exec.Cmd
if runtime.GOOS == "windows" {
shellCmd = exec.Command("cmd", "/C", command)
} else {
shellBin := os.Getenv("SHELL")
if len(shellBin) == 0 {
shellBin = "sh"
}
shellCmd = exec.Command(shellBin, "-c", command)
}

shellCmd.Stdin = os.Stdin
shellCmd.Stdout = os.Stdout
shellCmd.Stderr = os.Stderr
return shellCmd.Run()
}

func init() {
AddCommand(&Command{
Name: "shell",
Help: "Runs a local shell command (or prefix with '!')",
Handle: func(r *Request) error {
if len(r.Args) == 0 {
return errors.New("please provide a shell command to execute")
}
if r.Args[0] == "-h" {
fmt.Println("Usage: shell <command> or !<command>")
return nil
}
return RunShellCommand(strings.Join(r.Args, " "))
},
})
}