<commit_before>package main

import (
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"os/exec"

	contentapi "github.com/containerd/containerd/api/services/content"
	contentservice "github.com/containerd/containerd/services/content"
	digest "github.com/opencontainers/go-digest"
	"github.com/urfave/cli"
)

var editCommand = cli.Command{
	Name:        "edit",
	Usage:       "edit a blob and return a new digest.",
	ArgsUsage:   "[flags] <digest>",
	Description: `Edit a blob and return a new digest.`,
	Flags: []cli.Flag{
		cli.StringFlag{
			Name:  "validate",
			Usage: "validate the result against a format (json, mediatype, etc.)",
		},
	},
	Action: func(context *cli.Context) error {
		var (
			ctx      = background
			validate = context.String("validate")
			object   = context.Args().First()
		)

		if validate != "" {
			return errors.New("validating the edit result not supported")
		}

		// TODO(stevvooe): Support looking up objects by a reference through
		// the image metadata storage.

		dgst, err := digest.Parse(object)
		if err != nil {
			return err
		}

		conn, err := connectGRPC(context)
		if err != nil {
			return err
		}

		provider := contentservice.NewProviderFromClient(contentapi.NewContentClient(conn))
		ingester := contentservice.NewIngesterFromClient(contentapi.NewContentClient(conn))

		rc, err := provider.Reader(ctx, dgst)
		if err != nil {
			return err
		}
		defer rc.Close()

		nrc, err := edit(rc)
		if err != nil {
			return err
		}

		wr, err := ingester.Writer(ctx, "edit-"+object, 0, "") // TODO(stevvooe): Choose a better key?
		if err != nil {
			return err
		}

		if _, err := io.Copy(wr, nrc); err != nil {
			return err
		}

		if err := wr.Commit(0, wr.Digest()); err != nil {
			return err
		}

		fmt.Println(wr.Digest())
		return nil
	},
}

func edit(rd io.Reader) (io.ReadCloser, error) {
	tmp, err := ioutil.TempFile("", "edit-")
	if err != nil {
		return nil, err
	}

	if _, err := io.Copy(tmp, rd); err != nil {
		tmp.Close()
		return nil, err
	}

	cmd := exec.Command("sh", "-c", "$EDITOR "+tmp.Name())

	cmd.Stdin = os.Stdin
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	cmd.Env = os.Environ()

	if err := cmd.Run(); err != nil {
		tmp.Close()
		return nil, err
	}

	if _, err := tmp.Seek(0, io.SeekStart); err != nil {
		tmp.Close()
		return nil, err
	}

	return tmp, nil
}
<commit_msg>cmd/dist: close and remove tempfile after edit<commit_after>package main

import (
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"os/exec"

	contentapi "github.com/containerd/containerd/api/services/content"
	contentservice "github.com/containerd/containerd/services/content"
	digest "github.com/opencontainers/go-digest"
	"github.com/urfave/cli"
)

var editCommand = cli.Command{
	Name:        "edit",
	Usage:       "edit a blob and return a new digest.",
	ArgsUsage:   "[flags] <digest>",
	Description: `Edit a blob and return a new digest.`,
	Flags: []cli.Flag{
		cli.StringFlag{
			Name:  "validate",
			Usage: "validate the result against a format (json, mediatype, etc.)",
		},
	},
	Action: func(context *cli.Context) error {
		var (
			ctx      = background
			validate = context.String("validate")
			object   = context.Args().First()
		)

		if validate != "" {
			return errors.New("validating the edit result not supported")
		}

		// TODO(stevvooe): Support looking up objects by a reference through
		// the image metadata storage.

		dgst, err := digest.Parse(object)
		if err != nil {
			return err
		}

		conn, err := connectGRPC(context)
		if err != nil {
			return err
		}

		provider := contentservice.NewProviderFromClient(contentapi.NewContentClient(conn))
		ingester := contentservice.NewIngesterFromClient(contentapi.NewContentClient(conn))

		rc, err := provider.Reader(ctx, dgst)
		if err != nil {
			return err
		}
		defer rc.Close()

		nrc, err := edit(rc)
		if err != nil {
			return err
		}
		defer nrc.Close()

		wr, err := ingester.Writer(ctx, "edit-"+object, 0, "") // TODO(stevvooe): Choose a better key?
		if err != nil {
			return err
		}

		if _, err := io.Copy(wr, nrc); err != nil {
			return err
		}

		if err := wr.Commit(0, wr.Digest()); err != nil {
			return err
		}

		fmt.Println(wr.Digest())
		return nil
	},
}

func edit(rd io.Reader) (io.ReadCloser, error) {
	tmp, err := ioutil.TempFile("", "edit-")
	if err != nil {
		return nil, err
	}

	if _, err := io.Copy(tmp, rd); err != nil {
		tmp.Close()
		return nil, err
	}

	cmd := exec.Command("sh", "-c", "$EDITOR "+tmp.Name())

	cmd.Stdin = os.Stdin
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	cmd.Env = os.Environ()

	if err := cmd.Run(); err != nil {
		tmp.Close()
		return nil, err
	}

	if _, err := tmp.Seek(0, io.SeekStart); err != nil {
		tmp.Close()
		return nil, err
	}

	return onCloser{ReadCloser: tmp, onClose: func() error {
		return os.RemoveAll(tmp.Name())
	}}, nil
}

type onCloser struct {
	io.ReadCloser
	onClose func() error
}

func (oc onCloser) Close() error {
	var err error
	if err1 := oc.ReadCloser.Close(); err1 != nil {
		err = err1
	}

	if oc.onClose != nil {
		err1 := oc.onClose()
		if err == nil {
			err = err1
		}
	}

	return err
}

{'fraction_non_alphanumeric': 0.15236976106541325, 'fraction_numerical': 0.002154328241284763, 'mean_word_length': 3.1152296535052377, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '4962661', 'n_tokens_mistral': 2036, 'n_tokens_neox': 1851, 'n_words': 494}