-
-
Notifications
You must be signed in to change notification settings - Fork 392
Expand file tree
/
Copy pathroot.go
More file actions
192 lines (162 loc) · 5.3 KB
/
Copy pathroot.go
File metadata and controls
192 lines (162 loc) · 5.3 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package root
import (
"fmt"
"os"
"slices"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/ankitpokhrel/jira-cli/internal/cmd/board"
"github.com/ankitpokhrel/jira-cli/internal/cmd/completion"
"github.com/ankitpokhrel/jira-cli/internal/cmd/epic"
initCmd "github.com/ankitpokhrel/jira-cli/internal/cmd/init"
"github.com/ankitpokhrel/jira-cli/internal/cmd/issue"
"github.com/ankitpokhrel/jira-cli/internal/cmd/man"
"github.com/ankitpokhrel/jira-cli/internal/cmd/mcp"
"github.com/ankitpokhrel/jira-cli/internal/cmd/me"
"github.com/ankitpokhrel/jira-cli/internal/cmd/open"
"github.com/ankitpokhrel/jira-cli/internal/cmd/project"
"github.com/ankitpokhrel/jira-cli/internal/cmd/release"
"github.com/ankitpokhrel/jira-cli/internal/cmd/serverinfo"
"github.com/ankitpokhrel/jira-cli/internal/cmd/sprint"
"github.com/ankitpokhrel/jira-cli/internal/cmd/version"
"github.com/ankitpokhrel/jira-cli/internal/cmdutil"
jiraConfig "github.com/ankitpokhrel/jira-cli/internal/config"
"github.com/ankitpokhrel/jira-cli/pkg/jira"
"github.com/ankitpokhrel/jira-cli/pkg/netrc"
"github.com/zalando/go-keyring"
)
const (
jiraCLIHelpLink = "https://github.com/ankitpokhrel/jira-cli#getting-started"
jiraAPITokenLink = "https://id.atlassian.com/manage-profile/security/api-tokens"
)
var (
config string
debug bool
)
func init() {
cobra.OnInitialize(func() {
if config != "" {
// 1. Command line flag has the highest priority
viper.SetConfigFile(config)
} else if configFile := os.Getenv("JIRA_CONFIG_FILE"); configFile != "" {
// 2. Environment variable has second priority
viper.SetConfigFile(configFile)
} else {
// 3. Default location has the lowest priority
home, err := cmdutil.GetConfigHome()
if err != nil {
cmdutil.Failed("Error: %s", err)
return
}
viper.AddConfigPath(fmt.Sprintf("%s/%s", home, jiraConfig.Dir))
viper.SetConfigName(jiraConfig.FileName)
viper.SetConfigType(jiraConfig.FileType)
}
viper.AutomaticEnv()
viper.SetEnvPrefix("jira")
if err := viper.ReadInConfig(); err == nil && debug {
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
}
})
}
// NewCmdRoot is a root command.
func NewCmdRoot() *cobra.Command {
cmd := cobra.Command{
Use: "jira <command> <subcommand>",
Short: "Interactive Jira CLI",
Long: "Interactive Jira command line.",
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
},
PersistentPreRun: func(cmd *cobra.Command, _ []string) {
subCmd := cmd.Name()
if !cmdRequireToken(subCmd) {
return
}
// mTLS doesn't need Jira API Token.
if viper.GetString("auth_type") != string(jira.AuthTypeMTLS) {
checkForJiraToken(viper.GetString("server"), viper.GetString("login"))
}
configFile := viper.ConfigFileUsed()
if !jiraConfig.Exists(configFile) {
cmdutil.Failed("Missing configuration file.\nRun 'jira init' to configure the tool.")
}
},
}
configHome, err := cmdutil.GetConfigHome()
if err != nil {
cmdutil.Failed("Error: %s", err)
}
cmd.PersistentFlags().StringVarP(
&config, "config", "c", "",
fmt.Sprintf("Config file (default is %s/%s/%s.yml, can be overridden with JIRA_CONFIG_FILE env var)", configHome, jiraConfig.Dir, jiraConfig.FileName),
)
cmd.PersistentFlags().StringP(
"project", "p", "",
fmt.Sprintf(
"Jira project to look into (defaults to %s/%s/%s.yml)",
configHome, jiraConfig.Dir, jiraConfig.FileName,
),
)
cmd.PersistentFlags().BoolVar(&debug, "debug", false, "Turn on debug output")
cmd.SetHelpFunc(helpFunc)
_ = viper.BindPFlag("config", cmd.PersistentFlags().Lookup("config"))
_ = viper.BindPFlag("project.key", cmd.PersistentFlags().Lookup("project"))
_ = viper.BindPFlag("debug", cmd.PersistentFlags().Lookup("debug"))
addChildCommands(&cmd)
return &cmd
}
func addChildCommands(cmd *cobra.Command) {
cmd.AddCommand(
initCmd.NewCmdInit(),
issue.NewCmdIssue(),
epic.NewCmdEpic(),
sprint.NewCmdSprint(),
board.NewCmdBoard(),
project.NewCmdProject(),
open.NewCmdOpen(),
me.NewCmdMe(),
serverinfo.NewCmdServerInfo(),
completion.NewCmdCompletion(),
version.NewCmdVersion(),
release.NewCmdRelease(),
man.NewCmdMan(),
mcp.NewCmdMCP(),
)
}
func cmdRequireToken(cmd string) bool {
allowList := []string{
"init",
"help",
"jira",
"version",
"completion",
"__complete", "__completeNoDesc", // Subcommand name during autocompletion call.
"man",
}
return !slices.Contains(allowList, cmd)
}
func checkForJiraToken(server string, login string) {
if os.Getenv("JIRA_API_TOKEN") != "" {
return
}
netrcConfig, _ := netrc.Read(server, login)
if netrcConfig != nil {
return
}
secret, _ := keyring.Get("jira-cli", login)
if secret != "" {
return
}
msg := fmt.Sprintf(`The tool needs a Jira API token to function.
For cloud server: you can generate the token using this link: %s
For local server: you can use the password you use to log in to Jira for basic auth or get a token from your Jira profile for PAT.
After generating the token, you can either:
- Export API token to your shell as a JIRA_API_TOKEN env variable
- Or, you can use a .netrc file to define required machine details
Once you are done with the above steps, run 'jira init' to generate the config if you haven't already.
For more details, see: %s
`, jiraAPITokenLink, jiraCLIHelpLink)
cmdutil.Warn(msg)
os.Exit(1)
}