Skip to content

Commit 0e0f82d

Browse files
authored
fix: Skip auth type prompt if already set (#701)
1 parent 3eaadca commit 0e0f82d

File tree

5 files changed

+48
-15
lines changed

5 files changed

+48
-15
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ Follow the [installation guide](https://github.com/ankitpokhrel/jira-cli/wiki/In
101101
more [here](https://github.com/ankitpokhrel/jira-cli/discussions/356).
102102
2. Run `jira init`, select installation type as `Local`, and provide the required details to generate a config file required
103103
for the tool.
104+
- The most common auth type for on-premise installation is `basic`. If you are using your jira login credentials
105+
(username and password), select the `basic` auth type.
104106
- If you want to use `mtls` (client certificates), select auth type `mtls` and provide the CA Cert, client Key, and client cert.
105107

106108
**Note:** If your on-premise Jira installation is using a language other than `English`, then the issue/epic creation

api/client.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ func Client(config jira.Config) *jira.Client {
4040
secret, _ := keyring.Get("jira-cli", config.Login)
4141
config.APIToken = secret
4242
}
43-
if config.AuthType == "" {
44-
config.AuthType = jira.AuthType(viper.GetString("auth_type"))
43+
if config.AuthType == nil {
44+
authType := jira.AuthType(viper.GetString("auth_type"))
45+
config.AuthType = &authType
4546
}
4647
if config.Insecure == nil {
4748
insecure := viper.GetBool("insecure")

internal/cmd/init/init.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type initParams struct {
1818
installation string
1919
server string
2020
login string
21+
authType string
2122
project string
2223
board string
2324
force bool
@@ -39,6 +40,7 @@ func NewCmdInit() *cobra.Command {
3940
cmd.Flags().String("installation", "", "Is this a 'cloud' or 'local' jira installation?")
4041
cmd.Flags().String("server", "", "Link to your jira server")
4142
cmd.Flags().String("login", "", "Jira login username or email based on your setup")
43+
cmd.Flags().String("auth-type", "", "Authentication type can be basic, bearer or mtls")
4244
cmd.Flags().String("project", "", "Your default project key")
4345
cmd.Flags().String("board", "", "Name of your default board in the project")
4446
cmd.Flags().Bool("force", false, "Forcefully override existing config if it exists")
@@ -58,6 +60,15 @@ func parseFlags(flags query.FlagParser) *initParams {
5860
login, err := flags.GetString("login")
5961
cmdutil.ExitIfError(err)
6062

63+
authType, err := flags.GetString("auth-type")
64+
cmdutil.ExitIfError(err)
65+
66+
// If auth type is not provided, check if it's set in env.
67+
if authType == "" {
68+
authType = os.Getenv("JIRA_AUTH_TYPE")
69+
}
70+
authType = strings.ToLower(authType)
71+
6172
project, err := flags.GetString("project")
6273
cmdutil.ExitIfError(err)
6374

@@ -74,6 +85,7 @@ func parseFlags(flags query.FlagParser) *initParams {
7485
installation: installation,
7586
server: server,
7687
login: login,
88+
authType: authType,
7789
project: project,
7890
board: board,
7991
force: force,
@@ -89,6 +101,7 @@ func initialize(cmd *cobra.Command, _ []string) {
89101
Installation: strings.ToLower(params.installation),
90102
Server: params.server,
91103
Login: params.login,
104+
AuthType: params.authType,
92105
Project: params.project,
93106
Board: params.board,
94107
Force: params.force,

internal/config/generator.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ func NewJiraCLIConfigGenerator(cfg *JiraCLIConfig) *JiraCLIConfigGenerator {
114114
}
115115

116116
// Generate generates the config file.
117+
//
118+
//nolint:gocyclo
117119
func (c *JiraCLIConfigGenerator) Generate() (string, error) {
118120
var cfgFile string
119121

@@ -159,6 +161,10 @@ func (c *JiraCLIConfigGenerator) Generate() (string, error) {
159161
}
160162
}
161163

164+
if c.usrCfg.AuthType != "" {
165+
c.value.authType = jira.AuthType(c.usrCfg.AuthType)
166+
}
167+
162168
if c.value.authType == jira.AuthTypeMTLS {
163169
if err := c.configureMTLS(); err != nil {
164170
return "", err
@@ -218,12 +224,14 @@ func (c *JiraCLIConfigGenerator) configureInstallationType() error {
218224
}
219225

220226
func (c *JiraCLIConfigGenerator) configureLocalAuthType() error {
221-
var authType string
227+
authType := c.usrCfg.AuthType
222228

223229
if c.usrCfg.AuthType == "" {
224230
qs := &survey.Select{
225231
Message: "Authentication type:",
226-
Help: "basic (login), bearer (PAT) or mtls (client certs)?",
232+
Help: `Authentication type coud be: basic (login), bearer (PAT) or mtls (client certs)
233+
? If you are using your login credentials, the auth type is probably 'basic' (most common for local installation)
234+
? If you are using a personal access token, the auth type is probably 'bearer'`,
227235
Options: []string{"basic", "bearer", "mtls"},
228236
Default: "basic",
229237
}
@@ -413,7 +421,7 @@ func (c *JiraCLIConfigGenerator) verifyLoginDetails(server, login string) error
413421
Server: server,
414422
Login: login,
415423
Insecure: &c.usrCfg.Insecure,
416-
AuthType: c.value.authType,
424+
AuthType: &c.value.authType,
417425
Debug: viper.GetBool("debug"),
418426
MTLSConfig: jira.MTLSConfig{
419427
CaCert: c.value.mtls.caCert,
@@ -446,7 +454,7 @@ func (c *JiraCLIConfigGenerator) configureServerMeta(server, login string) error
446454
Server: server,
447455
Login: login,
448456
Insecure: &c.usrCfg.Insecure,
449-
AuthType: c.value.authType,
457+
AuthType: &c.value.authType,
450458
Debug: viper.GetBool("debug"),
451459
MTLSConfig: jira.MTLSConfig{
452460
CaCert: c.value.mtls.caCert,
@@ -750,14 +758,17 @@ func (c *JiraCLIConfigGenerator) write(path string) (string, error) {
750758
config.Set("epic", c.value.epic)
751759
config.Set("issue.types", c.value.issueTypes)
752760
config.Set("issue.fields.custom", c.value.customFields)
753-
config.Set("auth_type", c.value.authType)
761+
config.Set("auth_type", c.value.authType.String())
754762
config.Set("timezone", c.value.timezone)
755763

756-
// MTLS
757-
config.Set("mtls.ca_cert", c.value.mtls.caCert)
758-
config.Set("mtls.client_cert", c.value.mtls.clientCert)
759-
config.Set("mtls.client_key", c.value.mtls.clientKey)
764+
// MTLS.
765+
if c.value.mtls.caCert != "" {
766+
config.Set("mtls.ca_cert", c.value.mtls.caCert)
767+
config.Set("mtls.client_cert", c.value.mtls.clientCert)
768+
config.Set("mtls.client_key", c.value.mtls.clientKey)
769+
}
760770

771+
// Jira version.
761772
if c.value.version.major > 0 {
762773
config.Set("version.major", c.value.version.major)
763774
config.Set("version.minor", c.value.version.minor)

pkg/jira/client.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ type Config struct {
108108
Server string
109109
Login string
110110
APIToken string
111-
AuthType AuthType
111+
AuthType *AuthType
112112
Insecure *bool
113113
Debug bool
114114
MTLSConfig MTLSConfig
@@ -120,7 +120,7 @@ type Client struct {
120120
insecure bool
121121
server string
122122
login string
123-
authType AuthType
123+
authType *AuthType
124124
token string
125125
timeout time.Duration
126126
debug bool
@@ -154,8 +154,8 @@ func NewClient(c Config, opts ...ClientFunc) *Client {
154154
}).DialContext,
155155
}
156156

157-
if c.AuthType == AuthTypeMTLS {
158-
// Create a CA certificate pool and add cert.pem to it
157+
if c.AuthType != nil && *c.AuthType == AuthTypeMTLS {
158+
// Create a CA certificate pool and add cert.pem to it.
159159
caCert, err := os.ReadFile(c.MTLSConfig.CaCert)
160160
if err != nil {
161161
log.Fatalf("%s, %s", err, c.MTLSConfig.CaCert)
@@ -261,6 +261,12 @@ func (c *Client) request(ctx context.Context, method, endpoint string, body []by
261261
req.Header.Set(k, v)
262262
}
263263

264+
// Set default auth type to `basic`.
265+
if c.authType == nil {
266+
basic := AuthTypeBasic
267+
c.authType = &basic
268+
}
269+
264270
// When need to compare using `String()` here, it is used to handle cases where the
265271
// authentication type might be empty, ensuring it defaults to the appropriate value.
266272
switch c.authType.String() {

0 commit comments

Comments
 (0)