Refactor cli (#329)

- move cli files from `cli/drone` to `cli/`
- move cli main to `cmd/cli/main.go` to match agent and server
- use version from `version/version.go` to match agent and server
This commit is contained in:
Anbraten
2021-09-21 16:36:41 +02:00
committed by GitHub
parent ac46ee9345
commit 188b9e6eb5
51 changed files with 69 additions and 124 deletions

15
cli/user/user.go Normal file
View File

@@ -0,0 +1,15 @@
package user
import "github.com/urfave/cli"
// Command exports the user command set.
var Command = cli.Command{
Name: "user",
Usage: "manage users",
Subcommands: []cli.Command{
userListCmd,
userInfoCmd,
userAddCmd,
userRemoveCmd,
},
}

33
cli/user/user_add.go Normal file
View File

@@ -0,0 +1,33 @@
package user
import (
"fmt"
"github.com/urfave/cli"
"github.com/woodpecker-ci/woodpecker/drone-go/drone"
"github.com/woodpecker-ci/woodpecker/cli/internal"
)
var userAddCmd = cli.Command{
Name: "add",
Usage: "adds a user",
ArgsUsage: "<username>",
Action: userAdd,
}
func userAdd(c *cli.Context) error {
login := c.Args().First()
client, err := internal.NewClient(c)
if err != nil {
return err
}
user, err := client.UserPost(&drone.User{Login: login})
if err != nil {
return err
}
fmt.Printf("Successfully added user %s\n", user.Login)
return nil
}

52
cli/user/user_info.go Normal file
View File

@@ -0,0 +1,52 @@
package user
import (
"fmt"
"os"
"text/template"
"github.com/urfave/cli"
"github.com/woodpecker-ci/woodpecker/cli/internal"
)
var userInfoCmd = cli.Command{
Name: "info",
Usage: "show user details",
ArgsUsage: "<username>",
Action: userInfo,
Flags: []cli.Flag{
cli.StringFlag{
Name: "format",
Usage: "format output",
Value: tmplUserInfo,
},
},
}
func userInfo(c *cli.Context) error {
client, err := internal.NewClient(c)
if err != nil {
return err
}
login := c.Args().First()
if len(login) == 0 {
return fmt.Errorf("Missing or invalid user login")
}
user, err := client.User(login)
if err != nil {
return err
}
tmpl, err := template.New("_").Parse(c.String("format") + "\n")
if err != nil {
return err
}
return tmpl.Execute(os.Stdout, user)
}
// template for user information
var tmplUserInfo = `User: {{ .Login }}
Email: {{ .Email }}`

48
cli/user/user_list.go Normal file
View File

@@ -0,0 +1,48 @@
package user
import (
"os"
"text/template"
"github.com/urfave/cli"
"github.com/woodpecker-ci/woodpecker/cli/internal"
)
var userListCmd = cli.Command{
Name: "ls",
Usage: "list all users",
ArgsUsage: " ",
Action: userList,
Flags: []cli.Flag{
cli.StringFlag{
Name: "format",
Usage: "format output",
Value: tmplUserList,
},
},
}
func userList(c *cli.Context) error {
client, err := internal.NewClient(c)
if err != nil {
return err
}
users, err := client.UserList()
if err != nil || len(users) == 0 {
return err
}
tmpl, err := template.New("_").Parse(c.String("format") + "\n")
if err != nil {
return err
}
for _, user := range users {
tmpl.Execute(os.Stdout, user)
}
return nil
}
// template for user list items
var tmplUserList = `{{ .Login }}`

31
cli/user/user_rm.go Normal file
View File

@@ -0,0 +1,31 @@
package user
import (
"fmt"
"github.com/urfave/cli"
"github.com/woodpecker-ci/woodpecker/cli/internal"
)
var userRemoveCmd = cli.Command{
Name: "rm",
Usage: "remove a user",
ArgsUsage: "<username>",
Action: userRemove,
}
func userRemove(c *cli.Context) error {
login := c.Args().First()
client, err := internal.NewClient(c)
if err != nil {
return err
}
if err := client.UserDel(login); err != nil {
return err
}
fmt.Printf("Successfully removed user %s\n", login)
return nil
}