drone/cli/user/user.go

64 lines
1.2 KiB
Go

// Copyright 2021 Harness Inc. All rights reserved.
// Use of this source code is governed by the Polyform Free Trial License
// that can be found in the LICENSE.md file for this repository.
package user
import (
"encoding/json"
"os"
"text/template"
"github.com/bradrydzewski/my-app/cli/util"
"github.com/drone/funcmap"
"gopkg.in/alecthomas/kingpin.v2"
)
const userTmpl = `
email: {{ .Email }}
admin: {{ .Admin }}
`
type command struct {
tmpl string
json bool
}
func (c *command) run(*kingpin.ParseContext) error {
client, err := util.Client()
if err != nil {
return err
}
user, err := client.Self()
if err != nil {
return err
}
if c.json {
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
return enc.Encode(user)
}
tmpl, err := template.New("_").Funcs(funcmap.Funcs).Parse(c.tmpl)
if err != nil {
return err
}
return tmpl.Execute(os.Stdout, user)
}
// Register the command.
func Register(app *kingpin.Application) {
c := new(command)
cmd := app.Command("account", "display authenticated user").
Action(c.run)
cmd.Flag("json", "json encode the output").
BoolVar(&c.json)
cmd.Flag("format", "format the output using a Go template").
Default(userTmpl).
Hidden().
StringVar(&c.tmpl)
}