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
3 changes: 3 additions & 0 deletions .changelog/27906.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
cli: Automatically expand `nomad exec -it` to `-i -t`
```
17 changes: 17 additions & 0 deletions command/alloc_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func (l *AllocExecCommand) Run(args []string) int {
flags.StringVar(&task, "task", "", "")
flags.StringVar(&group, "group", "", "")

args = expandITFlags(args)
if err := flags.Parse(args); err != nil {
return 1
}
Expand Down Expand Up @@ -340,6 +341,22 @@ func setRawTerminalOutput(stream interface{}) (cleanup func(), err error) {
return func() { term.RestoreTerminal(fd, state) }, nil
}

// expandITFlags preprocesses args to expand the combined short boolean flags
// -it and -ti into their individual forms -i -t, mirroring the convention
// established by tools like docker and ssh.
func expandITFlags(args []string) []string {
expanded := make([]string, 0, len(args))
for _, arg := range args {
switch arg {
case "-it", "-ti":
expanded = append(expanded, "-i", "-t")
default:
expanded = append(expanded, arg)
}
}
return expanded
}

// watchTerminalSize watches terminal size changes to propagate to remote tty.
func watchTerminalSize(out io.Writer, resize chan<- api.TerminalSize) (func(), error) {
fd, isTerminal := term.GetFdInfo(out)
Expand Down
48 changes: 48 additions & 0 deletions command/alloc_exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ func TestAllocExecCommand_Fails(t *testing.T) {
[]string{"-address=" + url, "-e", "es", "26470238-5CF2-438F-8772-DC67CFB0705C", "/bin/bash"},
`-e requires 'none' or a single character`,
},
{
"-it with stdin disabled is rejected",
[]string{"-it", "-i=false", "26470238-5CF2-438F-8772-DC67CFB0705C", "/bin/bash"},
`-i must be enabled if running with tty`,
},
}

for _, c := range cases {
Expand Down Expand Up @@ -165,6 +170,49 @@ func TestAllocExecCommand_AutocompleteArgs(t *testing.T) {
must.Eq(t, a.ID, res[0])
}

func TestExpandITFlags(t *testing.T) {
ci.Parallel(t)

cases := []struct {
name string
input []string
expected []string
}{
{
name: "no combined flags",
input: []string{"-i", "-t", "alloc", "/bin/sh"},
expected: []string{"-i", "-t", "alloc", "/bin/sh"},
},
{
name: "-it expanded",
input: []string{"-it", "alloc", "/bin/sh"},
expected: []string{"-i", "-t", "alloc", "/bin/sh"},
},
{
name: "-ti expanded",
input: []string{"-ti", "alloc", "/bin/sh"},
expected: []string{"-i", "-t", "alloc", "/bin/sh"},
},
{
name: "-it with other flags",
input: []string{"-address=http://localhost", "-it", "alloc", "/bin/sh"},
expected: []string{"-address=http://localhost", "-i", "-t", "alloc", "/bin/sh"},
},
{
name: "empty args",
input: []string{},
expected: []string{},
},
}

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
result := expandITFlags(c.input)
must.Eq(t, c.expected, result)
})
}
}

func TestAllocExecCommand_Run(t *testing.T) {
ci.Parallel(t)
srv, client, url := testServer(t, true, nil)
Expand Down
Loading