<commit_before>package main

import (
	"errors"
	"flag"
	"fmt"
	"github.com/c-14/grue/config"
	"os"
	"text/tabwriter"
)

const version = "0.2.0-alpha"

func usage() string {
	return `usage: grue [--help] {add|delete|fetch|import|init_cfg|list} ...

Subcommands:
	add <name> <url>
	delete <name>
	fetch [-init]
	import <config>
	init_cfg
	list [name] [--full]`
}

func add(args []string, conf *config.GrueConfig) error {
	if len(args) != 2 {
		return errors.New("usage: grue add <name> <url>")
	}
	var name string = args[0]
	var uri string = args[1]
	return conf.AddAccount(name, uri)
}

func del(args []string, conf *config.GrueConfig) error {
	if len(args) != 1 {
		return errors.New("usage: grue delete <name>")
	}
	name := args[0]
	if err := conf.DeleteAccount(name); err != nil {
		return err
	}
	return DeleteHistory(name)
}

func list(args []string, conf *config.GrueConfig) error {
	const (
		fmtShort = "%s\t%s\n"
		fmtFull  = "%s:\n%s\n"
	)
	var full bool
	var listCmd = flag.NewFlagSet("list", flag.ContinueOnError)
	listCmd.BoolVar(&full, "full", false, "Show full account info")
	if err := listCmd.Parse(args); err != nil {
		return err
	}
	if len(listCmd.Args()) == 0 {
		if full {
			for name, cfg := range conf.Accounts {
				fmt.Printf(fmtFull, name, cfg.String())
			}
			return nil
		}
		w := tabwriter.NewWriter(os.Stdout, 0, 8, 0, '\t', 0)
		for name, cfg := range conf.Accounts {
			fmt.Fprintf(w, fmtShort, name, cfg.URI)
		}
		w.Flush()
		return nil
	}

	name := listCmd.Args()[0]
	if cfg, ok := conf.Accounts[name]; ok {
		if full {
			fmt.Printf(fmtFull, name, cfg.String())
		} else {
			fmt.Printf(fmtShort, name, cfg.URI)
		}
	}
	return nil
}

func main() {
	if len(os.Args) < 2 {
		fmt.Fprintln(os.Stderr, usage())
		os.Exit(EX_USAGE)
	}
	conf, err := config.ReadConfig()
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(EX_TEMPFAIL)
	}
	defer conf.Unlock()
	switch cmd := os.Args[1]; cmd {
	case "add":
		err = add(os.Args[2:], conf)
	case "delete":
		err = del(os.Args[2:], conf)
	case "fetch":
		var fetchCmd = flag.NewFlagSet("fetch", flag.ContinueOnError)
		var initFlag = fetchCmd.Bool("init", false, "Don't send emails, only initialize database of read entries")
		err = fetchCmd.Parse(os.Args[2:])
		if err == nil {
			err = fetchFeeds(conf, *initFlag)
		}
	case "import":
		err = config.ImportCfg(os.Args[2:])
	case "init_cfg":
		break
	case "list":
		err = list(os.Args[2:], conf)
		break
	case "-v":
		fallthrough
	case "--version":
		fmt.Println(version)
	case "-h":
		fallthrough
	case "--help":
		fmt.Println(usage())
	default:
		fmt.Fprintln(os.Stderr, usage())
		conf.Unlock()
		os.Exit(EX_USAGE)
	}<commit_msg>Move fetch command flag processing to standalone function<commit_after>package main

import (
	"errors"
	"flag"
	"fmt"
	"github.com/c-14/grue/config"
	"os"
	"text/tabwriter"
)

const version = "0.2.0-alpha"

func usage() string {
	return `usage: grue [--help] {add|delete|fetch|import|init_cfg|list} ...

Subcommands:
	add <name> <url>
	delete <name>
	fetch [-init]
	import <config>
	init_cfg
	list [name] [--full]`
}

func add(args []string, conf *config.GrueConfig) error {
	if len(args) != 2 {
		return errors.New("usage: grue add <name> <url>")
	}
	var name string = args[0]
	var uri string = args[1]
	return conf.AddAccount(name, uri)
}

func del(args []string, conf *config.GrueConfig) error {
	if len(args) != 1 {
		return errors.New("usage: grue delete <name>")
	}
	name := args[0]
	if err := conf.DeleteAccount(name); err != nil {
		return err
	}
	return DeleteHistory(name)
}

func fetch(args []string, conf *config.GrueConfig) error {
	var fetchCmd = flag.NewFlagSet("fetch", flag.ContinueOnError)
	var initFlag = fetchCmd.Bool("init", false, "Don't send emails, only initialize database of read entries")
	err := fetchCmd.Parse(os.Args[2:])
	if err == nil {
		err = fetchFeeds(conf, *initFlag)
	}
	return err
}

func list(args []string, conf *config.GrueConfig) error {
	const (
		fmtShort = "%s\t%s\n"
		fmtFull  = "%s:\n%s\n"
	)
	var full bool
	var listCmd = flag.NewFlagSet("list", flag.ContinueOnError)
	listCmd.BoolVar(&full, "full", false, "Show full account info")
	if err := listCmd.Parse(args); err != nil {
		return err
	}
	if len(listCmd.Args()) == 0 {
		if full {
			for name, cfg := range conf.Accounts {
				fmt.Printf(fmtFull, name, cfg.String())
			}
			return nil
		}
		w := tabwriter.NewWriter(os.Stdout, 0, 8, 0, '\t', 0)
		for name, cfg := range conf.Accounts {
			fmt.Fprintf(w, fmtShort, name, cfg.URI)
		}
		w.Flush()
		return nil
	}

	name := listCmd.Args()[0]
	if cfg, ok := conf.Accounts[name]; ok {
		if full {
			fmt.Printf(fmtFull, name, cfg.String())
		} else {
			fmt.Printf(fmtShort, name, cfg.URI)
		}
	}
	return nil
}

func main() {
	if len(os.Args) < 2 {
		fmt.Fprintln(os.Stderr, usage())
		os.Exit(EX_USAGE)
	}
	conf, err := config.ReadConfig()
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(EX_TEMPFAIL)
	}
	defer conf.Unlock()
	switch cmd := os.Args[1]; cmd {
	case "add":
		err = add(os.Args[2:], conf)
	case "delete":
		err = del(os.Args[2:], conf)
	case "fetch":
		err = fetch(os.Args[2:], conf)
	case "import":
		err = config.ImportCfg(os.Args[2:])
	case "init_cfg":
		break
	case "list":
		err = list(os.Args[2:], conf)
		break
	case "-v":
		fallthrough
	case "--version":
		fmt.Println(version)
	case "-h":
		fallthrough
	case "--help":
		fmt.Println(usage())
	default:
		fmt.Fprintln(os.Stderr, usage())
		conf.Unlock()
		os.Exit(EX_USAGE)
	}
{'fraction_non_alphanumeric': 0.17165814463111762, 'fraction_numerical': 0.00858290723155588, 'mean_word_length': 3.618043844856661, 'pattern_counts': {'":': 20, '<': 19, '<?xml version=': 0, '>': 17, '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': '1123876', 'n_tokens_mistral': 2294, 'n_tokens_neox': 2172, 'n_words': 536}