<commit_before>package cmd

import (
	"io"
	"os"
	"time"

	"github.com/inconshreveable/log15"
	"github.com/mmcloughlin/pearl"
	"github.com/mmcloughlin/pearl/check"
	"github.com/mmcloughlin/pearl/log"
	"github.com/mmcloughlin/pearl/meta"
	"github.com/mmcloughlin/pearl/telemetry"
	"github.com/mmcloughlin/pearl/telemetry/expvar"
	"github.com/mmcloughlin/pearl/torconfig"
	"github.com/spf13/cobra"
	"github.com/uber-go/tally"
)

// serveCmd represents the serve command
var serveCmd = &cobra.Command{
	Use:   "serve",
	Short: "Start a relay server",
	RunE: func(cmd *cobra.Command, args []string) error {
		return serve()
	},
}
var (
	nickname      string
	port          int
	logfile       string
	telemetryAddr string
)

func init() {
	serveCmd.Flags().StringVarP(&nickname, "nickname", "n", "pearl", "nickname")
	serveCmd.Flags().IntVarP(&port, "port", "p", 9111, "relay port")
	serveCmd.Flags().StringVarP(&logfile, "logfile", "l", "pearl.json", "log file")
	serveCmd.Flags().StringVarP(&telemetryAddr, "telemetry", "t", "localhost:7142", "telemetry address")

	rootCmd.AddCommand(serveCmd)
}

func logger(logfile string) (log.Logger, error) {
	base := log15.New()
	fh, err := log15.FileHandler(logfile, log15.JsonFormat())
	if err != nil {
		return nil, err
	}
	base.SetHandler(log15.MultiHandler(
		log15.LvlFilterHandler(log15.LvlInfo,
			log15.StreamHandler(os.Stdout, log15.TerminalFormat()),
		),
		fh,
	))
	return log.NewLog15(base), nil
}

func metrics() (tally.Scope, io.Closer) {
	return tally.NewRootScope(tally.ScopeOptions{
		Prefix:         "pearl",
		Tags:           map[string]string{},
		CachedReporter: expvar.NewReporter(),
	}, 1*time.Second)
}

func serve() error {
	config := &torconfig.Config{
		Nickname: nickname,
		ORPort:   uint16(port),
		Platform: meta.Platform.String(),
		Contact:  "https://github.com/mmcloughlin/pearl",
	}

	l, err := logger(logfile)
	if err != nil {
		return err
	}

	scope, closer := metrics()
	defer check.Close(l, closer)

	r, err := pearl.NewRouter(config, scope, l)
	if err != nil {
		return err
	}

	// Start telemetry server.
	go telemetry.Serve(telemetryAddr, l)

	// Report runtime metrics
	go telemetry.ReportRuntime(scope, 10*time.Second)

	// Start serving
	go func() {
		if err := r.Serve(); err != nil {
			log.Err(l, err, "router error")
		}
	}()

	authority := "127.0.0.1:7000"
	desc, err := r.Descriptor()
	if err != nil {
		return err
	}
	err = desc.PublishToAuthority(authority)
	if err != nil {
		return err
	}
	l.With("authority", authority).Info("published descriptor")

	select {}
}
<commit_msg>add metrics logging<commit_after>package cmd

import (
	"io"
	"os"
	"time"

	"github.com/inconshreveable/log15"
	"github.com/mmcloughlin/pearl"
	"github.com/mmcloughlin/pearl/check"
	"github.com/mmcloughlin/pearl/log"
	"github.com/mmcloughlin/pearl/meta"
	"github.com/mmcloughlin/pearl/telemetry"
	"github.com/mmcloughlin/pearl/telemetry/expvar"
	"github.com/mmcloughlin/pearl/telemetry/logging"
	"github.com/mmcloughlin/pearl/torconfig"
	"github.com/spf13/cobra"
	"github.com/uber-go/tally"
	"github.com/uber-go/tally/multi"
)

// serveCmd represents the serve command
var serveCmd = &cobra.Command{
	Use:   "serve",
	Short: "Start a relay server",
	RunE: func(cmd *cobra.Command, args []string) error {
		return serve()
	},
}
var (
	nickname      string
	port          int
	logfile       string
	telemetryAddr string
)

func init() {
	serveCmd.Flags().StringVarP(&nickname, "nickname", "n", "pearl", "nickname")
	serveCmd.Flags().IntVarP(&port, "port", "p", 9111, "relay port")
	serveCmd.Flags().StringVarP(&logfile, "logfile", "l", "pearl.json", "log file")
	serveCmd.Flags().StringVarP(&telemetryAddr, "telemetry", "t", "localhost:7142", "telemetry address")

	rootCmd.AddCommand(serveCmd)
}

func logger(logfile string) (log.Logger, error) {
	base := log15.New()
	fh, err := log15.FileHandler(logfile, log15.JsonFormat())
	if err != nil {
		return nil, err
	}
	base.SetHandler(log15.MultiHandler(
		log15.LvlFilterHandler(log15.LvlInfo,
			log15.StreamHandler(os.Stdout, log15.TerminalFormat()),
		),
		fh,
	))
	return log.NewLog15(base), nil
}

func metrics(l log.Logger) (tally.Scope, io.Closer) {
	return tally.NewRootScope(tally.ScopeOptions{
		Prefix: "pearl",
		Tags:   map[string]string{},
		CachedReporter: multi.NewMultiCachedReporter(
			expvar.NewReporter(),
			logging.NewReporter(l),
		),
	}, 1*time.Second)
}

func serve() error {
	config := &torconfig.Config{
		Nickname: nickname,
		ORPort:   uint16(port),
		Platform: meta.Platform.String(),
		Contact:  "https://github.com/mmcloughlin/pearl",
	}

	l, err := logger(logfile)
	if err != nil {
		return err
	}

	scope, closer := metrics(l)
	defer check.Close(l, closer)

	r, err := pearl.NewRouter(config, scope, l)
	if err != nil {
		return err
	}

	// Start telemetry server.
	go telemetry.Serve(telemetryAddr, l)

	// Report runtime metrics
	go telemetry.ReportRuntime(scope, 10*time.Second)

	// Start serving
	go func() {
		if err := r.Serve(); err != nil {
			log.Err(l, err, "router error")
		}
	}()

	authority := "127.0.0.1:7000"
	desc, err := r.Descriptor()
	if err != nil {
		return err
	}
	err = desc.PublishToAuthority(authority)
	if err != nil {
		return err
	}
	l.With("authority", authority).Info("published descriptor")

	select {}
}
<|endoftext|>


<commit_before>package main

import "github.com/modrzew/malusers"

func main() {
	db := malusers.OpenDb()
	manager := &malusers.RankingManager{DB: db}
	manager.RecreateTemporaryRankingTable()
	manager.PopulateTemporaryRankingTable()
	manager.MigrateRankingResults()
}
<commit_msg>More verbose ranking output<commit_after>package main

import (
	"fmt"

	"github.com/modrzew/malusers"
)

func main() {
	db := malusers.OpenDb()
	manager := &malusers.RankingManager{DB: db}
	fmt.Println("Recreating temporary table")
	manager.RecreateTemporaryRankingTable()
	fmt.Println("Populating temporary table")
	manager.PopulateTemporaryRankingTable()
	fmt.Println("Moving temporary to permanent")
	manager.MigrateRankingResults()
	fmt.Println("Done")
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"fmt"
	"os"
	"os/signal"
	"regexp"
	"strings"
	"syscall"
	"time"

	"github.com/gentlemanautomaton/volmgmt/usn"
)

func usage(errmsg string) {
	fmt.Fprintf(os.Stderr, "%s\n\n", errmsg)
	flag.Usage()
	os.Exit(2)
}

func main() {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "usage: %s [-t type[,type...]] [-i regexp] [-e regexp] <volume>\n", os.Args[0])
		flag.PrintDefaults()
	}

	var (
		reasonStr  string
		reason     usn.Reason
		includeStr string
		include    *regexp.Regexp
		excludeStr string
		exclude    *regexp.Regexp
	)

	flag.StringVar(&reasonStr, "t", "*", "journal record types to include (comma-separated)")
	flag.StringVar(&includeStr, "i", "", "regular expression for file match (inclusion)")
	flag.StringVar(&excludeStr, "e", "", "regular expression for file match (exclusion)")
	flag.Parse()

	if flag.NArg() == 0 {
		usage("No volume specified.")
	}
	if flag.NArg() > 1 {
		usage("Only a single volume may be specified.")
	}

	path := flag.Arg(0)

	reason, err := usn.ParseReason(reasonStr)
	if err != nil {
		usage(fmt.Sprintf("%v", err))
	}

	include = compileRegex(includeStr)
	exclude = compileRegex(excludeStr)

	journal, err := usn.NewJournal(path)
	if err != nil {
		fmt.Printf("Unable to create monitor: %v\n", err)
		os.Exit(2)
	}
	defer journal.Close()

	data, err := journal.Query()
	if err != nil {
		fmt.Printf("Unable to access USN Journal: %v\n", err)
		os.Exit(2)
	}

	monitor := journal.Monitor()
	defer monitor.Close()

	feed := monitor.Listen(64) // Register the feed before starting the monitor

	err = monitor.Run(data.NextUSN, time.Millisecond*100, reason)
	if err != nil {
		fmt.Printf("Unable to monitor USN Journal: %v\n", err)
		os.Exit(2)
	}

	done := make(chan struct{})
	go run(feed, include, exclude, done)

	ch := make(chan os.Signal)
	signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)

	select {
	case <-ch:
	case <-done:
	}
}

func run(feed <-chan usn.Record, include, exclude *regexp.Regexp, done chan struct{}) {
	defer close(done)

	for record := range feed {
		if include != nil && !include.MatchString(record.FileName) {
			continue
		}

		if exclude != nil && exclude.MatchString(record.FileName) {
			continue
		}

		action := strings.ToUpper(record.Reason.Join("|", usn.ReasonFormatShort))

		fmt.Printf("%s  %s  %s\n", record.TimeStamp.Format("2006-01-02 15:04:05.000000"), action, record.FileName)
	}
}

func compileRegex(re string) *regexp.Regexp {
	if re == "" {
		return nil
	}

	c, err := regexp.Compile(re)
	if err != nil {
		usage(fmt.Sprintf("Unable to compile regular expression \"%s\": %v\n", re, err))
	}
	return c
}
<commit_msg>usnjournal: Use case-insensitive regex matching<commit_after>package main

import (
	"flag"
	"fmt"
	"os"
	"os/signal"
	"regexp"
	"strings"
	"syscall"
	"time"

	"github.com/gentlemanautomaton/volmgmt/usn"
)

func usage(errmsg string) {
	fmt.Fprintf(os.Stderr, "%s\n\n", errmsg)
	flag.Usage()
	os.Exit(2)
}

func main() {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "usage: %s [-t type[,type...]] [-i regexp] [-e regexp] <volume>\n", os.Args[0])
		flag.PrintDefaults()
	}

	var (
		reasonStr  string
		reason     usn.Reason
		includeStr string
		include    *regexp.Regexp
		excludeStr string
		exclude    *regexp.Regexp
	)

	flag.StringVar(&reasonStr, "t", "*", "journal record types to include (comma-separated)")
	flag.StringVar(&includeStr, "i", "", "regular expression for file match (inclusion)")
	flag.StringVar(&excludeStr, "e", "", "regular expression for file match (exclusion)")
	flag.Parse()

	if flag.NArg() == 0 {
		usage("No volume specified.")
	}
	if flag.NArg() > 1 {
		usage("Only a single volume may be specified.")
	}

	path := flag.Arg(0)

	reason, err := usn.ParseReason(reasonStr)
	if err != nil {
		usage(fmt.Sprintf("%v", err))
	}

	include = compileRegex(includeStr)
	exclude = compileRegex(excludeStr)

	journal, err := usn.NewJournal(path)
	if err != nil {
		fmt.Printf("Unable to create monitor: %v\n", err)
		os.Exit(2)
	}
	defer journal.Close()

	data, err := journal.Query()
	if err != nil {
		fmt.Printf("Unable to access USN Journal: %v\n", err)
		os.Exit(2)
	}

	monitor := journal.Monitor()
	defer monitor.Close()

	feed := monitor.Listen(64) // Register the feed before starting the monitor

	err = monitor.Run(data.NextUSN, time.Millisecond*100, reason)
	if err != nil {
		fmt.Printf("Unable to monitor USN Journal: %v\n", err)
		os.Exit(2)
	}

	done := make(chan struct{})
	go run(feed, include, exclude, done)

	ch := make(chan os.Signal)
	signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)

	select {
	case <-ch:
	case <-done:
	}
}

