diff --git a/cli/keys.go b/cli/keys.go new file mode 100644 index 000000000..002908693 --- /dev/null +++ b/cli/keys.go @@ -0,0 +1,48 @@ +package main + +import ( + "fmt" + "io/ioutil" + + "github.com/codegangsta/cli" + "github.com/drone/drone/client" +) + +// NewSetKeyCommand returns the CLI command for "set-key". +func NewSetKeyCommand() cli.Command { + return cli.Command{ + Name: "set-key", + Usage: "sets the SSH private key used to clone", + Flags: []cli.Flag{}, + Action: func(c *cli.Context) { + handle(c, setKeyCommandFunc) + }, + } +} + +// setKeyCommandFunc executes the "set-key" command. +func setKeyCommandFunc(c *cli.Context, client *client.Client) error { + var host, owner, name, path string + var args = c.Args() + + if len(args) != 0 { + host, owner, name = parseRepo(args[0]) + } + + if len(args) == 2 { + path = args[1] + } + + pub, err := ioutil.ReadFile(path) + if err != nil { + return fmt.Errorf("Could not find private RSA key %s. %s", path, err) + } + + path_pub := path + ".pub" + priv, err := ioutil.ReadFile(path_pub) + if err != nil { + return fmt.Errorf("Could not find public RSA key %s. %s", path_pub, err) + } + + return client.Repos.SetKey(host, owner, name, string(pub), string(priv)) +} diff --git a/cli/main.go b/cli/main.go index 0a2e3b908..9b4407943 100644 --- a/cli/main.go +++ b/cli/main.go @@ -39,6 +39,7 @@ func main() { NewDisableCommand(), NewRestartCommand(), NewWhoamiCommand(), + NewSetKeyCommand(), } app.Run(os.Args) diff --git a/client/repos.go b/client/repos.go index cd42b2d32..fc22badb9 100644 --- a/client/repos.go +++ b/client/repos.go @@ -38,6 +38,16 @@ func (s *RepoService) Disable(host, owner, name string) error { return s.run("DELETE", path, nil, nil) } +// PUT /api/repos/{host}/{owner}/{name} +func (s *RepoService) SetKey(host, owner, name, pub, priv string) error { + var path = fmt.Sprintf("/api/repos/%s/%s/%s", host, owner, name) + var in = struct { + PublicKey string `json:"public_key"` + PrivateKey string `json:"private_key"` + }{pub, priv} + return s.run("PUT", path, &in, nil) +} + // GET /api/user/repos func (s *RepoService) List() ([]*model.Repo, error) { var repos []*model.Repo