-
Notifications
You must be signed in to change notification settings - Fork 306
/
sh.go
53 lines (44 loc) · 1.23 KB
/
sh.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package helper
import (
"context"
"fmt"
"os"
"github.com/loft-sh/devpod/pkg/shell"
"github.com/spf13/cobra"
)
type ShellCommand struct {
Command string
Login bool
}
// NewShellCmd creates a new command
func NewShellCmd() *cobra.Command {
cmd := &ShellCommand{}
shellCmd := &cobra.Command{
Use: "sh",
Short: "Executes a command in a shell",
RunE: func(_ *cobra.Command, args []string) error {
return cmd.Run(context.Background(), args)
},
}
shellCmd.Flags().BoolVarP(&cmd.Login, "login", "l", false, "If login shell should be used")
shellCmd.Flags().StringVarP(&cmd.Command, "command", "c", "", "Command to execute")
return shellCmd
}
func (cmd *ShellCommand) Run(ctx context.Context, args []string) error {
if cmd.Command == "" && len(args) == 0 {
return nil
} else if cmd.Command != "" && len(args) > 0 {
return fmt.Errorf("either use -c or provide a script file")
} else if len(args) > 1 {
return fmt.Errorf("only a single script file can be used")
}
// load command from file
if len(args) > 0 {
content, err := os.ReadFile(args[0])
if err != nil {
return err
}
cmd.Command = string(content)
}
return shell.ExecuteCommandWithShell(ctx, cmd.Command, os.Stdin, os.Stdout, os.Stderr, os.Environ())
}