<commit_before>package main

import (
	"fmt"
	"github.com/rubyist/gohat/pkg/parser"
	"github.com/spf13/cobra"
	"os"
	"strconv"
)

func main() {
	var gohatCmd = &cobra.Command{
		Use:   "gohat",
		Short: "gohat is go heap dump analysis tool",
		Long: `Gohat can read and query go heap dump files.
Complete documentation is available at http://github.com/rubyist/gohat`,
		Run: func(cmd *cobra.Command, args []string) {
		},
	}

	var objectsCommand = &cobra.Command{
		Use:   "objects",
		Short: "Dump a list of objects",
		Run: func(cmd *cobra.Command, args []string) {
			if len(args) != 1 {
				fmt.Println("Please specify a heap dump file")
				os.Exit(1)
			}
			heapFile, _ := parser.NewHeapFile(args[0])
			objects := heapFile.Objects()
			for _, object := range objects {
				typeName := "<unknown>"
				if object.Type != nil {
					typeName = object.Type.Name
				}
				fmt.Printf("%x %s %d %d\n", object.Address, typeName, object.Kind, object.Size)
			}
		},
	}
	gohatCmd.AddCommand(objectsCommand)

	var objectCommand = &cobra.Command{
		Use:   "object",
		Short: "Dump the contents of an object",
		Run: func(cmd *cobra.Command, args []string) {
			if len(args) != 2 {
				fmt.Println("object <heap file> <address>")
				os.Exit(1)
			}
			heapFile, _ := parser.NewHeapFile(args[0])
			addr, _ := strconv.ParseInt(args[1], 16, 64)
			object := heapFile.Object(addr)
			fmt.Println(object.Content)
		},
	}
	gohatCmd.AddCommand(objectCommand)

	var memStatsCommand = &cobra.Command{
		Use:   "memstats",
		Short: "Dump the memstats",
		Run: func(cmd *cobra.Command, args []string) {
			if len(args) != 1 {
				fmt.Println("memstats <heap file>")
				os.Exit(1)
			}
			heapFile, _ := parser.NewHeapFile(args[0])
			memstats := heapFile.MemStats()
			fmt.Println(memstats)
		},
	}
	gohatCmd.AddCommand(memStatsCommand)

	gohatCmd.Execute()
}<commit_msg>simplify opening the heap file<commit_after>package main

import (
	"fmt"
	"github.com/rubyist/gohat/pkg/parser"
	"github.com/spf13/cobra"
	"os"
	"strconv"
)

func main() {
	var gohatCmd = &cobra.Command{
		Use:   "gohat",
		Short: "gohat is go heap dump analysis tool",
		Long: `Gohat can read and query go heap dump files.
Complete documentation is available at http://github.com/rubyist/gohat`,
		Run: func(cmd *cobra.Command, args []string) {
		},
	}

	var objectsCommand = &cobra.Command{
		Use:   "objects",
		Short: "Dump a list of objects",
		Run: func(cmd *cobra.Command, args []string) {
			heapFile, err := verifyHeapDumpFile(args)
			if err != nil {
				fmt.Println("Error:", err)
				os.Exit(1)
			}

			objects := heapFile.Objects()
			for _, object := range objects {
				typeName := "<unknown>"
				if object.Type != nil {
					typeName = object.Type.Name
				}
				fmt.Printf("%x %s %d %d\n", object.Address, typeName, object.Kind, object.Size)
			}
		},
	}
	gohatCmd.AddCommand(objectsCommand)

	var objectCommand = &cobra.Command{
		Use:   "object",
		Short: "Dump the contents of an object",
		Run: func(cmd *cobra.Command, args []string) {
			heapFile, err := verifyHeapDumpFile(args)
			if err != nil {
				fmt.Println("Error:", err)
				os.Exit(1)
			}

			if len(args) != 2 {
				fmt.Println("object <heap file> <address>")
				os.Exit(1)
			}

			addr, _ := strconv.ParseInt(args[1], 16, 64)
			object := heapFile.Object(addr)
			fmt.Println(object.Content)
		},
	}
	gohatCmd.AddCommand(objectCommand)

	var memStatsCommand = &cobra.Command{
		Use:   "memstats",
		Short: "Dump the memstats",
		Run: func(cmd *cobra.Command, args []string) {
			heapFile, err := verifyHeapDumpFile(args)
			if err != nil {
				fmt.Println("Error:", err)
				os.Exit(1)
			}

			memstats := heapFile.MemStats()
			fmt.Println(memstats)
		},
	}
	gohatCmd.AddCommand(memStatsCommand)

	gohatCmd.Execute()
}

func verifyHeapDumpFile(args []string) (*parser.HeapFile, error) {
	if len(args) < 1 {
		return nil, fmt.Errorf("heap file required")
	}
	heapFile, err := parser.NewHeapFile(args[0])
	return heapFile, err
}
{'fraction_non_alphanumeric': 0.15229957275697412, 'fraction_numerical': 0.007539582809751194, 'mean_word_length': 3.456886898096305, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 10, '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': '3467969', 'n_tokens_mistral': 1703, 'n_tokens_neox': 1484, 'n_words': 341}