<commit_before>package main

import (
	"context"
	"flag"
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"
	"time"

	log "github.com/Sirupsen/logrus"
	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/ec2"
	"github.com/aws/aws-sdk-go/service/s3"
)

const timeoutVar = "LINUXKIT_UPLOAD_TIMEOUT"

func pushAWS(args []string) {
	awsCmd := flag.NewFlagSet("aws", flag.ExitOnError)
	invoked := filepath.Base(os.Args[0])
	awsCmd.Usage = func() {
		fmt.Printf("USAGE: %s push aws [options] [name]\n\n", invoked)
		fmt.Printf("'name' specifies the full path of an image file which will be uploaded\n")
		fmt.Printf("Options:\n\n")
		awsCmd.PrintDefaults()
	}
	timeoutFlag := awsCmd.Int("timeout", 0, "Upload timeout in seconds")
	bucketFlag := awsCmd.String("bucket", "", "S3 Bucket to upload to. *Required*")
	nameFlag := awsCmd.String("img-name", "", "Overrides the Name used to identify the file in Amazon S3 and Image. Defaults to [name] with the file extension removed.")

	if err := awsCmd.Parse(args); err != nil {
		log.Fatal("Unable to parse args")
	}

	remArgs := awsCmd.Args()
	if len(remArgs) == 0 {
		fmt.Printf("Please specify the path to the image to push\n")
		awsCmd.Usage()
		os.Exit(1)
	}
	src := remArgs[0]

	timeout := getIntValue(timeoutVar, *timeoutFlag, 600)
	bucket := getStringValue(bucketVar, *bucketFlag, "")
	name := getStringValue(nameVar, *nameFlag, "")

	sess := session.Must(session.NewSession())
	storage := s3.New(sess)

	ctx, cancelFn := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
	defer cancelFn()

	if bucket == "" {
		log.Fatalf("No bucket specified. Please provide one using the -bucket flag")
	}

	f, err := os.Open(src)
	if err != nil {
		log.Fatalf("Error opening file: %s", err)
	}
	defer f.Close()

	if name == "" {
		name = strings.TrimSuffix(name, filepath.Ext(src))
	}

	content, err := ioutil.ReadAll(f)
	if err != nil {
		log.Fatalf("error reading file: %s", err)
	}

	dst := name + filepath.Ext(src)
	putParams := &s3.PutObjectInput{
		Bucket:        aws.String(bucket),
		Key:           aws.String(dst),
		Body:          f,
		ContentLength: aws.Int64(int64(len(content))),
		ContentType:   aws.String("application/octet-stream"),
	}
	log.Debugf("PutObject:\n%v", putParams)

	_, err = storage.PutObjectWithContext(ctx, putParams)
	if err != nil {
		log.Fatalf("Error uploading to S3: %s", err)
	}

	compute := ec2.New(sess)

	importParams := &ec2.ImportSnapshotInput{
		Description: aws.String(fmt.Sprintf("LinuxKit: %s", name)),
		DiskContainer: &ec2.SnapshotDiskContainer{
			Description: aws.String(fmt.Sprintf("LinuxKit: %s disk", name)),<commit_msg>Fix empty AWS default image name<commit_after>package main

import (
	"context"
	"flag"
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"
	"time"

	log "github.com/Sirupsen/logrus"
	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/ec2"
	"github.com/aws/aws-sdk-go/service/s3"
)

const timeoutVar = "LINUXKIT_UPLOAD_TIMEOUT"

func pushAWS(args []string) {
	awsCmd := flag.NewFlagSet("aws", flag.ExitOnError)
	invoked := filepath.Base(os.Args[0])
	awsCmd.Usage = func() {
		fmt.Printf("USAGE: %s push aws [options] [name]\n\n", invoked)
		fmt.Printf("'name' specifies the full path of an image file which will be uploaded\n")
		fmt.Printf("Options:\n\n")
		awsCmd.PrintDefaults()
	}
	timeoutFlag := awsCmd.Int("timeout", 0, "Upload timeout in seconds")
	bucketFlag := awsCmd.String("bucket", "", "S3 Bucket to upload to. *Required*")
	nameFlag := awsCmd.String("img-name", "", "Overrides the Name used to identify the file in Amazon S3 and Image. Defaults to [name] with the file extension removed.")

	if err := awsCmd.Parse(args); err != nil {
		log.Fatal("Unable to parse args")
	}

	remArgs := awsCmd.Args()
	if len(remArgs) == 0 {
		fmt.Printf("Please specify the path to the image to push\n")
		awsCmd.Usage()
		os.Exit(1)
	}
	src := remArgs[0]

	timeout := getIntValue(timeoutVar, *timeoutFlag, 600)
	bucket := getStringValue(bucketVar, *bucketFlag, "")
	name := getStringValue(nameVar, *nameFlag, "")

	sess := session.Must(session.NewSession())
	storage := s3.New(sess)

	ctx, cancelFn := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
	defer cancelFn()

	if bucket == "" {
		log.Fatalf("No bucket specified. Please provide one using the -bucket flag")
	}

	f, err := os.Open(src)
	if err != nil {
		log.Fatalf("Error opening file: %s", err)
	}
	defer f.Close()

	if name == "" {
		name = strings.TrimSuffix(src, filepath.Ext(src))
	}

	content, err := ioutil.ReadAll(f)
	if err != nil {
		log.Fatalf("error reading file: %s", err)
	}

	dst := name + filepath.Ext(src)
	putParams := &s3.PutObjectInput{
		Bucket:        aws.String(bucket),
		Key:           aws.String(dst),
		Body:          f,
		ContentLength: aws.Int64(int64(len(content))),
		ContentType:   aws.String("application/octet-stream"),
	}
	log.Debugf("PutObject:\n%v", putParams)

	_, err = storage.PutObjectWithContext(ctx, putParams)
	if err != nil {
		log.Fatalf("Error uploading to S3: %s", err)
	}

	compute := ec2.New(sess)

	importParams := &ec2.ImportSnapshotInput{
		Description: aws.String(fmt.Sprintf("LinuxKit: %s", name)),
		DiskContainer: &ec2.SnapshotDiskContainer{
			Description: aws.String(fmt.Sprintf("LinuxKit: %s disk", name)),
{'fraction_non_alphanumeric': 0.16414048059149722, 'fraction_numerical': 0.008133086876155268, 'mean_word_length': 4.202884615384615, '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': '6860306', 'n_tokens_mistral': 2044, 'n_tokens_neox': 1983, 'n_words': 448}