~rbn/fit

ref: 277c36d91196048f9a8f73a0a74b014e1e25907d fit/cmd/fit/input.go -rw-r--r-- 651 bytes View raw
277c36d9Ruben Schuller moved fit.go to pack.go 1 year, 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package main

import (
	"bufio"
	"fmt"
	"io"
	"strconv"
	"strings"

	"sr.ht/~rbn/fit"
)

func parseDu(r io.Reader) ([]fit.Item, error) {
	items := []fit.Item{}

	s := bufio.NewScanner(r)
	for s.Scan() {
		fs := strings.SplitN(s.Text(), "\t", 2)
		if len(fs) != 2 {
			return nil, fmt.Errorf("invalid line in input: %v", s.Text())
		}

		size, err := strconv.ParseInt(fs[0], 10, 64)
		if err != nil {
			return nil, fmt.Errorf("invalid size in input: %v", s.Text())
		}

		items = append(items, fit.Item{Size: size, Path: fs[1]})
	}

	if err := s.Err(); err != nil {
		return nil, fmt.Errorf("reading standard input: %v", err)
	}

	return items, nil
}