<commit_before>package aws

import (
	mach "github.com/jeffjen/machine/lib/machine"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/credentials"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/ec2"
	"github.com/codegangsta/cli"

	"fmt"
	"math/rand"
	"net"
	"os"
	"time"
)

func init() {
	rand.Seed(time.Now().Unix())
}

var (
	// AWS EC2 client object for establishing command
	svc *ec2.EC2

	// Instance Roster
	instList = make(mach.RegisteredInstances)
)

func NewCommand() cli.Command {
	return cli.Command{
		Name:  "aws",
		Usage: "Create and Manage AWS machine",
		Flags: []cli.Flag{
			cli.StringFlag{Name: "region", EnvVar: "AWS_REGION", Usage: "AWS Region"},
			cli.StringFlag{Name: "key", EnvVar: "AWS_ACCESS_KEY_ID", Usage: "AWS access key"},
			cli.StringFlag{Name: "secret", EnvVar: "AWS_SECRET_ACCESS_KEY", Usage: "AWS secret key"},
			cli.StringFlag{Name: "token", EnvVar: "AWS_SESSION_TOKEN", Usage: "session token for temporary credentials"},
			cli.StringFlag{Name: "user", EnvVar: "MACHINE_USER", Usage: "Run command as user"},
			cli.StringFlag{Name: "cert", EnvVar: "MACHINE_CERT_FILE", Usage: "Private key to use in Authentication"},
		},
		Before: func(c *cli.Context) error {
			// bootstrap EC2 client with command line args
			cfg := aws.NewConfig()
			if region := c.String("region"); region != "" {
				cfg = cfg.WithRegion(region)
			}
			if id, secret, token := c.String("key"), c.String("secret"), c.String("token"); id != "" && secret != "" {
				cfg = cfg.WithCredentials(credentials.NewStaticCredentials(id, secret, token))
			}
			svc = ec2.New(session.New(cfg))
			return nil
		},
		Subcommands: []cli.Command{
			newConfigCommand(),
			newCreateCommand(),
			newStartCommand(),
			newStopCommand(),
			newRmCommand(),
			newImageCommand(),
		},
	}
}

func newStartCommand() cli.Command {
	return cli.Command{
		Name:  "start",
		Usage: "Start instance",
		Action: func(c *cli.Context) error {
			var name = c.Args().First()

			// Load from Instance Roster
			instList.Load()

			info, ok := instList[name]
			if !ok {
				fmt.Fprintln(os.Stderr, "Target machine not found")
				os.Exit(1)
			}

			_, err := svc.StartInstances(&ec2.StartInstancesInput{
				InstanceIds: []*string{
					aws.String(info.Id),
				},
			})
			if err != nil {
				fmt.Fprintln(os.Stderr, err)
				os.Exit(1)
			}

			return nil
		},
	}
}

func newStopCommand() cli.Command {
	return cli.Command{
		Name:  "stop",
		Usage: "Stop instance",
		Action: func(c *cli.Context) error {
			var name = c.Args().First()

			// Load from Instance Roster
			instList.Load()

			info, ok := instList[name]
			if !ok {
				fmt.Fprintln(os.Stderr, "Target machine not found")
				os.Exit(1)
			}

			_, err := svc.StopInstances(&ec2.StopInstancesInput{
				InstanceIds: []*string{
					aws.String(info.Id),
				},
			})
			if err != nil {
				fmt.Fprintln(os.Stderr, err)
				os.Exit(1)
			}

			return nil
		},
	}
}