func run(feed <-chan usn.Record, include, exclude *regexp.Regexp, done chan struct{}) {
	defer close(done)

	for record := range feed {
		if include != nil && !include.MatchString(record.FileName) {
			continue
		}

		if exclude != nil && exclude.MatchString(record.FileName) {
			continue
		}

		action := strings.ToUpper(record.Reason.Join("|", usn.ReasonFormatShort))

		fmt.Printf("%s  %s  %s\n", record.TimeStamp.Format("2006-01-02 15:04:05.000000"), action, record.FileName)
	}
}

func compileRegex(re string) *regexp.Regexp {
	if re == "" {
		return nil
	}

	// Force case-insensitive matching
	if !strings.HasPrefix(re, "(?i)") {
		re = "(?i)" + re
	}

	c, err := regexp.Compile(re)
	if err != nil {
		usage(fmt.Sprintf("Unable to compile regular expression \"%s\": %v\n", re, err))
	}
	return c
}
<|endoftext|>


<commit_before>// GoUblu github.com/jwoehr/goublu
// goublu launches and serves as a better-than-Java console for
// https://github.com/jwoehr/ublu Ublu, a Java-coded domain-specific language
// for remote programming of IBM midrange and mainframe systems.
// Neither this project nor Ublu are associated with IBM.
package main

import (
	"bufio"
	"fmt"
	"github.com/jroimartin/gocui"
	"github.com/jwoehr/goublu"
	"github.com/nsf/termbox-go"
	"io"
	"log"
	"os"
	"os/exec"
	"strings"
	// "unicode/utf8"
)

var commandLineEditor gocui.Editor

// How far from bottom we reserve our input area
const inputLineOffset = 3

// Obligatory layout redraw function
func layout(g *gocui.Gui) error {
	maxX, maxY := g.Size()
	if v, err := g.SetView("ubluout", 0, 0, maxX-1, maxY-inputLineOffset); err != nil {
		if err != gocui.ErrUnknownView {
			return err
		}
		v.Autoscroll = true
		v.Title = "Ublu Output"
	}
	if v, err := g.SetView("ubluin", 0, maxY-inputLineOffset, maxX-1, maxY-1); err != nil {
		if err != gocui.ErrUnknownView {
			return err
		}
		v.Autoscroll = true
		v.Editable = true
		v.Editor = commandLineEditor
		v.Wrap = true
		v.Title = "Ublu Input"
		if _, err := g.SetCurrentView("ubluin"); err != nil {
			return err
		}
	}
	return nil
}

/*
// Exit via the gui instead of via Ublu
func quit(g *gocui.Gui, v *gocui.View) error {
	return gocui.ErrQuit
}
*/

// Pipe input to Ublu
func ubluin(g *gocui.Gui, v *gocui.View, stdin io.WriteCloser, history *goublu.History) {
	var l string
	var err error
	cx, cy := v.Cursor()
	_, gy := g.Size()
	if l, err = v.Line(cy); err != nil {
		l = ""
	}
	l = strings.TrimSpace(l)
	w, _ := g.View("ubluout")
	if l != "" {
		fmt.Fprint(w, "> "+l+"\n")
		io.WriteString(stdin, l+"\n")
	}
	history.Append(l)
	v.Clear()
	v.MoveCursor(0-cx, (gy-inputLineOffset)-cy, false)
}

// Write to console output from Ublu
func ubluout(g *gocui.Gui, text string) {
	v, err := g.View("ubluout")
	if err != nil {
		// handle error
	}
	count := len(text)
	width, _ := g.Size()
	// This isn't right, we'll have to deal with rune width instead
	for i := 0; i < count; i = i + width {
		fmt.Fprint(v, text[i:goublu.Min(count-1, i+width)])
		if i < count-1 {
			fmt.Fprint(v, "\n")
		}
	}
	termbox.Interrupt()
}

func main() {

	history := goublu.NewHistory()

	// Prepare command
	myCmds := []string{"-jar", "/opt/ublu/ublu.jar", "-g", "--"}
	ubluArgs := append(myCmds, os.Args[1:]...)
	cmd := exec.Command("java", ubluArgs...)

	// Pipes
	stdin, _ := cmd.StdinPipe()
	stdout, _ := cmd.StdoutPipe()
	stderr, _ := cmd.StderrPipe()

	defer stdout.Close()
	defer stderr.Close()

	// Readers
	outreader := bufio.NewReader(stdout)
	errreader := bufio.NewReader(stderr)

	// cogui
	g, err := gocui.NewGui(gocui.OutputNormal)
	if err != nil {
		log.Panicln(err)
	} else {
		g.Mouse = true
	}

	// Deliver Ublu's stdout
	go func() {
		for {
			text, _ := outreader.ReadString('\n')
			ubluout(g, text)
		}
	}()

	// Deliver Ublu's stderr
	go func() {
		for {
			text, _ := errreader.ReadString('\n')
			ubluout(g, text)
		}
	}()

	commandLineEditor = gocui.EditorFunc(func(v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier) {
		gx, gy := g.Size()
		cx, cy := v.Cursor()
		text, _ := v.Line(cy)

		// Shut up compiler
		gx = gx
		cy = cy

		switch {
		case ch != 0 && mod == 0:
			v.EditWrite(ch)
		case key == gocui.KeySpace:
			v.EditWrite(' ')
		case key == gocui.KeyBackspace || key == gocui.KeyBackspace2:
			v.EditDelete(true)
		case key == gocui.KeyDelete:
			v.EditDelete(false)
		case key == gocui.KeyInsert:
			v.Overwrite = !v.Overwrite
		case key == gocui.KeyEnter:
			ubluin(g, v, stdin, history)
		case key == gocui.KeyArrowDown:
			v.MoveCursor(0-cx, 0, false)
			v.Clear()
			for _, ch := range history.Forward() {
				v.EditWrite(ch)
			}
		case key == gocui.KeyArrowUp:
			v.MoveCursor(0-cx, 0, false)
			v.Clear()
			for _, ch := range history.Back() {
				v.EditWrite(ch)
			}
		case key == gocui.KeyArrowLeft:
			v.MoveCursor(-1, 0, false)
		case key == gocui.KeyArrowRight:
			v.MoveCursor(1, 0, false)
		case key == gocui.KeyCtrlA:
			v.MoveCursor(0-cx, 0, false)
		case key == gocui.KeyCtrlB:
			v.MoveCursor(-1, 0, false)
		case key == gocui.KeyCtrlE:
			v.MoveCursor(len(text)-cx, 0, false)
		case key == gocui.KeyCtrlF:
			v.MoveCursor(1, 0, false)
		case key == gocui.KeyCtrlK:
			// this isn't quite correct but sorta works
			for i := cy; i < gy; i++ {
				v.EditDelete(false)
			}
		}
	})

	// defer g.Close()

	g.Cursor = true
	g.SetManagerFunc(layout)

	go func() {
		if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
			log.Panicln(err)
		}
	}()

	cmd.Run()

	g.Close()
	fmt.Println("Ublu has exited.")
	fmt.Println("Goodbye from Goublu!")
}
<commit_msg>minor user experience improvement<commit_after>// GoUblu github.com/jwoehr/goublu
// goublu launches and serves as a better-than-Java console for
// https://github.com/jwoehr/ublu Ublu, a Java-coded domain-specific language
// for remote programming of IBM midrange and mainframe systems.
// Neither this project nor Ublu are associated with IBM.
package main

import (
	"bufio"
	"fmt"
	"github.com/jroimartin/gocui"
	"github.com/jwoehr/goublu"
	"github.com/nsf/termbox-go"
	"io"
	"log"
	"os"
	"os/exec"
	"strings"
	// "unicode/utf8"
)

var commandLineEditor gocui.Editor

// How far from bottom we reserve our input area
const inputLineOffset = 3

// Obligatory layout redraw function
func layout(g *gocui.Gui) error {
	maxX, maxY := g.Size()
	if v, err := g.SetView("ubluout", 0, 0, maxX-1, maxY-inputLineOffset); err != nil {
		if err != gocui.ErrUnknownView {
			return err
		}
		v.Autoscroll = true
		v.Title = "Ublu Output"
	}
	if v, err := g.SetView("ubluin", 0, maxY-inputLineOffset, maxX-1, maxY-1); err != nil {
		if err != gocui.ErrUnknownView {
			return err
		}
		v.Autoscroll = true
		v.Editable = true
		v.Editor = commandLineEditor
		v.Wrap = true
		v.Title = "Ublu Input"
		if _, err := g.SetCurrentView("ubluin"); err != nil {
			return err
		}
	}
	return nil
}

/*
// Exit via the gui instead of via Ublu
func quit(g *gocui.Gui, v *gocui.View) error {
	return gocui.ErrQuit
}
*/

// Pipe input to Ublu
func ubluin(g *gocui.Gui, v *gocui.View, stdin io.WriteCloser, history *goublu.History) {
	var l string
	var err error
	cx, cy := v.Cursor()
	_, gy := g.Size()
	if l, err = v.Line(cy); err != nil {
		l = ""
	}
	l = strings.TrimSpace(l)
	w, _ := g.View("ubluout")
	fmt.Fprint(w, "> "+l+"\n")
	io.WriteString(stdin, l+"\n")
	if l != "" {
		history.Append(l)
	}
	v.Clear()
	v.MoveCursor(0-cx, (gy-inputLineOffset)-cy, false)
}

// Write to console output from Ublu
func ubluout(g *gocui.Gui, text string) {
	v, err := g.View("ubluout")
	if err != nil {
		// handle error
	}
	count := len(text)
	width, _ := g.Size()
	// This isn't right, we'll have to deal with rune width instead
	for i := 0; i < count; i = i + width {
		fmt.Fprint(v, text[i:goublu.Min(count-1, i+width)])
		if i < count-1 {
			fmt.Fprint(v, "\n")
		}
	}
	termbox.Interrupt()
}

func main() {

	history := goublu.NewHistory()

	// Prepare command
	myCmds := []string{"-jar", "/opt/ublu/ublu.jar", "-g", "--"}
	ubluArgs := append(myCmds, os.Args[1:]...)
	cmd := exec.Command("java", ubluArgs...)

	// Pipes
	stdin, _ := cmd.StdinPipe()
	stdout, _ := cmd.StdoutPipe()
	stderr, _ := cmd.StderrPipe()

	defer stdout.Close()
	defer stderr.Close()

	// Readers
	outreader := bufio.NewReader(stdout)
	errreader := bufio.NewReader(stderr)

	// cogui
	g, err := gocui.NewGui(gocui.OutputNormal)
	if err != nil {
		log.Panicln(err)
	} else {
		g.Mouse = true
	}

	// Deliver Ublu's stdout
	go func() {
		for {
			text, _ := outreader.ReadString('\n')
			ubluout(g, text)
		}
	}()

	// Deliver Ublu's stderr
	go func() {
		for {
			text, _ := errreader.ReadString('\n')
			ubluout(g, text)
		}
	}()

	commandLineEditor = gocui.EditorFunc(func(v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier) {
		gx, gy := g.Size()
		cx, cy := v.Cursor()
		text, _ := v.Line(cy)

		// Shut up compiler
		gx = gx
		cy = cy

		switch {
		case ch != 0 && mod == 0:
			v.EditWrite(ch)
		case key == gocui.KeySpace:
			v.EditWrite(' ')
		case key == gocui.KeyBackspace || key == gocui.KeyBackspace2:
			v.EditDelete(true)
		case key == gocui.KeyDelete:
			v.EditDelete(false)
		case key == gocui.KeyInsert:
			v.Overwrite = !v.Overwrite
		case key == gocui.KeyEnter:
			ubluin(g, v, stdin, history)
			termbox.Interrupt() // for good luck
		case key == gocui.KeyArrowDown:
			v.MoveCursor(0-cx, 0, false)
			v.Clear()
			for _, ch := range history.Forward() {
				v.EditWrite(ch)
			}
		case key == gocui.KeyArrowUp:
			v.MoveCursor(0-cx, 0, false)
			v.Clear()
			for _, ch := range history.Back() {
				v.EditWrite(ch)
			}
		case key == gocui.KeyArrowLeft:
			v.MoveCursor(-1, 0, false)
		case key == gocui.KeyArrowRight:
			v.MoveCursor(1, 0, false)
		case key == gocui.KeyCtrlA:
			v.MoveCursor(0-cx, 0, false)
		case key == gocui.KeyCtrlB:
			v.MoveCursor(-1, 0, false)
		case key == gocui.KeyCtrlE:
			v.MoveCursor(len(text)-cx, 0, false)
		case key == gocui.KeyCtrlF:
			v.MoveCursor(1, 0, false)
		case key == gocui.KeyCtrlK:
			// this isn't quite correct but sorta works
			for i := cy; i < gy; i++ {
				v.EditDelete(false)
			}
		}
	})

	// defer g.Close()

	g.Cursor = true
	g.SetManagerFunc(layout)

	go func() {
		if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
			log.Panicln(err)
		}
	}()

	cmd.Run()

	g.Close()
	fmt.Println("Ublu has exited.")
	fmt.Println("Goodbye from Goublu!")
}
<|endoftext|>


