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 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 { func printRepositories(total int) format.PrettyPrinter {
h := values.(*helper) return func(out io.Writer, values interface{}) error {
tw := tabwriter.New(out, " ") 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 { for _, column := range defaultColumns {
value, width := column.value(repository) tw.Column(ansi.Header(column.header), len(column.header))
tw.Column(value, width)
} }
tw.Line() tw.Line()
}
if err := tw.Flush(); err != nil {
return err
}
if len(h.repositories) < h.total { for _, repository := range repositories {
fmt.Fprintln(out, ansi.Info(fmt.Sprintf("%v/%v listed, use --all flag to show all", len(h.repositories), h.total))) for _, column := range defaultColumns {
} value, width := column.value(repository)
return nil tw.Column(value, width)
} }
tw.Line()
}
if err := tw.Flush(); err != nil {
return err
}
type helper struct { if len(repositories) < total {
repositories []hub.Repository fmt.Fprintln(out, ansi.Info(fmt.Sprintf("%v/%v listed, use --all flag to show all", len(repositories), total)))
total int }
return nil
}
} }

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

@ -170,34 +170,36 @@ func runList(streams command.Streams, hubClient *hub.Client, opts listOptions, r
defaultColumns = append(defaultColumns, platformColumn) 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 { func printTags(total int) format.PrettyPrinter {
h := values.(*helper) return func(out io.Writer, values interface{}) error {
tw := tabwriter.New(out, " ") tags := values.([]hub.Tag)
for _, column := range defaultColumns { tw := tabwriter.New(out, " ")
tw.Column(ansi.Header(column.header), len(column.header))
}
tw.Line()
for _, tag := range h.tags {
for _, column := range defaultColumns { for _, column := range defaultColumns {
value, width := column.value(tag) tw.Column(ansi.Header(column.header), len(column.header))
tw.Column(value, width)
} }
tw.Line() tw.Line()
}
if err := tw.Flush(); err != nil {
return err
}
if len(h.tags) < h.total { for _, tag := range tags {
fmt.Fprintln(out, ansi.Info(fmt.Sprintf("%v/%v listed, use --all flag to show all", len(h.tags), h.total))) 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 ( const (
@ -253,8 +255,3 @@ func promptCallToAction(out io.Writer, client accountInfo) error {
_, err = fmt.Fprint(out, ansi.Info(callToAction)) _, err = fmt.Fprint(out, ansi.Info(callToAction))
return err 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 { if err != nil {
return err return err
} }
return opts.Print(streams.Out(), &printHelper{hubClient.AuthConfig.Username, token, opts.quiet}, printCreatedToken) if opts.quiet {
} fmt.Fprintln(streams.Out(), token.Token)
func printCreatedToken(out io.Writer, value interface{}) error {
helper := value.(*printHelper)
if helper.quiet {
fmt.Fprintln(out, helper.token.Token)
return nil 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. When logging in from your Docker CLI client, use this token as a password.
`+ansi.Header("Description:")+` %s `+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. `+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. It will not be stored and cannot be retrieved. Please be sure to save it now.
`), `),
helper.token.Description, helper.Description,
helper.userName, hubClient.AuthConfig.Username,
ansi.Emphasise(helper.token.Token)) ansi.Emphasise(helper.Token))
return nil return nil
} }
type printHelper struct {
userName string
token *hub.Token
quiet bool
} }

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

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