mirror of
https://github.com/harness/drone.git
synced 2025-05-06 14:19:48 +08:00

This change introduces the concept of a principal (abstraction of call identity), and adds a new service account type principal. Also adds support for different tokens (session, PAT, SAT, OAuth2) and adds auth.Session which is being used to capture information about the caller and call method.
78 lines
1.6 KiB
Go
78 lines
1.6 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 users
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"os"
|
|
"text/template"
|
|
"time"
|
|
|
|
"github.com/harness/gitness/cli/util"
|
|
"github.com/harness/gitness/types"
|
|
|
|
"github.com/drone/funcmap"
|
|
"gopkg.in/alecthomas/kingpin.v2"
|
|
)
|
|
|
|
type createCommand struct {
|
|
email string
|
|
admin bool
|
|
tmpl string
|
|
json bool
|
|
}
|
|
|
|
func (c *createCommand) run(*kingpin.ParseContext) error {
|
|
client, err := util.Client()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
in := &types.User{
|
|
Admin: c.admin,
|
|
Email: c.email,
|
|
Password: util.Password(),
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
|
defer cancel()
|
|
user, err := client.UserCreate(ctx, in)
|
|
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)
|
|
}
|
|
|
|
// helper function registers the user create command.
|
|
func registerCreate(app *kingpin.CmdClause) {
|
|
c := new(createCommand)
|
|
|
|
cmd := app.Command("create", "create a user").
|
|
Action(c.run)
|
|
|
|
cmd.Arg("email", "user email").
|
|
Required().
|
|
StringVar(&c.email)
|
|
|
|
cmd.Arg("admin", "user is admin").
|
|
BoolVar(&c.admin)
|
|
|
|
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)
|
|
}
|