moved cli to drone-cli repository

This commit is contained in:
Brad Rydzewski 2017-05-22 16:14:20 +02:00
parent 3f3abe88f6
commit 552304a130
42 changed files with 0 additions and 2515 deletions

View file

@ -1,20 +0,0 @@
package build
import "github.com/urfave/cli"
// Command exports the build command set.
var Command = cli.Command{
Name: "build",
Usage: "manage builds",
Subcommands: []cli.Command{
buildListCmd,
buildLastCmd,
buildLogsCmd,
buildInfoCmd,
buildStopCmd,
buildStartCmd,
buildApproveCmd,
buildDeclineCmd,
buildQueueCmd,
},
}

View file

@ -1,40 +0,0 @@
package build
import (
"fmt"
"strconv"
"github.com/drone/drone/drone/internal"
"github.com/urfave/cli"
)
var buildApproveCmd = cli.Command{
Name: "approve",
Usage: "approve a build",
Action: buildApprove,
}
func buildApprove(c *cli.Context) (err error) {
repo := c.Args().First()
owner, name, err := internal.ParseRepo(repo)
if err != nil {
return err
}
number, err := strconv.Atoi(c.Args().Get(1))
if err != nil {
return err
}
client, err := internal.NewClient(c)
if err != nil {
return err
}
_, err = client.BuildApprove(owner, name, number)
if err != nil {
return err
}
fmt.Printf("Approving build %s/%s#%d\n", owner, name, number)
return nil
}

View file

@ -1,40 +0,0 @@
package build
import (
"fmt"
"strconv"
"github.com/drone/drone/drone/internal"
"github.com/urfave/cli"
)
var buildDeclineCmd = cli.Command{
Name: "decline",
Usage: "decline a build",
Action: buildDecline,
}
func buildDecline(c *cli.Context) (err error) {
repo := c.Args().First()
owner, name, err := internal.ParseRepo(repo)
if err != nil {
return err
}
number, err := strconv.Atoi(c.Args().Get(1))
if err != nil {
return err
}
client, err := internal.NewClient(c)
if err != nil {
return err
}
_, err = client.BuildDecline(owner, name, number)
if err != nil {
return err
}
fmt.Printf("Declining build %s/%s#%d\n", owner, name, number)
return nil
}

View file

@ -1,74 +0,0 @@
package build
import (
"os"
"strconv"
"text/template"
"github.com/drone/drone/drone/internal"
"github.com/urfave/cli"
)
var buildInfoCmd = cli.Command{
Name: "info",
Usage: "show build details",
Action: buildInfo,
Flags: []cli.Flag{
cli.StringFlag{
Name: "format",
Usage: "format output",
Value: tmplBuildInfo,
},
},
}
func buildInfo(c *cli.Context) error {
repo := c.Args().First()
owner, name, err := internal.ParseRepo(repo)
if err != nil {
return err
}
buildArg := c.Args().Get(1)
client, err := internal.NewClient(c)
if err != nil {
return err
}
var number int
if buildArg == "last" {
// Fetch the build number from the last build
build, err := client.BuildLast(owner, name, "")
if err != nil {
return err
}
number = build.Number
} else {
number, err = strconv.Atoi(buildArg)
if err != nil {
return err
}
}
build, err := client.Build(owner, name, number)
if err != nil {
return err
}
tmpl, err := template.New("_").Parse(c.String("format"))
if err != nil {
return err
}
return tmpl.Execute(os.Stdout, build)
}
// template for build information
var tmplBuildInfo = `Number: {{ .Number }}
Status: {{ .Status }}
Event: {{ .Event }}
Commit: {{ .Commit }}
Branch: {{ .Branch }}
Ref: {{ .Ref }}
Message: {{ .Message }}
Author: {{ .Author }}
`

View file

@ -1,51 +0,0 @@
package build
import (
"os"
"text/template"
"github.com/drone/drone/drone/internal"
"github.com/urfave/cli"
)
var buildLastCmd = cli.Command{
Name: "last",
Usage: "show latest build details",
Action: buildLast,
Flags: []cli.Flag{
cli.StringFlag{
Name: "format",
Usage: "format output",
Value: tmplBuildInfo,
},
cli.StringFlag{
Name: "branch",
Usage: "branch name",
Value: "master",
},
},
}
func buildLast(c *cli.Context) error {
repo := c.Args().First()
owner, name, err := internal.ParseRepo(repo)
if err != nil {
return err
}
client, err := internal.NewClient(c)
if err != nil {
return err
}
build, err := client.BuildLast(owner, name, c.String("branch"))
if err != nil {
return err
}
tmpl, err := template.New("_").Parse(c.String("format"))
if err != nil {
return err
}
return tmpl.Execute(os.Stdout, build)
}

View file

@ -1,97 +0,0 @@
package build
import (
"os"
"text/template"
"github.com/drone/drone/drone/internal"
"github.com/urfave/cli"
)
var buildListCmd = cli.Command{
Name: "list",
Usage: "show build history",
Action: buildList,
Flags: []cli.Flag{
cli.StringFlag{
Name: "format",
Usage: "format output",
Value: tmplBuildList,
},
cli.StringFlag{
Name: "branch",
Usage: "branch filter",
},
cli.StringFlag{
Name: "event",
Usage: "event filter",
},
cli.StringFlag{
Name: "status",
Usage: "status filter",
},
cli.IntFlag{
Name: "limit",
Usage: "limit the list size",
Value: 25,
},
},
}
func buildList(c *cli.Context) error {
repo := c.Args().First()
owner, name, err := internal.ParseRepo(repo)
if err != nil {
return err
}
client, err := internal.NewClient(c)
if err != nil {
return err
}
builds, err := client.BuildList(owner, name)
if err != nil {
return err
}
tmpl, err := template.New("_").Parse(c.String("format") + "\n")
if err != nil {
return err
}
branch := c.String("branch")
event := c.String("event")
status := c.String("status")
limit := c.Int("limit")
var count int
for _, build := range builds {
if count >= limit {
break
}
if branch != "" && build.Branch != branch {
continue
}
if event != "" && build.Event != event {
continue
}
if status != "" && build.Status != status {
continue
}
tmpl.Execute(os.Stdout, build)
count++
}
return nil
}
// template for build list information
var tmplBuildList = "\x1b[33mBuild #{{ .Number }} \x1b[0m" + `
Status: {{ .Status }}
Event: {{ .Event }}
Commit: {{ .Commit }}
Branch: {{ .Branch }}
Ref: {{ .Ref }}
Author: {{ .Author }} {{ if .Email }}<{{.Email}}>{{ end }}
Message: {{ .Message }}
`

View file