<commit_before>//
// Copyright 2015 Rakuten Marketing LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package simple_test

import (
	"fmt"
	"time"

	"github.com/mediaFORGE/gol"
	mfmock "github.com/mediaFORGE/gol/internal/mock"
	logger_mock "github.com/mediaFORGE/gol/loggers/mock"
	logger_simple "github.com/mediaFORGE/gol/loggers/simple"
	"github.com/mediaFORGE/gol/manager/simple"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/mock"
	"github.com/stretchr/testify/suite"
)

const (
	// Capacity the number of messages the log message channel can hold.
	Capacity = 10
)

type ManagerTestSuite struct {
	suite.Suite
	manager gol.LoggerManager
}

func (s *ManagerTestSuite) testIsEnabled(n string, b bool, e error) {

	status, err := s.manager.IsEnabled(n)
	if e == nil {
		assert.Equal(s.T(), b, status)
		assert.Nil(s.T(), err)
	} else {
		assert.False(s.T(), status)
		assert.NotNil(s.T(), err)
	}
}

func (s *ManagerTestSuite) SetupTest() {
	s.manager = simple.New(Capacity)
}

func (s *ManagerTestSuite) TestDeregister() {
	// setup
	l := logger_mock.New()
	s.manager.Register("mock", l)

	// deregister
	assert.Nil(s.T(), s.manager.Deregister("mock"))
	assert.Equal(s.T(), []string{}, s.manager.List())

	// inexistent
	assert.NotNil(s.T(), s.manager.Deregister("inexistent"))
}

func (s *ManagerTestSuite) TestDisable() {
	// setup
	l := logger_mock.New()
	s.manager.Register("mock", l)

	// disable
	assert.Nil(s.T(), s.manager.Disable("mock"))
	s.testIsEnabled("mock", false, nil)

	// inexistent
	assert.NotNil(s.T(), s.manager.Disable("inexistent"))
}

func (s *ManagerTestSuite) TestEnable() {
	// setup
	l := logger_mock.New()
	s.manager.Register("mock", l)

	// registered logger is enabled by default
	s.testIsEnabled("mock", true, nil)

	// enable a disabled logger
	s.manager.Disable("mock")
	assert.Nil(s.T(), s.manager.Enable("mock"))
	s.testIsEnabled("mock", true, nil)

	// inexistent
	assert.NotNil(s.T(), s.manager.Enable("inexistent"))
}

func (s *ManagerTestSuite) TestIsEnabled() {
	// setup
	l := logger_mock.New()
	s.manager.Register("mock", l)

	// enabled logger
	s.testIsEnabled("mock", true, nil)

	// disabled logger
	s.manager.Disable("mock")
	s.testIsEnabled("mock", false, nil)

	// inexistent logger
	s.testIsEnabled("inexistent", false, fmt.Errorf("error"))
}

func (s *ManagerTestSuite) TestList() {
	assert.Equal(s.T(), []string{}, s.manager.List())

	l := logger_mock.New()
	assert.Nil(s.T(), s.manager.Register("mock", l))
	assert.Equal(s.T(), []string{"mock"}, s.manager.List())
}

func (s *ManagerTestSuite) TestRegister() {
	l := logger_mock.New()
	assert.Nil(s.T(), s.manager.Register("mock", l))
	assert.Equal(s.T(), []string{"mock"}, s.manager.List())
	s.testIsEnabled("mock", true, nil)

	// duplicate
	assert.Nil(s.T(), s.manager.Register("mock", l))
	assert.Equal(s.T(), []string{"mock"}, s.manager.List())
	s.testIsEnabled("mock", true, nil)

	// nil
	assert.NotNil(s.T(), s.manager.Register("mock", nil))
}

func (s *ManagerTestSuite) TestSend() {
	m := gol.NewEmergency("field", "value")

	// l1 will not filter the message
	mf1 := &mfmock.LogFilter{}
	mf1.On("Filter", m).Return(false)
	mfmt1 := &mfmock.LogFormatter{}
	mfmt1.On("Format", m).Return("EMERGENCY field=value", nil)
	mw1 := &mfmock.Writer{}
	mw1.On("Write", mock.Anything).Return(21, nil)
	l1 := logger_simple.New(mf1, mfmt1, mw1)

	// l2 will filter the message
	mf2 := &mfmock.LogFilter{}
	mf2.On("Filter", m).Return(true)
	mfmt2 := &mfmock.LogFormatter{}
	mw2 := &mfmock.Writer{}
	l2 := logger_simple.New(mf2, mfmt2, mw2)

	s.manager.Register("l1", l1)
	s.manager.Register("l2", l2)

	s.manager.Run()
	assert.Nil(s.T(), s.manager.Send(m))
	time.Sleep(1 * time.Second)

	mf1.AssertExpectations(s.T())
	mfmt1.AssertExpectations(s.T())
	mw1.AssertExpectations(s.T())

	mf2.AssertExpectations(s.T())
	mfmt2.AssertExpectations(s.T())
	mw2.AssertExpectations(s.T())
}

func (s *ManagerTestSuite) TestSendWithoutRun() {
	m := gol.NewEmergency("field", "value")

	assert.Equal(s.T(), s.manager.Send(m), fmt.Errorf("manager.simple.LogManager is not running"))
}
<commit_msg>close manager to wait for all messages to be processed<commit_after>//
// Copyright 2015 Rakuten Marketing LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package simple_test

import (
	"fmt"

	"github.com/mediaFORGE/gol"
	mfmock "github.com/mediaFORGE/gol/internal/mock"
	logger_mock "github.com/mediaFORGE/gol/loggers/mock"
	logger_simple "github.com/mediaFORGE/gol/loggers/simple"
	"github.com/mediaFORGE/gol/manager/simple"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/mock"
	"github.com/stretchr/testify/suite"
)

const (
	// Capacity the number of messages the log message channel can hold.
	Capacity = 10
)

type ManagerTestSuite struct {
	suite.Suite
	manager gol.LoggerManager
}

func (s *ManagerTestSuite) testIsEnabled(n string, b bool, e error) {

	status, err := s.manager.IsEnabled(n)
	if e == nil {
		assert.Equal(s.T(), b, status)
		assert.Nil(s.T(), err)
	} else {
		assert.False(s.T(), status)
		assert.NotNil(s.T(), err)
	}
}

func (s *ManagerTestSuite) SetupTest() {
	s.manager = simple.New(Capacity)
}

func (s *ManagerTestSuite) TeardownTest() {
	s.manager.Close()
}

func (s *ManagerTestSuite) TestDeregister() {
	// setup
	l := logger_mock.New()
	s.manager.Register("mock", l)

	// deregister
	assert.Nil(s.T(), s.manager.Deregister("mock"))
	assert.Equal(s.T(), []string{}, s.manager.List())

	// inexistent
	assert.NotNil(s.T(), s.manager.Deregister("inexistent"))
}

func (s *ManagerTestSuite) TestDisable() {
	// setup
	l := logger_mock.New()
	s.manager.Register("mock", l)

	// disable
	assert.Nil(s.T(), s.manager.Disable("mock"))
	s.testIsEnabled("mock", false, nil)

	// inexistent
	assert.NotNil(s.T(), s.manager.Disable("inexistent"))
}

func (s *ManagerTestSuite) TestEnable() {
	// setup
	l := logger_mock.New()
	s.manager.Register("mock", l)

	// registered logger is enabled by default
	s.testIsEnabled("mock", true, nil)

	// enable a disabled logger
	s.manager.Disable("mock")
	assert.Nil(s.T(), s.manager.Enable("mock"))
	s.testIsEnabled("mock", true, nil)

	// inexistent
	assert.NotNil(s.T(), s.manager.Enable("inexistent"))
}

func (s *ManagerTestSuite) TestIsEnabled() {
	// setup
	l := logger_mock.New()
	s.manager.Register("mock", l)

	// enabled logger
	s.testIsEnabled("mock", true, nil)

	// disabled logger
	s.manager.Disable("mock")
	s.testIsEnabled("mock", false, nil)

	// inexistent logger
	s.testIsEnabled("inexistent", false, fmt.Errorf("error"))
}

func (s *ManagerTestSuite) TestList() {
	assert.Equal(s.T(), []string{}, s.manager.List())

	l := logger_mock.New()
	assert.Nil(s.T(), s.manager.Register("mock", l))
	assert.Equal(s.T(), []string{"mock"}, s.manager.List())
}

func (s *ManagerTestSuite) TestRegister() {
	l := logger_mock.New()
	assert.Nil(s.T(), s.manager.Register("mock", l))
	assert.Equal(s.T(), []string{"mock"}, s.manager.List())
	s.testIsEnabled("mock", true, nil)

	// duplicate
	assert.Nil(s.T(), s.manager.Register("mock", l))
	assert.Equal(s.T(), []string{"mock"}, s.manager.List())
	s.testIsEnabled("mock", true, nil)

	// nil
	assert.NotNil(s.T(), s.manager.Register("mock", nil))
}

func (s *ManagerTestSuite) TestSend() {
	m := gol.NewEmergency("field", "value")

	// l1 will not filter the message
	mf1 := &mfmock.LogFilter{}
	mf1.On("Filter", m).Return(false)
	mfmt1 := &mfmock.LogFormatter{}
	mfmt1.On("Format", m).Return("EMERGENCY field=value", nil)
	mw1 := &mfmock.Writer{}
	mw1.On("Write", mock.Anything).Return(21, nil)
	l1 := logger_simple.New(mf1, mfmt1, mw1)

	// l2 will filter the message
	mf2 := &mfmock.LogFilter{}
	mf2.On("Filter", m).Return(true)
	mfmt2 := &mfmock.LogFormatter{}
	mw2 := &mfmock.Writer{}
	l2 := logger_simple.New(mf2, mfmt2, mw2)

	s.manager.Register("l1", l1)
	s.manager.Register("l2", l2)

	s.manager.Run()
	assert.Nil(s.T(), s.manager.Send(m))
	s.manager.Close()

	mf1.AssertExpectations(s.T())
	mfmt1.AssertExpectations(s.T())
	mw1.AssertExpectations(s.T())

	mf2.AssertExpectations(s.T())
	mfmt2.AssertExpectations(s.T())
	mw2.AssertExpectations(s.T())
}

func (s *ManagerTestSuite) TestSendWithoutRun() {
	m := gol.NewEmergency("field", "value")

	assert.Equal(s.T(), s.manager.Send(m), fmt.Errorf("manager.simple.LogManager is not running"))
}
<|endoftext|>


<commit_before>// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by
// license that can be found in the LICENSE file.

// Package daemon windows version
package daemon

import (
	"errors"
	"fmt"
	"os/exec"
	"syscall"
	"unicode/utf16"
	"unsafe"
)

var ErrWindowsUnsupported = errors.New("Adding as a service failed. Download and place nssm.exe in the path to install this service as an service. NSSM url: https://nssm.cc/")

// windowsRecord - standard record (struct) for windows version of daemon package
type windowsRecord struct {
	name         string
	description  string
	dependencies []string
}

func newDaemon(name, description string, dependencies []string) (Daemon, error) {

	return &windowsRecord{name, description, dependencies}, nil
}

// Install the service
func (windows *windowsRecord) Install(args ...string) (string, error) {
	installAction := "Install " + windows.description + ":"
	adminAccessNecessary := "Administrator access is needed to install a service."

	execp, err := execPath()

	if err != nil {
		return installAction + failed, err
	}

	cmd := exec.Command("nssm.exe", "install", windows.name, execp)
	out, err := cmd.Output()
	if err != nil {
		if len(out) > 0 {
			fmt.Println(string(out))
		} else {
			fmt.Println("No output. Probably service already exists. Try uninstall first.")
		}
		return installAction + failed, err
	}
	if len(out) == 0 {
		return adminAccessNecessary, errors.New(adminAccessNecessary)
	}
	return installAction + " completed.", nil
}

