Fix token create --format json

It will now output the token in json format

Fixes #63

Signed-off-by: Djordje Lukic <djordje.lukic@docker.com>
This commit is contained in:
Djordje Lukic 2020-10-22 17:39:36 +02:00
Родитель dec3d97f45
Коммит 57a99cefbb
4 изменённых файлов: 80 добавлений и 93 удалений

Просмотреть файл

@ -109,37 +109,34 @@ func runList(streams command.Streams, hubClient *hub.Client, opts listOptions, a
return err
}
return opts.Print(streams.Out(), &helper{repositories, total}, printRepositories)
return opts.Print(streams.Out(), repositories, printRepositories(total))
}
func printRepositories(out io.Writer, values interface{}) error {
h := values.(*helper)
tw := tabwriter.New(out, " ")
func printRepositories(total int) format.PrettyPrinter {
return func(out io.Writer, values interface{}) error {
repositories := values.([]hub.Repository)
tw := tabwriter.New(out, " ")
for _, column := range defaultColumns {
tw.Column(ansi.Header(column.header), len(column.header))
}
tw.Line()
for _, repository := range h.repositories {
for _, column := range defaultColumns {
value, width := column.value(repository)
tw.Column(value, width)
tw.Column(ansi.Header(column.header), len(column.header))
}
tw.Line()
}
if err := tw.Flush(); err != nil {
return err
}
if len(h.repositories) < h.total {
fmt.Fprintln(out, ansi.Info(fmt.Sprintf("%v/%v listed, use --all flag to show all", len(h.repositories), h.total)))
}
return nil
}
for _, repository := range repositories {
for _, column := range defaultColumns {
value, width := column.value(repository)
tw.Column(value, width)
}
tw.Line()
}
if err := tw.Flush(); err != nil {
return err
}
type helper struct {
repositories []hub.Repository
total int
if len(repositories) < total {
fmt.Fprintln(out, ansi.Info(fmt.Sprintf("%v/%v listed, use --all flag to show all", len(repositories), total)))
}
return nil
}
}

Просмотреть файл

@ -170,34 +170,36 @@ func runList(streams command.Streams, hubClient *hub.Client, opts listOptions, r
defaultColumns = append(defaultColumns, platformColumn)
}
return opts.Print(streams.Out(), &helper{tags, total}, printTags)
return opts.Print(streams.Out(), tags, printTags(total))
}
func printTags(out io.Writer, values interface{}) error {
h := values.(*helper)
tw := tabwriter.New(out, " ")
for _, column := range defaultColumns {
tw.Column(ansi.Header(column.header), len(column.header))
}
tw.Line()
for _, tag := range h.tags {
func printTags(total int) format.PrettyPrinter {
return func(out io.Writer, values interface{}) error {
tags := values.([]hub.Tag)
tw := tabwriter.New(out, " ")
for _, column := range defaultColumns {
value, width := column.value(tag)
tw.Column(value, width)
tw.Column(ansi.Header(column.header), len(column.header))
}
tw.Line()
}
if err := tw.Flush(); err != nil {
return err
}
if len(h.tags) < h.total {
fmt.Fprintln(out, ansi.Info(fmt.Sprintf("%v/%v listed, use --all flag to show all", len(h.tags), h.total)))
}
for _, tag := range tags {
for _, column := range defaultColumns {
value, width := column.value(tag)
tw.Column(value, width)
}
tw.Line()
}
if err := tw.Flush(); err != nil {
return err
}
return nil
if len(tags) < total {
fmt.Fprintln(out, ansi.Info(fmt.Sprintf("%v/%v listed, use --all flag to show all", len(tags), total)))
}
return nil
}
}
const (
@ -253,8 +255,3 @@ func promptCallToAction(out io.Writer, client accountInfo) error {
_, err = fmt.Fprint(out, ansi.Info(callToAction))
return err
}
type helper struct {
tags []hub.Tag
total int
}

Просмотреть файл

@ -65,16 +65,17 @@ func runCreate(streams command.Streams, hubClient *hub.Client, opts createOption
if err != nil {
return err
}
return opts.Print(streams.Out(), &printHelper{hubClient.AuthConfig.Username, token, opts.quiet}, printCreatedToken)
}
func printCreatedToken(out io.Writer, value interface{}) error {
helper := value.(*printHelper)
if helper.quiet {
fmt.Fprintln(out, helper.token.Token)
if opts.quiet {
fmt.Fprintln(streams.Out(), token.Token)
return nil
}
fmt.Fprintf(out, ansi.Emphasise("Personal Access Token successfully created!")+`
return opts.Print(streams.Out(), token, printCreatedToken(hubClient))
}
func printCreatedToken(hubClient *hub.Client) format.PrettyPrinter {
return func(out io.Writer, value interface{}) error {
helper := value.(*hub.Token)
fmt.Fprintf(out, ansi.Emphasise("Personal Access Token successfully created!")+`
When logging in from your Docker CLI client, use this token as a password.
`+ansi.Header("Description:")+` %s
@ -88,14 +89,9 @@ To use the access token from your Docker CLI client:
`+ansi.Warn(`WARNING: This access token will only be displayed once.
It will not be stored and cannot be retrieved. Please be sure to save it now.
`),
helper.token.Description,
helper.userName,
ansi.Emphasise(helper.token.Token))
return nil
}
type printHelper struct {
userName string
token *hub.Token
quiet bool
helper.Description,
hubClient.AuthConfig.Username,
ansi.Emphasise(helper.Token))
return nil
}
}

Просмотреть файл

@ -98,35 +98,32 @@ func runList(streams command.Streams, hubClient *hub.Client, opts listOptions) e
if err != nil {
return err
}
return opts.Print(streams.Out(), &helper{tokens, total}, printTokens)
return opts.Print(streams.Out(), tokens, printTokens(total))
}
func printTokens(out io.Writer, values interface{}) error {
h := values.(*helper)
tw := tabwriter.New(out, " ")
for _, column := range defaultColumns {
tw.Column(ansi.Header(column.header), len(column.header))
}
tw.Line()
for _, token := range h.tokens {
func printTokens(total int) format.PrettyPrinter {
return func(out io.Writer, values interface{}) error {
tokens := values.([]hub.Token)
tw := tabwriter.New(out, " ")
for _, column := range defaultColumns {
value, width := column.value(token)
tw.Column(value, width)
tw.Column(ansi.Header(column.header), len(column.header))
}
tw.Line()
}
if err := tw.Flush(); err != nil {
return err
}
for _, token := range tokens {
for _, column := range defaultColumns {
value, width := column.value(token)
tw.Column(value, width)
}
tw.Line()
}
if err := tw.Flush(); err != nil {
return err
}
if len(h.tokens) < h.total {
fmt.Fprintln(out, ansi.Info(fmt.Sprintf("%v/%v listed, use --all flag to show all", len(h.tokens), h.total)))
if len(tokens) < total {
fmt.Fprintln(out, ansi.Info(fmt.Sprintf("%v/%v listed, use --all flag to show all", len(tokens), total)))
}
return nil
}
return nil
}
type helper struct {
tokens []hub.Token
total int
}