@ -1,84 +0,0 @@
package build
import (
"encoding/json"
"fmt"
"strconv"
"github.com/cncd/pipeline/pipeline/rpc"
"github.com/drone/drone/drone/internal"
"github.com/urfave/cli"
)
var buildLogsCmd = cli.Command{
Name: "logs",
Usage: "show build logs",
Action: buildLogsDisabled,
}
func buildLogsDisabled(c *cli.Context) error {
return fmt.Errorf("Command temporarily disabled. See https://github.com/drone/drone/issues/2005")
}
func buildLogs(c *cli.Context) error {
repo := c.Args().First()
owner, name, err := internal.ParseRepo(repo)
if err != nil {
return err
}
client, err := internal.NewClient(c)
if err != nil {
return err
}
buildArg := c.Args().Get(1)
var number int
if buildArg == "" {
// Fetch the build number from the last build
build, err := client.BuildLast(owner, name, "")
if err != nil {
return err
}
number = build.Number
} else {
number, err = strconv.Atoi(buildArg)
if err != nil {
return fmt.Errorf("Error: Invalid number or missing job number. eg 100")
}
}
job, _ := strconv.Atoi(c.Args().Get(2))
if job == 0 {
job = 1
}
r, err := client.BuildLogs(owner, name, number, job)
if err != nil {
return err
}
defer r.Close()
dec := json.NewDecoder(r)
fmt.Printf("Logs for build %s/%s#%d.%d\n", owner, name, number, job)
var line rpc.Line
_, err = dec.Token()
if err != nil {
return err
}
for dec.More() {
if err = dec.Decode(&line); err != nil {
return err
}
fmt.Printf("%s", line.Out)
}
_, err = dec.Token()
if err != nil {
return err
}
return nil
}

View file

@ -1,62 +0,0 @@
package build
import (
"fmt"
"os"
"text/template"
"github.com/drone/drone/drone/internal"
"github.com/urfave/cli"
)
var buildQueueCmd = cli.Command{
Name: "queue",
Usage: "show build queue",
Action: buildQueue,
Flags: []cli.Flag{
cli.StringFlag{
Name: "format",
Usage: "format output",
Value: tmplBuildQueue,
},
},
}
func buildQueue(c *cli.Context) error {
client, err := internal.NewClient(c)
if err != nil {
return err
}
builds, err := client.BuildQueue()
if err != nil {
return err
}
if len(builds) == 0 {
fmt.Println("there are no pending or running builds")
return nil
}
tmpl, err := template.New("_").Parse(c.String("format") + "\n")
if err != nil {
return err
}
for _, build := range builds {
tmpl.Execute(os.Stdout, build)
}
return nil
}
// template for build list information
var tmplBuildQueue = "\x1b[33m{{ .FullName }} #{{ .Number }} \x1b[0m" + `
Status: {{ .Status }}
Event: {{ .Event }}
Commit: {{ .Commit }}
Branch: {{ .Branch }}
Ref: {{ .Ref }}
Author: {{ .Author }} {{ if .Email }}<{{.Email}}>{{ end }}
Message: {{ .Message }}
`

View file

@ -1,70 +0,0 @@
package build
import (
"fmt"
"strconv"
"github.com/drone/drone/drone/internal"
"github.com/drone/drone/model"
"github.com/urfave/cli"
)
var buildStartCmd = cli.Command{
Name: "start",
Usage: "start a build",
Action: buildStart,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "fork",
Usage: "fork the build",
},
cli.StringSliceFlag{
Name: "param, p",
Usage: "custom parameters to be injected into the job environment. Format: KEY=value",
},
},
}
func buildStart(c *cli.Context) (err error) {
repo := c.Args().First()
owner, name, err := internal.ParseRepo(repo)
if err != nil {
return err
}
client, err := internal.NewClient(c)
if err != nil {
return err
}
buildArg := c.Args().Get(1)
var number int
if buildArg == "last" {
// Fetch the build number from the last build
build, err := client.BuildLast(owner, name, "")
if err != nil {
return err
}
number = build.Number
} else {
number, err = strconv.Atoi(buildArg)
if err != nil {
return err
}
}
params := internal.ParseKeyPair(c.StringSlice("param"))
var build *model.Build
if c.Bool("fork") {
build, err = client.BuildFork(owner, name, number, params)
} else {
build, err = client.BuildStart(owner, name, number, params)
}
if err != nil {
return err
}
fmt.Printf("Starting build %s/%s#%d\n", owner, name, build.Number)
return nil
}

View file

@ -1,44 +0,0 @@
package build
import (
"fmt"
"strconv"
"github.com/drone/drone/drone/internal"
"github.com/urfave/cli"
)
var buildStopCmd = cli.Command{
Name: "stop",
Usage: "stop a build",
Action: buildStop,
}
func buildStop(c *cli.Context) (err error) {
repo := c.Args().First()
owner, name, err := internal.ParseRepo(repo)
if err != nil {
return err
}
number, err := strconv.Atoi(c.Args().Get(1))
if err != nil {
return err
}
job, _ := strconv.Atoi(c.Args().Get(2))
if job == 0 {
job = 1
}
client, err := internal.NewClient(c)
if err != nil {
return err
}
err = client.BuildStop(owner, name, number, job)
if err != nil {
return err
}
fmt.Printf("Stopping build %s/%s#%d.%d\n", owner, name, number, job)
return nil
}

View file

@ -1,124 +0,0 @@
package deploy
import (
"fmt"
"html/template"
"os"
"strconv"
"github.com/drone/drone/drone/internal"
"github.com/drone/drone/model"
"github.com/urfave/cli"
)
// Command exports the deploy command.
var Command = cli.Command{
Name: "deploy",
Usage: "deploy code",
Action: deploy,
Flags: []cli.Flag{
cli.StringFlag{
Name: "format",
Usage: "format output",
Value: tmplDeployInfo,
},
cli.StringFlag{
Name: "branch",
Usage: "branch filter",
Value: "master",
},
cli.StringFlag{
Name: "event",
Usage: "event filter",
Value: model.EventPush,
},
cli.StringFlag{
Name: "status",
Usage: "status filter",
Value: model.StatusSuccess,
},
cli.StringSliceFlag{
Name: "param, p",
Usage: "custom parameters to be injected into the job environment. Format: KEY=value",
},
},
}
func deploy(c *cli.Context) error {
repo := c.Args().First()
owner, name, err := internal.ParseRepo(repo)
if err != nil {
return err
}
client, err := internal.NewClient(c)
if err != nil {
return err
}
branch := c.String("branch")
event := c.String("event")
status := c.String("status")
buildArg := c.Args().Get(1)
var number int
if buildArg == "last" {
// Fetch the build number from the last build
builds, berr := client.BuildList(owner, name)
if berr != nil {
return berr
}
for _, build := range builds {
if branch != "" && build.Branch != branch {
continue
}
if event != "" && build.Event != event {
continue
}
if status != "" && build.Status != status {
continue
}
if build.Number > number {
number = build.Number
}
}
if number == 0 {
return fmt.Errorf("Cannot deploy failure build")
}
} else {
number, err = strconv.Atoi(buildArg)
if err != nil {
return err
}
}
env := c.Args().Get(2)
if env == "" {
return fmt.Errorf("Please specify the target environment (ie production)")
}
params := internal.ParseKeyPair(c.StringSlice("param"))
deploy, err := client.Deploy(owner, name, number, env, params)
if err != nil {
return err
}
tmpl, err := template.New("_").Parse(c.String("format"))
if err != nil {
return err
}
return tmpl.Execute(os.Stdout, deploy)
}
// template for deployment information
var tmplDeployInfo = `Number: {{ .Number }}
Status: {{ .Status }}
Commit: {{ .Commit }}
Branch: {{ .Branch }}
Ref: {{ .Ref }}
Message: {{ .Message }}
Author: {{ .Author }}
Target: {{ .Deploy }}
`