// Remove the service
func (windows *windowsRecord) Remove() (string, error) {
	removeAction := "Removing " + windows.description + ":"
	cmd := exec.Command("nssm.exe", "remove", windows.name)
	err := cmd.Run()
	if err != nil {
		return removeAction + failed, err
	}
	return removeAction + " completed.", nil
}

// Start the service
func (windows *windowsRecord) Start() (string, error) {
	startAction := "Starting " + windows.description + ":"
	cmd := exec.Command("nssm.exe", "start", windows.name)
	err := cmd.Run()
	if err != nil {
		return startAction + failed, err
	}
}

// Stop the service
func (windows *windowsRecord) Stop() (string, error) {
	stopAction := "Stopping " + windows.description + ":"
	cmd := exec.Command("nssm.exe", "stop", windows.name)
	err := cmd.Run()
	if err != nil {
		return stopAction + failed, err
	}
}

// Status - Get service status
func (windows *windowsRecord) Status() (string, error) {
	cmd := exec.Command("nssm.exe", "status", windows.name)
	out, err := cmd.Output()
	if err != nil {
		return "Getting status:" + failed, err
	}
	return "Status: " + string(out), nil
}

// Get executable path
func execPath() (string, error) {
	var n uint32
	b := make([]uint16, syscall.MAX_PATH)
	size := uint32(len(b))

	r0, _, e1 := syscall.MustLoadDLL(
		"kernel32.dll",
	).MustFindProc(
		"GetModuleFileNameW",
	).Call(0, uintptr(unsafe.Pointer(&b[0])), uintptr(size))
	n = uint32(r0)
	if n == 0 {
		return "", e1
	}
	return string(utf16.Decode(b[0:n])), nil
}
<commit_msg>fix go build, missing return at end of function<commit_after>// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by
// license that can be found in the LICENSE file.

// Package daemon windows version
package daemon

import (
	"errors"
	"fmt"
	"os/exec"
	"syscall"
	"unicode/utf16"
	"unsafe"
)

var ErrWindowsUnsupported = errors.New("Adding as a service failed. Download and place nssm.exe in the path to install this service as an service. NSSM url: https://nssm.cc/")

// windowsRecord - standard record (struct) for windows version of daemon package
type windowsRecord struct {
	name         string
	description  string
	dependencies []string
}

func newDaemon(name, description string, dependencies []string) (Daemon, error) {

	return &windowsRecord{name, description, dependencies}, nil
}

// Install the service
func (windows *windowsRecord) Install(args ...string) (string, error) {
	installAction := "Install " + windows.description + ":"
	adminAccessNecessary := "Administrator access is needed to install a service."

	execp, err := execPath()

	if err != nil {
		return installAction + failed, err
	}

	cmd := exec.Command("nssm.exe", "install", windows.name, execp)
	out, err := cmd.Output()
	if err != nil {
		if len(out) > 0 {
			fmt.Println(string(out))
		} else {
			fmt.Println("No output. Probably service already exists. Try uninstall first.")
		}
		return installAction + failed, err
	}
	if len(out) == 0 {
		return adminAccessNecessary, errors.New(adminAccessNecessary)
	}
	return installAction + " completed.", nil
}

// Remove the service
func (windows *windowsRecord) Remove() (string, error) {
	removeAction := "Removing " + windows.description + ":"
	cmd := exec.Command("nssm.exe", "remove", windows.name)
	err := cmd.Run()
	if err != nil {
		return removeAction + failed, err
	}
	return removeAction + " completed.", nil
}

// Start the service
func (windows *windowsRecord) Start() (string, error) {
	startAction := "Starting " + windows.description + ":"
	cmd := exec.Command("nssm.exe", "start", windows.name)
	err := cmd.Run()
	if err != nil {
		return startAction + failed, err
	}
	return startAction + " completed.", nil
}

// Stop the service
func (windows *windowsRecord) Stop() (string, error) {
	stopAction := "Stopping " + windows.description + ":"
	cmd := exec.Command("nssm.exe", "stop", windows.name)
	err := cmd.Run()
	if err != nil {
		return stopAction + failed, err
	}
	return stopAction + " completed.", nil
}

// Status - Get service status
func (windows *windowsRecord) Status() (string, error) {
	cmd := exec.Command("nssm.exe", "status", windows.name)
	out, err := cmd.Output()
	if err != nil {
		return "Getting status:" + failed, err
	}
	return "Status: " + string(out), nil
}

// Get executable path
func execPath() (string, error) {
	var n uint32
	b := make([]uint16, syscall.MAX_PATH)
	size := uint32(len(b))

	r0, _, e1 := syscall.MustLoadDLL(
		"kernel32.dll",
	).MustFindProc(
		"GetModuleFileNameW",
	).Call(0, uintptr(unsafe.Pointer(&b[0])), uintptr(size))
	n = uint32(r0)
	if n == 0 {
		return "", e1
	}
	return string(utf16.Decode(b[0:n])), nil
}
<|endoftext|>


