<commit_before>package main

// Copyright (c) 2016, <NAME>
// This code is open source under the ISC license. See LICENSE for details.

import (
	"github.com/crasm/shield"
	"github.com/jessevdk/go-flags"
)

import (
	"fmt"
	"log"
	"os"
	"strings"
)

const DefaultPerm = 0644
const FileExtension = `.shd`

var opt struct {
	Create  bool `short:"C" long:"create" description:"Create a shield file."`
	Extract bool `short:"X" long:"extract" description:"Extract a contained file from a shield file."`
	Info    bool `short:"I" long:"info" description:"Show info on a shield file."`

	InferName bool `short:"i" long:"infer-name" description:"Infer output filename."`
	Force     bool `short:"f" long:"force" description:"Overwrite files."`
	//Timid      bool `short:"t" long:"timid" description:"Delete extracted file if its claim is found to be invalid."`
	//Lax   bool `short:"l" long:"lax" description:"Allow partial and unverifieid dextraction"`
	//Quiet bool `short:"q" long:"quiet" description:"Silence all non-data output to stdout or stderr."`
}

func main() {
	args, err := flags.Parse(&opt)
	if err != nil {
		log.Fatal(err)
	}

	if !xor(opt.Create, opt.Extract, opt.Info) {
		log.Fatal("more than one command (or no commands) specified")
	}

	in := os.Stdin
	out := os.Stdout

	if opt.InferName {
		if len(args) != 1 {
			log.Fatal("can only work on a single shield file")
		}
		in, err = os.Open(args[0])
		defer in.Close()
		if err != nil {
			log.Fatal(err)
		}

	} else {
		if len(args) > 2 {
			log.Fatal("more than two non-flag args remaining")
		}

		if len(args) == 2 && args[1] != "-" {
			out, err = safeFileCreate(args[1])
			defer out.Close()
			if err != nil {
				log.Fatal(err)
			}
		}

		if len(args) >= 1 && args[0] != "-" {
			in, err = os.Open(args[0])
			defer in.Close()
			if err != nil {
				log.Fatal(err)
			}
		}
	}

	switch {
	case opt.Create:
		if opt.InferName {
			out, err = safeFileCreate(fmt.Sprint(in.Name(), FileExtension))
			defer out.Close()
			if err != nil {
				log.Fatal(err)
			}
		}
		err = shield.Wrap(in, out)
	case opt.Extract:
		if opt.InferName {
			out, err = safeFileCreate(strings.TrimSuffix(in.Name(), FileExtension))
			defer out.Close()
			if err != nil {
				log.Fatal(err)
			}
		}
		err = shield.Unwrap(in, out)
	case opt.Info:
		fallthrough
	default:
		panic("info command not supported (yet)")
	}<commit_msg>Support for pipes using temporary files.<commit_after>package main

// Copyright (c) 2016, <NAME>
// This code is open source under the ISC license. See LICENSE for details.

import (
	"github.com/crasm/shield"
	"github.com/jessevdk/go-flags"
)

import (
	"fmt"
	"log"
	"os"
	"strings"
)

const DefaultPerm = 0644
const FileExtension = `.shd`

var opt struct {
	Create  bool `short:"C" long:"create" description:"Create a shield file."`
	Extract bool `short:"X" long:"extract" description:"Extract a contained file from a shield file."`
	Info    bool `short:"I" long:"info" description:"Show info on a shield file."`

	InferName bool `short:"i" long:"infer-name" description:"Infer output filename."`
	Force     bool `short:"f" long:"force" description:"Overwrite files."`
	//Timid      bool `short:"t" long:"timid" description:"Delete extracted file if its claim is found to be invalid."`
	//Lax   bool `short:"l" long:"lax" description:"Allow partial and unverifieid dextraction"`
	//Quiet bool `short:"q" long:"quiet" description:"Silence all non-data output to stdout or stderr."`
}

func main() {
	args, err := flags.Parse(&opt)
	if err != nil {
		log.Fatal(err)
	}

	if !xor(opt.Create, opt.Extract, opt.Info) {
		log.Fatal("more than one command (or no commands) specified")
	}

	in := os.Stdin
	out := os.Stdout

	if opt.InferName {
		if len(args) != 1 {
			log.Fatal("can only work on a single shield file")
		}
		in, err = os.Open(args[0])
		defer in.Close()
		if err != nil {
			log.Fatal(err)
		}

	} else {
		if len(args) > 2 {
			log.Fatal("more than two non-flag args remaining")
		}

		if len(args) == 2 && args[1] != "-" {
			out, err = safeFileCreate(args[1])
			defer out.Close()
			if err != nil {
				log.Fatal(err)
			}
		}

		if len(args) >= 1 && args[0] != "-" {
			in, err = os.Open(args[0])
			defer in.Close()
			if err != nil {
				log.Fatal(err)
			}
		}
	}

	switch {
	case opt.Create:
		if opt.InferName {
			out, err = safeFileCreate(fmt.Sprint(in.Name(), FileExtension))
			defer out.Close()
			if err != nil {
				log.Fatal(err)
			}
		}
		if out == os.Stdout {
			err = shield.WrapBuffered(in, out)
		} else {
			err = shield.Wrap(in, out)
		}
	case opt.Extract:
		if opt.InferName {
			out, err = safeFileCreate(strings.TrimSuffix(in.Name(), FileExtension))
			defer out.Close()
			if err != nil {
				log.Fatal(err)
			}
		}
		err = shield.Unwrap(in, out)
	case opt.Info:
		fallthrough
	default:
		panic("info command not supported (yet)")
	}
{'fraction_non_alphanumeric': 0.15392540696476406, 'fraction_numerical': 0.007005975685143211, 'mean_word_length': 3.272887323943662, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '5569995', 'n_tokens_mistral': 1965, 'n_tokens_neox': 1748, 'n_words': 496}