Skip to content

Commit c97613f

Browse files
committed
Add --non-interactive flag to auth
Signed-off-by: Peter Verraedt <peter@verraedt.be>
1 parent 636e377 commit c97613f

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed

cmd/iron/cli/app.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,12 @@ type App struct {
5252
updater *selfupdate.Updater
5353
repo selfupdate.RepositorySlug
5454

55-
Admin bool
56-
Debug int
57-
Native bool
58-
Workdir string
59-
PamTTL time.Duration
55+
Admin bool
56+
Debug int
57+
Native bool
58+
Workdir string
59+
PamTTL time.Duration
60+
NonInteractive bool
6061

6162
inShell bool
6263
}
@@ -376,13 +377,20 @@ func (a *App) init(cmd *cobra.Command, zone string) error {
376377
clientName = fmt.Sprintf("%s-%s", clientName, version.String())
377378
}
378379

380+
var prompt iron.Prompt
381+
382+
if a.NonInteractive {
383+
prompt = iron.Bot{}
384+
}
385+
379386
a.Client, err = iron.New(cmd.Context(), env, iron.Option{
380387
ClientName: clientName,
381388
Admin: a.Admin,
382389
UseNativeProtocol: a.Native,
383390
MaxConns: 16,
384391
DialFunc: dialer,
385392
GeneratedNativePasswordAge: env.GeneratedPasswordTimeout,
393+
AuthenticationPrompt: prompt,
386394
})
387395
if err != nil {
388396
// Doesn't make sense to print usage here

cmd/iron/cli/commands.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (a *App) auth() *cobra.Command {
5656
}
5757
}
5858

59-
return &cobra.Command{
59+
cmd := &cobra.Command{
6060
Use: use,
6161
Aliases: []string{"authenticate", "iinit"},
6262
Short: "Authenticate against the irods server.",
@@ -88,6 +88,10 @@ func (a *App) auth() *cobra.Command {
8888
}
8989
},
9090
}
91+
92+
cmd.Flags().BoolVar(&a.NonInteractive, "non-interactive", false, "Authenticate non-interactively, don't prompt any user input. Useful to fail gracefully in unattended scripts if the authentication method would unexpectedly request e.g. a password.")
93+
94+
return cmd
9195
}
9296

9397
func (a *App) mkdir() *cobra.Command {

cmd/iron/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,7 @@ func main() {
6666
_ = os.Chdir(home) //nolint:errcheck
6767
}
6868

69-
cmd.ExecuteContext(ctx) //nolint:errcheck
69+
if err := cmd.ExecuteContext(ctx); err != nil {
70+
os.Exit(1)
71+
}
7072
}

prompt.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ func (p *prompt) Password(message string) (string, error) {
107107
type Bot map[string]string
108108

109109
func (b Bot) Print(message string) error {
110+
// If we need to open an URL, print a newline
111+
if result := AuthenticateURL.FindStringSubmatch(message); len(result) > 1 {
112+
return errors.New("cannot open URL in bot mode")
113+
}
114+
110115
return nil
111116
}
112117

0 commit comments

Comments
 (0)