limit

package
v0.1.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 7, 2021 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Min float64
	Max float64

	Sum   float64
	Count uint64

	Cmd = &cobra.Command{
		Use:   "limit",
		Short: "read stdin and copy to stdout until one of the limits are reached",
		Long:  "Read stdin and copy to stdout until one of the limits are reached.",
		RunE: func(command *cobra.Command, args []string) (err error) {
			signal.Ignore(syscall.SIGPIPE)
			defer func() {
				if errors.Is(err, syscall.EPIPE) {
					err = nil
				}
			}()

			var minP, maxP, sumP, countP bool

			rf := command.PersistentFlags()

			minP = rf.Changed("min")
			maxP = rf.Changed("max")
			sumP = rf.Changed("sum")
			countP = rf.Changed("count")

			if (minP || maxP || sumP || countP) == false {
				return errors.New("No limits specified. At least one limit type must be applied.")
			}

			scanner := bufio.NewScanner(os.Stdin)

			var sum float64
			var count uint64

			var v float64
			for scanner.Scan() {
				v, err = strconv.ParseFloat(scanner.Text(), 64)
				if err != nil {
					return
				}

				if minP && v < Min {
					break
				}

				if maxP && v > Max {
					break
				}

				if sumP {
					sum += v

					if sum > Sum {
						break
					}
				}

				_, err = fmt.Printf("%g\n", v)
				if err != nil {
					return
				}

				if countP {
					count += 1

					if count >= Count {
						break
					}
				}
			}
			err = scanner.Err()
			if err != nil {
				return
			}

			return
		},
	}
)

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL