From 813223360fb8c529d1ac2b160801cac6e1fd2978 Mon Sep 17 00:00:00 2001 From: Paramveersingh-S Date: Mon, 23 Nov 2026 16:56:18 +0530 Subject: [PATCH] cli, cmd: add shell command and '!' prefix for local shell execution Fixes #215 --- cli/exec.go | 17 ++++++++++ cli/exec_test.go | 80 ++++++++++++++++++++++++++++++++++++++++++++++++ cmd/shell.go | 68 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 165 insertions(+) create mode 100644 cli/exec_test.go create mode 100644 cmd/shell.go diff --git a/cli/exec.go b/cli/exec.go index 1b4a0d4..a8e2457 100644 --- a/cli/exec.go +++ b/cli/exec.go @@ -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 == "|" { diff --git a/cli/exec_test.go b/cli/exec_test.go new file mode 100644 index 0000000..6777be1 --- /dev/null +++ b/cli/exec_test.go @@ -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) + } +} diff --git a/cmd/shell.go b/cmd/shell.go new file mode 100644 index 0000000..c89fa05 --- /dev/null +++ b/cmd/shell.go @@ -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 or !") + return nil + } + return RunShellCommand(strings.Join(r.Args, " ")) + }, + }) +}