View file

@ -1,456 +0,0 @@
package exec
import (
"context"
"io"
"log"
"os"
"path"
"path/filepath"
"strings"
"time"
"github.com/cncd/pipeline/pipeline"
"github.com/cncd/pipeline/pipeline/backend"
"github.com/cncd/pipeline/pipeline/backend/docker"
"github.com/cncd/pipeline/pipeline/frontend"
"github.com/cncd/pipeline/pipeline/frontend/yaml"
"github.com/cncd/pipeline/pipeline/frontend/yaml/compiler"
"github.com/cncd/pipeline/pipeline/frontend/yaml/linter"
"github.com/cncd/pipeline/pipeline/interrupt"
"github.com/cncd/pipeline/pipeline/multipart"
"github.com/drone/envsubst"
"github.com/urfave/cli"
)
// Command exports the exec command.
var Command = cli.Command{
Name: "exec",
Usage: "execute a local build",
Action: func(c *cli.Context) {
if err := exec(c); err != nil {
log.Fatalln(err)
}
},
Flags: []cli.Flag{
cli.BoolTFlag{
Name: "local",
Usage: "build from local directory",
EnvVar: "DRONE_LOCAL",
},
cli.DurationFlag{
Name: "timeout",
Usage: "build timeout",
Value: time.Hour,
EnvVar: "DRONE_TIMEOUT",
},
cli.StringSliceFlag{
Name: "volumes",
Usage: "build volumes",
EnvVar: "DRONE_VOLUMES",
},
cli.StringSliceFlag{
Name: "network",
Usage: "external networks",
EnvVar: "DRONE_NETWORKS",
},
cli.StringFlag{
Name: "prefix",
Value: "drone",
Usage: "prefix containers created by drone",
EnvVar: "DRONE_DOCKER_PREFIX",
Hidden: true,
},
cli.StringSliceFlag{
Name: "privileged",
Usage: "privileged plugins",
Value: &cli.StringSlice{
"plugins/docker",
"plugins/gcr",
"plugins/ecr",
},
},
//
// Please note the below flags are mirrored in the pipec and
// should be kept synchronized. Do not edit directly
// https://github.com/cncd/pipeline/pipec
//
//
// workspace default
//
cli.StringFlag{
Name: "workspace-base",
Value: "/pipeline",
EnvVar: "DRONE_WORKSPACE_BASE",
},
cli.StringFlag{
Name: "workspace-path",
Value: "src",
EnvVar: "DRONE_WORKSPACE_PATH",
},
//
// netrc parameters
//
cli.StringFlag{
Name: "netrc-username",
EnvVar: "DRONE_NETRC_USERNAME",
},
cli.StringFlag{
Name: "netrc-password",
EnvVar: "DRONE_NETRC_PASSWORD",
},
cli.StringFlag{
Name: "netrc-machine",
EnvVar: "DRONE_NETRC_MACHINE",
},
//
// metadata parameters
//
cli.StringFlag{
Name: "system-arch",
Value: "linux/amd64",
EnvVar: "DRONE_SYSTEM_ARCH",
},
cli.StringFlag{
Name: "system-name",
Value: "pipec",
EnvVar: "DRONE_SYSTEM_NAME",
},
cli.StringFlag{
Name: "system-link",
Value: "https://github.com/cncd/pipec",
EnvVar: "DRONE_SYSTEM_LINK",
},
cli.StringFlag{
Name: "repo-name",
EnvVar: "DRONE_REPO_NAME",
},
cli.StringFlag{
Name: "repo-link",
EnvVar: "DRONE_REPO_LINK",
},
cli.StringFlag{
Name: "repo-remote-url",
EnvVar: "DRONE_REPO_REMOTE",
},
cli.StringFlag{
Name: "repo-private",
EnvVar: "DRONE_REPO_PRIVATE",
},
cli.IntFlag{
Name: "build-number",
EnvVar: "DRONE_BUILD_NUMBER",
},
cli.IntFlag{
Name: "parent-build-number",
EnvVar: "DRONE_PARENT_BUILD_NUMBER",
},
cli.Int64Flag{
Name: "build-created",
EnvVar: "DRONE_BUILD_CREATED",
},
cli.Int64Flag{
Name: "build-started",
EnvVar: "DRONE_BUILD_STARTED",
},
cli.Int64Flag{
Name: "build-finished",
EnvVar: "DRONE_BUILD_FINISHED",
},
cli.StringFlag{
Name: "build-status",
EnvVar: "DRONE_BUILD_STATUS",
},
cli.StringFlag{
Name: "build-event",
EnvVar: "DRONE_BUILD_EVENT",
},
cli.StringFlag{
Name: "build-link",
EnvVar: "DRONE_BUILD_LINK",
},
cli.StringFlag{
Name: "build-target",
EnvVar: "DRONE_BUILD_TARGET",
},
cli.StringFlag{
Name: "commit-sha",
EnvVar: "DRONE_COMMIT_SHA",
},
cli.StringFlag{
Name: "commit-ref",
EnvVar: "DRONE_COMMIT_REF",
},
cli.StringFlag{
Name: "commit-refspec",
EnvVar: "DRONE_COMMIT_REFSPEC",
},
cli.StringFlag{
Name: "commit-branch",
EnvVar: "DRONE_COMMIT_BRANCH",
},
cli.StringFlag{
Name: "commit-message",
EnvVar: "DRONE_COMMIT_MESSAGE",
},
cli.StringFlag{
Name: "commit-author-name",
EnvVar: "DRONE_COMMIT_AUTHOR_NAME",
},
cli.StringFlag{
Name: "commit-author-avatar",
EnvVar: "DRONE_COMMIT_AUTHOR_AVATAR",
},
cli.StringFlag{
Name: "commit-author-email",
EnvVar: "DRONE_COMMIT_AUTHOR_EMAIL",
},
cli.IntFlag{
Name: "prev-build-number",
EnvVar: "DRONE_PREV_BUILD_NUMBER",
},
cli.Int64Flag{
Name: "prev-build-created",
EnvVar: "DRONE_PREV_BUILD_CREATED",
},
cli.Int64Flag{
Name: "prev-build-started",
EnvVar: "DRONE_PREV_BUILD_STARTED",
},
cli.Int64Flag{
Name: "prev-build-finished",
EnvVar: "DRONE_PREV_BUILD_FINISHED",
},
cli.StringFlag{
Name: "prev-build-status",
EnvVar: "DRONE_PREV_BUILD_STATUS",
},
cli.StringFlag{
Name: "prev-build-event",
EnvVar: "DRONE_PREV_BUILD_EVENT",
},
cli.StringFlag{
Name: "prev-build-link",
EnvVar: "DRONE_PREV_BUILD_LINK",
},
cli.StringFlag{
Name: "prev-commit-sha",
EnvVar: "DRONE_PREV_COMMIT_SHA",
},
cli.StringFlag{
Name: "prev-commit-ref",
EnvVar: "DRONE_PREV_COMMIT_REF",
},
cli.StringFlag{
Name: "prev-commit-refspec",
EnvVar: "DRONE_PREV_COMMIT_REFSPEC",
},
cli.StringFlag{
Name: "prev-commit-branch",
EnvVar: "DRONE_PREV_COMMIT_BRANCH",
},
cli.StringFlag{
Name: "prev-commit-message",
EnvVar: "DRONE_PREV_COMMIT_MESSAGE",
},
cli.StringFlag{
Name: "prev-commit-author-name",
EnvVar: "DRONE_PREV_COMMIT_AUTHOR_NAME",
},
cli.StringFlag{
Name: "prev-commit-author-avatar",
EnvVar: "DRONE_PREV_COMMIT_AUTHOR_AVATAR",
},
cli.StringFlag{
Name: "prev-commit-author-email",
EnvVar: "DRONE_PREV_COMMIT_AUTHOR_EMAIL",
},
cli.IntFlag{
Name: "job-number",
EnvVar: "DRONE_JOB_NUMBER",
},
},
}
func exec(c *cli.Context) error {
file := c.Args().First()
if file == "" {
file = ".drone.yml"
}
metadata := metadataFromContext(c)
environ := metadata.Environ()
secrets := []compiler.Secret{}
for k, v := range metadata.EnvironDrone() {
environ[k] = v
}
for _, env := range os.Environ() {
k := strings.Split(env, "=")[0]
v := strings.Split(env, "=")[1]
environ[k] = v
secrets = append(secrets, compiler.Secret{
Name: k,
Value: v,
})
}
tmpl, err := envsubst.ParseFile(file)
if err != nil {
return err
}
confstr, err := tmpl.Execute(func(name string) string {
return environ[name]
})
if err != nil {
return err
}
conf, err := yaml.ParseString(confstr)
if err != nil {
return err
}
// configure volumes for local execution
volumes := c.StringSlice("volumes")
if c.Bool("local") {
var (
workspaceBase = conf.Workspace.Base
workspacePath = conf.Workspace.Path
)
if workspaceBase == "" {
workspaceBase = c.String("workspace-base")
}
if workspacePath == "" {
workspacePath = c.String("workspace-path")
}
dir, _ := filepath.Abs(filepath.Dir(file))
volumes = append(volumes, dir+":"+path.Join(workspaceBase, workspacePath))
}
// lint the yaml file
if lerr := linter.New(linter.WithTrusted(true)).Lint(conf); lerr != nil {
return lerr
}
// compiles the yaml file
compiled := compiler.New(
compiler.WithEscalated(
c.StringSlice("privileged")...,
),
compiler.WithVolumes(volumes...),
compiler.WithWorkspace(
c.String("workspace-base"),
c.String("workspace-path"),
),
compiler.WithNetworks(
c.StringSlice("network")...,
),
compiler.WithPrefix(
c.String("prefix"),
),
compiler.WithProxy(),
compiler.WithLocal(
c.Bool("local"),
),
compiler.WithNetrc(
c.String("netrc-username"),
c.String("netrc-password"),
c.String("netrc-machine"),
),
compiler.WithMetadata(metadata),
compiler.WithSecret(secrets...),
).Compile(conf)
engine, err := docker.NewEnv()
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), c.Duration("timeout"))
defer cancel()
ctx = interrupt.WithContext(ctx)
return pipeline.New(compiled,
pipeline.WithContext(ctx),
pipeline.WithLogger(defaultLogger),
pipeline.WithTracer(pipeline.DefaultTracer),
pipeline.WithLogger(defaultLogger),
pipeline.WithEngine(engine),
).Run()
}
// return the metadata from the cli context.
func metadataFromContext(c *cli.Context) frontend.Metadata {
return frontend.Metadata{
Repo: frontend.Repo{
Name: c.String("repo-name"),
Link: c.String("repo-link"),
Remote: c.String("repo-remote-url"),
Private: c.Bool("repo-private"),
},
Curr: frontend.Build{
Number: c.Int("build-number"),
Parent: c.Int("parent-build-number"),
Created: c.Int64("build-created"),
Started: c.Int64("build-started"),
Finished: c.Int64("build-finished"),
Status: c.String("build-status"),
Event: c.String("build-event"),
Link: c.String("build-link"),
Target: c.String("build-target"),
Commit: frontend.Commit{
Sha: c.String("commit-sha"),
Ref: c.String("commit-ref"),
Refspec: c.String("commit-refspec"),
Branch: c.String("commit-branch"),
Message: c.String("commit-message"),
Author: frontend.Author{
Name: c.String("commit-author-name"),
Email: c.String("commit-author-email"),
Avatar: c.String("commit-author-avatar"),
},
},
},
Prev: frontend.Build{
Number: c.Int("prev-build-number"),
Created: c.Int64("prev-build-created"),
Started: c.Int64("prev-build-started"),
Finished: c.Int64("prev-build-finished"),
Status: c.String("prev-build-status"),
Event: c.String("prev-build-event"),
Link: c.String("prev-build-link"),
Commit: frontend.Commit{
Sha: c.String("prev-commit-sha"),
Ref: c.String("prev-commit-ref"),
Refspec: c.String("prev-commit-refspec"),
Branch: c.String("prev-commit-branch"),
Message: c.String("prev-commit-message"),
Author: frontend.Author{
Name: c.String("prev-commit-author-name"),
Email: c.String("prev-commit-author-email"),
Avatar: c.String("prev-commit-author-avatar"),
},
},
},
Job: frontend.Job{
Number: c.Int("job-number"),
},
Sys: frontend.System{
Name: c.String("system-name"),
Link: c.String("system-link"),
Arch: c.String("system-arch"),
},
}
}
var defaultLogger = pipeline.LogFunc(func(proc *backend.Step, rc multipart.Reader) error {
part, err := rc.NextPart()
if err != nil {
return err
}
io.Copy(os.Stderr, part)
return nil
})

