Skip to content

Commit 0bfb87c

Browse files
jujubotanastasiamac
authored andcommitted
Merge pull request juju#10892 from anastasiamac/lp1851849-lp1851852
juju#10892 ## Description of change add-k8s command is frequently piped. However, the behavior of the command has changed to prompt for user input when neither --client nor --controller option was specified to support an ASK-OR-TELL brief. Since the pipe and commands that require user input don't work together, if the pipe was used, the command to fail immediately with a descriptive message. As a drive-by, rename remaining reference to an ephemeral --client-only option. ## QA steps 1. $ microk8s.config | juju add-k8s k8s ``` This operation can be applied to both a copy on this client and to the one on a controller. ERROR The command is piped and Juju cannot prompt to clarify whether the --client or a --controller is to be used. Please clarify by re-running the command with the desired option(s). ``` 2. $ juju add-credential aws (to ensure everything else works) ``` This operation can be applied to both a copy on this client and to the one on a controller. No current controller was detected and there are no registered controllers on this client: either bootstrap one or register one. Do you ONLY want to add a credential to this client? (Y/n): ``` ## Bug reference https://bugs.launchpad.net/juju/+bug/1851849 https://bugs.launchpad.net/juju/+bug/1851852
1 parent 8c17186 commit 0bfb87c

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

cmd/juju/caas/add_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"io/ioutil"
99
"os"
1010
"path/filepath"
11+
"regexp"
1112
"strings"
1213

1314
"github.com/golang/mock/gomock"
@@ -1044,12 +1045,10 @@ func (s *addCAASSuite) TestCorrectPromptOrderFromStdIn(c *gc.C) {
10441045
c.Assert(stdIn, gc.NotNil)
10451046
defer stdIn.Close()
10461047
ctx, err := s.runCommand(c, stdIn, command, "myk8s")
1047-
c.Assert(err, gc.ErrorMatches, "EOF")
1048-
c.Assert(cmdtesting.Stdout(ctx), gc.Equals, "Do you want to add k8s cloud myk8s to:\n"+
1049-
" 1. client only (--client)\n"+
1050-
" 2. controller \"foo\" only (--controller foo)\n"+
1051-
" 3. both (--client --controller foo)\n"+
1052-
"Enter your choice, or type Q|q to quit: ")
1048+
c.Assert(errors.Cause(err), gc.ErrorMatches, regexp.QuoteMeta(`
1049+
The command is piped and Juju cannot prompt to clarify whether the --client or a --controller is to be used.
1050+
Please clarify by re-running the command with the desired option(s).`[1:]))
1051+
c.Assert(cmdtesting.Stdout(ctx), gc.Equals, "")
10531052
c.Assert(cmdtesting.Stderr(ctx), gc.Equals, "This operation can be applied to both a copy on this client and to the one on a controller.\n")
10541053
}
10551054

cmd/modelcmd/controller.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/juju/juju/api/controller"
1919
"github.com/juju/juju/api/modelmanager"
2020
"github.com/juju/juju/api/usermanager"
21+
jujucmd "github.com/juju/juju/cmd"
2122
"github.com/juju/juju/cmd/juju/interact"
2223
"github.com/juju/juju/juju/osenv"
2324
"github.com/juju/juju/jujuclient"
@@ -414,7 +415,7 @@ func (c *OptionalControllerCommand) SetFlags(f *gnuflag.FlagSet) {
414415
f.StringVar(&c.ControllerName, "c", "", "Controller to operate in")
415416
f.StringVar(&c.ControllerName, "controller", "", "")
416417
// TODO (juju3) remove me
417-
f.BoolVar(&c.Local, "local", false, "DEPRECATED (use --client-only): Local operation only; controller not affected")
418+
f.BoolVar(&c.Local, "local", false, "DEPRECATED (use --client): Local operation only; controller not affected")
418419
}
419420

420421
// Init populates the command with the args from the command line.
@@ -451,6 +452,10 @@ func (c *OptionalControllerCommand) MaybePrompt(ctxt *cmd.Context, action string
451452
}
452453

453454
ctxt.Infof("This operation can be applied to both a copy on this client and to the one on a controller.")
455+
if jujucmd.IsPiped(ctxt) {
456+
return errors.Errorf("The command is piped and Juju cannot prompt to clarify whether the --client or a --controller is to be used.\n" +
457+
"Please clarify by re-running the command with the desired option(s).")
458+
}
454459
if currentController == "" {
455460
msg := "No current controller was detected"
456461
all, err := c.Store.AllControllers()

cmd/supercommand.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/juju/os/series"
1414
"github.com/juju/utils/arch"
1515
"github.com/juju/version"
16+
"golang.org/x/crypto/ssh/terminal"
1617

1718
"github.com/juju/juju/juju/osenv"
1819
jujuversion "github.com/juju/juju/version"
@@ -86,3 +87,10 @@ func Info(i *cmd.Info) *cmd.Info {
8687
info.ShowSuperFlags = []string{"show-log", "debug", "logging-config", "verbose", "quiet", "h", "help"}
8788
return &info
8889
}
90+
91+
// IsPiped determines if the command was used in a pipe and,
92+
// hence, it's stdin is not usable for user input.
93+
func IsPiped(ctx *cmd.Context) bool {
94+
stdIn, ok := ctx.Stdin.(*os.File)
95+
return ok && !terminal.IsTerminal(int(stdIn.Fd()))
96+
}

0 commit comments

Comments
 (0)