github.com/koron/hk@v0.0.0-20150303213137-b8aeaa3ab34c/transfer.go (about)

     1  package main
     2  
     3  import (
     4  	"io"
     5  	"log"
     6  	"os"
     7  	"strings"
     8  	"text/tabwriter"
     9  
    10  	"github.com/heroku/hk/Godeps/_workspace/src/github.com/bgentry/heroku-go"
    11  )
    12  
    13  var cmdTransfer = &Command{
    14  	Run:      runTransfer,
    15  	Usage:    "transfer <email or org name>",
    16  	NeedsApp: true,
    17  	Category: "app",
    18  	Short:    "transfer app ownership to a collaborator or an org" + extra,
    19  	Long: `
    20  Transfer an app's ownership to a collaborator or a Heroku
    21  organization.
    22  
    23  Examples:
    24  
    25      $ hk transfer user@test.com
    26      Requested transfer of myapp to user@test.com.
    27  
    28      $ hk transfer myorg
    29      Transferred ownership of myapp to myorg.
    30  `,
    31  }
    32  
    33  func runTransfer(cmd *Command, args []string) {
    34  	appname := mustApp()
    35  	if len(args) != 1 {
    36  		cmd.PrintUsage()
    37  		os.Exit(2)
    38  	}
    39  	recipient := args[0]
    40  
    41  	// if this app has no org AND it's being transferred to another user (email)
    42  	// then we use the regular app transfer endpoint, otherwise use the org
    43  	// endpoint.
    44  	if !isAppInOrg(mustGetOrgApp(appname)) && strings.Contains(recipient, "@") {
    45  		xfer, err := client.AppTransferCreate(appname, recipient)
    46  		must(err)
    47  		log.Printf("Requested transfer of %s to %s.", xfer.App.Name, xfer.Recipient.Email)
    48  	} else {
    49  		_, err := client.OrganizationAppTransferToAccount(appname, recipient)
    50  		must(err)
    51  		log.Printf("Transferred ownership of %s to %s.", appname, recipient)
    52  	}
    53  }
    54  
    55  var cmdTransfers = &Command{
    56  	Run:      runTransfers,
    57  	Usage:    "transfers",
    58  	NeedsApp: true,
    59  	Category: "app",
    60  	Short:    "list existing app transfer requests" + extra,
    61  }
    62  
    63  func runTransfers(cmd *Command, args []string) {
    64  	if len(args) != 0 {
    65  		cmd.PrintUsage()
    66  		os.Exit(2)
    67  	}
    68  	transfers, err := client.AppTransferList(nil)
    69  	must(err)
    70  
    71  	w := tabwriter.NewWriter(os.Stdout, 1, 2, 2, ' ', 0)
    72  	defer w.Flush()
    73  	for i := range transfers {
    74  		listTransfer(w, transfers[i])
    75  	}
    76  }
    77  
    78  func listTransfer(w io.Writer, t heroku.AppTransfer) {
    79  	listRec(w,
    80  		t.App.Name,
    81  		abbrev(t.Owner.Email, 10),
    82  		abbrev(t.Recipient.Email, 10),
    83  		t.State,
    84  		prettyTime{t.UpdatedAt},
    85  	)
    86  }
    87  
    88  var cmdTransferAccept = &Command{
    89  	Run:      runTransferAccept,
    90  	Usage:    "transfer-accept",
    91  	NeedsApp: true,
    92  	Category: "app",
    93  	Short:    "accept an inbound app transfer request" + extra,
    94  }
    95  
    96  func runTransferAccept(cmd *Command, args []string) {
    97  	if len(args) != 0 {
    98  		cmd.PrintUsage()
    99  		os.Exit(2)
   100  	}
   101  	xfer, err := client.AppTransferUpdate(mustApp(), "accepted")
   102  	must(err)
   103  	log.Printf("Accepted transfer of %s from %s.", xfer.App.Name, xfer.Recipient.Email)
   104  }
   105  
   106  var cmdTransferDecline = &Command{
   107  	Run:      runTransferDecline,
   108  	Usage:    "transfer-decline",
   109  	NeedsApp: true,
   110  	Category: "app",
   111  	Short:    "decline an inbound app transfer request" + extra,
   112  }
   113  
   114  func runTransferDecline(cmd *Command, args []string) {
   115  	if len(args) != 0 {
   116  		cmd.PrintUsage()
   117  		os.Exit(2)
   118  	}
   119  	xfer, err := client.AppTransferUpdate(mustApp(), "declined")
   120  	must(err)
   121  	log.Printf("Declined transfer of %s to %s.", xfer.App.Name, xfer.Recipient.Email)
   122  }
   123  
   124  var cmdTransferCancel = &Command{
   125  	Run:      runTransferCancel,
   126  	Usage:    "transfer-cancel",
   127  	NeedsApp: true,
   128  	Category: "app",
   129  	Short:    "cancel an outbound app transfer request" + extra,
   130  }
   131  
   132  func runTransferCancel(cmd *Command, args []string) {
   133  	if len(args) != 0 {
   134  		cmd.PrintUsage()
   135  		os.Exit(2)
   136  	}
   137  	appname := mustApp()
   138  	must(client.AppTransferDelete(appname))
   139  	log.Printf("Canceled transfer of %s.", appname)
   140  }