<commit_before>package main

import (
	"encoding/json"
	"fmt"
	"github.com/xconstruct/go-pushbullet"
	"log"
	"os"
)

type Config struct {
	ApiKey  string   `json:"api_key"`
	Devices []Device `json:"devices"`
}

type Device struct {
	Id   int    `json:"id"`
	Name string `json:"name"`
}

func getArg(i int, fallback string) string {
	if len(os.Args) <= i {
		return ""
	}
	return os.Args[i]
}

func main() {
	cmd := getArg(1, "")

	switch cmd {
	case "login":
		login()
	case "note":
		pushNote()
	default:
		printHelp()
	}
}

func login() {
	key := getArg(2, "")
	var cfg Config

	cfg.ApiKey = key
	cfg.Devices = make([]Device, 0)

	if key == "" {
		writeConfig(cfg)
		return
	}

	pb := pushbullet.New(key)
	devs, err := pb.Devices()
	if err != nil {
		log.Fatalln(err)
	}
	for _, dev := range devs {
		name := dev.Extras.Nickname
		if name == "" {
			name = dev.Extras.Model
		}
		cfg.Devices = append(cfg.Devices, Device{
			Id:   dev.Id,
			Name: name,
		})
	}
	writeConfig(cfg)
}

func readConfig() (Config, error) {
	path := os.Getenv("XDG_CONFIG_HOME") + "/pushb"
	f, err := os.Open(path + "/config.json")
	if err != nil {
		return Config{}, err
	}
	defer func() {
		f.Close()
	}()

	var cfg Config
	dec := json.NewDecoder(f)
	if err = dec.Decode(&cfg); err != nil {
		return Config{}, err
	}
	return cfg, nil
}

func writeConfig(cfg Config) {
	path := os.Getenv("XDG_CONFIG_HOME") + "/pushb"
	f, err := os.OpenFile(path+"/config.json", os.O_WRONLY|os.O_CREATE, 0600)
	if err != nil {
		log.Fatalln(err)
	}
	defer func() {
		f.Close()
	}()

	enc := json.NewEncoder(f)
	if err = enc.Encode(cfg); err != nil {
		log.Fatalln(err)
	}
}

func pushNote() {
	cfg, err := readConfig()
	if err != nil {
		log.Fatalln(err)
	}

	title := getArg(2, "")
	body := getArg(3, "")
	pb := pushbullet.New(cfg.ApiKey)
	err = pb.PushNote(cfg.Devices[0].Id, title, body)
	if err != nil {
		log.Fatalln(err)
	}
}

func printHelp() {
	topic := getArg(2, "")

	switch topic {
	default:
		fmt.Println(`Pushb is a simple client for PushBullet.

Usage:
    pushb command [flags] [arguments]

Commands:
    login      Saves the api key in the config
    devices    Shows a list of registered devices
	help       Shows this help

    address    Pushes an address to a device
    link       Pushes a link to a device
    list       Pushes a list to a device
    note       Pushes a note to a device
	
Use "pushb help [topic] for more information about that topic.`)
	}
}<commit_msg>pushb: fix formatting of help message<commit_after>package main

import (
	"encoding/json"
	"fmt"
	"github.com/xconstruct/go-pushbullet"
	"log"
	"os"
)

type Config struct {
	ApiKey  string   `json:"api_key"`
	Devices []Device `json:"devices"`
}

type Device struct {
	Id   int    `json:"id"`
	Name string `json:"name"`
}

func getArg(i int, fallback string) string {
	if len(os.Args) <= i {
		return ""
	}
	return os.Args[i]
}

func main() {
	cmd := getArg(1, "")

	switch cmd {
	case "login":
		login()
	case "note":
		pushNote()
	default:
		printHelp()
	}
}

func login() {
	key := getArg(2, "")
	var cfg Config

	cfg.ApiKey = key
	cfg.Devices = make([]Device, 0)

	if key == "" {
		writeConfig(cfg)
		return
	}

	pb := pushbullet.New(key)
	devs, err := pb.Devices()
	if err != nil {
		log.Fatalln(err)
	}
	for _, dev := range devs {
		name := dev.Extras.Nickname
		if name == "" {
			name = dev.Extras.Model
		}
		cfg.Devices = append(cfg.Devices, Device{
			Id:   dev.Id,
			Name: name,
		})
	}
	writeConfig(cfg)
}

func readConfig() (Config, error) {
	path := os.Getenv("XDG_CONFIG_HOME") + "/pushb"
	f, err := os.Open(path + "/config.json")
	if err != nil {
		return Config{}, err
	}
	defer func() {
		f.Close()
	}()

	var cfg Config
	dec := json.NewDecoder(f)
	if err = dec.Decode(&cfg); err != nil {
		return Config{}, err
	}
	return cfg, nil
}

func writeConfig(cfg Config) {
	path := os.Getenv("XDG_CONFIG_HOME") + "/pushb"
	f, err := os.OpenFile(path+"/config.json", os.O_WRONLY|os.O_CREATE, 0600)
	if err != nil {
		log.Fatalln(err)
	}
	defer func() {
		f.Close()
	}()

	enc := json.NewEncoder(f)
	if err = enc.Encode(cfg); err != nil {
		log.Fatalln(err)
	}
}

func pushNote() {
	cfg, err := readConfig()
	if err != nil {
		log.Fatalln(err)
	}

	title := getArg(2, "")
	body := getArg(3, "")
	pb := pushbullet.New(cfg.ApiKey)
	err = pb.PushNote(cfg.Devices[0].Id, title, body)
	if err != nil {
		log.Fatalln(err)
	}
}

func printHelp() {
	topic := getArg(2, "")

	switch topic {
	default:
		fmt.Println(`Pushb is a simple client for PushBullet.

Usage:
    pushb command [flags] [arguments]

Commands:
    login      Saves the api key in the config
    devices    Shows a list of registered devices
    help       Shows this help

    address    Pushes an address to a device
    link       Pushes a link to a device
    list       Pushes a list to a device
    note       Pushes a note to a device
	
Use "pushb help [topic] for more information about that topic.`)
	}
}
{'fraction_non_alphanumeric': 0.15361262902246509, 'fraction_numerical': 0.004452539971665654, 'mean_word_length': 2.9951495553759093, 'pattern_counts': {'":': 4, '<': 5, '<?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': '6114004', 'n_tokens_mistral': 2007, 'n_tokens_neox': 1920, 'n_words': 519}