<commit_before>package main

import (
	"fmt"
	"log"
	"os"
	"text/tabwriter"

	"github.com/coreos-inc/updatectl/client/update/v1"
	"github.com/coreos-inc/updatectl/third_party/github.com/codegangsta/cli"
)

func AppCommands() []cli.Command {
	return []cli.Command{
		{
			Name:  "list-apps",
			Usage: "list-apps",
			Description: `List all of the apps that exist including their label,
token and update state.`,
			Action: handle(listApps),
		},
		{
			Name:        "update-app",
			Usage:       "update-app <appId> <channelId> <label>",
			Description: `Update an app given a label.`,
			Action:      handle(updateApp),
		},
		{
			Name:        "delete-app",
			Usage:       "delete-app <appId>",
			Description: `Delete a app given an id.`,
			Action:      handle(deleteApp),
		},
	}
}

func formatApp(app *update.App) string {
	return fmt.Sprintf("%s\t%s\t%s\n", app.Id, app.Label, app.Description)
}

func listApps(c *cli.Context, service *update.Service, out *tabwriter.Writer) {
	listCall := service.App.List()
	list, err := listCall.Do()

	if err != nil {
		log.Fatal(err)
	}

	fmt.Fprintln(out, "Id\tLabel\tDescription")
	for _, app := range list.Items {
		fmt.Fprintf(out, "%s", formatApp(app))
	}

	out.Flush()
}

func updateApp(c *cli.Context, service *update.Service, out *tabwriter.Writer) {
	args := c.Args()

	if len(args) != 3 {
		cli.ShowCommandHelp(c, "update-app")
		os.Exit(1)
	}
	appReq := &update.AppUpdateReq{Label: args[1], Description: args[2]}
	call := service.App.Update(args[0], appReq)
	app, err := call.Do()

	if err != nil {
		log.Fatal(err)
	}

	fmt.Fprintf(out, "%s", formatApp(app))

	out.Flush()
}

func deleteApp(c *cli.Context, service *update.Service, out *tabwriter.Writer) {
	args := c.Args()

	if len(args) != 1 {
		fmt.Println("App token is required")
		os.Exit(1)
	}

	call := service.App.Delete(args[0])
	app, err := call.Do()

	if err != nil {
		log.Fatal(err)
	}

	fmt.Fprintf(out, "%s", formatApp(app))
<commit_msg>feat(create-app): add create-app alias for update-app<commit_after>package main

import (
	"fmt"
	"log"
	"os"
	"text/tabwriter"

	"github.com/coreos-inc/updatectl/third_party/code.google.com/p/go-uuid/uuid"
	"github.com/coreos-inc/updatectl/client/update/v1"
	"github.com/coreos-inc/updatectl/third_party/github.com/codegangsta/cli"
)

func AppCommands() []cli.Command {
	return []cli.Command{
		{
			Name:  "list-apps",
			Usage: "list-apps",
			Description: `List all of the apps that exist including their label,
token and update state.`,
			Action: handle(listApps),
		},
		{
			Name:        "create-app",
			Usage:       "create-app [<appId>] <label> <description>",
			Description: `Create a new application. If appId is not provided it will be created randomly.`,
			Action:      handle(createApp),
		},
		{
			Name:        "update-app",
			Usage:       "update-app <appId> <label> <description>",
			Description: `Update an app given a label.`,
			Action:      handle(updateApp),
		},
		{
			Name:        "delete-app",
			Usage:       "delete-app <appId>",
			Description: `Delete a app given an id.`,
			Action:      handle(deleteApp),
		},
	}
}

func formatApp(app *update.App) string {
	return fmt.Sprintf("%s\t%s\t%s\n", app.Id, app.Label, app.Description)
}

func listApps(c *cli.Context, service *update.Service, out *tabwriter.Writer) {
	listCall := service.App.List()
	list, err := listCall.Do()

	if err != nil {
		log.Fatal(err)
	}

	fmt.Fprintln(out, "Id\tLabel\tDescription")
	for _, app := range list.Items {
		fmt.Fprintf(out, "%s", formatApp(app))
	}

	out.Flush()
}

func createApp(c *cli.Context, service *update.Service, out *tabwriter.Writer) {
	args := c.Args()

	if !(len(args) == 2 || len(args) == 3) {
		cli.ShowCommandHelp(c, "create-app")
		os.Exit(1)
	}

	if len(args) == 2 {
		appId := uuid.New()
		args = append([]string{appId}, args...)
	}

	updateAppHelper(c, service, out, args)
}

func updateApp(c *cli.Context, service *update.Service, out *tabwriter.Writer) {
	args := c.Args()
	if len(args) != 3 {
		cli.ShowCommandHelp(c, "update-app")
		os.Exit(1)
	}
	updateAppHelper(c, service, out, args)
}

func updateAppHelper(c *cli.Context, service *update.Service, out *tabwriter.Writer, args []string) {
	appReq := &update.AppUpdateReq{Label: args[1], Description: args[2]}
	call := service.App.Update(args[0], appReq)
	app, err := call.Do()

	if err != nil {
		log.Fatal(err)
	}

	fmt.Fprintf(out, "%s", formatApp(app))

	out.Flush()
}

func deleteApp(c *cli.Context, service *update.Service, out *tabwriter.Writer) {
	args := c.Args()

	if len(args) != 1 {
		fmt.Println("App token is required")
		os.Exit(1)
	}

	call := service.App.Delete(args[0])
	app, err := call.Do()

	if err != nil {
		log.Fatal(err)
	}

	fmt.Fprintf(out, "%s", formatApp(app))

{'fraction_non_alphanumeric': 0.17420576478013886, 'fraction_numerical': 0.004628655585945719, 'mean_word_length': 3.523311132254995, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1572727', 'n_tokens_mistral': 1917, 'n_tokens_neox': 1789, 'n_words': 398}