<commit_before>/*
Copyright 2016 The Kubernetes Authors All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package util

import (
	"fmt"
	"github.com/golang/glog"
	federation_v1alpha1 "k8s.io/kubernetes/federation/apis/federation/v1alpha1"
	"k8s.io/kubernetes/pkg/client/restclient"
	client "k8s.io/kubernetes/pkg/client/unversioned"
	"k8s.io/kubernetes/pkg/client/unversioned/clientcmd"
	clientcmdapi "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api"
	utilnet "k8s.io/kubernetes/pkg/util/net"
	"net"
	"os"
)

const (
	KubeAPIQPS              = 20.0
	KubeAPIBurst            = 30
	KubeconfigSecretDataKey = "kubeconfig"
)

func BuildClusterConfig(c *federation_v1alpha1.Cluster) (*restclient.Config, error) {
	var serverAddress string
	var clusterConfig *restclient.Config
	hostIP, err := utilnet.ChooseHostInterface()
	if err != nil {
		return nil, err
	}

	for _, item := range c.Spec.ServerAddressByClientCIDRs {
		_, cidrnet, err := net.ParseCIDR(item.ClientCIDR)
		if err != nil {
			return nil, err
		}
		myaddr := net.ParseIP(hostIP.String())
		if cidrnet.Contains(myaddr) == true {
			serverAddress = item.ServerAddress
			break
		}
	}
	if serverAddress != "" {
		if c.Spec.SecretRef == nil {
			glog.Infof("didnt find secretRef for cluster %s. Trying insecure access", c.Name)
			clusterConfig, err = clientcmd.BuildConfigFromFlags(serverAddress, "")
		} else {
			kubeconfigGetter := KubeconfigGetterForCluster(c)
			clusterConfig, err = clientcmd.BuildConfigFromKubeconfigGetter(serverAddress, kubeconfigGetter)
		}
		if err != nil {
			return nil, err
		}
		clusterConfig.QPS = KubeAPIQPS
		clusterConfig.Burst = KubeAPIBurst
	}
	return clusterConfig, nil
}

// This is to inject a different kubeconfigGetter in tests.
// We dont use the standard one which calls NewInCluster in tests to avoid having to setup service accounts and mount files with secret tokens.
var KubeconfigGetterForCluster = func(c *federation_v1alpha1.Cluster) clientcmd.KubeconfigGetter {
	return func() (*clientcmdapi.Config, error) {
		secretRefName := ""
		if c.Spec.SecretRef != nil {
			secretRefName = c.Spec.SecretRef.Name
		} else {
			glog.Infof("didnt find secretRef for cluster %s. Trying insecure access", c.Name)
		}
		return KubeconfigGetterForSecret(secretRefName)()
	}
}

// KubeconfigGettterForSecret is used to get the kubeconfig from the given secret.
var KubeconfigGetterForSecret = func(secretName string) clientcmd.KubeconfigGetter {
	return func() (*clientcmdapi.Config, error) {
		var data []byte
		if secretName != "" {
			// Get the namespace this is running in from the env variable.
			namespace := os.Getenv("POD_NAMESPACE")
			if namespace == "" {
				return nil, fmt.Errorf("unexpected: POD_NAMESPACE env var returned empty string")
			}
			// Get a client to talk to the k8s apiserver, to fetch secrets from it.
			client, err := client.NewInCluster()
			if err != nil {
				return nil, fmt.Errorf("error in creating in-cluster client: %s", err)
			}
			data = []byte{}
			secret, err := client.Secrets(namespace).Get(secretName)
			if err != nil {
				return nil, fmt.Errorf("error in fetching secret: %s", err)
			}
			ok := false
			data, ok = secret.Data[KubeconfigSecretDataKey]
			if !ok {
				return nil, fmt.Errorf("secret does not have data with key: %s", KubeconfigSecretDataKey)
			}
		}
		return clientcmd.Load(data)
	}
}
<commit_msg>Adding retries to fetching secret in controller manager<commit_after>/*
Copyright 2016 The Kubernetes Authors All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package util

import (
	"fmt"
	"net"
	"os"
	"time"

	"github.com/golang/glog"
	federation_v1alpha1 "k8s.io/kubernetes/federation/apis/federation/v1alpha1"
	"k8s.io/kubernetes/pkg/api"
	"k8s.io/kubernetes/pkg/client/restclient"
	client "k8s.io/kubernetes/pkg/client/unversioned"
	"k8s.io/kubernetes/pkg/client/unversioned/clientcmd"
	clientcmdapi "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api"
	utilnet "k8s.io/kubernetes/pkg/util/net"
	"k8s.io/kubernetes/pkg/util/wait"
)

const (
	KubeAPIQPS              = 20.0
	KubeAPIBurst            = 30
	KubeconfigSecretDataKey = "kubeconfig"
	getSecretTimeout        = 1 * time.Minute
)

func BuildClusterConfig(c *federation_v1alpha1.Cluster) (*restclient.Config, error) {
	var serverAddress string
	var clusterConfig *restclient.Config
	hostIP, err := utilnet.ChooseHostInterface()
	if err != nil {
		return nil, err
	}

	for _, item := range c.Spec.ServerAddressByClientCIDRs {
		_, cidrnet, err := net.ParseCIDR(item.ClientCIDR)
		if err != nil {
			return nil, err
		}
		myaddr := net.ParseIP(hostIP.String())
		if cidrnet.Contains(myaddr) == true {
			serverAddress = item.ServerAddress
			break
		}
	}
	if serverAddress != "" {
		if c.Spec.SecretRef == nil {
			glog.Infof("didnt find secretRef for cluster %s. Trying insecure access", c.Name)
			clusterConfig, err = clientcmd.BuildConfigFromFlags(serverAddress, "")
		} else {
			kubeconfigGetter := KubeconfigGetterForCluster(c)
			clusterConfig, err = clientcmd.BuildConfigFromKubeconfigGetter(serverAddress, kubeconfigGetter)
		}
		if err != nil {
			return nil, err
		}
		clusterConfig.QPS = KubeAPIQPS
		clusterConfig.Burst = KubeAPIBurst
	}
	return clusterConfig, nil
}

// This is to inject a different kubeconfigGetter in tests.
// We dont use the standard one which calls NewInCluster in tests to avoid having to setup service accounts and mount files with secret tokens.
var KubeconfigGetterForCluster = func(c *federation_v1alpha1.Cluster) clientcmd.KubeconfigGetter {
	return func() (*clientcmdapi.Config, error) {
		secretRefName := ""
		if c.Spec.SecretRef != nil {
			secretRefName = c.Spec.SecretRef.Name
		} else {
			glog.Infof("didnt find secretRef for cluster %s. Trying insecure access", c.Name)
		}
		return KubeconfigGetterForSecret(secretRefName)()
	}
}

// KubeconfigGettterForSecret is used to get the kubeconfig from the given secret.
var KubeconfigGetterForSecret = func(secretName string) clientcmd.KubeconfigGetter {
	return func() (*clientcmdapi.Config, error) {
		var data []byte
		if secretName != "" {
			// Get the namespace this is running in from the env variable.
			namespace := os.Getenv("POD_NAMESPACE")
			if namespace == "" {
				return nil, fmt.Errorf("unexpected: POD_NAMESPACE env var returned empty string")
			}
			// Get a client to talk to the k8s apiserver, to fetch secrets from it.
			client, err := client.NewInCluster()
			if err != nil {
				return nil, fmt.Errorf("error in creating in-cluster client: %s", err)
			}
			data = []byte{}
			var secret *api.Secret
			err = wait.PollImmediate(1*time.Second, getSecretTimeout, func() (bool, error) {
				secret, err = client.Secrets(namespace).Get(secretName)
				if err == nil {
					return true, nil
				}
				glog.Warningf("error in fetching secret: %s", err)
				return false, nil
			})
			if err != nil {
				return nil, fmt.Errorf("timed out waiting for secret: %s", err)
			}
			if secret == nil {
				return nil, fmt.Errorf("unexpected: received null secret %s", secretName)
			}
			ok := false
			data, ok = secret.Data[KubeconfigSecretDataKey]
			if !ok {
				return nil, fmt.Errorf("secret does not have data with key: %s", KubeconfigSecretDataKey)
			}
		}
		return clientcmd.Load(data)
	}
}
<|endoftext|>


<commit_before>//go:build linux
// +build linux

/*
Copyright 2017 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cm

import (
	"fmt"
	"strconv"
	"strings"
	"time"

	v1 "k8s.io/api/core/v1"
	"k8s.io/apimachinery/pkg/api/resource"
	"k8s.io/apimachinery/pkg/types"
	utilfeature "k8s.io/apiserver/pkg/util/feature"
	"k8s.io/klog/v2"
	kubefeatures "k8s.io/kubernetes/pkg/features"
	"k8s.io/kubernetes/pkg/kubelet/events"
	"k8s.io/kubernetes/pkg/kubelet/stats/pidlimit"
	kubetypes "k8s.io/kubernetes/pkg/kubelet/types"
)

const (
	defaultNodeAllocatableCgroupName = "kubepods"
)

//createNodeAllocatableCgroups creates Node Allocatable Cgroup when CgroupsPerQOS flag is specified as true
func (cm *containerManagerImpl) createNodeAllocatableCgroups() error {
	nodeAllocatable := cm.internalCapacity
	// Use Node Allocatable limits instead of capacity if the user requested enforcing node allocatable.
	nc := cm.NodeConfig.NodeAllocatableConfig
	if cm.CgroupsPerQOS && nc.EnforceNodeAllocatable.Has(kubetypes.NodeAllocatableEnforcementKey) {
		nodeAllocatable = cm.getNodeAllocatableInternalAbsolute()
	}

	cgroupConfig := &CgroupConfig{
		Name: cm.cgroupRoot,
		// The default limits for cpu shares can be very low which can lead to CPU starvation for pods.
		ResourceParameters: getCgroupConfig(nodeAllocatable),
	}
	if cm.cgroupManager.Exists(cgroupConfig.Name) {
		return nil
	}
	if err := cm.cgroupManager.Create(cgroupConfig); err != nil {
		klog.ErrorS(err, "Failed to create cgroup", "cgroupName", cm.cgroupRoot)
		return err
	}
	return nil
}

// enforceNodeAllocatableCgroups enforce Node Allocatable Cgroup settings.
func (cm *containerManagerImpl) enforceNodeAllocatableCgroups() error {
	nc := cm.NodeConfig.NodeAllocatableConfig

	// We need to update limits on node allocatable cgroup no matter what because
	// default cpu shares on cgroups are low and can cause cpu starvation.
	nodeAllocatable := cm.internalCapacity
	// Use Node Allocatable limits instead of capacity if the user requested enforcing node allocatable.
	if cm.CgroupsPerQOS && nc.EnforceNodeAllocatable.Has(kubetypes.NodeAllocatableEnforcementKey) {
		nodeAllocatable = cm.getNodeAllocatableInternalAbsolute()
	}

	klog.V(4).InfoS("Attempting to enforce Node Allocatable", "config", nc)

	cgroupConfig := &CgroupConfig{
		Name:               cm.cgroupRoot,
		ResourceParameters: getCgroupConfig(nodeAllocatable),
	}

	// Using ObjectReference for events as the node maybe not cached; refer to #42701 for detail.
	nodeRef := &v1.ObjectReference{
		Kind:      "Node",
		Name:      cm.nodeInfo.Name,
		UID:       types.UID(cm.nodeInfo.Name),
		Namespace: "",
	}

	// If Node Allocatable is enforced on a node that has not been drained or is updated on an existing node to a lower value,
	// existing memory usage across pods might be higher than current Node Allocatable Memory Limits.
	// Pod Evictions are expected to bring down memory usage to below Node Allocatable limits.
	// Until evictions happen retry cgroup updates.
	// Update limits on non root cgroup-root to be safe since the default limits for CPU can be too low.
	// Check if cgroupRoot is set to a non-empty value (empty would be the root container)
	if len(cm.cgroupRoot) > 0 {
		go func() {
			for {
				err := cm.cgroupManager.Update(cgroupConfig)
				if err == nil {
					cm.recorder.Event(nodeRef, v1.EventTypeNormal, events.SuccessfulNodeAllocatableEnforcement, "Updated Node Allocatable limit across pods")
					return
				}
				message := fmt.Sprintf("Failed to update Node Allocatable Limits %q: %v", cm.cgroupRoot, err)
				cm.recorder.Event(nodeRef, v1.EventTypeWarning, events.FailedNodeAllocatableEnforcement, message)
				time.Sleep(time.Minute)
			}
		}()
	}
	// Now apply kube reserved and system reserved limits if required.
	if nc.EnforceNodeAllocatable.Has(kubetypes.SystemReservedEnforcementKey) {
		klog.V(2).InfoS("Enforcing system reserved on cgroup", "cgroupName", nc.SystemReservedCgroupName, "limits", nc.SystemReserved)
		if err := enforceExistingCgroup(cm.cgroupManager, cm.cgroupManager.CgroupName(nc.SystemReservedCgroupName), nc.SystemReserved); err != nil {
			message := fmt.Sprintf("Failed to enforce System Reserved Cgroup Limits on %q: %v", nc.SystemReservedCgroupName, err)
			cm.recorder.Event(nodeRef, v1.EventTypeWarning, events.FailedNodeAllocatableEnforcement, message)
			return fmt.Errorf(message)
		}
		cm.recorder.Eventf(nodeRef, v1.EventTypeNormal, events.SuccessfulNodeAllocatableEnforcement, "Updated limits on system reserved cgroup %v", nc.SystemReservedCgroupName)
	}
	if nc.EnforceNodeAllocatable.Has(kubetypes.KubeReservedEnforcementKey) {
		klog.V(2).InfoS("Enforcing kube reserved on cgroup", "cgroupName", nc.KubeReservedCgroupName, "limits", nc.KubeReserved)
		if err := enforceExistingCgroup(cm.cgroupManager, cm.cgroupManager.CgroupName(nc.KubeReservedCgroupName), nc.KubeReserved); err != nil {
			message := fmt.Sprintf("Failed to enforce Kube Reserved Cgroup Limits on %q: %v", nc.KubeReservedCgroupName, err)
			cm.recorder.Event(nodeRef, v1.EventTypeWarning, events.FailedNodeAllocatableEnforcement, message)
			return fmt.Errorf(message)
		}
		cm.recorder.Eventf(nodeRef, v1.EventTypeNormal, events.SuccessfulNodeAllocatableEnforcement, "Updated limits on kube reserved cgroup %v", nc.KubeReservedCgroupName)
	}
	return nil
}

// enforceExistingCgroup updates the limits `rl` on existing cgroup `cName` using `cgroupManager` interface.
func enforceExistingCgroup(cgroupManager CgroupManager, cName CgroupName, rl v1.ResourceList) error {
	rp := getCgroupConfig(rl)

	// Enforce MemoryQoS for cgroups of kube-reserved/system-reserved. For more information,
	// see https://github.com/kubernetes/enhancements/tree/master/keps/sig-node/2570-memory-qos
	if utilfeature.DefaultFeatureGate.Enabled(kubefeatures.MemoryQoS) {
		if rp.Memory != nil {
			if rp.Unified == nil {
				rp.Unified = make(map[string]string)
			}
			rp.Unified[MemoryMin] = strconv.FormatInt(*rp.Memory, 10)
		}
	}

	cgroupConfig := &CgroupConfig{
		Name:               cName,
		ResourceParameters: rp,
	}
	if cgroupConfig.ResourceParameters == nil {
		return fmt.Errorf("%q cgroup is not config properly", cgroupConfig.Name)
	}
	klog.V(4).InfoS("Enforcing limits on cgroup", "cgroupName", cName, "cpuShares", cgroupConfig.ResourceParameters.CpuShares, "memory", cgroupConfig.ResourceParameters.Memory, "pidsLimit", cgroupConfig.ResourceParameters.PidsLimit)
	if !cgroupManager.Exists(cgroupConfig.Name) {
		return fmt.Errorf("%q cgroup does not exist", cgroupConfig.Name)
	}
	if err := cgroupManager.Update(cgroupConfig); err != nil {
		return err
	}
	return nil
}

// getCgroupConfig returns a ResourceConfig object that can be used to create or update cgroups via CgroupManager interface.
func getCgroupConfig(rl v1.ResourceList) *ResourceConfig {
	// TODO(vishh): Set CPU Quota if necessary.
	if rl == nil {
		return nil
	}
	var rc ResourceConfig
	if q, exists := rl[v1.ResourceMemory]; exists {
		// Memory is defined in bytes.
		val := q.Value()
		rc.Memory = &val
	}
	if q, exists := rl[v1.ResourceCPU]; exists {
		// CPU is defined in milli-cores.
		val := MilliCPUToShares(q.MilliValue())
		rc.CpuShares = &val
	}
	if q, exists := rl[pidlimit.PIDs]; exists {
		val := q.Value()
		rc.PidsLimit = &val
	}
	rc.HugePageLimit = HugePageLimits(rl)

	return &rc
}

// GetNodeAllocatableAbsolute returns the absolute value of Node Allocatable which is primarily useful for enforcement.
// Note that not all resources that are available on the node are included in the returned list of resources.
// Returns a ResourceList.
func (cm *containerManagerImpl) GetNodeAllocatableAbsolute() v1.ResourceList {
	return cm.getNodeAllocatableAbsoluteImpl(cm.capacity)
}

func (cm *containerManagerImpl) getNodeAllocatableAbsoluteImpl(capacity v1.ResourceList) v1.ResourceList {
	result := make(v1.ResourceList)
	for k, v := range capacity {
		value := v.DeepCopy()
		if cm.NodeConfig.SystemReserved != nil {
			value.Sub(cm.NodeConfig.SystemReserved[k])
		}
		if cm.NodeConfig.KubeReserved != nil {
			value.Sub(cm.NodeConfig.KubeReserved[k])
		}
		if value.Sign() < 0 {
			// Negative Allocatable resources don't make sense.
			value.Set(0)
		}
		result[k] = value
	}
	return result
}

// getNodeAllocatableInternalAbsolute is similar to getNodeAllocatableAbsolute except that
// it also includes internal resources (currently process IDs).  It is intended for setting
// up top level cgroups only.
func (cm *containerManagerImpl) getNodeAllocatableInternalAbsolute() v1.ResourceList {
	return cm.getNodeAllocatableAbsoluteImpl(cm.internalCapacity)
}

// GetNodeAllocatableReservation returns amount of compute or storage resource that have to be reserved on this node from scheduling.
func (cm *containerManagerImpl) GetNodeAllocatableReservation() v1.ResourceList {
	evictionReservation := hardEvictionReservation(cm.HardEvictionThresholds, cm.capacity)
	result := make(v1.ResourceList)
	for k := range cm.capacity {
		value := resource.NewQuantity(0, resource.DecimalSI)
		if cm.NodeConfig.SystemReserved != nil {
			value.Add(cm.NodeConfig.SystemReserved[k])
		}
		if cm.NodeConfig.KubeReserved != nil {
			value.Add(cm.NodeConfig.KubeReserved[k])
		}
		if evictionReservation != nil {
			value.Add(evictionReservation[k])
		}
		if !value.IsZero() {
			result[k] = *value
		}
	}
	return result
}

// validateNodeAllocatable ensures that the user specified Node Allocatable Configuration doesn't reserve more than the node capacity.
// Returns error if the configuration is invalid, nil otherwise.
func (cm *containerManagerImpl) validateNodeAllocatable() error {
	var errors []string
	nar := cm.GetNodeAllocatableReservation()
	for k, v := range nar {
		value := cm.capacity[k].DeepCopy()
		value.Sub(v)

		if value.Sign() < 0 {
			errors = append(errors, fmt.Sprintf("Resource %q has an allocatable of %v, capacity of %v", k, v, value))
		}
	}

	if len(errors) > 0 {
		return fmt.Errorf("invalid Node Allocatable configuration. %s", strings.Join(errors, " "))
	}
	return nil
}
<commit_msg>pkg/kubelet/cm: fix potential nil dereference in enforceExistingCgroup<commit_after>//go:build linux
// +build linux

/*
Copyright 2017 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cm

import (
	"fmt"
	"strconv"
	"strings"
	"time"

	v1 "k8s.io/api/core/v1"
	"k8s.io/apimachinery/pkg/api/resource"
	"k8s.io/apimachinery/pkg/types"
	utilfeature "k8s.io/apiserver/pkg/util/feature"
	"k8s.io/klog/v2"
	kubefeatures "k8s.io/kubernetes/pkg/features"
	"k8s.io/kubernetes/pkg/kubelet/events"
	"k8s.io/kubernetes/pkg/kubelet/stats/pidlimit"
	kubetypes "k8s.io/kubernetes/pkg/kubelet/types"
)

const (
	defaultNodeAllocatableCgroupName = "kubepods"
)

//createNodeAllocatableCgroups creates Node Allocatable Cgroup when CgroupsPerQOS flag is specified as true
func (cm *containerManagerImpl) createNodeAllocatableCgroups() error {
	nodeAllocatable := cm.internalCapacity
	// Use Node Allocatable limits instead of capacity if the user requested enforcing node allocatable.
	nc := cm.NodeConfig.NodeAllocatableConfig
	if cm.CgroupsPerQOS && nc.EnforceNodeAllocatable.Has(kubetypes.NodeAllocatableEnforcementKey) {
		nodeAllocatable = cm.getNodeAllocatableInternalAbsolute()
	}

	cgroupConfig := &CgroupConfig{
		Name: cm.cgroupRoot,
		// The default limits for cpu shares can be very low which can lead to CPU starvation for pods.
		ResourceParameters: getCgroupConfig(nodeAllocatable),
	}
	if cm.cgroupManager.Exists(cgroupConfig.Name) {
		return nil
	}
	if err := cm.cgroupManager.Create(cgroupConfig); err != nil {
		klog.ErrorS(err, "Failed to create cgroup", "cgroupName", cm.cgroupRoot)
		return err
	}
	return nil
}

// enforceNodeAllocatableCgroups enforce Node Allocatable Cgroup settings.
func (cm *containerManagerImpl) enforceNodeAllocatableCgroups() error {
	nc := cm.NodeConfig.NodeAllocatableConfig

	// We need to update limits on node allocatable cgroup no matter what because
	// default cpu shares on cgroups are low and can cause cpu starvation.
	nodeAllocatable := cm.internalCapacity
	// Use Node Allocatable limits instead of capacity if the user requested enforcing node allocatable.
	if cm.CgroupsPerQOS && nc.EnforceNodeAllocatable.Has(kubetypes.NodeAllocatableEnforcementKey) {
		nodeAllocatable = cm.getNodeAllocatableInternalAbsolute()
	}

	klog.V(4).InfoS("Attempting to enforce Node Allocatable", "config", nc)

	cgroupConfig := &CgroupConfig{
		Name:               cm.cgroupRoot,
		ResourceParameters: getCgroupConfig(nodeAllocatable),
	}

	// Using ObjectReference for events as the node maybe not cached; refer to #42701 for detail.
	nodeRef := &v1.ObjectReference{
		Kind:      "Node",
		Name:      cm.nodeInfo.Name,
		UID:       types.UID(cm.nodeInfo.Name),
		Namespace: "",
	}

	// If Node Allocatable is enforced on a node that has not been drained or is updated on an existing node to a lower value,
	// existing memory usage across pods might be higher than current Node Allocatable Memory Limits.
	// Pod Evictions are expected to bring down memory usage to below Node Allocatable limits.
	// Until evictions happen retry cgroup updates.
	// Update limits on non root cgroup-root to be safe since the default limits for CPU can be too low.
	// Check if cgroupRoot is set to a non-empty value (empty would be the root container)
	if len(cm.cgroupRoot) > 0 {
		go func() {
			for {
				err := cm.cgroupManager.Update(cgroupConfig)
				if err == nil {
					cm.recorder.Event(nodeRef, v1.EventTypeNormal, events.SuccessfulNodeAllocatableEnforcement, "Updated Node Allocatable limit across pods")
					return
				}
				message := fmt.Sprintf("Failed to update Node Allocatable Limits %q: %v", cm.cgroupRoot, err)
				cm.recorder.Event(nodeRef, v1.EventTypeWarning, events.FailedNodeAllocatableEnforcement, message)
				time.Sleep(time.Minute)
			}
		}()
	}
	// Now apply kube reserved and system reserved limits if required.
	if nc.EnforceNodeAllocatable.Has(kubetypes.SystemReservedEnforcementKey) {
		klog.V(2).InfoS("Enforcing system reserved on cgroup", "cgroupName", nc.SystemReservedCgroupName, "limits", nc.SystemReserved)
		if err := enforceExistingCgroup(cm.cgroupManager, cm.cgroupManager.CgroupName(nc.SystemReservedCgroupName), nc.SystemReserved); err != nil {
			message := fmt.Sprintf("Failed to enforce System Reserved Cgroup Limits on %q: %v", nc.SystemReservedCgroupName, err)
			cm.recorder.Event(nodeRef, v1.EventTypeWarning, events.FailedNodeAllocatableEnforcement, message)
			return fmt.Errorf(message)
		}
		cm.recorder.Eventf(nodeRef, v1.EventTypeNormal, events.SuccessfulNodeAllocatableEnforcement, "Updated limits on system reserved cgroup %v", nc.SystemReservedCgroupName)
	}
	if nc.EnforceNodeAllocatable.Has(kubetypes.KubeReservedEnforcementKey) {
		klog.V(2).InfoS("Enforcing kube reserved on cgroup", "cgroupName", nc.KubeReservedCgroupName, "limits", nc.KubeReserved)
		if err := enforceExistingCgroup(cm.cgroupManager, cm.cgroupManager.CgroupName(nc.KubeReservedCgroupName), nc.KubeReserved); err != nil {
			message := fmt.Sprintf("Failed to enforce Kube Reserved Cgroup Limits on %q: %v", nc.KubeReservedCgroupName, err)
			cm.recorder.Event(nodeRef, v1.EventTypeWarning, events.FailedNodeAllocatableEnforcement, message)
			return fmt.Errorf(message)
		}
		cm.recorder.Eventf(nodeRef, v1.EventTypeNormal, events.SuccessfulNodeAllocatableEnforcement, "Updated limits on kube reserved cgroup %v", nc.KubeReservedCgroupName)
	}
	return nil
}

// enforceExistingCgroup updates the limits `rl` on existing cgroup `cName` using `cgroupManager` interface.
func enforceExistingCgroup(cgroupManager CgroupManager, cName CgroupName, rl v1.ResourceList) error {
	rp := getCgroupConfig(rl)
	if rp == nil {
		return fmt.Errorf("%q cgroup is not configured properly", cName)
	}

	// Enforce MemoryQoS for cgroups of kube-reserved/system-reserved. For more information,
	// see https://github.com/kubernetes/enhancements/tree/master/keps/sig-node/2570-memory-qos
	if utilfeature.DefaultFeatureGate.Enabled(kubefeatures.MemoryQoS) {
		if rp.Memory != nil {
			if rp.Unified == nil {
				rp.Unified = make(map[string]string)
			}
			rp.Unified[MemoryMin] = strconv.FormatInt(*rp.Memory, 10)
		}
	}

	cgroupConfig := &CgroupConfig{
		Name:               cName,
		ResourceParameters: rp,
	}
	klog.V(4).InfoS("Enforcing limits on cgroup", "cgroupName", cName, "cpuShares", cgroupConfig.ResourceParameters.CpuShares, "memory", cgroupConfig.ResourceParameters.Memory, "pidsLimit", cgroupConfig.ResourceParameters.PidsLimit)
	if !cgroupManager.Exists(cgroupConfig.Name) {
		return fmt.Errorf("%q cgroup does not exist", cgroupConfig.Name)
	}
	if err := cgroupManager.Update(cgroupConfig); err != nil {
		return err
	}
	return nil
}

// getCgroupConfig returns a ResourceConfig object that can be used to create or update cgroups via CgroupManager interface.
func getCgroupConfig(rl v1.ResourceList) *ResourceConfig {
	// TODO(vishh): Set CPU Quota if necessary.
	if rl == nil {
		return nil
	}
	var rc ResourceConfig
	if q, exists := rl[v1.ResourceMemory]; exists {
		// Memory is defined in bytes.
		val := q.Value()
		rc.Memory = &val
	}
	if q, exists := rl[v1.ResourceCPU]; exists {
		// CPU is defined in milli-cores.
		val := MilliCPUToShares(q.MilliValue())
		rc.CpuShares = &val
	}
	if q, exists := rl[pidlimit.PIDs]; exists {
		val := q.Value()
		rc.PidsLimit = &val
	}
	rc.HugePageLimit = HugePageLimits(rl)

	return &rc
}

// GetNodeAllocatableAbsolute returns the absolute value of Node Allocatable which is primarily useful for enforcement.
// Note that not all resources that are available on the node are included in the returned list of resources.
// Returns a ResourceList.
func (cm *containerManagerImpl) GetNodeAllocatableAbsolute() v1.ResourceList {
	return cm.getNodeAllocatableAbsoluteImpl(cm.capacity)
}

func (cm *containerManagerImpl) getNodeAllocatableAbsoluteImpl(capacity v1.ResourceList) v1.ResourceList {
	result := make(v1.ResourceList)
	for k, v := range capacity {
		value := v.DeepCopy()
		if cm.NodeConfig.SystemReserved != nil {
			value.Sub(cm.NodeConfig.SystemReserved[k])
		}
		if cm.NodeConfig.KubeReserved != nil {
			value.Sub(cm.NodeConfig.KubeReserved[k])
		}
		if value.Sign() < 0 {
			// Negative Allocatable resources don't make sense.
			value.Set(0)
		}
		result[k] = value
	}
	return result
}

// getNodeAllocatableInternalAbsolute is similar to getNodeAllocatableAbsolute except that
// it also includes internal resources (currently process IDs).  It is intended for setting
// up top level cgroups only.
func (cm *containerManagerImpl) getNodeAllocatableInternalAbsolute() v1.ResourceList {
	return cm.getNodeAllocatableAbsoluteImpl(cm.internalCapacity)
}

// GetNodeAllocatableReservation returns amount of compute or storage resource that have to be reserved on this node from scheduling.
func (cm *containerManagerImpl) GetNodeAllocatableReservation() v1.ResourceList {
	evictionReservation := hardEvictionReservation(cm.HardEvictionThresholds, cm.capacity)
	result := make(v1.ResourceList)
	for k := range cm.capacity {
		value := resource.NewQuantity(0, resource.DecimalSI)
		if cm.NodeConfig.SystemReserved != nil {
			value.Add(cm.NodeConfig.SystemReserved[k])
		}
		if cm.NodeConfig.KubeReserved != nil {
			value.Add(cm.NodeConfig.KubeReserved[k])
		}
		if evictionReservation != nil {
			value.Add(evictionReservation[k])
		}
		if !value.IsZero() {
			result[k] = *value
		}
	}
	return result
}

// validateNodeAllocatable ensures that the user specified Node Allocatable Configuration doesn't reserve more than the node capacity.
// Returns error if the configuration is invalid, nil otherwise.
func (cm *containerManagerImpl) validateNodeAllocatable() error {
	var errors []string
	nar := cm.GetNodeAllocatableReservation()
	for k, v := range nar {
		value := cm.capacity[k].DeepCopy()
		value.Sub(v)

		if value.Sign() < 0 {
			errors = append(errors, fmt.Sprintf("Resource %q has an allocatable of %v, capacity of %v", k, v, value))
		}
	}

	if len(errors) > 0 {
		return fmt.Errorf("invalid Node Allocatable configuration. %s", strings.Join(errors, " "))
	}
	return nil
}
<|endoftext|>


<commit_before>/*
Copyright 2019 The Skaffold Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package custom

import (
	"context"
	"path/filepath"
	"testing"

	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
	"github.com/GoogleContainerTools/skaffold/testutil"
)

func TestGetDependenciesDockerfile(t *testing.T) {
	tmpDir, cleanup := testutil.NewTempDir(t)
	defer cleanup()

	// Directory structure:
	//   foo
	//   bar
	// - baz
	//     file
	//   Dockerfile
	tmpDir.Touch("foo", "bar", "baz/file")
	tmpDir.Write("Dockerfile", "FROM scratch \n ARG file \n COPY $file baz/file .")

	customArtifact := &latest.CustomArtifact{
		Dependencies: &latest.CustomDependencies{
			Dockerfile: &latest.DockerfileDependency{
				Path: "Dockerfile",
				BuildArgs: map[string]*string{
					"file": util.StringPtr("foo"),
				},
			},
		},
	}

	expected := []string{"Dockerfile", filepath.FromSlash("baz/file"), "foo"}
	deps, err := GetDependencies(context.Background(), tmpDir.Root(), customArtifact, nil)

	testutil.CheckErrorAndDeepEqual(t, false, err, expected, deps)
}

func TestGetDependenciesCommand(t *testing.T) {
	testutil.Run(t, "", func(t *testutil.T) {
		t.Override(&util.DefaultExecCommand, testutil.CmdRunOut(
			"echo [\"file1\",\"file2\",\"file3\"]",
			"[\"file1\",\"file2\",\"file3\"]",
		))

		customArtifact := &latest.CustomArtifact{
			Dependencies: &latest.CustomDependencies{
				Command: "echo [\"file1\",\"file2\",\"file3\"]",
			},
		}

		expected := []string{"file1", "file2", "file3"}
		deps, err := GetDependencies(context.Background(), "", customArtifact, nil)

		t.CheckNoError(err)
		t.CheckDeepEqual(expected, deps)
	})
}

func TestGetDependenciesPaths(t *testing.T) {
	tests := []struct {
		description string
		ignore      []string
		paths       []string
		expected    []string
	}{
		{
			description: "watch everything",
			paths:       []string{"."},
			expected:    []string{"bar", filepath.FromSlash("baz/file"), "foo"},
		}, {
			description: "watch nothing",
		}, {
			description: "ignore some paths",
			paths:       []string{"."},
			ignore:      []string{"b*"},
			expected:    []string{"foo"},
		},
	}
	for _, test := range tests {
		testutil.Run(t, test.description, func(t *testutil.T) {
			// Directory structure:
			//   foo
			//   bar
			// - baz
			//     file
			tmpDir := t.NewTempDir().
				Touch("foo", "bar", "baz/file")

			deps, err := GetDependencies(context.Background(), tmpDir.Root(), &latest.CustomArtifact{
				Dependencies: &latest.CustomDependencies{
					Paths:  test.paths,
					Ignore: test.ignore,
				},
			}, nil)

			t.CheckNoError(err)
			t.CheckDeepEqual(test.expected, deps)
		})
	}
}
<commit_msg>[custom] Test error case<commit_after>/*
Copyright 2019 The Skaffold Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package custom

import (
	"context"
	"path/filepath"
	"testing"

	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
	"github.com/GoogleContainerTools/skaffold/testutil"
)

func TestGetDependenciesDockerfile(t *testing.T) {
	tmpDir, cleanup := testutil.NewTempDir(t)
	defer cleanup()

	// Directory structure:
	//   foo
	//   bar
	// - baz
	//     file
	//   Dockerfile
	tmpDir.Touch("foo", "bar", "baz/file")
	tmpDir.Write("Dockerfile", "FROM scratch \n ARG file \n COPY $file baz/file .")

	customArtifact := &latest.CustomArtifact{
		Dependencies: &latest.CustomDependencies{
			Dockerfile: &latest.DockerfileDependency{
				Path: "Dockerfile",
				BuildArgs: map[string]*string{
					"file": util.StringPtr("foo"),
				},
			},
		},
	}

	expected := []string{"Dockerfile", filepath.FromSlash("baz/file"), "foo"}
	deps, err := GetDependencies(context.Background(), tmpDir.Root(), customArtifact, nil)

	testutil.CheckErrorAndDeepEqual(t, false, err, expected, deps)
}

func TestGetDependenciesCommand(t *testing.T) {
	testutil.Run(t, "", func(t *testutil.T) {
		t.Override(&util.DefaultExecCommand, testutil.CmdRunOut(
			"echo [\"file1\",\"file2\",\"file3\"]",
			"[\"file1\",\"file2\",\"file3\"]",
		))

		customArtifact := &latest.CustomArtifact{
			Dependencies: &latest.CustomDependencies{
				Command: "echo [\"file1\",\"file2\",\"file3\"]",
			},
		}

		expected := []string{"file1", "file2", "file3"}
		deps, err := GetDependencies(context.Background(), "", customArtifact, nil)

		t.CheckNoError(err)
		t.CheckDeepEqual(expected, deps)
	})
}

func TestGetDependenciesPaths(t *testing.T) {
	tests := []struct {
		description string
		ignore      []string
		paths       []string
		expected    []string
		shouldErr   bool
	}{
		{
			description: "watch everything",
			paths:       []string{"."},
			expected:    []string{"bar", filepath.FromSlash("baz/file"), "foo"},
		}, {
			description: "watch nothing",
		}, {
			description: "ignore some paths",
			paths:       []string{"."},
			ignore:      []string{"b*"},
			expected:    []string{"foo"},
		}, {
			description: "error",
			paths:       []string{"unknown"},
			shouldErr:   true,
		},
	}
	for _, test := range tests {
		testutil.Run(t, test.description, func(t *testutil.T) {
			// Directory structure:
			//   foo
			//   bar
			// - baz
			//     file
			tmpDir := t.NewTempDir().
				Touch("foo", "bar", "baz/file")

			deps, err := GetDependencies(context.Background(), tmpDir.Root(), &latest.CustomArtifact{
				Dependencies: &latest.CustomDependencies{
					Paths:  test.paths,
					Ignore: test.ignore,
				},
			}, nil)

			t.CheckErrorAndDeepEqual(test.shouldErr, err, test.expected, deps)
		})
	}
}
<|endoftext|>


<commit_before>package main

import (
	"database/sql"
	"encoding/json"
	"html/template"
	"log"
	"math/rand"
	"net/http"
	"runtime"
	"sort"
	"strconv"

	_ "github.com/go-sql-driver/mysql"
)

type Message struct {
	Message string `json:"message"`
}

type World struct {
	Id           uint16 `json:"id"`
	RandomNumber uint16 `json:"randomNumber"`
}

type Fortune struct {
	Id      uint16 `json:"id"`
	Message string `json:"message"`
}

const (
	// Database
	connectionString   = "benchmarkdbuser:benchmarkdbpass@tcp(localhost:3306)/hello_world?interpolateParams=true"
	worldSelect        = "SELECT id, randomNumber FROM World WHERE id = ?"
	worldUpdate        = "UPDATE World SET randomNumber = ? WHERE id = ?"
	fortuneSelect      = "SELECT id, message FROM Fortune;"
	worldRowCount      = 10000
	maxConnectionCount = 256

	helloWorldString = "Hello, World!"
)

var (
	// Templates
	tmpl = template.Must(template.ParseFiles("templates/layout.html", "templates/fortune.html"))

	// Database
	db *sql.DB

	helloWorldBytes = []byte(helloWorldString)
)

func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())

	var err error
	db, err = sql.Open("mysql", connectionString)
	if err != nil {
		log.Fatalf("Error opening database: %v", err)
	}
	db.SetMaxIdleConns(maxConnectionCount)

	http.HandleFunc("/db", dbHandler)
	http.HandleFunc("/queries", queriesHandler)
	http.HandleFunc("/json", jsonHandler)
	http.HandleFunc("/fortune", fortuneHandler)
	http.HandleFunc("/update", updateHandler)
	http.HandleFunc("/plaintext", plaintextHandler)
	http.ListenAndServe(":8080", nil)
}

// Test 1: JSON serialization
func jsonHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/javascript")
	json.NewEncoder(w).Encode(&Message{helloWorldString})
}

// Test 2: Single database query
func dbHandler(w http.ResponseWriter, r *http.Request) {
	var world World
	err := db.QueryRow(worldSelect, rand.Intn(worldRowCount)+1).Scan(&world.Id, &world.RandomNumber)
	if err != nil {
		log.Fatalf("Error scanning world row: %s", err.Error())
	}

	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(&world)
}

// Test 3: Multiple database queries
func queriesHandler(w http.ResponseWriter, r *http.Request) {
	n := 1
	if nStr := r.URL.Query().Get("queries"); len(nStr) > 0 {
		n, _ = strconv.Atoi(nStr)
	}

	if n <= 1 {
		dbHandler(w, r)
		return
	}

	world := make([]World, n)
	for i := 0; i < n; i++ {
		err := db.QueryRow(worldSelect, rand.Intn(worldRowCount)+1).Scan(&world[i].Id, &world[i].RandomNumber)
		if err != nil {
			log.Fatalf("Error scanning world row: %s", err.Error())
		}
	}

	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(world)
}

// Test 4: Fortunes
func fortuneHandler(w http.ResponseWriter, r *http.Request) {
	rows, err := db.Query(fortuneSelect)
	if err != nil {
		log.Fatalf("Error preparing statement: %v", err)
	}

	fortunes := make(Fortunes, 0, 16)
	for rows.Next() { //Fetch rows
		fortune := Fortune{}
		if err := rows.Scan(&fortune.Id, &fortune.Message); err != nil {
			log.Fatalf("Error scanning fortune row: %s", err.Error())
		}
		fortunes = append(fortunes, &fortune)
	}
	rows.Close()
	fortunes = append(fortunes, &Fortune{Message: "Additional fortune added at request time."})

	sort.Sort(ByMessage{fortunes})
	w.Header().Set("Content-Type", "text/html")
	if err := tmpl.Execute(w, fortunes); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
}

// Test 5: Database updates
func updateHandler(w http.ResponseWriter, r *http.Request) {
	n := 1
	if nStr := r.URL.Query().Get("queries"); len(nStr) > 0 {
		n, _ = strconv.Atoi(nStr)
	}

	w.Header().Set("Content-Type", "application/json")
	encoder := json.NewEncoder(w)

	if n <= 1 {
		var world World
		db.QueryRow(worldSelect, rand.Intn(worldRowCount)+1).Scan(&world.Id, &world.RandomNumber)
		world.RandomNumber = uint16(rand.Intn(worldRowCount) + 1)
		db.Exec(worldUpdate, world.RandomNumber, world.Id)
		encoder.Encode(&world)
	} else {
		world := make([]World, n)
		for i := 0; i < n; i++ {
			if err := db.QueryRow(worldSelect, rand.Intn(worldRowCount)+1).Scan(&world[i].Id, &world[i].RandomNumber); err != nil {
				log.Fatalf("Error scanning world row: %s", err.Error())
			}
			world[i].RandomNumber = uint16(rand.Intn(worldRowCount) + 1)
			if _, err := db.Exec(worldUpdate, world[i].RandomNumber, world[i].Id); err != nil {
				log.Fatalf("Error updating world row: %s", err.Error())
			}
		}
		encoder.Encode(world)
	}
}

// Test 6: Plaintext
func plaintextHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "text/plain")
	w.Write(helloWorldBytes)
}

type Fortunes []*Fortune

func (s Fortunes) Len() int      { return len(s) }
func (s Fortunes) Swap(i, j int) { s[i], s[j] = s[j], s[i] }

type ByMessage struct{ Fortunes }

func (s ByMessage) Less(i, j int) bool { return s.Fortunes[i].Message < s.Fortunes[j].Message }
<commit_msg>Add comment about performance issue with prepared statement<commit_after>package main

import (
	"database/sql"
	"encoding/json"
	"html/template"
	"log"
	"math/rand"
	"net/http"
	"runtime"
	"sort"
	"strconv"

	_ "github.com/go-sql-driver/mysql"
)

type Message struct {
	Message string `json:"message"`
}

type World struct {
	Id           uint16 `json:"id"`
	RandomNumber uint16 `json:"randomNumber"`
}

type Fortune struct {
	Id      uint16 `json:"id"`
	Message string `json:"message"`
}

// Databases
const (
	// Go 1.4's sql.DB has scalability problem when using (explicitly reused) prepared statement.
	// https://github.com/golang/go/issues/9484
	//
	// Using db.Query() instead of stmt.Query() avoid the issue.
	// But it makes 3 round trips per query: prepare, execute and close.
	// `interpolateParams=true` enables client side parameter interpolation.
	// It reduces round trips without prepared statement.
	//
	// Before Go 1.5 is released, we can see real power of Go with this benchmark.
	// After Go 1.5 is released, we can see prepared statement vs interpolation by comparing
	// this and another lightweight Go framework.
	connectionString   = "benchmarkdbuser:benchmarkdbpass@tcp(localhost:3306)/hello_world?interpolateParams=true"
	worldSelect        = "SELECT id, randomNumber FROM World WHERE id = ?"
	worldUpdate        = "UPDATE World SET randomNumber = ? WHERE id = ?"
	fortuneSelect      = "SELECT id, message FROM Fortune;"
	worldRowCount      = 10000
	maxConnectionCount = 256
)

const helloWorldString = "Hello, World!"

var (
	// Templates
	tmpl = template.Must(template.ParseFiles("templates/layout.html", "templates/fortune.html"))

	// Database
	db *sql.DB

	helloWorldBytes = []byte(helloWorldString)
)

func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())

	var err error
	db, err = sql.Open("mysql", connectionString)
	if err != nil {
		log.Fatalf("Error opening database: %v", err)
	}
	db.SetMaxIdleConns(maxConnectionCount)

	http.HandleFunc("/db", dbHandler)
	http.HandleFunc("/queries", queriesHandler)
	http.HandleFunc("/json", jsonHandler)
	http.HandleFunc("/fortune", fortuneHandler)
	http.HandleFunc("/update", updateHandler)
	http.HandleFunc("/plaintext", plaintextHandler)
	http.ListenAndServe(":8080", nil)
}

// Test 1: JSON serialization
func jsonHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/javascript")
	json.NewEncoder(w).Encode(&Message{helloWorldString})
}

// Test 2: Single database query
func dbHandler(w http.ResponseWriter, r *http.Request) {
	var world World
	err := db.QueryRow(worldSelect, rand.Intn(worldRowCount)+1).Scan(&world.Id, &world.RandomNumber)
	if err != nil {
		log.Fatalf("Error scanning world row: %s", err.Error())
	}

	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(&world)
}

// Test 3: Multiple database queries
func queriesHandler(w http.ResponseWriter, r *http.Request) {
	n := 1
	if nStr := r.URL.Query().Get("queries"); len(nStr) > 0 {
		n, _ = strconv.Atoi(nStr)
	}

	if n <= 1 {
		dbHandler(w, r)
		return
	}

	world := make([]World, n)
	for i := 0; i < n; i++ {
		err := db.QueryRow(worldSelect, rand.Intn(worldRowCount)+1).Scan(&world[i].Id, &world[i].RandomNumber)
		if err != nil {
			log.Fatalf("Error scanning world row: %s", err.Error())
		}
	}

	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(world)
}

// Test 4: Fortunes
func fortuneHandler(w http.ResponseWriter, r *http.Request) {
	rows, err := db.Query(fortuneSelect)
	if err != nil {
		log.Fatalf("Error preparing statement: %v", err)
	}

	fortunes := make(Fortunes, 0, 16)
	for rows.Next() { //Fetch rows
		fortune := Fortune{}
		if err := rows.Scan(&fortune.Id, &fortune.Message); err != nil {
			log.Fatalf("Error scanning fortune row: %s", err.Error())
		}
		fortunes = append(fortunes, &fortune)
	}
	rows.Close()
	fortunes = append(fortunes, &Fortune{Message: "Additional fortune added at request time."})

	sort.Sort(ByMessage{fortunes})
	w.Header().Set("Content-Type", "text/html")
	if err := tmpl.Execute(w, fortunes); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
}

// Test 5: Database updates
func updateHandler(w http.ResponseWriter, r *http.Request) {
	n := 1
	if nStr := r.URL.Query().Get("queries"); len(nStr) > 0 {
		n, _ = strconv.Atoi(nStr)
	}

	w.Header().Set("Content-Type", "application/json")
	encoder := json.NewEncoder(w)

	if n <= 1 {
		var world World
		db.QueryRow(worldSelect, rand.Intn(worldRowCount)+1).Scan(&world.Id, &world.RandomNumber)
		world.RandomNumber = uint16(rand.Intn(worldRowCount) + 1)
		db.Exec(worldUpdate, world.RandomNumber, world.Id)
		encoder.Encode(&world)
	} else {
		world := make([]World, n)
		for i := 0; i < n; i++ {
			if err := db.QueryRow(worldSelect, rand.Intn(worldRowCount)+1).Scan(&world[i].Id, &world[i].RandomNumber); err != nil {
				log.Fatalf("Error scanning world row: %s", err.Error())
			}
			world[i].RandomNumber = uint16(rand.Intn(worldRowCount) + 1)
			if _, err := db.Exec(worldUpdate, world[i].RandomNumber, world[i].Id); err != nil {
				log.Fatalf("Error updating world row: %s", err.Error())
			}
		}
		encoder.Encode(world)
	}
}

// Test 6: Plaintext
func plaintextHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "text/plain")
	w.Write(helloWorldBytes)
}

type Fortunes []*Fortune

func (s Fortunes) Len() int      { return len(s) }
func (s Fortunes) Swap(i, j int) { s[i], s[j] = s[j], s[i] }

type ByMessage struct{ Fortunes }

func (s ByMessage) Less(i, j int) bool { return s.Fortunes[i].Message < s.Fortunes[j].Message }
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"fmt"
	"os"
	"time"
)

func usage() {
	fmt.Fprintln(os.Stderr, `Usage:

sd [options] 'command'

Examples

  echo -e "1\n2\n3\n4\n5" | sd 'echo -e "2\n4"'

  while [ 0 ]; do echo $RANDOM; sleep .1; done | sd -h 1 'seq 500'

  mysql schema_1 -Nsr -e "SELECT city FROM users" | sd -h 120 mysql schema_2 -Nsr -e "SELECT city FROM excluded_cities"

  mysql -Nsr -e "SELECT city FROM users" | sd -p 0 -t 10 kafka_consumer --topic excluded_cities > active_cities.txt

Options

	-f --follow: keeps reading from STDIN until SIGINT (think tail -f).
	-p --patience %seconds%: wait for the specified seconds for the first received line. Use 0 for waiting forever.
	-t --timeout %seconds%: exit(0) after specified seconds from last received line. STDIN and command have independent timeouts. When with -f, timeout only applies to the command (not to STDIN).
	-h --hard-timeout %seconds%: exit(0) after the specified seconds (or earlier). Overrides all other options

`)
	os.Exit(2)
}

func mustResolveOptions() (bool, int, int, int) {
	followHelp := "keeps reading from STDIN until SIGINT (think tail -f)."
	patienceHelp := "wait for the specified seconds for the first received line. Use 0 for waiting forever"
	timeoutHelp := "exit(0) after specified seconds from last received line. STDIN and command have independent timeouts. When with -f, timeout only applies to the command (not to STDIN)."
	hardTimeoutHelp := "exit(0) after the specified seconds (or earlier). Overrides all other options"

	var follow bool
	var patience, timeout, hardTimeout int

	flag.BoolVar(&follow, "follow", false, followHelp)
	flag.BoolVar(&follow, "f", false, followHelp)
	flag.IntVar(&patience, "patience", -1, patienceHelp)
	flag.IntVar(&patience, "p", -1, patienceHelp)
	flag.IntVar(&timeout, "timeout", 10, timeoutHelp)
	flag.IntVar(&timeout, "t", 10, timeoutHelp)
	flag.IntVar(&hardTimeout, "hard-timeout", 0, hardTimeoutHelp)
	flag.IntVar(&hardTimeout, "h", 0, hardTimeoutHelp)

	flag.Usage = usage
	flag.Parse()

	return follow, patience, timeout, hardTimeout
}

func mustResolveTimeouts(follow bool, patience int, timeoutF int, hardTimeout int) (timeout, timeout) {
	var stdinTimeout, cmdTimeout timeout

	if follow {
		stdinTimeout.infinite = true
	}

	if patience == 0 {
		stdinTimeout.firstTimeInfinite = true
		cmdTimeout.firstTimeInfinite = true
	} else if patience == -1 {
		stdinTimeout.firstTime = time.Duration(timeoutF) * time.Second
		cmdTimeout.firstTime = time.Duration(timeoutF) * time.Second
	} else {
		stdinTimeout.firstTime = time.Duration(patience) * time.Second
		cmdTimeout.firstTime = time.Duration(patience) * time.Second
	}

	stdinTimeout.time = time.Duration(timeoutF) * time.Second
	cmdTimeout.time = time.Duration(timeoutF) * time.Second

	if hardTimeout > 0 {
		stdinTimeout.hard = true
		cmdTimeout.hard = true
		stdinTimeout.firstTime = time.Duration(hardTimeout) * time.Second
		cmdTimeout.firstTime = time.Duration(hardTimeout) * time.Second
	}

	return stdinTimeout, cmdTimeout
}
<commit_msg>Adds -i --infinite argument.<commit_after>package main

import (
	"flag"
	"fmt"
	"os"
	"time"
)

func usage() {
	fmt.Fprintln(os.Stderr, `Usage:

sd [options] 'command'

Examples

  echo -e "1\n2\n3\n4\n5" | sd 'echo -e "2\n4"'

  while [ 0 ]; do echo $RANDOM; sleep .1; done | sd -h 1 'seq 500'

  mysql schema_1 -Nsr -e "SELECT city FROM users" | sd -h 120 mysql schema_2 -Nsr -e "SELECT city FROM excluded_cities"

  mysql -Nsr -e "SELECT city FROM users" | sd -p 0 -t 10 kafka_consumer --topic excluded_cities > active_cities.txt

Options

	-f --follow: keeps reading from STDIN until SIGINT or its end.
	-i --infinite: keeps reading from COMMAND until it ends rather than timing it out. Note that if the stream doesn't end, sd just blocks forever and does nothing.
	-p --patience %seconds%: wait for the specified seconds for the first received line. Use 0 for waiting forever.
	-t --timeout %seconds%: exit(0) after specified seconds from last received line. STDIN and command have independent timeouts. When with -f, timeout only applies to the command (not to STDIN).
	-h --hard-timeout %seconds%: exit(0) after the specified seconds (or earlier). Overrides all other options

`)
	os.Exit(2)
}

func mustResolveOptions() (bool, bool, int, int, int) {
	followHelp := "keeps reading from STDIN until SIGINT or its end."
	infiniteHelp := "keeps reading from COMMAND until it ends rather than timing it out. Note that if the stream doesn't end, sd just blocks forever and does nothing."
	patienceHelp := "wait for the specified seconds for the first received line. Use 0 for waiting forever."
	timeoutHelp := "exit(0) after specified seconds from last received line. STDIN and command have independent timeouts. When with -f, timeout only applies to the command (not to STDIN)."
	hardTimeoutHelp := "exit(0) after the specified seconds (or earlier). Overrides all other options."

	var follow, infinite bool
	var patience, timeout, hardTimeout int

	flag.BoolVar(&follow, "follow", false, followHelp)
	flag.BoolVar(&follow, "f", false, followHelp)
	flag.BoolVar(&infinite, "infinite", false, infiniteHelp)
	flag.BoolVar(&infinite, "i", false, infiniteHelp)
	flag.IntVar(&patience, "patience", -1, patienceHelp)
	flag.IntVar(&patience, "p", -1, patienceHelp)
	flag.IntVar(&timeout, "timeout", 10, timeoutHelp)
	flag.IntVar(&timeout, "t", 10, timeoutHelp)
	flag.IntVar(&hardTimeout, "hard-timeout", 0, hardTimeoutHelp)
	flag.IntVar(&hardTimeout, "h", 0, hardTimeoutHelp)

	flag.Usage = usage
	flag.Parse()

	return follow, infinite, patience, timeout, hardTimeout
}

func mustResolveTimeouts(follow bool, infinite bool, patience int, timeoutF int, hardTimeout int) (timeout, timeout) {
	var stdinTimeout, cmdTimeout timeout

	if follow {
		stdinTimeout.infinite = true
	}

	if infinite {
		cmdTimeout.infinite = true
	}

	if patience == 0 {
		stdinTimeout.firstTimeInfinite = true
		cmdTimeout.firstTimeInfinite = true
	} else if patience == -1 {
		stdinTimeout.firstTime = time.Duration(timeoutF) * time.Second
		cmdTimeout.firstTime = time.Duration(timeoutF) * time.Second
	} else {
		stdinTimeout.firstTime = time.Duration(patience) * time.Second
		cmdTimeout.firstTime = time.Duration(patience) * time.Second
	}

	stdinTimeout.time = time.Duration(timeoutF) * time.Second
	cmdTimeout.time = time.Duration(timeoutF) * time.Second

	if hardTimeout > 0 {
		stdinTimeout.hard = true
		cmdTimeout.hard = true
		stdinTimeout.firstTime = time.Duration(hardTimeout) * time.Second
		cmdTimeout.firstTime = time.Duration(hardTimeout) * time.Second
	}

	return stdinTimeout, cmdTimeout
}
<|endoftext|>