Compare commits

...

29 Commits

Author SHA1 Message Date
Cory Bennett 4b9873b323 version bump 2018-04-01 12:04:52 -07:00
Cory Bennett cd106df78a Updated Changelog 2018-04-01 12:04:52 -07:00
Cory Bennett 50b5360cfe Updated Usage 2018-04-01 12:04:52 -07:00
Cory Bennett 359bec2fdf [#159] fix slice bounds out of range error in abbrev template function 2018-04-01 11:47:23 -07:00
Cory Bennett 79c83f6911 [#158] always print usage to stdout 2018-04-01 11:31:09 -07:00
coryb 585382eaea Merge pull request #150 from catskul/parameterized-go-makefile
make it easier to compile with aribtrary version of go via GO env var
2018-03-08 14:15:11 -08:00
Cory Bennett 9c818d427c use latest xgo since the project has been updated again 2018-03-08 13:54:04 -08:00
Cory Bennett 8621d9e698 version bump 2018-03-08 13:54:04 -08:00
Cory Bennett 5610707c30 Updated Changelog 2018-03-08 13:54:04 -08:00
Cory Bennett 0b4e16a35d Updated Usage 2018-03-08 13:54:04 -08:00
coryb 57bc97a378 Merge pull request #153 from catskul/document-shell-completion
Document shell completion
2018-03-08 13:48:56 -08:00
Andrew Somerville 2d02cf8132 fix md link 2018-03-08 16:47:25 -05:00
coryb 18a687e78a Merge pull request #152 from catskul/fix-missing-priority
prevent crash if priority is not set
2018-03-08 13:47:02 -08:00
Andrew Somerville 5d058536d2 Add in a stanza to the Usage section about tab completion 2018-03-08 16:38:36 -05:00
Cory Bennett d4153be0ec fix toc 2018-03-08 11:39:41 -08:00
Cory Bennett edb06621f8 [#148] [#149] add support for api token based authentication 2018-03-08 10:58:04 -08:00
coryb 161a68920d Merge pull request #151 from catskul/build-instructions
more explicit build instructions for newbies
2018-03-07 17:11:39 -08:00
Andrew Somerville fd30bc1392 prevent crash if priority is not set 2018-03-07 16:59:47 -05:00
Andrew Somerville 84f77be87c Add some text for newbies on handling forking and the difficulty of fully qualified import paths 2018-03-07 16:34:38 -05:00
Andrew Somerville dea794f037 make it easier to compile with aribtrary version of go via GO env var 2018-03-07 15:59:40 -05:00
Cory Bennett 43ebc846b1 refactor to simplify main 2018-03-06 22:36:32 -08:00
Cory Bennett 0d7c1a7931 refactor to simplify main 2018-03-06 22:34:49 -08:00
Cory Bennett 80325a5955 [#145] fix to match AuthProvider interface 2018-02-21 20:16:12 -08:00
Cory Bennett 20a9666fcd [#141] better handling in responseError for non-json error responses 2018-02-11 16:46:25 -08:00
coryb 4ae760f18f Merge pull request #142 from anthonyrisinger/patch-1
Update unexportTemplates.go
2018-01-19 12:40:13 -08:00
C Anthony Risinger 6da9974380 Update unexportTemplates.go
Logic error, `-d` can be set but was never used.
2018-01-19 13:29:21 -06:00
Cory Bennett 8c7ca383f6 [#139] add shellquote and toMinJson template functions 2018-01-07 10:49:00 -08:00
Cory Bennett 425fa63d33 [#137] update kingpeon dep to allow access to dynamic command structure 2018-01-06 17:18:34 -08:00
Cory Bennett 464742c9ba field name is "comment" not "comments" 2017-12-13 11:09:25 -08:00
29 changed files with 519 additions and 348 deletions
+16
View File
@@ -1,5 +1,21 @@
# Changelog
## 1.0.16 - 2018-04-01
* [[#159](https://github.com/Netflix-Skunkworks/go-jira/issues/159)] fix `slice bounds out of range` error in `abbrev` template function [Cory Bennett] [[359bec2](https://github.com/Netflix-Skunkworks/go-jira/commit/359bec2)]
* [[#158](https://github.com/Netflix-Skunkworks/go-jira/issues/158)] always print usage to stdout [Cory Bennett] [[79c83f6](https://github.com/Netflix-Skunkworks/go-jira/commit/79c83f6)]
## 1.0.15 - 2018-03-08
* [[#147](https://github.com/Netflix-Skunkworks/go-jira/issues/147)] [[#148](https://github.com/Netflix-Skunkworks/go-jira/issues/148)] add support for api token based authentication [Cory Bennett] [[edb0662](https://github.com/Netflix-Skunkworks/go-jira/commit/edb0662)]
* refactor to simplify main [Cory Bennett] [[43ebc84](https://github.com/Netflix-Skunkworks/go-jira/commit/43ebc84)] [[0d7c1a7](https://github.com/Netflix-Skunkworks/go-jira/commit/0d7c1a7)]
* [[#145](https://github.com/Netflix-Skunkworks/go-jira/issues/145)] fix to match AuthProvider interface [Cory Bennett] [[80325a5](https://github.com/Netflix-Skunkworks/go-jira/commit/80325a5)]
* [[#141](https://github.com/Netflix-Skunkworks/go-jira/issues/141)] better handling in responseError for non-json error responses [Cory Bennett] [[20a9666](https://github.com/Netflix-Skunkworks/go-jira/commit/20a9666)]
* Update unexportTemplates.go [GitHub] [[6da9974](https://github.com/Netflix-Skunkworks/go-jira/commit/6da9974)]
* [[#139](https://github.com/Netflix-Skunkworks/go-jira/issues/139)] add shellquote and toMinJson template functions [Cory Bennett] [[8c7ca38](https://github.com/Netflix-Skunkworks/go-jira/commit/8c7ca38)]
* [[#137](https://github.com/Netflix-Skunkworks/go-jira/issues/137)] update kingpeon dep to allow access to dynamic command structure [Cory Bennett] [[425fa63](https://github.com/Netflix-Skunkworks/go-jira/commit/425fa63)]
* field name is "comment" not "comments" [Cory Bennett] [[464742c](https://github.com/Netflix-Skunkworks/go-jira/commit/464742c)]
## 1.0.14 - 2017-11-04
* [[#131](https://github.com/Netflix-Skunkworks/go-jira/issues/131)] fix parsing global options before command execution (allow unixproxy/socksproxy to be set in config.yml) [Cory Bennett] [[042bc48](https://github.com/Netflix-Skunkworks/go-jira/commit/042bc48)]
Generated
+1 -1
View File
@@ -29,7 +29,7 @@
branch = "master"
name = "github.com/coryb/kingpeon"
packages = ["."]
revision = "64b561ae2d0f895b94719c486bed798f4236a4b3"
revision = "9a669f143f2e7454e80064c47365d139420a3fff"
[[projects]]
branch = "master"
+10 -9
View File
@@ -1,4 +1,5 @@
NAME=jira
GO?=go
OS=$(shell uname -s)
ifeq ($(filter CYGWIN%,$(OS)),$(OS))
@@ -20,17 +21,17 @@ CURVER ?= $(patsubst v%,%,$(shell [ -d .git ] && git describe --abbrev=0 --tags
LDFLAGS:= -w
build:
go build -gcflags="-e" -v -ldflags "$(LDFLAGS) -s" -o '$(BIN)' cmd/jira/main.go
$(GO) build -gcflags="-e" -v -ldflags "$(LDFLAGS) -s" -o '$(BIN)' cmd/jira/main.go
vet:
@go vet .
@go vet ./jiracli
@go vet ./jiracmd
@go vet ./jiradata
@go vet ./cmd/jira
@$(GO) vet .
@$(GO) vet ./jiracli
@$(GO) vet ./jiracmd
@$(GO) vet ./jiradata
@$(GO) vet ./cmd/jira
lint:
@go get github.com/golang/lint/golint
@$(GO) get github.com/golang/lint/golint
@golint .
@golint ./jiracli
@golint ./jiracmd
@@ -38,10 +39,10 @@ lint:
@golint ./cmd/jira
all:
go get -u github.com/karalabe/xgo
$(GO) get -u github.com/karalabe/xgo
rm -rf dist
mkdir -p dist
xgo --go 1.9.0 --targets="freebsd/amd64,linux/386,linux/amd64,windows/386,windows/amd64,darwin/amd64" -dest ./dist -ldflags="-w -s" ./cmd/jira
xgo --targets="freebsd/amd64,linux/386,linux/amd64,windows/386,windows/amd64,darwin/amd64" -dest ./dist -ldflags="-w -s" ./cmd/jira
install:
${MAKE} GOBIN=$$HOME/bin build
+49 -13
View File
@@ -27,9 +27,11 @@
* [Templates](#templates)
* [Writing/Editing Templates](#writingediting-templates)
* [Authentication](#authentication)
* [user vs login](#user-vs-login)
* [keyring password source](#keyring-password-source)
* [pass password source](#pass-password-source)
* [Usage](#usage)
* [TAB completion](#setting-up-tab-completion)
# go-jira
simple command line client for Atlassian's Jira service written in Go
@@ -42,11 +44,23 @@ You can download one of the pre-built binaries for **go-jira** [here](https://gi
### Build
You can build and install with [Go](https://golang.org/dl/):
You can build and install the official repository with [Go](https://golang.org/dl/):
```
go get gopkg.in/Netflix-Skunkworks/go-jira.v1/cmd/jira
```
This will checkout this repository into `$GOPATH/src/gopkg.in/Netflix-Skunkworks/go-jira.v1`, build, and install it.
Because golang likes fully qualified import paths, forking and contributing can be a bit tricky.
If you want to tinker or hack on go-jira, the [easiest way to do so](http://code.openark.org/blog/development/forking-golang-repositories-on-github-and-managing-the-import-path) is to fork the repository and clone directly into the official path like this:
`git clone https://github.com/YOUR_USER_NAME_HERE/go-jira $GOPATH/src/gopkg.in/Netflix-Skunkworks/go-jira.v1`
From within that source dir you can build and install modifications from within that directory like:
`go install ./...`
## v1 vs v0 changes
@@ -103,7 +117,7 @@ Flags:
```
###### **Incompatible command changes**
Unfortunately during the rewrite between v0 and v1 there were some changes necessary that broke backwards compatibility with existing commands. Specifically the `dups`, `blocks`, `add worklog` and `add|remove|set labels` commands have had the command word swapped around:
Unfortunately during the rewrite between v0 and v1 there were some necessary changes that broke backwards compatibility with existing commands. Specifically the `dups`, `blocks`, `add worklog` and `add|remove|set labels` commands have had the command word swapped around:
* `jira DUPLICATE dups ISSUE` => `jira dup DUPLICATE ISSUE`
* `jira BLOCKER blocks ISSUE` => `jira block BLOCKER ISSUE`
* `jira add worklog` => `jira worklog add`
@@ -112,20 +126,17 @@ Unfortunately during the rewrite between v0 and v1 there were some changes neces
* `jira set labels` => `jira labels set`
###### **Login process change**
We have, once again, changed how login happens for Jira. When authenticating against Atlassian Cloud Jira [API Tokens are now required](https://developer.atlassian.com/cloud/jira/platform/deprecation-notice-basic-auth-and-cookie-based-auth/). Please read [the Authentication section](#authentication) below for more information.
If you use a privately hosted Jira service, you can chose to use the API Token method or continue using the session login api. Please read [the Authentication section](#authentication) below for more information.
Previously `jira` used attempt to get a `JSESSION` cookies by authenticating with the webservice standard GUI login process. This has been especially problematic as users need to authenticate with various credential providers (google auth, etc). We now attempt to authenticate via the [session login api](https://docs.atlassian.com/jira/REST/cloud/#auth/1/session-login). This may be problematic for users if admins have locked down the session-login api, so we might have to bring back the error-prone Basic-Auth approach. For users that are unable to authenticate via `jira` hopefully someone in your organization can provide me with details on a process for you to authenticate and we can try to update `jira`.
## Configuration
**go-jira** uses a configuration hierarchy. When loading the configuration from disk it will recursively look through
all parent directories in your current path looking for a **.jira.d** directory. If your current directory is not
a child directory of your homedir, then your homedir will also be inspected for a **.jira.d** directory. From all of **.jira.d** directories
discovered **go-jira** will load a **<command>.yml** file (ie for `jira list` it will load `.jira.d/list.yml`) then it will merge in any properties from the **config.yml** if found. The configuration properties found in a file closests to your current working directory
will have precedence. Properties overriden with command line options will have final precedence.
**go-jira** uses a configuration hierarchy. When loading the configuration from disk it will recursively look through all parent directories in your current path looking for a **.jira.d** directory. If your current directory is not a child directory of your homedir, then your homedir will also be inspected for a **.jira.d** directory. From all of **.jira.d** directories discovered **go-jira** will load a **<command>.yml** file (ie for `jira list` it will load `.jira.d/list.yml`) then it will merge in any properties from the **config.yml** if found. The configuration properties found in a file closests to your current working directory will have precedence. Properties overriden with command line options will have final precedence.
The complicated configuration hierarchy is used because **go-jira** attempts to be context aware. For example, if you are working on a "foo" project and
you `cd` into your project workspace, wouldn't it be nice if `jira ls` automatically knew to list only issues related to the "foo" project? Likewise when you
`cd` to the "bar" project then `jira ls` should only list issues related to "bar" project. You can do this with by creating a configuration under your project
workspace at **./.jira.d/config.yml** that looks like:
The complicated configuration hierarchy is used because **go-jira** attempts to be context aware. For example, if you are working on a "foo" project and you `cd` into your project workspace, wouldn't it be nice if `jira ls` automatically knew to list only issues related to the "foo" project? Likewise when you `cd` to the "bar" project then `jira ls` should only list issues related to "bar" project. You can do this with by creating a configuration under your project workspace at **./.jira.d/config.yml** that looks like:
```
project: foo
@@ -358,7 +369,18 @@ jira list -t debug
### Authentication
By default `go-jira` will prompt for a password automatically when get a response header from the Jira service that indicates you do not have an active session (ie the `X-Ausername` header is set to `anonymous`). Then after authentication we cache the `cloud.session.token` cookie returned by the service [session login api](https://docs.atlassian.com/jira/REST/cloud/#auth/1/session-login) and reuse that on subsequent requests. Typically this cookie will be valid for several hours (depending on the service configuration). To automatically securely store your password for easy reuse by jira You can enable a `password-source` via `.jira.d/config.yml` with possible values of `keyring` or `pass`.
For Atlassian Cloud hosted Jira [API Tokens are now required](https://developer.atlassian.com/cloud/jira/platform/deprecation-notice-basic-auth-and-cookie-based-auth/). You will automatically be prompted for an API Token if your jira endoint ends in `.atlassian.net`. If you are using a private Jira service, you can force `jira` to use an api-token by setting the `authentication-method: api-token` property in your `$HOME/.jira.d/config.yml` file. The API Token needs to be presented to the Jira service on every request, so it is recommended to store this API Token security within your OS's keyring, or using the `pass` service as documented below so that it can be programatically accessed via `jira` and not prompt you every time. For a less-secure option you can also provide the API token via a `JIRA_API_TOKEN` environment variable. If you are unable to use an api-token for an Atlassian Cloud hosted Jira then you can still force `jira` to use the old session based authentication (until it the hosted system stops accepting it) by setting `authentication-method: session`.
If your Jira service still allows you to use the Session based authention method then `jira` will prompt for a password automatically when get a response header from the Jira service that indicates you do not have an active session (ie the `X-Ausername` header is set to `anonymous`). Then after authentication we cache the `cloud.session.token` cookie returned by the service [session login api](https://docs.atlassian.com/jira/REST/cloud/#auth/1/session-login) and reuse that on subsequent requests. Typically this cookie will be valid for several hours (depending on the service configuration). To automatically securely store your password for easy reuse by jira You can enable a `password-source` via `.jira.d/config.yml` with possible values of `keyring` or `pass`.
#### User vs Login
The Jira service has sometimes differing opinions about how a user is identified. In other words the ID you login with might not be ID that the jira system recognized you as. This matters when trying to identify a user via various Jira REST APIs (like issue assignment). This is especially relevent when trying to authenticate with an API Token where the authentication user is usually an email address, but within the Jira system the user is identified by a user name. To accomodate this `jira` now supports two different properties in the config file. So when authentication using the API Tokens you will likely want something like this in your `$HOME/.jira.d/config.yml` file:
```
user: person
login: person@example.com
```
You can also override these values on the command line with `jira --user person --login person@example.com`. The `login` value will be used only for authentication purposes, the `user` value will be used when a user name is required for any Jira service API calls.
#### keyring password source
On OSX and Linux there are a few keyring providers that `go-jira` can use (via this [golang module](https://github.com/tmc/keyring)). To integrate `go-jira` with a supported keyring just add this configuration to `$HOME/.jira.d/config.yml`:
@@ -422,6 +444,19 @@ export GPG_TTY=$(tty)
## Usage
#### Setting up TAB completion
Since go-jira is build with the "kingpin" golang command line library we supports bash/zsh shell completion automatically:
* <https://github.com/alecthomas/kingpin/tree/v2.2.5#bashzsh-shell-completion>
For example, in bash, adding something along the lines of:
`eval "$(jira --completion-script-bash)"`
to your bashrc, or .profile (assuming go-jira binary is already in your path) will cause jira to offer tab completion behavior.
```
usage: jira [<flags>] <command> [<args> ...]
@@ -435,7 +470,8 @@ Global flags:
-Q, --quiet Suppress output to console
--unixproxy=UNIXPROXY Path for a unix-socket proxy
--socksproxy=SOCKSPROXY Address for a socks proxy
-u, --user=USER Login name used for authentication with Jira service
-u, --user=USER user name used within the Jira service
--login=LOGIN login name that corresponds to the user used for authentication
Commands:
help: Show help.
+5 -275
View File
@@ -3,34 +3,19 @@ package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"runtime/debug"
"strconv"
"syscall"
"github.com/coryb/figtree"
"github.com/coryb/kingpeon"
"github.com/coryb/oreo"
jira "gopkg.in/Netflix-Skunkworks/go-jira.v1"
"gopkg.in/Netflix-Skunkworks/go-jira.v1/jiracli"
"gopkg.in/Netflix-Skunkworks/go-jira.v1/jiracmd"
kingpin "gopkg.in/alecthomas/kingpin.v2"
"gopkg.in/op/go-logging.v1"
)
var (
log = logging.MustGetLogger("jira")
defaultFormat = func() string {
format := os.Getenv("JIRA_LOG_FORMAT")
if format != "" {
return format
}
return "%{color}%{level:-5s}%{color:reset} %{message}"
}()
log = logging.MustGetLogger("jira")
)
func handleExit() {
@@ -44,138 +29,10 @@ func handleExit() {
}
}
func increaseLogLevel(verbosity int) {
logging.SetLevel(logging.GetLevel("")+logging.Level(verbosity), "")
if logging.GetLevel("") > logging.DEBUG {
oreo.TraceRequestBody = true
oreo.TraceResponseBody = true
}
}
var usage = `{{define "FormatCommand"}}\
{{if .FlagSummary}} {{.FlagSummary}}{{end}}\
{{range .Args}} {{if not .Required}}[{{end}}<{{.Name}}>{{if .Value|IsCumulative}}...{{end}}{{if not .Required}}]{{end}}{{end}}\
{{end}}\
{{define "FormatBriefCommands"}}\
{{range .FlattenedCommands}}\
{{if not .Hidden}}\
{{ print .FullCommand ":" | printf "%-20s"}} {{.Help}}
{{end}}\
{{end}}\
{{end}}\
{{define "FormatCommands"}}\
{{range .FlattenedCommands}}\
{{if not .Hidden}}\
{{.FullCommand}}{{if .Default}}*{{end}}{{template "FormatCommand" .}}
{{.Help|Wrap 4}}
{{with .Flags|FlagsToTwoColumns}}{{FormatTwoColumnsWithIndent . 4 2}}{{end}}
{{end}}\
{{end}}\
{{end}}\
{{define "FormatUsage"}}\
{{template "FormatCommand" .}}{{if .Commands}} <command> [<args> ...]{{end}}
{{if .Help}}
{{.Help|Wrap 0}}\
{{end}}\
{{end}}\
{{if .Context.SelectedCommand}}\
usage: {{.App.Name}} {{.Context.SelectedCommand}}{{template "FormatCommand" .Context.SelectedCommand}}
{{if .Context.SelectedCommand.Aliases }}\
{{range $top := .App.Commands}}\
{{if eq $top.FullCommand $.Context.SelectedCommand.FullCommand}}\
{{range $alias := $.Context.SelectedCommand.Aliases}}\
alias: {{$.App.Name}} {{$alias}}{{template "FormatCommand" $.Context.SelectedCommand}}
{{end}}\
{{else}}\
{{range $sub := $top.Commands}}\
{{if eq $sub.FullCommand $.Context.SelectedCommand.FullCommand}}\
{{range $alias := $.Context.SelectedCommand.Aliases}}\
alias: {{$.App.Name}} {{$top.Name}} {{$alias}}{{template "FormatCommand" $.Context.SelectedCommand}}
{{end}}\
{{end}}\
{{end}}\
{{end}}\
{{end}}\
{{end}}
{{if .Context.SelectedCommand.Help}}\
{{.Context.SelectedCommand.Help|Wrap 0}}
{{end}}\
{{else}}\
usage: {{.App.Name}}{{template "FormatUsage" .App}}
{{end}}\
{{if .App.Flags}}\
Global flags:
{{.App.Flags|FlagsToTwoColumns|FormatTwoColumns}}
{{end}}\
{{if .Context.SelectedCommand}}\
{{if and .Context.SelectedCommand.Flags|RequiredFlags}}\
Required flags:
{{.Context.SelectedCommand.Flags|RequiredFlags|FlagsToTwoColumns|FormatTwoColumns}}
{{end}}\
{{if .Context.SelectedCommand.Flags|OptionalFlags}}\
Optional flags:
{{.Context.SelectedCommand.Flags|OptionalFlags|FlagsToTwoColumns|FormatTwoColumns}}
{{end}}\
{{end}}\
{{if .Context.Args}}\
Args:
{{.Context.Args|ArgsToTwoColumns|FormatTwoColumns}}
{{end}}\
{{if .Context.SelectedCommand}}\
{{if .Context.SelectedCommand.Commands}}\
Subcommands:
{{template "FormatCommands" .Context.SelectedCommand}}
{{end}}\
{{else if .App.Commands}}\
Commands:
{{template "FormatBriefCommands" .App}}
{{end}}\
`
func main() {
defer handleExit()
logBackend := logging.NewLogBackend(os.Stderr, "", 0)
format := os.Getenv("JIRA_LOG_FORMAT")
if format == "" {
format = defaultFormat
}
logging.SetBackend(
logging.NewBackendFormatter(
logBackend,
logging.MustStringFormatter(format),
),
)
if os.Getenv("JIRA_DEBUG") == "" {
logging.SetLevel(logging.NOTICE, "")
} else {
logging.SetLevel(logging.DEBUG, "")
}
app := kingpin.New("jira", "Jira Command Line Interface")
app.Command("version", "Prints version").PreAction(func(*kingpin.ParseContext) error {
fmt.Println(jira.VERSION)
panic(jiracli.Exit{Code: 0})
})
app.UsageTemplate(usage)
var verbosity int
app.Flag("verbose", "Increase verbosity for debugging").Short('v').PreAction(func(_ *kingpin.ParseContext) error {
os.Setenv("JIRA_DEBUG", fmt.Sprintf("%d", verbosity))
increaseLogLevel(1)
return nil
}).CounterVar(&verbosity)
if os.Getenv("JIRA_DEBUG") != "" {
if verbosity, err := strconv.Atoi(os.Getenv("JIRA_DEBUG")); err == nil {
increaseLogLevel(verbosity)
}
}
jiracli.InitLogging()
fig := figtree.NewFigTree()
fig.EnvPrefix = "JIRA"
@@ -188,135 +45,8 @@ func main() {
o := oreo.New().WithCookieFile(filepath.Join(jiracli.Homedir(), fig.ConfigDir, "cookies.js"))
registry := []jiracli.CommandRegistry{
{Command: "acknowledge", Entry: jiracmd.CmdTransitionRegistry("acknowledge"), Aliases: []string{"ack"}},
{Command: "assign", Entry: jiracmd.CmdAssignRegistry(), Aliases: []string{"give"}},
{Command: "attach create", Entry: jiracmd.CmdAttachCreateRegistry()},
{Command: "attach get", Entry: jiracmd.CmdAttachGetRegistry()},
{Command: "attach list", Entry: jiracmd.CmdAttachListRegistry(), Aliases: []string{"ls"}},
{Command: "attach remove", Entry: jiracmd.CmdAttachRemoveRegistry(), Aliases: []string{"rm"}},
{Command: "backlog", Entry: jiracmd.CmdTransitionRegistry("Backlog")},
{Command: "block", Entry: jiracmd.CmdBlockRegistry()},
{Command: "browse", Entry: jiracmd.CmdBrowseRegistry(), Aliases: []string{"b"}},
{Command: "close", Entry: jiracmd.CmdTransitionRegistry("close")},
{Command: "comment", Entry: jiracmd.CmdCommentRegistry()},
{Command: "component add", Entry: jiracmd.CmdComponentAddRegistry()},
{Command: "components", Entry: jiracmd.CmdComponentsRegistry()},
{Command: "create", Entry: jiracmd.CmdCreateRegistry()},
{Command: "createmeta", Entry: jiracmd.CmdCreateMetaRegistry()},
{Command: "done", Entry: jiracmd.CmdTransitionRegistry("Done")},
{Command: "dup", Entry: jiracmd.CmdDupRegistry()},
{Command: "edit", Entry: jiracmd.CmdEditRegistry()},
{Command: "editmeta", Entry: jiracmd.CmdEditMetaRegistry()},
{Command: "epic add", Entry: jiracmd.CmdEpicAddRegistry()},
{Command: "epic create", Entry: jiracmd.CmdEpicCreateRegistry()},
{Command: "epic list", Entry: jiracmd.CmdEpicListRegistry(), Aliases: []string{"ls"}},
{Command: "epic remove", Entry: jiracmd.CmdEpicRemoveRegistry(), Aliases: []string{"rm"}},
{Command: "export-templates", Entry: jiracmd.CmdExportTemplatesRegistry()},
{Command: "fields", Entry: jiracmd.CmdFieldsRegistry()},
{Command: "in-progress", Entry: jiracmd.CmdTransitionRegistry("Progress"), Aliases: []string{"prog", "progress"}},
{Command: "issuelink", Entry: jiracmd.CmdIssueLinkRegistry()},
{Command: "issuelinktypes", Entry: jiracmd.CmdIssueLinkTypesRegistry()},
{Command: "issuetypes", Entry: jiracmd.CmdIssueTypesRegistry()},
{Command: "labels add", Entry: jiracmd.CmdLabelsAddRegistry()},
{Command: "labels remove", Entry: jiracmd.CmdLabelsRemoveRegistry(), Aliases: []string{"rm"}},
{Command: "labels set", Entry: jiracmd.CmdLabelsSetRegistry()},
{Command: "list", Entry: jiracmd.CmdListRegistry(), Aliases: []string{"ls"}},
{Command: "login", Entry: jiracmd.CmdLoginRegistry()},
{Command: "logout", Entry: jiracmd.CmdLogoutRegistry()},
{Command: "rank", Entry: jiracmd.CmdRankRegistry()},
{Command: "reopen", Entry: jiracmd.CmdTransitionRegistry("reopen")},
{Command: "request", Entry: jiracmd.CmdRequestRegistry(), Aliases: []string{"req"}},
{Command: "resolve", Entry: jiracmd.CmdTransitionRegistry("resolve")},
{Command: "start", Entry: jiracmd.CmdTransitionRegistry("start")},
{Command: "stop", Entry: jiracmd.CmdTransitionRegistry("stop")},
{Command: "subtask", Entry: jiracmd.CmdSubtaskRegistry()},
{Command: "take", Entry: jiracmd.CmdTakeRegistry()},
{Command: "todo", Entry: jiracmd.CmdTransitionRegistry("To Do")},
{Command: "transition", Entry: jiracmd.CmdTransitionRegistry(""), Aliases: []string{"trans"}},
{Command: "transitions", Entry: jiracmd.CmdTransitionsRegistry("transitions")},
{Command: "transmeta", Entry: jiracmd.CmdTransitionsRegistry("debug")},
{Command: "unassign", Entry: jiracmd.CmdUnassignRegistry()},
{Command: "unexport-templates", Entry: jiracmd.CmdUnexportTemplatesRegistry()},
{Command: "view", Entry: jiracmd.CmdViewRegistry()},
{Command: "vote", Entry: jiracmd.CmdVoteRegistry()},
{Command: "watch", Entry: jiracmd.CmdWatchRegistry()},
{Command: "worklog add", Entry: jiracmd.CmdWorklogAddRegistry()},
{Command: "worklog list", Entry: jiracmd.CmdWorklogListRegistry(), Default: true},
}
jiracmd.RegisterAllCommands()
jiracli.Register(app, o, fig, registry)
// register custom commands
data := struct {
CustomCommands kingpeon.DynamicCommands `yaml:"custom-commands" json:"custom-commands"`
}{}
if err := fig.LoadAllConfigs("config.yml", &data); err != nil {
log.Errorf("%s", err)
panic(jiracli.Exit{Code: 1})
}
if len(data.CustomCommands) > 0 {
runner := syscall.Exec
if runtime.GOOS == "windows" {
runner = func(binary string, cmd []string, env []string) error {
command := exec.Command(binary, cmd[1:]...)
command.Stdin = os.Stdin
command.Stdout = os.Stdout
command.Stderr = os.Stderr
command.Env = env
return command.Run()
}
}
tmp := map[string]interface{}{}
fig.LoadAllConfigs("config.yml", &tmp)
kingpeon.RegisterDynamicCommandsWithRunner(runner, app, data.CustomCommands, jiracli.TemplateProcessor())
}
app.Terminate(func(status int) {
for _, arg := range os.Args {
if arg == "-h" || arg == "--help" || len(os.Args) == 1 {
panic(jiracli.Exit{Code: 0})
}
}
panic(jiracli.Exit{Code: 1})
})
// checking for default usage of `jira ISSUE-123` but need to allow
// for global options first like: `jira --user mothra ISSUE-123`
ctx, err := app.ParseContext(os.Args[1:])
if err != nil && ctx == nil {
// This is an internal kingpin usage error, duplicate options/commands
log.Fatalf("error: %s, ctx: %v", err, ctx)
}
if ctx != nil {
if ctx.SelectedCommand == nil {
next := ctx.Next()
if next != nil {
if ok, err := regexp.MatchString("^[A-Z]+-[0-9]+$", next.Value); err != nil {
log.Errorf("Invalid Regex: %s", err)
} else if ok {
// insert "view" at i=1 (2nd position)
os.Args = append(os.Args[:1], append([]string{"view"}, os.Args[1:]...)...)
}
}
}
}
if _, err := app.Parse(os.Args[1:]); err != nil {
if _, ok := err.(*jiracli.Error); ok {
log.Errorf("%s", err)
panic(jiracli.Exit{Code: 1})
} else {
ctx, _ := app.ParseContext(os.Args[1:])
if ctx != nil {
app.UsageForContext(ctx)
}
log.Errorf("Invalid Usage: %s", err)
panic(jiracli.Exit{Code: 1})
}
}
app := jiracli.CommandLine(fig, o)
jiracli.ParseCommandLine(app, os.Args[1:])
}
+4 -3
View File
@@ -1,7 +1,6 @@
package jira
import (
"fmt"
"net/http"
"gopkg.in/Netflix-Skunkworks/go-jira.v1/jiradata"
@@ -10,10 +9,12 @@ import (
func responseError(resp *http.Response) error {
results := &jiradata.ErrorCollection{}
if err := readJSON(resp.Body, results); err != nil {
return err
results.Status = resp.StatusCode
results.ErrorMessages = append(results.ErrorMessages, err.Error())
}
if len(results.ErrorMessages) == 0 && len(results.Errors) == 0 {
return fmt.Errorf(resp.Status)
results.Status = resp.StatusCode
results.ErrorMessages = append(results.ErrorMessages, resp.Status)
}
return results
}
+1 -1
View File
@@ -7,7 +7,7 @@ import (
var log = logging.MustGetLogger("jira")
const VERSION = "1.0.14"
const VERSION = "1.0.16"
type Jira struct {
Endpoint string `json:"endpoint,omitempty" yaml:"endpoint,omitempty"`
+61 -26
View File
@@ -3,6 +3,7 @@ package jiracli
import (
"bytes"
"crypto/tls"
"encoding/base64"
"fmt"
"io"
"io/ioutil"
@@ -20,23 +21,22 @@ import (
"gopkg.in/AlecAivazis/survey.v1"
kingpin "gopkg.in/alecthomas/kingpin.v2"
yaml "gopkg.in/coryb/yaml.v2"
logging "gopkg.in/op/go-logging.v1"
)
var log = logging.MustGetLogger("jira")
type Exit struct {
Code int
}
type GlobalOptions struct {
Endpoint figtree.StringOption `yaml:"endpoint,omitempty" json:"endpoint,omitempty"`
Insecure figtree.BoolOption `yaml:"insecure,omitempty" json:"insecure,omitempty"`
PasswordSource figtree.StringOption `yaml:"password-source,omitempty" json:"password-source,omitempty"`
Quiet figtree.BoolOption `yaml:"quiet,omitempty" json:"quiet,omitempty"`
UnixProxy figtree.StringOption `yaml:"unixproxy,omitempty" json:"unixproxy,omitempty"`
SocksProxy figtree.StringOption `yaml:"socksproxy,omitempty" json:"socksproxy,omitempty"`
User figtree.StringOption `yaml:"user,omitempty" json:"user,omitempty"`
AuthenticationMethod figtree.StringOption `yaml:"authentication-method,omitempty" json:"authentication-method,omitempty"`
Endpoint figtree.StringOption `yaml:"endpoint,omitempty" json:"endpoint,omitempty"`
Insecure figtree.BoolOption `yaml:"insecure,omitempty" json:"insecure,omitempty"`
Login figtree.StringOption `yaml:"login,omitempty" json:"login,omitempty"`
PasswordSource figtree.StringOption `yaml:"password-source,omitempty" json:"password-source,omitempty"`
Quiet figtree.BoolOption `yaml:"quiet,omitempty" json:"quiet,omitempty"`
SocksProxy figtree.StringOption `yaml:"socksproxy,omitempty" json:"socksproxy,omitempty"`
UnixProxy figtree.StringOption `yaml:"unixproxy,omitempty" json:"unixproxy,omitempty"`
User figtree.StringOption `yaml:"user,omitempty" json:"user,omitempty"`
}
type CommonOptions struct {
@@ -66,39 +66,68 @@ type kingpinAppOrCommand interface {
GetCommand(string) *kingpin.CmdClause
}
func Register(app *kingpin.Application, o *oreo.Client, fig *figtree.FigTree, reg []CommandRegistry) {
var globalCommandRegistry = []CommandRegistry{}
func RegisterCommand(regEntry CommandRegistry) {
globalCommandRegistry = append(globalCommandRegistry, regEntry)
}
func (o *GlobalOptions) AuthMethod() string {
if strings.Contains(o.Endpoint.Value, ".atlassian.net") && o.AuthenticationMethod.Source == "default" {
return "api-token"
}
return o.AuthenticationMethod.Value
}
func register(app *kingpin.Application, o *oreo.Client, fig *figtree.FigTree) {
globals := GlobalOptions{
User: figtree.NewStringOption(os.Getenv("USER")),
User: figtree.NewStringOption(os.Getenv("USER")),
AuthenticationMethod: figtree.NewStringOption("session"),
}
app.Flag("endpoint", "Base URI to use for Jira").Short('e').SetValue(&globals.Endpoint)
app.Flag("insecure", "Disable TLS certificate verification").Short('k').SetValue(&globals.Insecure)
app.Flag("quiet", "Suppress output to console").Short('Q').SetValue(&globals.Quiet)
app.Flag("unixproxy", "Path for a unix-socket proxy").SetValue(&globals.UnixProxy)
app.Flag("socksproxy", "Address for a socks proxy").SetValue(&globals.SocksProxy)
app.Flag("user", "Login name used for authentication with Jira service").Short('u').SetValue(&globals.User)
app.Flag("user", "user name used within the Jira service").Short('u').SetValue(&globals.User)
app.Flag("login", "login name that corresponds to the user used for authentication").SetValue(&globals.Login)
o = o.WithPreCallback(
func(req *http.Request) (*http.Request, error) {
if globals.AuthMethod() == "api-token" {
// need to set basic auth header with user@domain:api-token
token := globals.GetPass()
authHeader := fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", globals.Login.Value, token))))
req.Header.Add("Authorization", authHeader)
}
return req, nil
},
)
o = o.WithPostCallback(
func(req *http.Request, resp *http.Response) (*http.Response, error) {
authUser := resp.Header.Get("X-Ausername")
if authUser == "" || authUser == "anonymous" {
// preserve the --quiet value, we need to temporarily disable it so
// the normal login output is surpressed
defer func(quiet bool) {
globals.Quiet.Value = quiet
}(globals.Quiet.Value)
globals.Quiet.Value = true
if globals.AuthMethod() == "session" {
authUser := resp.Header.Get("X-Ausername")
if authUser == "" || authUser == "anonymous" {
// preserve the --quiet value, we need to temporarily disable it so
// the normal login output is surpressed
defer func(quiet bool) {
globals.Quiet.Value = quiet
}(globals.Quiet.Value)
globals.Quiet.Value = true
// we are not logged in, so force login now by running the "login" command
app.Parse([]string{"login"})
// we are not logged in, so force login now by running the "login" command
app.Parse([]string{"login"})
// rerun the original request
return o.Do(req)
// rerun the original request
return o.Do(req)
}
}
return resp, nil
},
)
for _, command := range reg {
for _, command := range globalCommandRegistry {
copy := command
commandFields := strings.Fields(copy.Command)
var appOrCmd kingpinAppOrCommand = app
@@ -129,6 +158,12 @@ func Register(app *kingpin.Application, o *oreo.Client, fig *figtree.FigTree, re
} else if globals.SocksProxy.Value != "" {
o = o.WithTransport(socksProxy(globals.SocksProxy.Value))
}
if globals.AuthMethod() == "api-token" {
o = o.WithCookieFile("")
}
if globals.Login.Value == "" {
globals.Login = globals.User
}
return nil
})
+43
View File
@@ -0,0 +1,43 @@
package jiracli
import (
"os"
"strconv"
"github.com/coryb/oreo"
logging "gopkg.in/op/go-logging.v1"
)
var (
log = logging.MustGetLogger("jira")
)
func IncreaseLogLevel(verbosity int) {
logging.SetLevel(logging.GetLevel("")+logging.Level(verbosity), "")
if logging.GetLevel("") > logging.DEBUG {
oreo.TraceRequestBody = true
oreo.TraceResponseBody = true
}
}
func InitLogging() {
logBackend := logging.NewLogBackend(os.Stderr, "", 0)
format := os.Getenv("JIRA_LOG_FORMAT")
if format == "" {
format = "%{color}%{level:-5s}%{color:reset} %{message}"
}
logging.SetBackend(
logging.NewBackendFormatter(
logBackend,
logging.MustStringFormatter(format),
),
)
if os.Getenv("JIRA_DEBUG") == "" {
logging.SetLevel(logging.NOTICE, "")
} else {
logging.SetLevel(logging.DEBUG, "")
if verbosity, err := strconv.Atoi(os.Getenv("JIRA_DEBUG")); err == nil {
IncreaseLogLevel(verbosity)
}
}
}
+33 -6
View File
@@ -3,6 +3,7 @@ package jiracli
import (
"bytes"
"fmt"
"os"
"os/exec"
"strings"
@@ -12,24 +13,36 @@ import (
func (o *GlobalOptions) ProvideAuthParams() *jiradata.AuthParams {
return &jiradata.AuthParams{
Username: o.User.Value,
Username: o.Login.Value,
Password: o.GetPass(),
}
}
func (o *GlobalOptions) keyName() string {
user := o.Login.Value
if o.AuthMethod() == "api-token" {
user = "api-token:" + user
}
if o.PasswordSource.Value == "pass" {
return fmt.Sprintf("GoJira/%s", user)
}
return user
}
func (o *GlobalOptions) GetPass() string {
passwd := ""
if o.PasswordSource.Value != "" {
if o.PasswordSource.Value == "keyring" {
var err error
passwd, err = keyringGet(o.User.Value)
passwd, err = keyringGet(o.keyName())
if err != nil {
panic(err)
}
} else if o.PasswordSource.Value == "pass" {
if bin, err := exec.LookPath("pass"); err == nil {
buf := bytes.NewBufferString("")
cmd := exec.Command(bin, fmt.Sprintf("GoJira/%s", o.User))
cmd := exec.Command(bin, o.keyName())
cmd.Stdout = buf
cmd.Stderr = buf
if err := cmd.Run(); err == nil {
@@ -44,9 +57,23 @@ func (o *GlobalOptions) GetPass() string {
if passwd != "" {
return passwd
}
if passwd = os.Getenv("JIRA_API_TOKEN"); passwd != "" && o.AuthMethod() == "api-token" {
return passwd
}
prompt := fmt.Sprintf("Jira Password [%s]: ", o.Login)
help := ""
if o.AuthMethod() == "api-token" {
prompt = fmt.Sprintf("Jira API-Token [%s]: ", o.Login)
help = "API Tokens may be required by your Jira service endpoint: https://developer.atlassian.com/cloud/jira/platform/deprecation-notice-basic-auth-and-cookie-based-auth/"
}
err := survey.AskOne(
&survey.Password{
Message: fmt.Sprintf("Jira Password [%s]: ", o.User),
Message: prompt,
Help: help,
},
&passwd,
nil,
@@ -62,7 +89,7 @@ func (o *GlobalOptions) GetPass() string {
func (o *GlobalOptions) SetPass(passwd string) error {
if o.PasswordSource.Value == "keyring" {
// save password in keychain so that it can be used for subsequent http requests
err := keyringSet(o.User.Value, passwd)
err := keyringSet(o.keyName(), passwd)
if err != nil {
log.Errorf("Failed to set password in keyring: %s", err)
return err
@@ -70,7 +97,7 @@ func (o *GlobalOptions) SetPass(passwd string) error {
} else if o.PasswordSource.Value == "pass" {
if bin, err := exec.LookPath("pass"); err == nil {
log.Debugf("using %s", bin)
passName := fmt.Sprintf("GoJira/%s", o.User)
passName := o.keyName()
if passwd != "" {
in := bytes.NewBufferString(fmt.Sprintf("%s\n%s\n", passwd, passwd))
out := bytes.NewBufferString("")
+14 -3
View File
@@ -8,8 +8,8 @@ import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
"reflect"
"regexp"
"strconv"
"strings"
"text/template"
@@ -17,6 +17,7 @@ import (
yaml "gopkg.in/coryb/yaml.v2"
"github.com/coryb/figtree"
shellquote "github.com/kballard/go-shellquote"
"github.com/mgutz/ansi"
"golang.org/x/crypto/ssh/terminal"
)
@@ -74,6 +75,16 @@ func TemplateProcessor() *template.Template {
}
return out
},
"shellquote": func(content string) string {
return shellquote.Join(content)
},
"toMinJson": func(content interface{}) (string, error) {
bytes, err := json.Marshal(content)
if err != nil {
return "", err
}
return string(bytes), nil
},
"toJson": func(content interface{}) (string, error) {
bytes, err := json.MarshalIndent(content, "", " ")
if err != nil {
@@ -147,7 +158,7 @@ func TemplateProcessor() *template.Template {
return strings.Join(vals, sep)
},
"abbrev": func(max int, content string) string {
if len(content) > max {
if len(content) > max && max > 2 {
var buffer bytes.Buffer
buffer.WriteString(content[:max-3])
buffer.WriteString("...")
@@ -279,7 +290,7 @@ const defaultTableTemplate = `{{/* table template */ -}}
| {{ "Issue" | printf "%-14s" }} | {{ "Summary" | printf (printf "%%-%ds" (sub $w 2)) }} | {{ "Type" | printf "%-12s"}} | {{ "Priority" | printf "%-12s" }} | {{ "Status" | printf "%-12s" }} | {{ "Age" | printf "%-10s" }} | {{ "Reporter" | printf "%-12s" }} | {{ "Assignee" | printf "%-12s" }} |
+{{ "-" | rep 16 }}+{{ "-" | rep $w }}+{{ "-" | rep 14 }}+{{ "-" | rep 14 }}+{{ "-" | rep 14 }}+{{ "-" | rep 12 }}+{{ "-" | rep 14 }}+{{ "-" | rep 14 }}+
{{ range .issues -}}
| {{ .key | printf "%-14s"}} | {{ .fields.summary | abbrev (sub $w 2) | printf (printf "%%-%ds" (sub $w 2)) }} | {{.fields.issuetype.name | printf "%-12s" }} | {{.fields.priority.name | printf "%-12s" }} | {{.fields.status.name | printf "%-12s" }} | {{.fields.created | age | printf "%-10s" }} | {{if .fields.reporter}}{{ .fields.reporter.name | printf "%-12s"}}{{else}}<unassigned>{{end}} | {{if .fields.assignee }}{{.fields.assignee.name | printf "%-12s" }}{{else}}<unassigned>{{end}} |
| {{ .key | printf "%-14s"}} | {{ .fields.summary | abbrev (sub $w 2) | printf (printf "%%-%ds" (sub $w 2)) }} | {{.fields.issuetype.name | printf "%-12s" }} | {{if .fields.priority}}{{.fields.priority.name | printf "%-12s" }}{{else}}<unassigned>{{end}} | {{.fields.status.name | printf "%-12s" }} | {{.fields.created | age | printf "%-10s" }} | {{if .fields.reporter}}{{ .fields.reporter.name | printf "%-12s"}}{{else}}<unassigned>{{end}} | {{if .fields.assignee }}{{.fields.assignee.name | printf "%-12s" }}{{else}}<unassigned>{{end}} |
{{ end -}}
+{{ "-" | rep 16 }}+{{ "-" | rep $w }}+{{ "-" | rep 14 }}+{{ "-" | rep 14 }}+{{ "-" | rep 14 }}+{{ "-" | rep 12 }}+{{ "-" | rep 14 }}+{{ "-" | rep 14 }}+
`
+199
View File
@@ -0,0 +1,199 @@
package jiracli
import (
"fmt"
"os"
"os/exec"
"regexp"
"runtime"
"syscall"
"github.com/coryb/figtree"
"github.com/coryb/kingpeon"
"github.com/coryb/oreo"
jira "gopkg.in/Netflix-Skunkworks/go-jira.v1"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)
var usage = `{{define "FormatCommand"}}\
{{if .FlagSummary}} {{.FlagSummary}}{{end}}\
{{range .Args}} {{if not .Required}}[{{end}}<{{.Name}}>{{if .Value|IsCumulative}}...{{end}}{{if not .Required}}]{{end}}{{end}}\
{{end}}\
{{define "FormatBriefCommands"}}\
{{range .FlattenedCommands}}\
{{if not .Hidden}}\
{{ print .FullCommand ":" | printf "%-20s"}} {{.Help}}
{{end}}\
{{end}}\
{{end}}\
{{define "FormatCommands"}}\
{{range .FlattenedCommands}}\
{{if not .Hidden}}\
{{.FullCommand}}{{if .Default}}*{{end}}{{template "FormatCommand" .}}
{{.Help|Wrap 4}}
{{with .Flags|FlagsToTwoColumns}}{{FormatTwoColumnsWithIndent . 4 2}}{{end}}
{{end}}\
{{end}}\
{{end}}\
{{define "FormatUsage"}}\
{{template "FormatCommand" .}}{{if .Commands}} <command> [<args> ...]{{end}}
{{if .Help}}
{{.Help|Wrap 0}}\
{{end}}\
{{end}}\
{{if .Context.SelectedCommand}}\
usage: {{.App.Name}} {{.Context.SelectedCommand}}{{template "FormatCommand" .Context.SelectedCommand}}
{{if .Context.SelectedCommand.Aliases }}\
{{range $top := .App.Commands}}\
{{if eq $top.FullCommand $.Context.SelectedCommand.FullCommand}}\
{{range $alias := $.Context.SelectedCommand.Aliases}}\
alias: {{$.App.Name}} {{$alias}}{{template "FormatCommand" $.Context.SelectedCommand}}
{{end}}\
{{else}}\
{{range $sub := $top.Commands}}\
{{if eq $sub.FullCommand $.Context.SelectedCommand.FullCommand}}\
{{range $alias := $.Context.SelectedCommand.Aliases}}\
alias: {{$.App.Name}} {{$top.Name}} {{$alias}}{{template "FormatCommand" $.Context.SelectedCommand}}
{{end}}\
{{end}}\
{{end}}\
{{end}}\
{{end}}\
{{end}}
{{if .Context.SelectedCommand.Help}}\
{{.Context.SelectedCommand.Help|Wrap 0}}
{{end}}\
{{else}}\
usage: {{.App.Name}}{{template "FormatUsage" .App}}
{{end}}\
{{if .App.Flags}}\
Global flags:
{{.App.Flags|FlagsToTwoColumns|FormatTwoColumns}}
{{end}}\
{{if .Context.SelectedCommand}}\
{{if and .Context.SelectedCommand.Flags|RequiredFlags}}\
Required flags:
{{.Context.SelectedCommand.Flags|RequiredFlags|FlagsToTwoColumns|FormatTwoColumns}}
{{end}}\
{{if .Context.SelectedCommand.Flags|OptionalFlags}}\
Optional flags:
{{.Context.SelectedCommand.Flags|OptionalFlags|FlagsToTwoColumns|FormatTwoColumns}}
{{end}}\
{{end}}\
{{if .Context.Args}}\
Args:
{{.Context.Args|ArgsToTwoColumns|FormatTwoColumns}}
{{end}}\
{{if .Context.SelectedCommand}}\
{{if .Context.SelectedCommand.Commands}}\
Subcommands:
{{template "FormatCommands" .Context.SelectedCommand}}
{{end}}\
{{else if .App.Commands}}\
Commands:
{{template "FormatBriefCommands" .App}}
{{end}}\
`
func CommandLine(fig *figtree.FigTree, o *oreo.Client) *kingpin.Application {
app := kingpin.New("jira", "Jira Command Line Interface")
app.UsageWriter(os.Stdout)
app.ErrorWriter(os.Stderr)
app.Command("version", "Prints version").PreAction(func(*kingpin.ParseContext) error {
fmt.Println(jira.VERSION)
panic(Exit{Code: 0})
})
app.UsageTemplate(usage)
var verbosity int
app.Flag("verbose", "Increase verbosity for debugging").Short('v').PreAction(func(_ *kingpin.ParseContext) error {
os.Setenv("JIRA_DEBUG", fmt.Sprintf("%d", verbosity))
IncreaseLogLevel(1)
return nil
}).CounterVar(&verbosity)
app.Terminate(func(status int) {
for _, arg := range os.Args {
if arg == "-h" || arg == "--help" || len(os.Args) == 1 {
panic(Exit{Code: 0})
}
}
panic(Exit{Code: 1})
})
register(app, o, fig)
// register custom commands
data := struct {
CustomCommands kingpeon.DynamicCommands `yaml:"custom-commands" json:"custom-commands"`
}{}
if err := fig.LoadAllConfigs("config.yml", &data); err != nil {
log.Errorf("%s", err)
panic(Exit{Code: 1})
}
if len(data.CustomCommands) > 0 {
runner := syscall.Exec
if runtime.GOOS == "windows" {
runner = func(binary string, cmd []string, env []string) error {
command := exec.Command(binary, cmd[1:]...)
command.Stdin = os.Stdin
command.Stdout = os.Stdout
command.Stderr = os.Stderr
command.Env = env
return command.Run()
}
}
tmp := map[string]interface{}{}
fig.LoadAllConfigs("config.yml", &tmp)
kingpeon.RegisterDynamicCommandsWithRunner(runner, app, data.CustomCommands, TemplateProcessor())
}
return app
}
func ParseCommandLine(app *kingpin.Application, args []string) {
// checking for default usage of `jira ISSUE-123` but need to allow
// for global options first like: `jira --user mothra ISSUE-123`
ctx, err := app.ParseContext(args)
if err != nil && ctx == nil {
// This is an internal kingpin usage error, duplicate options/commands
log.Fatalf("error: %s, ctx: %v", err, ctx)
}
if ctx != nil {
if ctx.SelectedCommand == nil {
next := ctx.Next()
if next != nil {
if ok, err := regexp.MatchString("^[A-Z]+-[0-9]+$", next.Value); err != nil {
log.Errorf("Invalid Regex: %s", err)
} else if ok {
// insert "view" at i=1 (2nd position)
os.Args = append(os.Args[:1], append([]string{"view"}, os.Args[1:]...)...)
}
}
}
}
if _, err := app.Parse(os.Args[1:]); err != nil {
if _, ok := err.(*Error); ok {
log.Errorf("%s", err)
panic(Exit{Code: 1})
} else {
ctx, _ := app.ParseContext(os.Args[1:])
if ctx != nil {
app.UsageForContext(ctx)
}
log.Errorf("Invalid Usage: %s", err)
panic(Exit{Code: 1})
}
}
}
+1 -1
View File
@@ -37,7 +37,7 @@ func CmdEditRegistry() *jiracli.CommandRegistryEntry {
},
func(o *oreo.Client, globals *jiracli.GlobalOptions) error {
if opts.QueryFields == "" {
opts.QueryFields = "assignee,created,priority,reporter,status,summary,updated,issuetype,comments,description,votes,created,customfield_10110,components"
opts.QueryFields = "assignee,created,priority,reporter,status,summary,updated,issuetype,comment,description,votes,created,customfield_10110,components"
}
return CmdEdit(o, globals, &opts)
},
+5
View File
@@ -46,6 +46,11 @@ func authCallback(req *http.Request, resp *http.Response) (*http.Response, error
// CmdLogin will attempt to login into jira server
func CmdLogin(o *oreo.Client, globals *jiracli.GlobalOptions, opts *jiracli.CommonOptions) error {
if globals.AuthMethod() == "api-token" {
log.Noticef("No need to login when using api-token authentication method")
return nil
}
ua := o.WithoutRedirect().WithRetries(0).WithoutCallbacks().WithPostCallback(authCallback)
for {
if session, err := jira.GetSession(o, globals.Endpoint.Value); err != nil {
+4
View File
@@ -27,6 +27,10 @@ func CmdLogoutRegistry() *jiracli.CommandRegistryEntry {
// CmdLogout will attempt to terminate an active Jira session
func CmdLogout(o *oreo.Client, globals *jiracli.GlobalOptions, opts *jiracli.CommonOptions) error {
if globals.AuthMethod() == "api-token" {
log.Noticef("No need to logout when using api-token authentication method")
return nil
}
ua := o.WithoutRedirect().WithRetries(0).WithoutCallbacks()
err := jira.DeleteSession(ua, globals.Endpoint.Value)
if err == nil {
+60
View File
@@ -0,0 +1,60 @@
package jiracmd
import "gopkg.in/Netflix-Skunkworks/go-jira.v1/jiracli"
func RegisterAllCommands() {
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "acknowledge", Entry: CmdTransitionRegistry("acknowledge"), Aliases: []string{"ack"}})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "assign", Entry: CmdAssignRegistry(), Aliases: []string{"give"}})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "attach create", Entry: CmdAttachCreateRegistry()})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "attach get", Entry: CmdAttachGetRegistry()})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "attach list", Entry: CmdAttachListRegistry(), Aliases: []string{"ls"}})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "attach remove", Entry: CmdAttachRemoveRegistry(), Aliases: []string{"rm"}})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "backlog", Entry: CmdTransitionRegistry("Backlog")})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "block", Entry: CmdBlockRegistry()})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "browse", Entry: CmdBrowseRegistry(), Aliases: []string{"b"}})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "close", Entry: CmdTransitionRegistry("close")})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "comment", Entry: CmdCommentRegistry()})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "component add", Entry: CmdComponentAddRegistry()})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "components", Entry: CmdComponentsRegistry()})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "create", Entry: CmdCreateRegistry()})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "createmeta", Entry: CmdCreateMetaRegistry()})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "done", Entry: CmdTransitionRegistry("Done")})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "dup", Entry: CmdDupRegistry()})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "edit", Entry: CmdEditRegistry()})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "editmeta", Entry: CmdEditMetaRegistry()})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "epic add", Entry: CmdEpicAddRegistry()})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "epic create", Entry: CmdEpicCreateRegistry()})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "epic list", Entry: CmdEpicListRegistry(), Aliases: []string{"ls"}})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "epic remove", Entry: CmdEpicRemoveRegistry(), Aliases: []string{"rm"}})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "export-templates", Entry: CmdExportTemplatesRegistry()})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "fields", Entry: CmdFieldsRegistry()})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "in-progress", Entry: CmdTransitionRegistry("Progress"), Aliases: []string{"prog", "progress"}})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "issuelink", Entry: CmdIssueLinkRegistry()})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "issuelinktypes", Entry: CmdIssueLinkTypesRegistry()})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "issuetypes", Entry: CmdIssueTypesRegistry()})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "labels add", Entry: CmdLabelsAddRegistry()})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "labels remove", Entry: CmdLabelsRemoveRegistry(), Aliases: []string{"rm"}})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "labels set", Entry: CmdLabelsSetRegistry()})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "list", Entry: CmdListRegistry(), Aliases: []string{"ls"}})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "login", Entry: CmdLoginRegistry()})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "logout", Entry: CmdLogoutRegistry()})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "rank", Entry: CmdRankRegistry()})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "reopen", Entry: CmdTransitionRegistry("reopen")})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "request", Entry: CmdRequestRegistry(), Aliases: []string{"req"}})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "resolve", Entry: CmdTransitionRegistry("resolve")})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "start", Entry: CmdTransitionRegistry("start")})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "stop", Entry: CmdTransitionRegistry("stop")})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "subtask", Entry: CmdSubtaskRegistry()})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "take", Entry: CmdTakeRegistry()})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "todo", Entry: CmdTransitionRegistry("To Do")})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "transition", Entry: CmdTransitionRegistry(""), Aliases: []string{"trans"}})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "transitions", Entry: CmdTransitionsRegistry("transitions")})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "transmeta", Entry: CmdTransitionsRegistry("debug")})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "unassign", Entry: CmdUnassignRegistry()})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "unexport-templates", Entry: CmdUnexportTemplatesRegistry()})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "view", Entry: CmdViewRegistry()})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "vote", Entry: CmdVoteRegistry()})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "watch", Entry: CmdWatchRegistry()})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "worklog add", Entry: CmdWorklogAddRegistry()})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "worklog list", Entry: CmdWorklogListRegistry(), Default: true})
}
+3 -1
View File
@@ -17,7 +17,9 @@ func CmdTakeRegistry() *jiracli.CommandRegistryEntry {
return CmdAssignUsage(cmd, &opts)
},
func(o *oreo.Client, globals *jiracli.GlobalOptions) error {
opts.Assignee = globals.User.Value
if opts.Assignee == "" {
opts.Assignee = globals.User.Value
}
return CmdAssign(o, globals, &opts)
},
}
+1 -1
View File
@@ -23,7 +23,7 @@ func CmdUnexportTemplatesRegistry() *jiracli.CommandRegistryEntry {
return CmdExportTemplatesUsage(cmd, &opts)
},
func(o *oreo.Client, globals *jiracli.GlobalOptions) error {
if opts.Dir != "" {
if opts.Dir == "" {
opts.Dir = fmt.Sprintf("%s/.jira.d/templates", jiracli.Homedir())
}
return CmdUnexportTemplates(globals, &opts)
+1 -1
View File
@@ -17,7 +17,7 @@ type AuthOptions struct {
Password string
}
func (a *AuthOptions) AuthParams() *jiradata.AuthParams {
func (a *AuthOptions) ProvideAuthParams() *jiradata.AuthParams {
return &jiradata.AuthParams{
Username: a.Username,
Password: a.Password,
+1
View File
@@ -3,6 +3,7 @@ config:
password-source: pass
endpoint: https://go-jira.atlassian.net
user: gojira
login: gojira@corybennett.org
project: BASIC
+1 -1
View File
@@ -245,7 +245,7 @@ EOF
# reset login for mothra for voting
###############################################################################
jira="$jira --user mothra"
jira="$jira --user mothra --login mothra@corybennett.org"
RUNS $jira logout
RUNS $jira login
+1 -1
View File
@@ -185,7 +185,7 @@ EOF
# reset login for mothra for voting
###############################################################################
jira="$jira --user mothra"
jira="$jira --user mothra --login mothra@corybennett.org"
RUNS $jira logout
echo "mothra123" | RUNS $jira login
+1 -1
View File
@@ -185,7 +185,7 @@ EOF
# reset login for mothra for voting
###############################################################################
jira="$jira --user mothra"
jira="$jira --user mothra --login mothra@corybennett.org"
RUNS $jira logout
echo "mothra123" | RUNS $jira login
+1 -1
View File
@@ -185,7 +185,7 @@ EOF
# reset login for mothra for voting
###############################################################################
jira="$jira --user mothra"
jira="$jira --user mothra --login mothra@corybennett.org"
RUNS $jira logout
echo "mothra123" | RUNS $jira login
+1 -1
View File
@@ -194,7 +194,7 @@ EOF
# reset login for mothra for voting
###############################################################################
jira="$jira --user mothra"
jira="$jira --user mothra --login mothra@corybennett.org"
RUNS $jira logout
echo "mothra123" | RUNS $jira login
+1 -1
View File
@@ -187,7 +187,7 @@ EOF
# reset login for mothra for voting
###############################################################################
jira="$jira --user mothra"
jira="$jira --user mothra --login mothra@corybennett.org"
RUNS $jira logout
echo "mothra123" | RUNS $jira login
+1 -1
View File
@@ -22,7 +22,7 @@ func runDynamicCommand(run runner, dynamiccommand *DynamicCommand, t *template.T
if err != nil {
return err
}
err = t.Execute(buf, nil)
err = t.Execute(buf, dynamiccommand)
if err != nil {
return err
}