func newRmCommand() cli.Command {
	return cli.Command{
		Name:  "rm",
		Usage: "Remove and Terminate instance",
		Action: func(c *cli.Context) error {
			var name = c.Args().First()

			// Load from Instance Roster
			instList.Load()

			info, ok := instList[name]
			if !ok {
				fmt.Fprintln(os.Stderr, "Target machine not found")
				os.Exit(1)
			}

			_, err := svc.TerminateInstances(&ec2.TerminateInstancesInput{
				InstanceIds: []*string{
					aws.String(info.Id),
				},
			})
			if err != nil {
				fmt.Fprintln(os.Stderr, err)
				os.Exit(1)
			}
			return nil
		},<commit_msg>FIX: aws remove instance from roster on remove<commit_after>package aws

import (
	mach "github.com/jeffjen/machine/lib/machine"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/credentials"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/ec2"
	"github.com/codegangsta/cli"

	"fmt"
	"math/rand"
	"net"
	"os"
	"time"
)

func init() {
	rand.Seed(time.Now().Unix())
}

var (
	// AWS EC2 client object for establishing command
	svc *ec2.EC2

	// Instance Roster
	instList = make(mach.RegisteredInstances)
)

func NewCommand() cli.Command {
	return cli.Command{
		Name:  "aws",
		Usage: "Create and Manage AWS machine",
		Flags: []cli.Flag{
			cli.StringFlag{Name: "region", EnvVar: "AWS_REGION", Usage: "AWS Region"},
			cli.StringFlag{Name: "key", EnvVar: "AWS_ACCESS_KEY_ID", Usage: "AWS access key"},
			cli.StringFlag{Name: "secret", EnvVar: "AWS_SECRET_ACCESS_KEY", Usage: "AWS secret key"},
			cli.StringFlag{Name: "token", EnvVar: "AWS_SESSION_TOKEN", Usage: "session token for temporary credentials"},
			cli.StringFlag{Name: "user", EnvVar: "MACHINE_USER", Usage: "Run command as user"},
			cli.StringFlag{Name: "cert", EnvVar: "MACHINE_CERT_FILE", Usage: "Private key to use in Authentication"},
		},
		Before: func(c *cli.Context) error {
			// bootstrap EC2 client with command line args
			cfg := aws.NewConfig()
			if region := c.String("region"); region != "" {
				cfg = cfg.WithRegion(region)
			}
			if id, secret, token := c.String("key"), c.String("secret"), c.String("token"); id != "" && secret != "" {
				cfg = cfg.WithCredentials(credentials.NewStaticCredentials(id, secret, token))
			}
			svc = ec2.New(session.New(cfg))
			return nil
		},
		Subcommands: []cli.Command{
			newConfigCommand(),
			newCreateCommand(),
			newStartCommand(),
			newStopCommand(),
			newRmCommand(),
			newImageCommand(),
		},
	}
}

func newStartCommand() cli.Command {
	return cli.Command{
		Name:  "start",
		Usage: "Start instance",
		Action: func(c *cli.Context) error {
			var name = c.Args().First()

			// Load from Instance Roster
			instList.Load()

			info, ok := instList[name]
			if !ok {
				fmt.Fprintln(os.Stderr, "Target machine not found")
				os.Exit(1)
			}

			_, err := svc.StartInstances(&ec2.StartInstancesInput{
				InstanceIds: []*string{
					aws.String(info.Id),
				},
			})
			if err != nil {
				fmt.Fprintln(os.Stderr, err)
				os.Exit(1)
			}

			return nil
		},
	}
}

func newStopCommand() cli.Command {
	return cli.Command{
		Name:  "stop",
		Usage: "Stop instance",
		Action: func(c *cli.Context) error {
			var name = c.Args().First()

			// Load from Instance Roster
			instList.Load()

			info, ok := instList[name]
			if !ok {
				fmt.Fprintln(os.Stderr, "Target machine not found")
				os.Exit(1)
			}

			_, err := svc.StopInstances(&ec2.StopInstancesInput{
				InstanceIds: []*string{
					aws.String(info.Id),
				},
			})
			if err != nil {
				fmt.Fprintln(os.Stderr, err)
				os.Exit(1)
			}

			return nil
		},
	}
}

func newRmCommand() cli.Command {
	return cli.Command{
		Name:  "rm",
		Usage: "Remove and Terminate instance",
		Action: func(c *cli.Context) error {
			var name = c.Args().First()

			// Load from Instance Roster to register and defer write back
			defer instList.Load().Dump()

			info, ok := instList[name]
			if !ok {
				fmt.Fprintln(os.Stderr, "Target machine not found")
				os.Exit(1)
			}

			_, err := svc.TerminateInstances(&ec2.TerminateInstancesInput{
				InstanceIds: []*string{
					aws.String(info.Id),
				},
			})
			if err != nil {
				fmt.Fprintln(os.Stderr, err)
				os.Exit(1)
			}

			delete(instList, name)

			return nil
		},
{'fraction_non_alphanumeric': 0.15920742690868783, 'fraction_numerical': 0.004156851877511431, 'mean_word_length': 3.2887700534759357, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3051188', 'n_tokens_mistral': 3074, 'n_tokens_neox': 2650, 'n_words': 584}