View file

@ -1,48 +0,0 @@
package info
import (
"os"
"text/template"
"github.com/urfave/cli"
"github.com/drone/drone/drone/internal"
)
// Command exports the info command.
var Command = cli.Command{
Name: "info",
Usage: "show information about the current user",
Action: info,
Flags: []cli.Flag{
cli.StringFlag{
Name: "format",
Usage: "format output",
Value: tmplInfo,
Hidden: true,
},
},
}
func info(c *cli.Context) error {
client, err := internal.NewClient(c)
if err != nil {
return err
}
user, err := client.Self()
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 tmplInfo = `User: {{ .Login }}
Email: {{ .Email }}`

View file

@ -1,78 +0,0 @@
package internal
import (
"crypto/tls"
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/drone/drone/client"
"github.com/jackspirou/syscerts"
"github.com/urfave/cli"
)
// NewClient returns a new client from the CLI context.
func NewClient(c *cli.Context) (client.Client, error) {
var token = c.GlobalString("token")
var server = strings.TrimRight(c.GlobalString("server"), "/")
// if no server url is provided we can default
// to the hosted Drone service.
if len(server) == 0 {
return nil, fmt.Errorf("Error: you must provide the Drone server address.")
}
if len(token) == 0 {
return nil, fmt.Errorf("Error: you must provide your Drone access token.")
}
// attempt to find system CA certs
certs := syscerts.SystemRootsPool()
tlsConfig := &tls.Config{RootCAs: certs}
// create the drone client with TLS options
return client.NewClientTokenTLS(server, token, tlsConfig)
}
// ParseRepo parses the repository owner and name from a string.
func ParseRepo(str string) (user, repo string, err error) {
var parts = strings.Split(str, "/")
if len(parts) != 2 {
err = fmt.Errorf("Error: Invalid or missing repository. eg octocat/hello-world.")
return
}
user = parts[0]
repo = parts[1]
return
}
func readInput(in string) ([]byte, error) {
if in == "-" {
return ioutil.ReadAll(os.Stdin)
}
return ioutil.ReadFile(in)
}
func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
// ParseKeyPair parses a key=value pair.
func ParseKeyPair(p []string) map[string]string {
params := map[string]string{}
for _, i := range p {
parts := strings.Split(i, "=")
if len(parts) != 2 {
continue
}
params[parts[0]] = parts[1]
}
return params
}

View file

@ -1,17 +0,0 @@
package internal
import "testing"
func TestParseKeyPair(t *testing.T) {
s := []string{"FOO=bar", "BAR=", "INVALID"}
p := ParseKeyPair(s)
if p["FOO"] != "bar" {
t.Errorf("Wanted %q, got %q.", "bar", p["FOO"])
}
if _, exists := p["BAR"]; !exists {
t.Error("Missing a key with no value. Keys with empty values are also valid.")
}
if _, exists := p["INVALID"]; exists {
t.Error("Keys without an equal sign suffix are invalid.")
}
}

View file

@ -7,15 +7,7 @@ import (
"os"
"github.com/drone/drone/drone/agent"
"github.com/drone/drone/drone/build"
"github.com/drone/drone/drone/deploy"
"github.com/drone/drone/drone/exec"
"github.com/drone/drone/drone/info"
"github.com/drone/drone/drone/registry"
"github.com/drone/drone/drone/repo"
"github.com/drone/drone/drone/secret"
"github.com/drone/drone/drone/server"
"github.com/drone/drone/drone/user"
"github.com/drone/drone/version"
"github.com/ianschenck/envflag"
@ -44,15 +36,7 @@ func main() {
}
app.Commands = []cli.Command{
agent.Command,
build.Command,
deploy.Command,
exec.Command,
info.Command,
registry.Command,
secret.Command,
server.Command,
repo.Command,
user.Command,
}
if err := app.Run(os.Args); err != nil {

View file

@ -7,14 +7,6 @@ import (
"os"
"github.com/drone/drone/drone/agent"
"github.com/drone/drone/drone/build"
"github.com/drone/drone/drone/deploy"
"github.com/drone/drone/drone/exec"
"github.com/drone/drone/drone/info"
"github.com/drone/drone/drone/registry"
"github.com/drone/drone/drone/repo"
"github.com/drone/drone/drone/secret"
"github.com/drone/drone/drone/user"
"github.com/drone/drone/version"
"github.com/drone/drone/extras/cmd/drone/server"
@ -45,15 +37,7 @@ func main() {
}
app.Commands = []cli.Command{
agent.Command,
build.Command,
deploy.Command,
exec.Command,
info.Command,
registry.Command,
secret.Command,
server.Command,
repo.Command,
user.Command,
}
if err := app.Run(os.Args); err != nil {

View file

@ -1,16 +0,0 @@
package registry
import "github.com/urfave/cli"
// Command exports the registry command set.
var Command = cli.Command{
Name: "registry",
Usage: "manage registries",
Subcommands: []cli.Command{
registryCreateCmd,
registryDeleteCmd,
registryUpdateCmd,
registryInfoCmd,
registryListCmd,
},
}

View file

@ -1,74 +0,0 @@
package registry
import (
"io/ioutil"
"strings"
"github.com/drone/drone/drone/internal"
"github.com/drone/drone/model"
"github.com/urfave/cli"
)
var registryCreateCmd = cli.Command{
Name: "add",
Usage: "adds a registry",
Action: registryCreate,
Flags: []cli.Flag{
cli.StringFlag{
Name: "repository",
Usage: "repository name (e.g. octocat/hello-world)",
},
cli.StringFlag{
Name: "hostname",
Usage: "registry hostname",
Value: "docker.io",
},
cli.StringFlag{
Name: "username",
Usage: "registry username",
},
cli.StringFlag{
Name: "password",
Usage: "registry password",
},
},
}
func registryCreate(c *cli.Context) error {
var (
hostname = c.String("hostname")
username = c.String("username")
password = c.String("password")
reponame = c.String("repository")
)
if reponame == "" {
reponame = c.Args().First()
}
owner, name, err := internal.ParseRepo(reponame)
if err != nil {
return err
}
client, err := internal.NewClient(c)
if err != nil {
return err
}
registry := &model.Registry{
Address: hostname,
Username: username,
Password: password,
}
if strings.HasPrefix(registry.Password, "@") {
path := strings.TrimPrefix(registry.Password, "@")
out, ferr := ioutil.ReadFile(path)
if ferr != nil {
return ferr
}
registry.Password = string(out)
}
_, err = client.RegistryCreate(owner, name, registry)
if err != nil {
return err
}
return nil
}

View file

@ -1,61 +0,0 @@
package registry
import (
"html/template"
"os"
"github.com/drone/drone/drone/internal"
"github.com/urfave/cli"
)
var registryInfoCmd = cli.Command{
Name: "info",
Usage: "display registry info",
Action: registryInfo,
Flags: []cli.Flag{
cli.StringFlag{
Name: "repository",
Usage: "repository name (e.g. octocat/hello-world)",
},
cli.StringFlag{
Name: "hostname",
Usage: "registry hostname",
Value: "docker.io",
},
cli.StringFlag{
Name: "format",
Usage: "format output",
Value: tmplRegistryList,
Hidden: true,
},
},
}
func registryInfo(c *cli.Context) error {
var (
hostname = c.String("hostname")
reponame = c.String("repository")
format = c.String("format") + "\n"
)
if reponame == "" {
reponame = c.Args().First()
}
owner, name, err := internal.ParseRepo(reponame)
if err != nil {
return err
}
client, err := internal.NewClient(c)
if err != nil {
return err
}
registry, err := client.Registry(owner, name, hostname)
if err != nil {
return err
}
tmpl, err := template.New("_").Parse(format)
if err != nil {
return err
}
return tmpl.Execute(os.Stdout, registry)
}

View file

@ -1,64 +0,0 @@
package registry
import (
"html/template"
"os"
"github.com/urfave/cli"
"github.com/drone/drone/drone/internal"
)
var registryListCmd = cli.Command{
Name: "ls",
Usage: "list regitries",
Action: registryList,
Flags: []cli.Flag{
cli.StringFlag{
Name: "repository",
Usage: "repository name (e.g. octocat/hello-world)",
},
cli.StringFlag{
Name: "format",
Usage: "format output",
Value: tmplRegistryList,
Hidden: true,
},
},
}
func registryList(c *cli.Context) error {
var (
format = c.String("format") + "\n"
reponame = c.String("repository")
)
if reponame == "" {
reponame = c.Args().First()
}
owner, name, err := internal.ParseRepo(reponame)
if err != nil {
return err
}
client, err := internal.NewClient(c)
if err != nil {
return err
}
list, err := client.RegistryList(owner, name)
if err != nil {
return err
}
tmpl, err := template.New("_").Parse(format)
if err != nil {
return err
}
for _, registry := range list {
tmpl.Execute(os.Stdout, registry)
}
return nil
}
// template for build list information
var tmplRegistryList = "\x1b[33m{{ .Address }} \x1b[0m" + `
Username: {{ .Username }}
Email: {{ .Email }}
`

View file

@ -1,43 +0,0 @@
package registry
import (
"github.com/drone/drone/drone/internal"
"github.com/urfave/cli"
)
var registryDeleteCmd = cli.Command{
Name: "rm",
Usage: "remove a registry",
Action: registryDelete,
Flags: []cli.Flag{
cli.StringFlag{
Name: "repository",
Usage: "repository name (e.g. octocat/hello-world)",
},
cli.StringFlag{
Name: "hostname",
Usage: "registry hostname",
Value: "docker.io",
},
},
}
func registryDelete(c *cli.Context) error {
var (
hostname = c.String("hostname")
reponame = c.String("repository")
)
if reponame == "" {
reponame = c.Args().First()
}
owner, name, err := internal.ParseRepo(reponame)
if err != nil {
return err
}
client, err := internal.NewClient(c)
if err != nil {
return err
}
return client.RegistryDelete(owner, name, hostname)
}

View file

@ -1,74 +0,0 @@
package registry
import (
"io/ioutil"
"strings"
"github.com/drone/drone/drone/internal"
"github.com/drone/drone/model"
"github.com/urfave/cli"
)
var registryUpdateCmd = cli.Command{
Name: "update",
Usage: "update a registry",
Action: registryUpdate,
Flags: []cli.Flag{
cli.StringFlag{
Name: "repository",
Usage: "repository name (e.g. octocat/hello-world)",
},
cli.StringFlag{
Name: "hostname",
Usage: "registry hostname",
Value: "docker.io",
},
cli.StringFlag{
Name: "username",
Usage: "registry username",
},
cli.StringFlag{
Name: "password",
Usage: "registry password",
},
},
}
func registryUpdate(c *cli.Context) error {
var (
hostname = c.String("hostname")
username = c.String("username")
password = c.String("password")
reponame = c.String("repository")
)
if reponame == "" {
reponame = c.Args().First()
}
owner, name, err := internal.ParseRepo(reponame)
if err != nil {
return err
}
client, err := internal.NewClient(c)
if err != nil {
return err
}
registry := &model.Registry{
Address: hostname,
Username: username,
Password: password,
}
if strings.HasPrefix(registry.Password, "@") {
path := strings.TrimPrefix(registry.Password, "@")
out, ferr := ioutil.ReadFile(path)
if ferr != nil {
return ferr
}
registry.Password = string(out)
}
_, err = client.RegistryUpdate(owner, name, registry)
if err != nil {
return err
}
return nil
}

View file

@ -1,18 +0,0 @@
package repo
import "github.com/urfave/cli"
// Command exports the repository command.
var Command = cli.Command{
Name: "repo",
Usage: "manage repositories",
Subcommands: []cli.Command{
repoListCmd,
repoInfoCmd,
repoAddCmd,
repoUpdateCmd,
repoRemoveCmd,
repoRepairCmd,
repoChownCmd,
},
}

View file

@ -1,33 +0,0 @@
package repo
import (
"fmt"
"github.com/drone/drone/drone/internal"
"github.com/urfave/cli"
)
var repoAddCmd = cli.Command{
Name: "add",
Usage: "add a repository",
Action: repoAdd,
}
func repoAdd(c *cli.Context) error {
repo := c.Args().First()
owner, name, err := internal.ParseRepo(repo)
if err != nil {
return err
}
client, err := internal.NewClient(c)
if err != nil {
return err
}
if _, err := client.RepoPost(owner, name); err != nil {
return err
}
fmt.Printf("Successfully activated repository %s/%s\n", owner, name)
return nil
}

View file

@ -1,33 +0,0 @@
package repo
import (
"fmt"
"github.com/drone/drone/drone/internal"
"github.com/urfave/cli"
)
var repoChownCmd = cli.Command{
Name: "chown",
Usage: "assume ownership of a repository",
Action: repoChown,
}
func repoChown(c *cli.Context) error {
repo := c.Args().First()
owner, name, err := internal.ParseRepo(repo)
if err != nil {
return err
}
client, err := internal.NewClient(c)
if err != nil {
return err
}
if _, err := client.RepoChown(owner, name); err != nil {
return err
}
fmt.Printf("Successfully assumed ownership of repository %s/%s\n", owner, name)
return nil
}

View file

@ -1,57 +0,0 @@
package repo
import (
"os"
"text/template"
"github.com/drone/drone/drone/internal"
"github.com/urfave/cli"
)
var repoInfoCmd = cli.Command{
Name: "info",
Usage: "show repository details",
Action: repoInfo,
Flags: []cli.Flag{
cli.StringFlag{
Name: "format",
Usage: "format output",
Value: tmplRepoInfo,
},
},
}
func repoInfo(c *cli.Context) error {
arg := c.Args().First()
owner, name, err := internal.ParseRepo(arg)
if err != nil {
return err
}
client, err := internal.NewClient(c)
if err != nil {
return err
}
repo, err := client.Repo(owner, name)
if err != nil {
return err
}
tmpl, err := template.New("_").Parse(c.String("format"))
if err != nil {
return err
}
return tmpl.Execute(os.Stdout, repo)
}
// template for repo information
var tmplRepoInfo = `Owner: {{ .Owner }}
Repo: {{ .Name }}
Type: {{ .Kind }}
Config: {{ .Config }}
Private: {{ .IsPrivate }}
Trusted: {{ .IsTrusted }}
Gated: {{ .IsGated }}
Remote: {{ .Clone }}
`

View file

@ -1,55 +0,0 @@
package repo
import (
"os"
"text/template"
"github.com/drone/drone/drone/internal"
"github.com/urfave/cli"
)
var repoListCmd = cli.Command{
Name: "ls",
Usage: "list all repos",
Action: repoList,
Flags: []cli.Flag{
cli.StringFlag{
Name: "format",
Usage: "format output",
Value: tmplRepoList,
},
cli.StringFlag{
Name: "org",
Usage: "filter by organization",
},
},
}
func repoList(c *cli.Context) error {
client, err := internal.NewClient(c)
if err != nil {
return err
}
repos, err := client.RepoList()
if err != nil || len(repos) == 0 {
return err
}
tmpl, err := template.New("_").Parse(c.String("format") + "\n")
if err != nil {
return err
}
org := c.String("org")
for _, repo := range repos {
if org != "" && org != repo.Owner {
continue
}
tmpl.Execute(os.Stdout, repo)
}
return nil
}
// template for repository list items
var tmplRepoList = `{{ .FullName }}`

View file

@ -1,25 +0,0 @@
package repo
import (
"github.com/drone/drone/drone/internal"
"github.com/urfave/cli"
)
var repoRepairCmd = cli.Command{
Name: "repair",
Usage: "repair repository webhooks",
Action: repoRepair,
}
func repoRepair(c *cli.Context) error {
repo := c.Args().First()
owner, name, err := internal.ParseRepo(repo)
if err != nil {
return err
}
client, err := internal.NewClient(c)
if err != nil {
return err
}
return client.RepoRepair(owner, name)
}

View file

@ -1,33 +0,0 @@
package repo
import (
"fmt"
"github.com/drone/drone/drone/internal"
"github.com/urfave/cli"
)
var repoRemoveCmd = cli.Command{
Name: "rm",
Usage: "remove a repository",
Action: repoRemove,
}
func repoRemove(c *cli.Context) error {
repo := c.Args().First()
owner, name, err := internal.ParseRepo(repo)
if err != nil {
return err
}
client, err := internal.NewClient(c)
if err != nil {
return err
}
if err := client.RepoDel(owner, name); err != nil {
return err
}
fmt.Printf("Successfully removed repository %s/%s\n", owner, name)
return nil
}

View file

@ -1,75 +0,0 @@
package repo
import (
"fmt"
"time"
"github.com/drone/drone/drone/internal"
"github.com/drone/drone/model"
"github.com/urfave/cli"
)
var repoUpdateCmd = cli.Command{
Name: "update",
Usage: "update a repository",
Action: repoUpdate,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "trusted",
Usage: "repository is trusted",
},
cli.BoolFlag{
Name: "gated",
Usage: "repository is gated",
},
cli.DurationFlag{
Name: "timeout",
Usage: "repository timeout",
},
cli.StringFlag{
Name: "config",
Usage: "repository configuration path (e.g. .drone.yml)",
},
},
}
func repoUpdate(c *cli.Context) error {
repo := c.Args().First()
owner, name, err := internal.ParseRepo(repo)
if err != nil {
return err
}
client, err := internal.NewClient(c)
if err != nil {
return err
}
var (
config = c.String("config")
timeout = c.Duration("timeout")
trusted = c.Bool("trusted")
gated = c.Bool("gated")
)
patch := new(model.RepoPatch)
if c.IsSet("trusted") {
patch.IsTrusted = &trusted
}
if c.IsSet("gated") {
patch.IsGated = &gated
}
if c.IsSet("timeout") {
v := int64(timeout / time.Minute)
patch.Timeout = &v
}
if c.IsSet("config") {
patch.Config = &config
}
if _, err := client.RepoPatch(owner, name, patch); err != nil {
return err
}
fmt.Printf("Successfully updated repository %s/%s\n", owner, name)
return nil
}

View file

@ -1,16 +0,0 @@
package secret
import "github.com/urfave/cli"
// Command exports the secret command.
var Command = cli.Command{
Name: "secret",
Usage: "manage secrets",
Subcommands: []cli.Command{
secretCreateCmd,
secretDeleteCmd,
secretUpdateCmd,
secretInfoCmd,
secretListCmd,
},
}

View file

@ -1,79 +0,0 @@
package secret
import (
"io/ioutil"
"strings"
"github.com/drone/drone/drone/internal"
"github.com/drone/drone/model"
"github.com/urfave/cli"
)
var secretCreateCmd = cli.Command{
Name: "add",
Usage: "adds a secret",
Action: secretCreate,
Flags: []cli.Flag{
cli.StringFlag{
Name: "repository",
Usage: "repository name (e.g. octocat/hello-world)",
},
cli.StringFlag{
Name: "name",
Usage: "secret name",
},
cli.StringFlag{
Name: "value",
Usage: "secret value",
},
cli.StringSliceFlag{
Name: "event",
Usage: "secret limited to these events",
},
cli.StringSliceFlag{
Name: "image",
Usage: "secret limited to these images",
},
},
}
func secretCreate(c *cli.Context) error {
reponame := c.String("repository")
if reponame == "" {
reponame = c.Args().First()
}
owner, name, err := internal.ParseRepo(reponame)
if err != nil {
return err
}
client, err := internal.NewClient(c)
if err != nil {
return err
}
secret := &model.Secret{
Name: c.String("name"),
Value: c.String("value"),
Images: c.StringSlice("image"),
Events: c.StringSlice("event"),
}
if len(secret.Events) == 0 {
secret.Events = defaultSecretEvents
}
if strings.HasPrefix(secret.Value, "@") {
path := strings.TrimPrefix(secret.Value, "@")
out, ferr := ioutil.ReadFile(path)
if ferr != nil {
return ferr
}
secret.Value = string(out)
}
_, err = client.SecretCreate(owner, name, secret)
return err
}
var defaultSecretEvents = []string{
model.EventPush,
model.EventTag,
model.EventDeploy,
}

View file

@ -1,60 +0,0 @@
package secret
import (
"html/template"
"os"
"github.com/urfave/cli"
"github.com/drone/drone/drone/internal"
)
var secretInfoCmd = cli.Command{
Name: "info",
Usage: "display secret info",
Action: secretInfo,
Flags: []cli.Flag{
cli.StringFlag{
Name: "repository",
Usage: "repository name (e.g. octocat/hello-world)",
},
cli.StringFlag{
Name: "name",
Usage: "secret name",
},
cli.StringFlag{
Name: "format",
Usage: "format output",
Value: tmplSecretList,
Hidden: true,
},
},
}
func secretInfo(c *cli.Context) error {
var (
secretName = c.String("name")
repoName = c.String("repository")
format = c.String("format") + "\n"
)
if repoName == "" {
repoName = c.Args().First()
}
owner, name, err := internal.ParseRepo(repoName)
if err != nil {
return err
}
client, err := internal.NewClient(c)
if err != nil {
return err
}
secret, err := client.Secret(owner, name, secretName)
if err != nil {
return err
}
tmpl, err := template.New("_").Funcs(secretFuncMap).Parse(format)
if err != nil {
return err
}
return tmpl.Execute(os.Stdout, secret)
}

View file

@ -1,75 +0,0 @@
package secret
import (
"html/template"
"os"
"strings"
"github.com/urfave/cli"
"github.com/drone/drone/drone/internal"
)
var secretListCmd = cli.Command{
Name: "ls",
Usage: "list secrets",
Action: secretList,
Flags: []cli.Flag{
cli.StringFlag{
Name: "repository",
Usage: "repository name (e.g. octocat/hello-world)",
},
cli.StringFlag{
Name: "format",
Usage: "format output",
Value: tmplSecretList,
Hidden: true,
},
},
}
func secretList(c *cli.Context) error {
var (
format = c.String("format") + "\n"
reponame = c.String("repository")
)
if reponame == "" {
reponame = c.Args().First()
}
owner, name, err := internal.ParseRepo(reponame)
if err != nil {
return err
}
client, err := internal.NewClient(c)
if err != nil {
return err
}
list, err := client.SecretList(owner, name)
if err != nil {
return err
}
tmpl, err := template.New("_").Funcs(secretFuncMap).Parse(format)
if err != nil {
return err
}
for _, registry := range list {
tmpl.Execute(os.Stdout, registry)
}
return nil
}
// template for secret list items
var tmplSecretList = "\x1b[33m{{ .Name }} \x1b[0m" + `
Events: {{ list .Events }}
{{- if .Images }}
Images: {{ list .Images }}
{{- else }}
Images: <any>
{{- end }}
`
var secretFuncMap = template.FuncMap{
"list": func(s []string) string {
return strings.Join(s, ", ")
},
}

View file

@ -1,42 +0,0 @@
package secret
import (
"github.com/urfave/cli"
"github.com/drone/drone/drone/internal"
)
var secretDeleteCmd = cli.Command{
Name: "rm",
Usage: "remove a secret",
Action: secretDelete,
Flags: []cli.Flag{
cli.StringFlag{
Name: "repository",
Usage: "repository name (e.g. octocat/hello-world)",
},
cli.StringFlag{
Name: "name",
Usage: "secret name",
},
},
}
func secretDelete(c *cli.Context) error {
var (
secret = c.String("name")
reponame = c.String("repository")
)
if reponame == "" {
reponame = c.Args().First()
}
owner, name, err := internal.ParseRepo(reponame)
if err != nil {
return err
}
client, err := internal.NewClient(c)
if err != nil {
return err
}
return client.SecretDelete(owner, name, secret)
}

View file

@ -1,70 +0,0 @@
package secret
import (
"io/ioutil"
"strings"
"github.com/drone/drone/drone/internal"
"github.com/drone/drone/model"
"github.com/urfave/cli"
)
var secretUpdateCmd = cli.Command{
Name: "update",
Usage: "update a secret",
Action: secretUpdate,
Flags: []cli.Flag{
cli.StringFlag{
Name: "repository",
Usage: "repository name (e.g. octocat/hello-world)",
},
cli.StringFlag{
Name: "name",
Usage: "secret name",
},
cli.StringFlag{
Name: "value",
Usage: "secret value",
},
cli.StringSliceFlag{
Name: "event",
Usage: "secret limited to these events",
},
cli.StringSliceFlag{
Name: "image",
Usage: "secret limited to these images",
},
},
}
func secretUpdate(c *cli.Context) error {
reponame := c.String("repository")
if reponame == "" {
reponame = c.Args().First()
}
owner, name, err := internal.ParseRepo(reponame)
if err != nil {
return err
}
client, err := internal.NewClient(c)
if err != nil {
return err
}
secret := &model.Secret{
Name: c.String("name"),
Value: c.String("value"),
Images: c.StringSlice("image"),
Events: c.StringSlice("events"),
}
if strings.HasPrefix(secret.Value, "@") {
path := strings.TrimPrefix(secret.Value, "@")
out, ferr := ioutil.ReadFile(path)
if ferr != nil {
return ferr
}
secret.Value = string(out)
}
_, err = client.SecretUpdate(owner, name, secret)
return err
}

View file

@ -1,15 +0,0 @@
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,
},
}

View file

@ -1,32 +0,0 @@
package user
import (
"fmt"
"github.com/drone/drone/model"
"github.com/urfave/cli"
"github.com/drone/drone/drone/internal"
)
var userAddCmd = cli.Command{
Name: "add",
Usage: "adds a user",
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(&model.User{Login: login})
if err != nil {
return err
}
fmt.Printf("Successfully added user %s\n", user.Login)
return nil
}

View file

@ -1,51 +0,0 @@
package user
import (
"fmt"
"os"
"text/template"
"github.com/urfave/cli"
"github.com/drone/drone/drone/internal"
)
var userInfoCmd = cli.Command{
Name: "info",
Usage: "show user details",
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 }}`

View file

@ -1,47 +0,0 @@
package user
import (
"os"
"text/template"
"github.com/urfave/cli"
"github.com/drone/drone/drone/internal"
)
var userListCmd = cli.Command{
Name: "ls",
Usage: "list all users",
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 }}`

View file

@ -1,30 +0,0 @@
package user
import (
"fmt"
"github.com/urfave/cli"
"github.com/drone/drone/drone/internal"
)
var userRemoveCmd = cli.Command{
Name: "rm",
Usage: "remove a user",
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
}