// Copyright 2016-2019 The Libsacloud 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 types

// VPCRouterPlans VPCルータのプラン
var VPCRouterPlans = struct {
	// Standard スタンダードプラン シングル構成/最大スループット 80Mbps/一部機能は利用不可
	Standard ID
	// Premium プレミアムプラン 冗長構成/最大スループット400Mbps
	Premium ID
	// HighSpec ハイスペックプラン 冗長構成/最大スループット1,200Mbps
	HighSpec ID
}{
	Standard: ID(1),
	Premium:  ID(2),
	HighSpec: ID(3),
}

// Copyright 2016-2019 The Libsacloud 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 types

// VPCRouterPlans VPCルータのプラン
var VPCRouterPlans = struct {
	// Standard スタンダードプラン シングル構成/最大スループット 80Mbps/一部機能は利用不可
	Standard ID
	// Premium プレミアムプラン 冗長構成/最大スループット400Mbps
	Premium ID
	// HighSpec ハイスペックプラン 冗長構成/最大スループット1,600Mbps
	HighSpec ID
	// HighSpec ハイスペックプラン 冗長構成/最大スループット4,000Mbps
	HighSpec4000 ID
}{
	Standard:     ID(1),
	Premium:      ID(2),
	HighSpec:     ID(3),
	HighSpec4000: ID(4),
}

Add 4,000Mbps plan to VPCRouter


package model

import (
	"github.com/materials-commons/mcstore/pkg/db/schema"
)

// Groups is a default model for the usergroups table.
var Groups = &rModel{
	schema: schema.Group{},
	table:  "usergroups",
}

// Users is a default model for the users table.
var Users = &rModel{
	schema: schema.User{},
	table:  "users",
}

// Dirs is a default model for the datadirs table.
var Dirs = &rModel{
	schema: schema.Directory{},
	table:  "datadirs",
}

// DirsDenorm is a default model for the denormalized datadirs_denorm table
var DirsDenorm = &rModel{
	schema: schema.DataDirDenorm{},
	table:  "datadirs_denorm",
}

// Files is a default model for the datafiles table
var Files = &rModel{
	schema: schema.File{},
	table:  "datafiles",
}

// Projects is a default model for the projects table
var Projects = &rModel{
	schema: schema.Project{},
	table:  "projects",
}

package model

import (
	"github.com/materials-commons/mcstore/pkg/db/schema"
)

// Groups is a default model for the usergroups table.
var Groups = &rModel{
	schema: schema.Group{},
	table:  "usergroups",
}

// Users is a default model for the users table.
var Users = &rModel{
	schema: schema.User{},
	table:  "users",
}

// Dirs is a default model for the datadirs table.
var Dirs = &rModel{
	schema: schema.Directory{},
	table:  "datadirs",
}

// Files is a default model for the datafiles table
var Files = &rModel{
	schema: schema.File{},
	table:  "datafiles",
}

// Projects is a default model for the projects table
var Projects = &rModel{
	schema: schema.Project{},
	table:  "projects",
}

// Project files
var ProjectFiles = &rModel{
	schema: schema.Project2DataFile{},
	table:  "project2datafile",
}

// Project directories
var ProjectDirs = &rModel{
	schema: schema.Project2DataDir{},
	table:  "project2datadir",
}

// Directory files
var DirFiles = &rModel{
	schema: schema.DataDir2DataFile{},
	table:  "datadir2datafile",
}

Add model definitions for join tables.


package main

import (
	"fmt"
	"os"

	"github.com/Cepave/open-falcon-backend/cmd"
	"github.com/spf13/cobra"
	flag "github.com/spf13/pflag"
)

var versionFlag bool

var RootCmd = &cobra.Command{
	Use: "open-falcon",
}

func init() {
	RootCmd.AddCommand(cmd.Start)
	RootCmd.AddCommand(cmd.Stop)
	RootCmd.AddCommand(cmd.Restart)
	RootCmd.AddCommand(cmd.Check)
	RootCmd.AddCommand(cmd.Monitor)
	RootCmd.AddCommand(cmd.Reload)
	cmd.Start.Flags().BoolVar(&cmd.PreqOrderFlag, "preq-order", false, "start modules in the order of prerequisites")
	cmd.Start.Flags().BoolVar(&cmd.ConsoleOutputFlag, "console-output", false, "print the module's output to the console")
	flag.BoolVarP(&versionFlag, "version", "v", false, "show version")
	flag.Parse()
}

func main() {
	if versionFlag {
		fmt.Printf("Open-Falcon version %s, build %s\n", Version, GitCommit)
		os.Exit(0)
	}
	if err := RootCmd.Execute(); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}

package main

import (
	"fmt"
	"os"

	"github.com/Cepave/open-falcon-backend/cmd"
	"github.com/spf13/cobra"
)

var versionFlag bool

var RootCmd = &cobra.Command{
	Use: "open-falcon",
	Run: func(cmd *cobra.Command, args []string) {
		if versionFlag {
			fmt.Printf("Open-Falcon version %s, build %s\n", Version, GitCommit)
			os.Exit(0)
		}
	},
}

func init() {
	RootCmd.AddCommand(cmd.Start)
	RootCmd.AddCommand(cmd.Stop)
	RootCmd.AddCommand(cmd.Restart)
	RootCmd.AddCommand(cmd.Check)
	RootCmd.AddCommand(cmd.Monitor)
	RootCmd.AddCommand(cmd.Reload)

	RootCmd.Flags().BoolVarP(&versionFlag, "version", "v", false, "show version")
	cmd.Start.Flags().BoolVar(&cmd.PreqOrderFlag, "preq-order", false, "start modules in the order of prerequisites")
	cmd.Start.Flags().BoolVar(&cmd.ConsoleOutputFlag, "console-output", false, "print the module's output to the console")
}

func main() {
	if err := RootCmd.Execute(); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}

Fix the flags of subcommands


package main

import (
	"github.com/albertyw/reaction-pics/server"
	"github.com/albertyw/reaction-pics/tumblr"
	_ "github.com/joho/godotenv/autoload"
	"os"
	"strings"
)

const readPostsFromTumblrEnv = "READ_POSTS_FROM_TUMBLR"

func getReadPostsFromTumblr() bool {
	readPostsEnv := os.Getenv(readPostsFromTumblrEnv)
	if strings.ToLower(readPostsEnv) == "true" {
		return true
	}
	return false
}

func main() {
	readPosts := getReadPostsFromTumblr()
	posts := make(chan tumblr.Post)
	go tumblr.GetPosts(readPosts, posts)
	// Need to split the channel in order for both server.Run and
	// tumblr.WritePostsToCSV to read all posts
	// go tumblr.WritePostsToCSV(posts)
	server.Run(posts)
}

package main

import (
	"github.com/albertyw/reaction-pics/server"
	"github.com/albertyw/reaction-pics/tumblr"
	_ "github.com/joho/godotenv/autoload"
	"os"
	"strings"
)

const readPostsFromTumblrEnv = "READ_POSTS_FROM_TUMBLR"

func getReadPostsFromTumblr() bool {
	readPostsEnv := os.Getenv(readPostsFromTumblrEnv)
	if strings.ToLower(readPostsEnv) == "true" {
		return true
	}
	return false
}

func duplicateChan(in chan tumblr.Post, out1, out2 chan tumblr.Post) {
	for p := range in {
		out1 <- p
		out2 <- p
	}
	close(out1)
	close(out2)
}

func main() {
	readPosts := getReadPostsFromTumblr()
	posts := make(chan tumblr.Post)
	posts1 := make(chan tumblr.Post)
	posts2 := make(chan tumblr.Post)
	go tumblr.GetPosts(readPosts, posts)
	go duplicateChan(posts, posts1, posts2)
	go tumblr.WritePostsToCSV(posts1)
	server.Run(posts2)
}

Split post channel for server and writing to csv


package main

import "github.com/couchbaselabs/sgload/cmd"

func main() {
	cmd.Execute()
}

package main

import (
	"net/http"

	_ "expvar"

	"github.com/couchbaselabs/sgload/cmd"
)

func main() {

	// Expose expvars via http -- needed by mobile-testkit to figure out
	// when the process is finished.
	go func() {
		http.ListenAndServe(":9876", http.DefaultServeMux)
	}()

	cmd.Execute()
}

Add expvar endpoint to all sgload commands


package main

import (
	"github.com/solidfire/solidfire-docker-driver/sfapi"
	"github.com/solidfire/solidfire-docker-driver/sfcli"
	"os"
)

const (
	VERSION = "1.0.0"
)

var (
	client *sfapi.Client
)

func main() {
	cli := sfcli.NewCli(VERSION)
	cli.Run(os.Args)

}

package main

import (
	"github.com/solidfire/solidfire-docker-driver/sfapi"
	"github.com/solidfire/solidfire-docker-driver/sfcli"
	"os"
)

const (
	VERSION = "1.2.0"
)

var (
	client *sfapi.Client
)

func main() {
	cli := sfcli.NewCli(VERSION)
	cli.Run(os.Args)

}

Make the declared version 1.2


package main

import (
	"log"
	"os"

	"go/ast"
	"go/parser"
	"go/token"
)

func validator(name string, s *ast.StructType) {
	log.Print(name)
	for _, fld := range(s.Fields.List) {
		nam := fld.Names[0].Name
		typ := fld.Type.(*ast.Ident)
		log.Printf("%s %s", nam, typ)
	}
}

func main() {
	log.SetFlags(0)

	fs := token.NewFileSet()
	f, err := parser.ParseFile(fs, "-", os.Stdin, 0)
	if err != nil {
		log.Fatal(err)
	}

	for _, obj := range f.Scope.Objects {
		if obj.Kind != ast.Typ { continue }
		ts, ok := obj.Decl.(*ast.TypeSpec)
		if !ok { continue }
		s, ok := ts.Type.(*ast.StructType)
		if !ok { continue }
		if s.Fields == nil {
			log.Fatalf("type %s struct has empty field list %v", ts.Name, ts)
		}
		validator(ts.Name.Name, s)
	}
}

package main

import (
	"log"
	"os"
	"fmt"

	"go/ast"
	"go/parser"
	"go/token"
)

func validator(name string, s *ast.StructType) {
	fmt.Println(name)
	for _, fld := range(s.Fields.List) {
		nam := fld.Names[0].Name
		typ := fld.Type.(*ast.Ident)
		fmt.Printf("%s %s\n", nam, typ)
	}
}

func main() {
	log.SetFlags(0)

	fs := token.NewFileSet()
	f, err := parser.ParseFile(fs, "-", os.Stdin, 0)
	if err != nil {
		log.Fatal(err)
	}

	for _, obj := range f.Scope.Objects {
		if obj.Kind != ast.Typ { continue }
		ts, ok := obj.Decl.(*ast.TypeSpec)
		if !ok { continue }
		s, ok := ts.Type.(*ast.StructType)
		if !ok { continue }
		if s.Fields == nil {
			log.Fatalf("type %s struct has empty field list %v", ts.Name, ts)
		}
		validator(ts.Name.Name, s)
	}
}

Send actual output to standard out instead of error.


/*
 * Copyright (c) 2014 PolyFloyd
 */

package main

import "flag"

const (
	INFO = "PolyFloyd's LEDCube Simulator v0.1"
	UI_DETAIL     int     = 2
	UI_DRAGDIV    float32 = 240.0
	UI_FOVY       float32 = 45.0
	UI_SPACING    float32 = 8.0
	UI_WIN_H      int     = 768
	UI_WIN_W      int     = 1280
	UI_ZFAR       float32 = 640
	UI_ZNEAR      float32 = 1
	UI_ZOOMACCEL  float32 = 12.0
)

var VoxelDisplay *Display

func main() {
	l := flag.String("l", ":54746", "The TCP host and port for incoming connections")
	cx := flag.Int("cx", 16, "The width of the cube")
	cy := flag.Int("cy", 16, "The length of the cube")
	cz := flag.Int("cz", 16, "The height of the cube")
	flag.Parse()

	go StartServer(*l)
	VoxelDisplay = NewDisplay(*cx, *cy, *cz)
	VoxelDisplay.Start()
}

/*
 * Copyright (c) 2014 PolyFloyd
 */

package main

import "flag"

const (
	INFO = "PolyFloyd's LEDCube Simulator v0.1"
	UI_DETAIL     int     = 1
	UI_DRAGDIV    float32 = 240.0
	UI_FOVY       float32 = 45.0
	UI_SPACING    float32 = 8.0
	UI_WIN_H      int     = 768
	UI_WIN_W      int     = 1280
	UI_ZFAR       float32 = 640
	UI_ZNEAR      float32 = 1
	UI_ZOOMACCEL  float32 = 12.0
)

var VoxelDisplay *Display

func main() {
	l := flag.String("l", ":54746", "The TCP host and port for incoming connections")
	cx := flag.Int("cx", 16, "The width of the cube")
	cy := flag.Int("cy", 16, "The length of the cube")
	cz := flag.Int("cz", 16, "The height of the cube")
	flag.Parse()

	go StartServer(*l)
	VoxelDisplay = NewDisplay(*cx, *cy, *cz)
	VoxelDisplay.Start()
}

Decrease detail to increase performance


// Package throttled implements various helpers to manage the lifecycle of goroutines.
package throttled

// WaitGroup limits the number of concurrent goroutines.
type WaitGroup struct {
	throttle    int
	completed   chan bool
	outstanding int
}

// NewWaitGroup instantiates a new WaitGroup with the given throttle.
func NewWaitGroup(throttle int) *WaitGroup {
	return &WaitGroup{
		outstanding: 0,
		throttle:    throttle,
		completed:   make(chan bool, throttle),
	}
}

// Add will block until the number of goroutines being throttled
// has fallen below the throttle
func (w *WaitGroup) Add() {
	if w.outstanding+1 > w.throttle {
		select {
		case <-w.completed:
			w.outstanding--
			return
		}
	}
	w.outstanding++
}

// Done signal that a goroutine has completed
func (w *WaitGroup) Done() {
	w.completed <- true
}

// Wait until all of the throttled goroutines have signaled they are done
func (w *WaitGroup) Wait() {
	if w.outstanding == 0 {
		return
	}
	for w.outstanding > 0 {
		select {
		case <-w.completed:
			w.outstanding--
		}
	}
}

// Package throttled implements various helpers to manage the lifecycle of goroutines.
package throttled

// WaitGroup limits the number of concurrent goroutines that can execute at once.
type WaitGroup struct {
	throttle    int
	completed   chan bool
	outstanding int
}

// NewWaitGroup instantiates a new WaitGroup with the given throttle.
func NewWaitGroup(throttle int) *WaitGroup {
	return &WaitGroup{
		outstanding: 0,
		throttle:    throttle,
		completed:   make(chan bool, throttle),
	}
}

// Add will block until the number of goroutines being throttled
// has fallen below the throttle.
func (w *WaitGroup) Add() {
	w.outstanding++
	if w.outstanding > w.throttle {
		select {
		case <-w.completed:
			w.outstanding--
			return
		}
	}
}

// Done signal that a goroutine has completed.
func (w *WaitGroup) Done() {
	w.completed <- true
}

// Wait until all of the throttled goroutines have signaled they are done.
func (w *WaitGroup) Wait() {
	if w.outstanding == 0 {
		return
	}
	for w.outstanding > 0 {
		select {
		case <-w.completed:
			w.outstanding--
		}
	}
}

Add bugfix - needed to increment outstanding at point of add


// Copyright 2015 The LUCI 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 milo

// ContentTypeAnnotations is a stream content type for annotation streams.
const ContentTypeAnnotations = "text/x-chrome-infra-annotations; version=2"

// ContentTypeSourceManifest is a stream content type for source manifests.
const ContentTypeSourceManifest = "text/x-chrome-infra-source-manifest; version=1"

Add source manifest content type


package main

import (
	"github.com/Sirupsen/logrus"
	"github.com/verath/archipelago/lib"
	"golang.org/x/net/context"
	"os"
	"os/signal"
)

func main() {
	log := logrus.New()
	log.Level = logrus.DebugLevel
	log.Formatter = &logrus.TextFormatter{}

	ctx, halt := context.WithCancel(context.Background())

	// Listen for interrupts
	sigs := make(chan os.Signal, 2)
	signal.Notify(sigs, os.Interrupt, os.Kill)
	go func() {
		<-sigs
		log.WithField("module", "main").Info("Caught interrupt, shutting down")
		halt()
	}()

	archipelago.Run(ctx, log)
	halt()
}

package main

import (
	"github.com/Sirupsen/logrus"
	"github.com/verath/archipelago/lib"
	"os"
	"os/signal"
	"context"
)

func main() {
	log := logrus.New()
	log.Level = logrus.DebugLevel
	log.Formatter = &logrus.TextFormatter{}

	ctx, halt := context.WithCancel(context.Background())

	// Listen for interrupts
	sigs := make(chan os.Signal, 2)
	signal.Notify(sigs, os.Interrupt, os.Kill)
	go func() {
		<-sigs
		log.WithField("module", "main").Info("Caught interrupt, shutting down")
		halt()
	}()

	archipelago.Run(ctx, log)
	halt()
}

Use context from std lib.


package main

import "github.com/waltzofpearls/relay-api/rapi"

func main() {
	api := rapi.New("/v1").
		setListenProtocol("http").
		setListenAddr("localhost:8080").
		setDownstreamProtocol("http").
		setDownstreamAddr("localhost:8094")
	api.NewEndpoint("GET", "/users")
	api.Run()
}

package main

import "github.com/waltzofpearls/relay-api/rapi"

func main() {
	api := rapi.New("/v1").
		setListenAddr("http://localhost:8080").
		setDownstreamAddr("http://localhost:8094")
	api.NewEndpoint("GET", "/users")
	api.Run()
}

Simplify listening address and downstream setters


package main

import (
	log "github.com/Sirupsen/logrus"
	"github.com/prometheus/client_golang/prometheus"
	"github.com/siebenmann/go-kstat"
	"net/http"
	"time"
)

func collectARCstats() {
	log.Debugf("Start collecting ARC stats")
	token, err := kstat.Open()
	if err != nil {
		log.Fatalf("Open failure: %s", err)
	}
	for {
		log.Debugf("Collecting...")
		ks, err := token.Lookup("zfs", 0, "arcstats")
		if err != nil {
			log.Fatalf("lookup failure on %s:0:%s: %s", "zfs", "arcstats", err)
		}
		log.Debugf("Collected: %v", ks)
		time.Sleep(10 * time.Second)
	}
}

func main() {
	log.SetLevel(log.DebugLevel)
	log.Debugf("Starting")
	go collectARCstats()
	http.Handle("/metrics", prometheus.Handler())
	err := http.ListenAndServe("0.0.0.0:9102", nil)
	if err != nil {
		log.Fatal("ListenAndServe: ", err)
	}
}

package main

import (
	log "github.com/Sirupsen/logrus"
	"github.com/prometheus/client_golang/prometheus"
	"github.com/siebenmann/go-kstat"
	"net/http"
	"time"
)

func collectARCstats() {
	log.Debugf("Start collecting ARC stats")
	token, err := kstat.Open()
	if err != nil {
		log.Fatalf("Open failure: %s", err)
	}
	for {
		log.Debugf("Collecting...")
		ks, err := token.Lookup("zfs", 0, "arcstats")
		if err != nil {
			log.Fatalf("lookup failure on %s:0:%s: %s", "zfs", "arcstats", err)
		}
		log.Debugf("Collected: %v", ks)
		n, err := ks.GetNamed("hits")
		if err != nil {
			log.Fatalf("getting '%s' from %s: %s", "hits", ks, err)
		}
		log.Debugf("Hits: %v", n)
		n, err = ks.GetNamed("misses")
		if err != nil {
			log.Fatalf("getting '%s' from %s: %s", "misses", ks, err)
		}
		log.Debugf("Misses: %v", n)
		time.Sleep(10 * time.Second)
	}
}

func main() {
	log.SetLevel(log.DebugLevel)
	log.Debugf("Starting")
	go collectARCstats()
	http.Handle("/metrics", prometheus.Handler())
	err := http.ListenAndServe("0.0.0.0:9102", nil)
	if err != nil {
		log.Fatal("ListenAndServe: ", err)
	}
}

Read hits and misses stats


package main

import (
	"log"
	"os"

	docker "github.com/fsouza/go-dockerclient"
)

func getEnvVar(name, defval string) string {
	val := os.Getenv(name)
	if val == "" {
		val = defval
	}
	return val
}

func main() {
	host := os.Getenv("DOCKER_HOST")
	if host == "" {
		log.Fatalln("Please provide DOCKER_HOST environment variable!")
	}

	client, err := docker.NewClient(host)
	if err != nil {
		log.Fatalln(err)
	}

	gateway := NewGateway(client)
	if gateway.DefaultDomain == "" {
		log.Fatalln("Please provide GW_DOMAIN environment variable!")
	}

	listener := NewListener(client, gateway)
	listener.Init()
	go listener.Start()

	listenHost := getEnvVar("GW_HOST", "0.0.0.0")
	listenPort := getEnvVar("GW_PORT", "2377")

	err = gateway.Start(listenHost + ":" + listenPort)
	if err != nil {
		log.Fatalln(err)
	}
}

package main

import (
	"log"
	"os"

	docker "github.com/fsouza/go-dockerclient"
)

const VERSION = "0.4.0"

func getEnvVar(name, defval string) string {
	val := os.Getenv(name)
	if val == "" {
		val = defval
	}
	return val
}

func main() {
	host := os.Getenv("DOCKER_HOST")
	if host == "" {
		log.Fatalln("Please provide DOCKER_HOST environment variable!")
	}

	client, err := docker.NewClient(host)
	if err != nil {
		log.Fatalln(err)
	}

	gateway := NewGateway(client)
	if gateway.DefaultDomain == "" {
		log.Fatalln("Please provide GW_DOMAIN environment variable!")
	}

	listener := NewListener(client, gateway)
	listener.Init()
	go listener.Start()

	listenHost := getEnvVar("GW_HOST", "0.0.0.0")
	listenPort := getEnvVar("GW_PORT", "2377")

	err = gateway.Start(listenHost + ":" + listenPort)
	if err != nil {
		log.Fatalln(err)
	}
}

Introduce version: 0.4.0, this is a fourth edition so far


package anidb

import (
	"github.com/Kovensky/go-fscache"
	"strconv"
	"strings"
	"time"
)

type MyListStats struct {
	Anime         int
	Episodes      int
	Files         int
	Filesize      int64
	AddedAnime    int
	AddedEpisodes int
	AddedFiles    int
	AddedGroups   int

	Leech             float32 // rate of Files to AddedFiles
	Glory             float32 // meaning undocumented
	ViewedPctDatabase float32
	MyListPctDatabase float32
	AnimePctDatabase  float32 // Only valid if the titles database is loaded
	ViewedPctMyList   float32
	ViewedEpisodes    int
	Votes             int
	Reviews           int

	ViewedTime time.Duration

	Cached time.Time
}

package anidb

import (
	"time"
)

type MyListStats struct {
	Anime         int
	Episodes      int
	Files         int
	Filesize      int64
	AddedAnime    int
	AddedEpisodes int
	AddedFiles    int
	AddedGroups   int

	Leech             float32 // rate of Files to AddedFiles
	Glory             float32 // meaning undocumented
	ViewedPctDatabase float32
	MyListPctDatabase float32
	AnimePctDatabase  float32 // Only valid if the titles database is loaded
	ViewedPctMyList   float32
	ViewedEpisodes    int
	Votes             int
	Reviews           int

	ViewedTime time.Duration

	Cached time.Time
}

Remove unused imports, fixes compilation


package observers

import (
	"github.com/sirupsen/logrus"

	"github.com/ivan1993spb/snake-server/objects/apple"
	"github.com/ivan1993spb/snake-server/world"
)

const chanAppleObserverEventsBuffer = 32

const defaultAppleCount = 1

const oneAppleArea = 50

type AppleObserver struct{}

func (AppleObserver) Observe(stop <-chan struct{}, w *world.World, logger logrus.FieldLogger) {
	appleCount := defaultAppleCount
	size := w.Size()

	if size > oneAppleArea {
		appleCount = int(size / oneAppleArea)
	}

	logger.Debugf("apple count for size %d = %d", size, appleCount)

	for i := 0; i < appleCount; i++ {
		if _, err := apple.NewApple(w); err != nil {
			logger.WithError(err).Error("cannot create apple")
		}
	}

	go func() {
		for event := range w.Events(stop, chanAppleObserverEventsBuffer) {
			if event.Type == world.EventTypeObjectDelete {
				if _, ok := event.Payload.(*apple.Apple); ok {
					if _, err := apple.NewApple(w); err != nil {
						logger.WithError(err).Error("cannot create apple")
					}
				}
			}
		}
	}()
}

package observers

import (
	"github.com/sirupsen/logrus"

	"github.com/ivan1993spb/snake-server/objects/apple"
	"github.com/ivan1993spb/snake-server/world"
)

const chanAppleObserverEventsBuffer = 32

const defaultAppleCount = 1

const oneAppleArea = 50

type AppleObserver struct{}

func (AppleObserver) Observe(stop <-chan struct{}, w *world.World, logger logrus.FieldLogger) {
	go func() {
		appleCount := defaultAppleCount
		size := w.Size()

		if size > oneAppleArea {
			appleCount = int(size / oneAppleArea)
		}

		logger.Debugf("apple count for size %d = %d", size, appleCount)

		for i := 0; i < appleCount; i++ {
			if _, err := apple.NewApple(w); err != nil {
				logger.WithError(err).Error("cannot create apple")
			}
		}

		for event := range w.Events(stop, chanAppleObserverEventsBuffer) {
			if event.Type == world.EventTypeObjectDelete {
				if _, ok := event.Payload.(*apple.Apple); ok {
					if _, err := apple.NewApple(w); err != nil {
						logger.WithError(err).Error("cannot create apple")
					}
				}
			}
		}
	}()
}

Fix apple observer to create apple in starting goroutine


package cartel

import (
	"sync"
)

type Pool struct {
	Input  chan Task
	Output chan OutputValue
	wg     *sync.WaitGroup
}

func (p Pool) End() {
	close(p.Input)
	p.wg.Wait()
}

func (p Pool) Do(t Task) {
	p.Input <- t
}

func (p Pool) GetOutput() []OutputValue {
	values := []OutputValue{}
	for {
		select {
		case r, ok := <-p.Output:
			if ok {
				values = append(values, r)
			} else {
				return values
			}
		default:
			return values
		}
	}
}

func (p Pool) worker() {

	for {
		t, ok := <-p.Input
		if !ok {
			p.wg.Done()
			break
		}
		v := t.Execute()
		p.Output <- v
	}
}

func NewPool(numberOfWorkers int) Pool {

	jobs := make(chan Task, 100)
	results := make(chan OutputValue, 100)

	var wg sync.WaitGroup
	p := Pool{jobs, results, &wg}

	for w := 1; w <= numberOfWorkers; w++ {
		wg.Add(1)
		go p.worker()
	}
	return p
}

package cartel

import (
	"sync"
	"time"
)

type Pool struct {
	Input  chan Task
	Output chan OutputValue
	wg     *sync.WaitGroup
}

func (p Pool) End() {
	close(p.Input)
	p.wg.Wait()
}

func (p Pool) Do(t Task) {
	p.Input <- t
}

func (p Pool) GetOutput() []OutputValue {
	values := []OutputValue{}
	for {
		select {
		case r, ok := <-p.Output:
			if ok {
				values = append(values, r)
			} else {
				return values
			}
		default:
			return values
		}
	}
}

func (p Pool) worker() {
	t := time.Now()
	for {

		since := time.Since(t)

		if since.Minutes() > 5 {
			p.wg.Done()
			p.addWorker()
			break
		}

		t, ok := <-p.Input
		if !ok {
			p.wg.Done()
			break
		}
		v := t.Execute()
		p.Output <- v
	}
}

func (p Pool) addWorker() {
	p.wg.Add(1)
	go p.worker()
}

func NewPool(numberOfWorkers int) Pool {

	jobs := make(chan Task, 100)
	results := make(chan OutputValue, 100)

	var wg sync.WaitGroup
	p := Pool{jobs, results, &wg}

	for w := 1; w <= numberOfWorkers; w++ {
		p.addWorker()
	}
	return p
}

Add time limit for worker threads


package sms

import "encoding/json"

type NexmoSms struct {
	ApiKey    string `json:"api_key"`
	ApiSecret string `json:"api_secret"`
	To        string `json:"to"`
	From      string `json:"from"`
	SmsBody   string `json:"text"`
}

func (sms *NexmoSms) EncodeNexmoSms(apiKey, apiSecret string) ([]byte, error) {
	sms.ApiKey = apiKey
	sms.ApiSecret = apiSecret

	d, err := json.Marshal(&sms)
	if err != nil {
		logger.WithField("error", err.Error()).Error("Could not encode sms as json")
		return nil, err
	}
	return d, nil

}

package sms

import "encoding/json"

type NexmoSms struct {
	ApiKey    string `json:"api_key"`
	ApiSecret string `json:"api_secret"`
	To        string `json:"to"`
	From      string `json:"from"`
	Text      string `json:"text"`
}

func (sms *NexmoSms) EncodeNexmoSms(apiKey, apiSecret string) ([]byte, error) {
	sms.ApiKey = apiKey
	sms.ApiSecret = apiSecret

	d, err := json.Marshal(&sms)
	if err != nil {
		logger.WithField("error", err.Error()).Error("Could not encode sms as json")
		return nil, err
	}
	return d, nil
}

Modify field to match json


package testdb

import (
	"database/sql/driver"
	"io"
)

type rows struct {
	closed  bool
	columns []string
	rows    [][]driver.Value
	pos     int
}

func (rs *rows) clone() *rows {
	if rs == nil {
		return nil
	}

	return &rows{closed: false, columns: rs.columns, rows: rs.rows, pos: 0}
}

func (rs *rows) Next(dest []driver.Value) error {
	rs.pos++
	if rs.pos > len(rs.rows) {
		rs.closed = true
		rs.pos = 0

		return io.EOF // per interface spec
	}

	for i, col := range rs.rows[rs.pos-1] {
		dest[i] = col
	}

	return nil
}

func (rs *rows) Err() error {
	return nil
}

func (rs *rows) Columns() []string {
	return rs.columns
}

func (rs *rows) Close() error {
	return nil
}

package testdb

import (
	"database/sql/driver"
	"io"
)

type rows struct {
	closed  bool
	columns []string
	rows    [][]driver.Value
	pos     int
}

func (rs *rows) clone() *rows {
	if rs == nil {
		return nil
	}

	return &rows{closed: false, columns: rs.columns, rows: rs.rows, pos: 0}
}

func (rs *rows) Next(dest []driver.Value) error {
	rs.pos++
	if rs.pos > len(rs.rows) {
		rs.closed = true

		return io.EOF // per interface spec
	}

	for i, col := range rs.rows[rs.pos-1] {
		dest[i] = col
	}

	return nil
}

func (rs *rows) Err() error {
	return nil
}

func (rs *rows) Columns() []string {
	return rs.columns
}

func (rs *rows) Close() error {
	return nil
}

Remove position resetting from fork after upstream clone solution


package main

import (
	"fmt"
	"io/ioutil"
)

type Page struct {
	Title string
	Body  []byte
}

func (p *Page) save() error {
	filename := p.Title + ".txt"
	return ioutil.WriteFile(filename, p.Body, 0600)
}

func loadPage(title string) (*Page, error) {
	filename := title + ".txt"
	body, err := ioutil.ReadFile(filename)
	if err != nil {
		return nil, err
	}

	return &Page{title, body}, err
}

func main() {
	p1 := &Page{"Test Page", []byte("This is a sample page.")}
	p1.save()
	p2, _ := loadPage("Test Page")
	fmt.Println(string(p2.Body))
}

package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
)

const viewPath = "/view/"

type Page struct {
	Title string
	Body  []byte
}

func (p *Page) save() error {
	filename := p.Title + ".txt"
	return ioutil.WriteFile(filename, p.Body, 0600)
}

func loadPage(title string) (*Page, error) {
	filename := title + ".txt"
	body, err := ioutil.ReadFile(filename)
	if err != nil {
		return nil, err
	}

	return &Page{title, body}, err
}

func viewHandler(w http.ResponseWriter, r *http.Request) {
	title := r.URL.Path[len(viewPath):]
	page, _ := loadPage(title)
	fmt.Fprintf(w, "
%s
%s", page.Title, page.Body)
}

func main() {
	page := &Page{"TestPage", []byte("This is a sample page.")}
	page.save()

	http.HandleFunc(viewPath, viewHandler)
	http.ListenAndServe("localhost:8080", nil)
}

Add a simple view method


// +build go1.16

package compiler

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"strconv"
)

// Version is the GopherJS compiler version string.
const Version = "1.16.0+go1.16.3"

// GoVersion is the current Go 1.x version that GopherJS is compatible with.
const GoVersion = 16

// CheckGoVersion checks the version of the Go distribution
// at goroot, and reports an error if it's not compatible
// with this version of the GopherJS compiler.
func CheckGoVersion(goroot string) error {
	if nvc, err := strconv.ParseBool(os.Getenv("GOPHERJS_SKIP_VERSION_CHECK")); err == nil && nvc {
		return nil
	}
	v, err := ioutil.ReadFile(filepath.Join(goroot, "VERSION"))
	if err != nil {
		return fmt.Errorf("GopherJS %s requires a Go 1.16.x distribution, but failed to read its VERSION file: %v", Version, err)
	}
	if !bytes.HasPrefix(v, []byte("go1.16")) {
		return fmt.Errorf("GopherJS %s requires a Go 1.16.x distribution, but found version %s", Version, v)
	}
	return nil
}

// +build go1.16

package compiler

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"strconv"
)

// Version is the GopherJS compiler version string.
const Version = "1.16.1+go1.16.3"

// GoVersion is the current Go 1.x version that GopherJS is compatible with.
const GoVersion = 16

// CheckGoVersion checks the version of the Go distribution
// at goroot, and reports an error if it's not compatible
// with this version of the GopherJS compiler.
func CheckGoVersion(goroot string) error {
	if nvc, err := strconv.ParseBool(os.Getenv("GOPHERJS_SKIP_VERSION_CHECK")); err == nil && nvc {
		return nil
	}
	v, err := ioutil.ReadFile(filepath.Join(goroot, "VERSION"))
	if err != nil {
		return fmt.Errorf("GopherJS %s requires a Go 1.16.x distribution, but failed to read its VERSION file: %v", Version, err)
	}
	if !bytes.HasPrefix(v, []byte("go1.16")) {
		return fmt.Errorf("GopherJS %s requires a Go 1.16.x distribution, but found version %s", Version, v)
	}
	return nil
}

Increment GopherJS version to 1.16.1.


package models_test

import (
	"encoding/json"
	"github.com/herald-it/goncord/models"
	. "github.com/smartystreets/goconvey/convey"
	"testing"
)

func TestNewDumpTokenModel(t *testing.T) {
	dump_token := &models.DumpToken{}

	Convey("Create new dump token", t, func() {
		So(dump_token, ShouldNotBeNil)
	})
}

func TestJsonDumpTokenModel(t *testing.T) {
	dump_token := models.DumpToken{
		Token: "my_secret_token"}

	const str = `{"token":"my_secret_token"}`
	b, e := json.Marshal(&dump_token)

	Convey("Marshal struct to json", t, func() {
		So(e, ShouldBeNil)
	})

	Convey("Test correct jsonify", t, func() {
		So(string(b), ShouldEqual, str)
	})
}

package models_test

import (
	"encoding/json"
	"github.com/herald-it/goncord/models"
	. "github.com/smartystreets/goconvey/convey"
	"testing"
)

func TestNewDumpTokenModel(t *testing.T) {
	Convey("Create new dump token", t, func() {
		dump_token := &models.DumpToken{}
		So(dump_token, ShouldNotBeNil)
	})
}

func TestJsonDumpTokenModel(t *testing.T) {
	Convey("Model to json format", t, func() {
		dump_token := models.DumpToken{
			Token: "my_secret_token"}

		const str = `{"token":"my_secret_token"}`
		b, e := json.Marshal(&dump_token)

		Convey("Marshal struct to json", func() {
			So(e, ShouldBeNil)
		})

		Convey("Test correct jsonify", func() {
			So(string(b), ShouldEqual, str)
		})
	})
}

Change test fraemwork on Convey.


package db

import (
	"database/sql"
	_ "github.com/go-sql-driver/mysql"
	"github.com/open-falcon/hbs/g"
	"log"
)

var DB *sql.DB

func Init() {
	err := dbInit(g.Config().Database)

	if err != nil {
		log.Fatalf("open db fail: %v", err)
	}

	DB.SetMaxIdleConns(g.Config().MaxIdle)
}

func dbInit(dsn string) (err error) {
	if DB, err = sql.Open("mysql", dsn)
		err != nil {
		return
	}

	if err = DB.Ping()
		err != nil {
		return
	}

	return
}

// Convenient IoC for transaction processing
func inTx(txCallback func(tx *sql.Tx) error) (err error) {
	var tx *sql.Tx

	if tx, err = DB.Begin()
		err != nil {
		return
	}

	/**
	 * The transaction result by whether or not the callback has error
	 */
	defer func() {
		if err == nil {
			tx.Commit()
		} else {
			tx.Rollback()
		}
	}()
	// :~)

	err = txCallback(tx)

	return
}

package db

import (
	"database/sql"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"github.com/open-falcon/hbs/g"
	"log"
)

var DB *sql.DB

func Init() {
	err := dbInit(g.Config().Database)

	if err != nil {
		log.Fatalln(err)
	}

	DB.SetMaxIdleConns(g.Config().MaxIdle)
}

func dbInit(dsn string) (err error) {
	if DB, err = sql.Open("mysql", dsn)
		err != nil {
		return fmt.Errorf("Open DB error: %v", err)
	}

	if err = DB.Ping()
		err != nil {
		return fmt.Errorf("Ping DB error: %v", err)
	}

	return
}

// Convenient IoC for transaction processing
func inTx(txCallback func(tx *sql.Tx) error) (err error) {
	var tx *sql.Tx

	if tx, err = DB.Begin()
		err != nil {
		return
	}

	/**
	 * The transaction result by whether or not the callback has error
	 */
	defer func() {
		if err == nil {
			tx.Commit()
		} else {
			tx.Rollback()
		}
	}()
	// :~)

	err = txCallback(tx)

	return
}

Fix ambiguous message while connecting to database


package imgscale

import (
	"github.com/stretchr/testify/assert"
	"testing"
	"net/http"
	"net/http/httptest"
	"fmt"
)

func TestHttpFetchOK(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		http.ServeFile(w, r, "../data/kth.jpg")
	}))
	defer ts.Close()
	fmt.Println(ts.URL)
	provider := NewImageProviderHTTP("")
	img, err := provider.Fetch(ts.URL)
	defer img.Destroy()

	assert.Nil(t, err)
	assert.Equal(t, img.GetImageWidth(), 320)
	assert.Equal(t, img.GetImageHeight(), 240)
}

package imgscale

import (
	"github.com/stretchr/testify/assert"
	"testing"
	"net/http"
	"net/http/httptest"
)

func TestHttpFetchOK(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		http.ServeFile(w, r, "../data/kth.jpg")
	}))
	defer ts.Close()

	provider := NewImageProviderHTTP("")
	img, err := provider.Fetch(ts.URL)
	defer img.Destroy()

	assert.Nil(t, err)
	assert.Equal(t, img.GetImageWidth(), 320)
	assert.Equal(t, img.GetImageHeight(), 240)
}

func TestHttpFetchOKBaseUrl(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		http.ServeFile(w, r, "../data/kth.jpg")
	}))
	defer ts.Close()

	provider := NewImageProviderHTTP(ts.URL)
	img, err := provider.Fetch("kth.jpg")
	defer img.Destroy()

	assert.Nil(t, err)
	assert.Equal(t, img.GetImageWidth(), 320)
	assert.Equal(t, img.GetImageHeight(), 240)
}

Test case with none empty base url


package provision

import (
	"github.com/docker/machine/libmachine/drivers"
	"github.com/docker/machine/libmachine/ssh"
)

type RedHatSSHCommander struct {
	Driver drivers.Driver
}

func (sshCmder RedHatSSHCommander) SSHCommand(args string) (string, error) {
	client, err := drivers.GetSSHClientFromDriver(sshCmder.Driver)
	if err != nil {
		return "", err
	}

	// redhat needs "-t" for tty allocation on ssh therefore we check for the
	// external client and add as needed.
	// Note: CentOS 7.0 needs multiple "-tt" to force tty allocation when ssh has
	// no local tty.
	switch c := client.(type) {
	case *ssh.ExternalClient:
		c.BaseArgs = append(c.BaseArgs, "-tt")
		client = c
	case *ssh.NativeClient:
		return c.OutputWithPty(args)
	}

	return client.Output(args)
}

package provision

import (
	"fmt"

	"github.com/docker/machine/libmachine/drivers"
	"github.com/docker/machine/libmachine/log"
	"github.com/docker/machine/libmachine/ssh"
)

type RedHatSSHCommander struct {
	Driver drivers.Driver
}

func (sshCmder RedHatSSHCommander) SSHCommand(args string) (string, error) {
	client, err := drivers.GetSSHClientFromDriver(sshCmder.Driver)
	if err != nil {
		return "", err
	}

	log.Debugf("About to run SSH command:\n%s", args)

	// redhat needs "-t" for tty allocation on ssh therefore we check for the
	// external client and add as needed.
	// Note: CentOS 7.0 needs multiple "-tt" to force tty allocation when ssh has
	// no local tty.
	var output string
	switch c := client.(type) {
	case *ssh.ExternalClient:
		c.BaseArgs = append(c.BaseArgs, "-tt")
		output, err = c.Output(args)
	case *ssh.NativeClient:
		output, err = c.OutputWithPty(args)
	}

	log.Debugf("SSH cmd err, output: %v: %s", err, output)
	if err != nil {
		return "", fmt.Errorf(`Something went wrong running an SSH command!
command : %s
err     : %v
output  : %s
`, args, err, output)
	}

	return output, nil
}

Add SSH logs when provisioning with RedHat derivatives


package main

import (
	"bufio"
	"io"
)

type P1 struct {
	rd *bufio.Reader
}

// NewReader returns a Reader whose read telegrams from a Reader object.
func NewP1(rd io.Reader) P1 {
	return P1{rd: bufio.NewReader(rd)}
}

// Read reads data into p. It returns the number of bytes read into p. It reads
// till it encounters a `!` byte.
func (p1 *P1) Read(p []byte) (n int, err error) {
	b, err := p1.rd.ReadBytes('!')
	p = append(p, b...)

	return len(b), err
}

package main

import (
	"bufio"
	"io"
)

type P1 struct {
	rd *bufio.Reader
}

// NewReader returns a Reader whose read telegrams from a Reader object.
func NewP1(rd io.Reader) P1 {
	return P1{rd: bufio.NewReader(rd)}
}

// Read reads data into p. It returns the number of bytes read into p. It reads
// till it encounters a `!` byte.
func (p1 P1) Read(p []byte) (n int, err error) {
	b, err := p1.rd.ReadBytes('!')
	p = append(p, b...)

	return len(b), err
}

Fix P1 so it adheres Reader interface.


package main

import (
	"flag"
	"os"

	"github.com/foomo/petze/service"
	"github.com/foomo/petze/config"
	log "github.com/sirupsen/logrus"
)

func main() {
	flag.Usage = usage
	flag.Parse()
	if flag.NArg() != 1 {
		flag.Usage()
		os.Exit(1)
	}

	configurationDirectory := os.Args[1]
	if stat, err := os.Stat(configurationDirectory); err == nil && stat.IsDir() {
		runServer(configurationDirectory)
	} else {
		log.Fatal("specified configuration directory does not exist or is not a directory")
	}
}

func runServer(configurationDirectory string) {
	serverConfig, err := config.LoadServer(configurationDirectory)
	if err != nil {
		log.Fatal(err)
	}
	log.Info(service.Run(serverConfig, configurationDirectory))
}

func usage() {
	log.Printf("Usage: %s configuration-directory \n", os.Args[0])
	flag.PrintDefaults()
}

package main

import (
	"flag"
	"os"

	"github.com/foomo/petze/service"
	"github.com/foomo/petze/config"
	log "github.com/sirupsen/logrus"
)

var flagJsonOutput bool

func main() {
	flag.Usage = usage
	flag.BoolVar(&flagJsonOutput, "json-output", false, "specifies if the logging format is json or not")

	flag.Parse()

	initializeLogger()

	configurationDirectory := flag.Args()[0]
	if stat, err := os.Stat(configurationDirectory); err == nil && stat.IsDir() {
		runServer(configurationDirectory)
	} else {
		log.Fatal("specified configuration directory does not exist or is not a directory")
	}
}

func runServer(configurationDirectory string) {
	serverConfig, err := config.LoadServer(configurationDirectory)
	if err != nil {
		log.Fatal(err)
	}
	log.Info(service.Run(serverConfig, configurationDirectory))
}

func usage() {
	log.Printf("Usage: %s configuration-directory \n", os.Args[0])
	flag.PrintDefaults()
}

func initializeLogger() {
	if flagJsonOutput {
		log.SetFormatter(&log.JSONFormatter{})
	}
}

Add --json-output flag (optional) to format standard output


package service

import (
	"strconv"

	"github.com/rancher/norman/types"
	"github.com/rancher/norman/types/convert"
	v3 "github.com/rancher/types/client/project/v3"
	"github.com/sirupsen/logrus"
	"k8s.io/apimachinery/pkg/util/intstr"
)

func New(store types.Store) types.Store {
	return &Store{
		store,
	}
}

type Store struct {
	types.Store
}

func (p *Store) Create(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}) (map[string]interface{}, error) {
	formatData(data)
	data, err := p.Store.Create(apiContext, schema, data)
	return data, err
}

func formatData(data map[string]interface{}) {
	var ports []interface{}
	port := int64(42)
	servicePort := v3.ServicePort{
		Port:       &port,
		TargetPort: intstr.Parse(strconv.FormatInt(42, 10)),
		Protocol:   "TCP",
		Name:       "default",
	}
	m, err := convert.EncodeToMap(servicePort)
	if err != nil {
		logrus.Warnf("Failed to transform service port to map: %v", err)
		return
	}
	ports = append(ports, m)
	data["ports"] = ports
}

package service

import (
	"strconv"

	"github.com/rancher/norman/types"
	"github.com/rancher/norman/types/convert"
	v3 "github.com/rancher/types/client/project/v3"
	"github.com/sirupsen/logrus"
	"k8s.io/apimachinery/pkg/util/intstr"
)

func New(store types.Store) types.Store {
	return &Store{
		store,
	}
}

type Store struct {
	types.Store
}

func (p *Store) Create(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}) (map[string]interface{}, error) {
	formatData(data)
	data, err := p.Store.Create(apiContext, schema, data)
	return data, err
}

func formatData(data map[string]interface{}) {
	var ports []interface{}
	servicePort := v3.ServicePort{
		Port:       42,
		TargetPort: intstr.Parse(strconv.FormatInt(42, 10)),
		Protocol:   "TCP",
		Name:       "default",
	}
	m, err := convert.EncodeToMap(servicePort)
	if err != nil {
		logrus.Warnf("Failed to transform service port to map: %v", err)
		return
	}
	ports = append(ports, m)
	data["ports"] = ports
}

Update now that ints aren't nullable


package sysinfo

import (
	"errors"
	"io/ioutil"
	"strings"
)

var (
	ErrUserNotFound      = errors.New("user not found")
	ErrGroupNotFound     = errors.New("group not found")
	ErrInvalidFileFormat = errors.New("invalid file format")
)

func readSingleValueFile(path string) (string, error) {
	content, err := ioutil.ReadFile(path)
	if err != nil {
		return "", err
	}

	return strings.TrimSpace(string(content)), nil
}

package sysinfo

import (
	"errors"
	"io/ioutil"
	"strings"
)

// #include <unistd.h>
import "C"

var (
	TicksPerSecond uint64

	ErrUserNotFound      = errors.New("user not found")
	ErrGroupNotFound     = errors.New("group not found")
	ErrInvalidFileFormat = errors.New("invalid file format")
)

func init() {
	TicksPerSecond = uint64(C.sysconf(C._SC_CLK_TCK))
}

func readSingleValueFile(path string) (string, error) {
	content, err := ioutil.ReadFile(path)
	if err != nil {
		return "", err
	}

	return strings.TrimSpace(string(content)), nil
}

Add TicksPerSecond constant obtained from sysconf(_SC_CLK_TCK) call


package handler

import (
	"fmt"
	"net/http"
	"runtime/debug"
	"time"
)

var PanicDateFormat = "Jan 2, 2006 at 3:04pm (MST)"

type PanicLogger interface {
	Print(v ...interface{})
}

// Panic returns a handler that invokes the passed handler h, catching any
// panics. If one occurs, an HTTP 500 response is produced. If the logger l is
// not nil, it will be used to print out a detailed message, including the
// timestamp and stack trace. If showStack is true, the detailed message is
// also written to the ResponseWriter.
func Panic(l PanicLogger, showStack bool, h http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		defer func() {
			if rec := recover(); rec != nil {
				stack := debug.Stack()
				timestamp := time.Now().Format(PanicDateFormat)
				message := fmt.Sprintf("%s - %s\n%s\n", timestamp, rec, stack)

				if l != nil {
					l.Print(message)
				}

				w.WriteHeader(http.StatusInternalServerError)

				if !showStack {
					message = "Internal Server Error"
				}

				w.Write([]byte(message))
			}
		}()

		h.ServeHTTP(w, r)
	})
}

package handler

import (
	"fmt"
	"net/http"
	"runtime/debug"
	"time"
)

var PanicDateFormat = "Jan 2, 2006 at 3:04pm (MST)"

// PanicLogger is used to print out detailed messages when a panic occurs.
type PanicLogger interface {
	Print(v ...interface{})
}

// Panic returns a handler that invokes the passed handler h, catching any
// panics. If one occurs, an HTTP 500 response is produced. If the logger l is
// not nil, it will be used to print out a detailed message, including the
// timestamp and stack trace. If showStack is true, the detailed message is
// also written to the ResponseWriter.
func Panic(l PanicLogger, showStack bool, h http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		defer func() {
			if rec := recover(); rec != nil {
				stack := debug.Stack()
				timestamp := time.Now().Format(PanicDateFormat)
				message := fmt.Sprintf("%s - %s\n%s\n", timestamp, rec, stack)

				if l != nil {
					l.Print(message)
				}

				w.WriteHeader(http.StatusInternalServerError)

				if !showStack {
					message = "Internal Server Error"
				}

				w.Write([]byte(message))
			}
		}()

		h.ServeHTTP(w, r)
	})
}

Add a doc for the PanicLogger


package marvel

import (
	"flag"
	"testing"
)

var (
	apiKey = flag.String("pub", "", "Public API key")
	secret = flag.String("priv", "", "Private API secret")
)

func TestRequest(t *testing.T) {
	flag.Parse()

	r, err := Client{
		PublicKey:  *apiKey,
		PrivateKey: *secret,
	}.SingleSeries(2258).Comics(ComicsParams{})
	if err != nil {
		t.Errorf("error: %v", err)
		return
	}
	t.Logf("%+v", r.Data)
	for _, iss := range r.Data.Results {
		t.Logf(iss.Modified.Parse().String())
		t.Logf(iss.Thumbnail.URL(PortraitIncredible))
	}
}

package marvel

import (
	"flag"
	"testing"
)

var (
	apiKey = flag.String("pub", "", "Public API key")
	secret = flag.String("priv", "", "Private API secret")
)

func TestRequest(t *testing.T) {
	flag.Parse()

	c := Client{
		PublicKey:  *apiKey,
		PrivateKey: *secret,
	}
	r, err := c.SingleSeries(2258).Comics(ComicsParams{})
	if err != nil {
		t.Errorf("error: %v", err)
		return
	}
	t.Logf("%+v", r.Data)
	for _, iss := range r.Data.Results {
		t.Logf("%v %s", *iss.IssueNumber, iss.Modified.Parse().String())
		t.Logf(iss.Thumbnail.URL(PortraitIncredible))
	}
	comic, err := r.Data.Results[0].Get(c)
	if err != nil {
		t.Errorf("error getting: %v", err)
	}
	t.Logf("%+v", comic.Data.Results[0])
}

Add test case for getting single entity


/* Copyright (c) 2015, Daniel Martí <mvdan@mvdan.cc> */
/* See LICENSE for licensing information */

package main

import (
	"bufio"
	"fmt"
	"os"
	"regexp"
)

var regexLink = regexp.MustCompile(
	`([^\s'"<>\(\)]+:(//)?|(http|ftp|www)[^.]*\.)[^\s'"<>\(\)]*[^\s.,;:'"<>\(\)]`)

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	for scanner.Scan() {
		line := scanner.Text()
		urls := regexLink.FindAllString(line, -1)
		if urls == nil {
			continue
		}
		for _, url := range urls {
			fmt.Println(url)
		}
	}
}

/* Copyright (c) 2015, Daniel Martí <mvdan@mvdan.cc> */
/* See LICENSE for licensing information */

package main

import (
	"bufio"
	"fmt"
	"os"
	"regexp"
)

var regexLink = regexp.MustCompile(
	`(([^\s'"<>\(\)]+:(//)?|(http|ftp|www)[^.]*\.)[^\s'"<>\(\)]*|[^\s'"<>\(\)]+\.(com|org|net|edu|info)(/[^\s'"<>\(\)]*)?)[^.,;:\s'"<>\(\)]`)

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	for scanner.Scan() {
		line := scanner.Text()
		urls := regexLink.FindAllString(line, -1)
		if urls == nil {
			continue
		}
		for _, url := range urls {
			fmt.Println(url)
		}
	}
}

Add support for the main TLDs


package main

import (
	"fmt"

	"github.com/nightexcessive/websocket"
)

func testMessage(socket *websocket.WebSocket, message string) {
	if err := socket.SendString("Hello, World!"); err != nil {
		panic(fmt.Sprintf("Error when sending: %s\n", err))
		return
	}
	fmt.Printf("Message sent: %q\n", message)

	messageEvent, errorEvent := socket.Receive()
	if errorEvent != nil {
		panic(fmt.Sprintf("Error when receiving: %s\n", errorEvent))
		return
	} else if receivedMessage := messageEvent.Data.Str(); receivedMessage != message {
		fmt.Printf("Received unexecpected message: %q (expected %q)\n", receivedMessage, message)
		return
	}
	fmt.Printf("Message received: %#v\n", messageEvent.Data.Interface())
}

func main() {
	go func() {
		fmt.Println("Creating...")
		socket, err := websocket.New("ws://localhost:3000/echo")
		if err != nil {
			fmt.Printf("Failed to connect: %s\n", err)
			return
		}

		defer func() {
			socket.Close()
			fmt.Println("Disconnected.")
		}()

		fmt.Println("Connected.")

		testMessage(socket, "Hello, World!")
	}()
}

package main

import (
	"fmt"

	"github.com/gopherjs/websocket"
)

func testMessage(socket *websocket.WebSocket, message string) {
	if err := socket.SendString("Hello, World!"); err != nil {
		panic(fmt.Sprintf("Error when sending: %s\n", err))
		return
	}
	fmt.Printf("Message sent: %q\n", message)

	messageEvent, errorEvent := socket.Receive()
	if errorEvent != nil {
		panic(fmt.Sprintf("Error when receiving: %s\n", errorEvent))
		return
	} else if receivedMessage := messageEvent.Data.Str(); receivedMessage != message {
		fmt.Printf("Received unexecpected message: %q (expected %q)\n", receivedMessage, message)
		return
	}
	fmt.Printf("Message received: %#v\n", messageEvent.Data.Interface())
}

func main() {
	go func() {
		fmt.Println("Creating...")
		socket, err := websocket.New("ws://localhost:3000/echo")
		if err != nil {
			fmt.Printf("Failed to connect: %s\n", err)
			return
		}

		defer func() {
			socket.Close()
			fmt.Println("Disconnected.")
		}()

		fmt.Println("Connected.")

		testMessage(socket, "Hello, World!")
	}()
}

Change example import to GopherJS version


package alita

import (
	"bufio"
	"fmt"
	"io"
	"strings"
)

type Aligner struct {
	w         io.Writer
	Margin    *Margin
	Delimiter *Delimiter
	Padding   *Padding
	lines     [][]string
}

func NewAligner(w io.Writer) *Aligner {
	return &Aligner{
		w:         w,
		Margin:    NewMargin(),
		Delimiter: NewDelimiter(),
		Padding:   NewPadding(),
	}
}

func (a *Aligner) appendLine(s string) {
	sp := a.Delimiter.Split(s)
	a.lines = append(a.lines, sp)

	a.Padding.UpdateWidth(sp)
}

func (a *Aligner) ReadAll(r io.Reader) error {
	s := bufio.NewScanner(r)
	for s.Scan() {
		a.appendLine(s.Text())
	}
	return s.Err()
}

func (a *Aligner) Flush() error {
	for _, sp := range a.lines {
		s := strings.TrimSpace(a.Margin.Join(a.Padding.Format(sp)))
		_, err := fmt.Fprintln(a.w, s)
		if err != nil {
			return err
		}
	}
	return nil
}

package alita

import (
	"bufio"
	"fmt"
	"io"
	"strings"
)

type Aligner struct {
	w         io.Writer
	Margin    *Margin
	Delimiter *Delimiter
	Padding   *Padding
	lines     [][]string
}

func NewAligner(w io.Writer) *Aligner {
	return &Aligner{
		w:         w,
		Margin:    NewMargin(),
		Delimiter: NewDelimiter(),
		Padding:   NewPadding(),
	}
}

func (a *Aligner) appendLine(s string) {
	sp := a.Delimiter.Split(s)
	a.lines = append(a.lines, sp)

	a.Padding.UpdateWidth(sp)
}

func (a *Aligner) ReadAll(r io.Reader) error {
	s := bufio.NewScanner(r)
	for s.Scan() {
		a.appendLine(s.Text())
	}
	return s.Err()
}

func (a *Aligner) format(sp []string) string {
	return strings.TrimSpace(a.Margin.Join(a.Padding.Format(sp)))
}

func (a *Aligner) Flush() error {
	for _, sp := range a.lines {
		_, err := fmt.Fprintln(a.w, a.format(sp))
		if err != nil {
			return err
		}
	}
	return nil
}

Split too long funcall chain


package options

import (
	"os"
	"path/filepath"
)

var (
	VerboseClass    bool
	ThreadStackSize uint
	AbsJavaHome     string // /path/to/jre
	AbsJreLib       string // /path/to/jre/lib
)

func InitOptions(verboseClass bool, xss int, useJavaHome bool) {
	VerboseClass = verboseClass
	ThreadStackSize = uint(xss)
	initJavaHome(useJavaHome)
}

func initJavaHome(useOsEnv bool) {
	jh := "./jre"
	if useOsEnv {
		jh = os.Getenv("JAVA_HOME")
		if jh == "" {
			panic("$JAVA_HOME not set!")
		}
	}

	if absJh, err := filepath.Abs(jh); err == nil {
		AbsJavaHome = absJh
		AbsJreLib = filepath.Join(absJh, "lib")
	} else {
		panic(err)
	}
}

package options

import (
	"os"
	"path/filepath"
	"strings"
)

var (
	VerboseClass    bool
	ThreadStackSize uint
	AbsJavaHome     string // /path/to/jre
	AbsJreLib       string // /path/to/jre/lib
)

func InitOptions(verboseClass bool, xss int, useJavaHome bool) {
	VerboseClass = verboseClass
	ThreadStackSize = uint(xss)
	initJavaHome(useJavaHome)
}

func initJavaHome(useOsEnv bool) {
	jh := "./jre"
	if useOsEnv {
		jh = os.Getenv("JAVA_HOME")
		if jh == "" {
			panic("$JAVA_HOME not set!")
		}
	}

	if absJh, err := filepath.Abs(jh); err == nil {
		if strings.Contains(absJh, "jre") {
			AbsJavaHome = absJh
			AbsJreLib = filepath.Join(absJh, "lib")
		} else {
			AbsJavaHome = filepath.Join(absJh, "jre")
			AbsJreLib = filepath.Join(absJh, "jre", "lib")
		}
	} else {
		panic(err)
	}
}

Support both jre and jdk path for -XuseJavaHome


package helpers

import "github.com/sclevine/agouti"

func AgoutiDriver() *agouti.WebDriver {
	return agouti.PhantomJS()
}

package helpers

import (
	"fmt"
	"os"
	"os/exec"

	"github.com/onsi/ginkgo"
	"github.com/sclevine/agouti"
)

func AgoutiDriver() *agouti.WebDriver {
	if _, err := exec.LookPath("phantomjs"); err == nil && os.Getenv("FORCE_SELENIUM") != "true" {
		fmt.Fprintln(ginkgo.GinkgoWriter, "WARNING: using phantomjs, which is flaky in CI, but is more convenient during development")
		return agouti.PhantomJS()
	} else {
		return agouti.Selenium(agouti.Browser("firefox"))
	}
}

Revert "never use selenium ever"


package containers

import (
	"github.com/fsouza/go-dockerclient"
)

type DockerRuntime struct {
	Client *docker.Client
}

func isRelated(volume string, container *docker.Container) bool {
	for _, mount := range container.Mounts {
		if mount.Name == volume && mount.Driver == "dvol" {
			return true
		}
	}
	return false
}

func (runtime DockerRuntime) Related(volume string) ([]string, error) {
	relatedContainers := make([]string, 0)
	containers, err := runtime.Client.ListContainers(docker.ListContainersOptions{})
	if err != nil {
		return relatedContainers, err
	}
	for _, c := range containers {
		container, err := runtime.Client.InspectContainer(c.ID)
		if err != nil {
			return relatedContainers, err
		}
		if isRelated(volume, container) && container.State.Running {
			relatedContainers = append(relatedContainers, container.Name)
		}
	}
	return relatedContainers, nil
}

func (runtime DockerRuntime) Start(volume string) error {
	return nil
}

func (runtime DockerRuntime) Stop(volume string) error {
	return nil
}

func (runtime DockerRuntime) Remove(volume string) error {
	return nil
}

package containers

import (
	"github.com/fsouza/go-dockerclient"
)

type DockerRuntime struct {
	Client *docker.Client
}

func isRelated(volume string, container *docker.Container) bool {
	for _, mount := range container.Mounts {
		if mount.Name == volume && mount.Driver == "dvol" {
			return true
		}
	}
	return false
}

// Related determines which containers are related to a particular volume.
// A container is deemed to be related if a dvol volume with the same name appears
// in the Mounts information for a container which is also currently running.
// Related returns an array of related container names and any error encountered.
func (runtime DockerRuntime) Related(volume string) ([]string, error) {
	relatedContainers := make([]string, 0)
	containers, err := runtime.Client.ListContainers(docker.ListContainersOptions{})
	if err != nil {
		return relatedContainers, err
	}
	for _, c := range containers {
		container, err := runtime.Client.InspectContainer(c.ID)
		if err != nil {
			return relatedContainers, err
		}
		if isRelated(volume, container) && container.State.Running {
			relatedContainers = append(relatedContainers, container.Name)
		}
	}
	return relatedContainers, nil
}

func (runtime DockerRuntime) Start(volume string) error {
	return nil
}

func (runtime DockerRuntime) Stop(volume string) error {
	return nil
}

func (runtime DockerRuntime) Remove(volume string) error {
	return nil
}

Add a docstring for Related.


package aggregate

import (
	"github.com/loadimpact/speedboat/runner"
)

func Aggregate(stats *Stats, in <-chan runner.Result) <-chan runner.Result {
	ch := make(chan runner.Result)

	go func() {
		defer close(ch)

		defer stats.End()
		for res := range in {
			if res.Abort {
				continue
			}
			stats.Ingest(&res)
			ch <- res
		}
	}()

	return ch
}

package aggregate

import (
	"github.com/loadimpact/speedboat/runner"
)

func Aggregate(stats *Stats, in <-chan runner.Result) <-chan runner.Result {
	ch := make(chan runner.Result)

	go func() {
		defer close(ch)

		defer stats.End()
		for res := range in {
			if !res.Abort {
				stats.Ingest(&res)
			}
			ch <- res
		}
	}()

	return ch
}

Abort results weren't forwarded properly


// Copyright 2017 Kumina, https://kumina.nl/
// 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 libvirt_schema

type Domain struct {
	Devices Devices `xml:"devices"`
}

type Devices struct {
	Disks []Disk `xml:"disk"`
}

type Disk struct {
	Driver Driver `xml:"driver"`
	Source Source `xml:"source"`
	Target Target `xml:"target"`
}

type Driver struct {
	Type string `xml:"type,attr"`
}

type Source struct {
	File string `xml:"file,attr"`
}

type Target struct {
	Device string `xml:"dev,attr"`
}

// Copyright 2017 Kumina, https://kumina.nl/
// 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 libvirt_schema

type Domain struct {
	Devices Devices `xml:"devices"`
}

type Devices struct {
	Disks []Disk `xml:"disk"`
}

type Disk struct {
	Source Source `xml:"source"`
	Target Target `xml:"target"`
}

type Source struct {
	File string `xml:"file,attr"`
}

type Target struct {
	Device string `xml:"dev,attr"`
}

Remove field from the XML that we don't use.


package issue

import "net/http"

type HttpBody struct {
	ContentEncoding string `json:"contentEncoding"`
	Content         []byte `json:"content"`
}

type HttpEntity struct {
	Status string      `json:"status"`
	Header http.Header `json:"header"`
	Body   *HttpBody   `json:"body,omitempty"`
}

type HttpTransaction struct {
	Id       int         `json:"id,omitempty"`
	Url      string      `json:"url"`
	Params   []string    `json:"params,omitempty"`
	Method   string      `json:"method"`
	Request  *HttpEntity `json:"request,omitempty"`
	Response *HttpEntity `json:"response,omitempty"`
}

type Vector struct {
	Url              string             `json:"url,omitempty" description:"where this issue is happened"`
	HttpTransactions []*HttpTransaction `json:"httpTransactions,omitempty" bson:"httpTransactions"`
}

type Vectors []*Vector

package issue

import "net/http"

type HttpBody struct {
	ContentEncoding string `json:"contentEncoding"`
	Content         []byte `json:"content,string"`
}

type HttpEntity struct {
	Status string      `json:"status"`
	Header http.Header `json:"header"`
	Body   *HttpBody   `json:"body,omitempty"`
}

type HttpTransaction struct {
	Id       int         `json:"id,omitempty"`
	Url      string      `json:"url"`
	Params   []string    `json:"params,omitempty"`
	Method   string      `json:"method"`
	Request  *HttpEntity `json:"request,omitempty"`
	Response *HttpEntity `json:"response,omitempty"`
}

type Vector struct {
	Url              string             `json:"url,omitempty" description:"where this issue is happened"`
	HttpTransactions []*HttpTransaction `json:"httpTransactions,omitempty" bson:"httpTransactions"`
}

type Vectors []*Vector

Set Content type to string for swagger


package pdf

import "testing"

// Unit tests follow

func TestIsWhite (t *testing.T) {
	for _,b := range []byte("\000\t\n\f\r ") {
		if (!IsWhiteSpace (b)) {
			t.Errorf ("IsWhiteSpace('\\%.3o') failed", b)
		}
	}
}

func TestIsDelimiter (t *testing.T) {
	for _,b := range []byte("%()/<>[]{}") {
		if (!IsDelimiter (b)) {
			t.Errorf ("IsDelimiter('%c') failed", b)
		}
	}
}

package pdf

import "testing"
import "strings"

// Unit tests follow

func removeIf (f func (b byte) bool, s string) string {
	return strings.Map (
		func (r rune) rune {
			if f(byte(r)) {
				return r
			}
			return -1
		},
		s)
}

func TestAlt (t *testing.T) {
	arrayOfAllLatin1Runes := make([]rune, 256, 256)

	for i,_ := range arrayOfAllLatin1Runes {
		arrayOfAllLatin1Runes[i] = rune(i)
	}

	stringOfAllLatin1Runes := string(arrayOfAllLatin1Runes)

	justDelimiters := removeIf (IsDelimiter, stringOfAllLatin1Runes)
	justWhite := removeIf (IsWhiteSpace, stringOfAllLatin1Runes)

	if justDelimiters != "%()/<>[]{}" {
		t.Errorf ("Incorrect delimiter character list: \"%s\"", justDelimiters)
	}

	if justWhite != "\000\t\n\f\r " {
		t.Errorf ("Incorrect delimiter character list: \"%s\"", justWhite)
	}
}

Simplify unit tests for character-set predicates.


// Package routing contains various functions related to HTTP routing
package routing

import (
	"errors"
	"strings"
)

// getContentType returns the http header safe content-type attribute for the
// requested path. If the requested path is not formatted correctly with an extension
// then the function will return the zero-value for a string and an error.
// BUG(george-e-shaw-iv) Cannot handle interpreted files such as .php files
// BUG(george-e-shaw-iv) Does not cover the bulk of encountered content types on the web
func GetContentType(path string) (string, error) {
	var contentType string
	splitPath := strings.Split(path, ".")

	if len(splitPath) == 1 && splitPath[0] == path {
		return contentType, errors.New("Invalid path, contained no period-separated content-type")
	}

	fileType := splitPath[len(splitPath)-1]

	switch fileType {
	case "html":
		contentType = "text/html"
	case "css":
		contentType = "text/css"
	case "js":
		contentType = "text/javascript"
	case "png":
		contentType = "image/png"
	case "jpg":
		fallthrough
	case "jpeg":
		contentType = "image/jpeg"
	default:
		contentType = "text/plain"
	}

	return contentType, nil
}

// Package routing contains various functions related to HTTP routing
package routing

import (
	"errors"
	"path/filepath"
)

// getContentType returns the http header safe content-type attribute for the
// requested path. If the requested path is not formatted correctly with an extension
// then the function will return the zero-value for a string and an error.
// BUG(george-e-shaw-iv) Cannot handle interpreted files such as .php files
// BUG(george-e-shaw-iv) Does not cover the bulk of encountered content types on the web
func GetContentType(path string) (string, error) {
	var contentType string
	fileType := filepath.Ext(path)

	if fileType == "" {
		return contentType, errors.New("Invalid path, contained no period-separated content-type")
	}

	switch fileType {
	case "html":
		contentType = "text/html"
	case "css":
		contentType = "text/css"
	case "js":
		contentType = "text/javascript"
	case "png":
		contentType = "image/png"
	case "jpg":
		fallthrough
	case "jpeg":
		contentType = "image/jpeg"
	default:
		contentType = "text/plain"
	}

	return contentType, nil
}

Use filepath/Ext in case a user decides to name a file with multiple .


package stringset

type StringSet struct {
	strMap map[string]bool
}

func New(elements ...string) *StringSet {
	s := &StringSet{}
	s.Clear()
	for _, el := range elements {
		s.Add(el)
	}
	return s
}
func (s *StringSet) Len() int {
	return len(s.strMap)
}
func (s *StringSet) Add(str string) {
	s.strMap[str] = true
}
func (s *StringSet) Remove(str string) {
	delete(s.strMap, str)
}
func (s *StringSet) Has(str string) bool {
	_, has := s.strMap[str]
	return has
}
func (s *StringSet) All() []string {
	l := make([]string, 0, len(s.strMap))
	for str, _ := range s.strMap {
		l = append(l, str)
	}
	return l
}
func (s *StringSet) Clear() {
	s.strMap = make(map[string]bool)
}

package stringset

type StringSet struct {
	strMap map[string]bool
}

func New(elements ...string) *StringSet {
	s := &StringSet{}
	s.Clear()
	for _, el := range elements {
		s.Add(el)
	}
	return s
}
func (s *StringSet) Len() int {
	return len(s.strMap)
}
func (s *StringSet) Add(str string) bool {
	if _, exists := s.strMap[str]; exists {
		return false
	}
	s.strMap[str] = true
	return true
}
func (s *StringSet) Remove(str string) {
	delete(s.strMap, str)
}
func (s *StringSet) Has(str string) bool {
	_, has := s.strMap[str]
	return has
}
func (s *StringSet) All() []string {
	l := make([]string, 0, len(s.strMap))
	for str, _ := range s.strMap {
		l = append(l, str)
	}
	return l
}
func (s *StringSet) Clear() {
	s.strMap = make(map[string]bool)
}
func (s *StringSet) Raw() map[string]bool {
	return s.strMap
}

Add return value to Add function indicating whether key was added or was already present


package api

import (
	"encoding/json"
	"github.com/pkg/errors"
	"github.com/pterodactyl/sftp-server"
)

func (r *PanelRequest) ValidateSftpCredentials(request sftp_server.AuthenticationRequest) (*sftp_server.AuthenticationResponse, error) {
	b, err := json.Marshal(request)
	if err != nil {
		return nil, err
	}

	resp, err := r.Post("/sftp/auth/login", b)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	r.Response = resp

	if r.HasError() {
		if r.HttpResponseCode() == 403 {
			return nil, sftp_server.InvalidCredentialsError{}
		}

		return nil, errors.WithStack(errors.New(r.Error()))
	}

	response := new(sftp_server.AuthenticationResponse)
	body, _ := r.ReadBody()

	if err := json.Unmarshal(body, response); err != nil {
		return nil, err
	}

	return response, nil
}

package api

import (
	"encoding/json"
	"github.com/pkg/errors"
	"github.com/pterodactyl/sftp-server"
)

func (r *PanelRequest) ValidateSftpCredentials(request sftp_server.AuthenticationRequest) (*sftp_server.AuthenticationResponse, error) {
	b, err := json.Marshal(request)
	if err != nil {
		return nil, err
	}

	resp, err := r.Post("/sftp/auth", b)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	r.Response = resp

	if r.HasError() {
		if r.HttpResponseCode() == 403 {
			return nil, sftp_server.InvalidCredentialsError{}
		}

		return nil, errors.WithStack(errors.New(r.Error()))
	}

	response := new(sftp_server.AuthenticationResponse)
	body, _ := r.ReadBody()

	if err := json.Unmarshal(body, response); err != nil {
		return nil, err
	}

	return response, nil
}

Change endpoint for SFTP credentials


package main

import (
	"log"
	"os"
	"os/signal"

	"github.com/ecc1/medtronic"
)

const (
	verbose = false
)

var (
	signalChan = make(chan os.Signal, 1)
)

func main() {
	pump, err := medtronic.Open()
	if err != nil {
		log.Fatal(err)
	}

	signal.Notify(signalChan, os.Interrupt)
	go catchInterrupt(pump)

	log.SetFlags(log.Ltime | log.Lmicroseconds | log.LUTC)
	for packet := range pump.Radio.Incoming() {
		if verbose {
			log.Printf("raw data: % X (RSSI = %d)\n", packet.Data, packet.Rssi)
		}
		data, err := pump.DecodePacket(packet)
		if err != nil {
			log.Printf("%v\n", err)
			continue
		}
		if verbose {
			log.Printf(" decoded: % X\n", data)
		} else {
			log.Printf("% X (RSSI = %d)\n", data, packet.Rssi)
		}
	}
}

func catchInterrupt(pump *medtronic.Pump) {
	<-signalChan
	pump.PrintStats()
	os.Exit(0)
}

package main

import (
	"log"
	"os"
	"os/signal"

	"github.com/ecc1/medtronic"
)

const (
	verbose = true
)

var (
	signalChan = make(chan os.Signal, 1)
)

func main() {
	if verbose {
		log.SetFlags(log.Ltime | log.Lmicroseconds | log.LUTC)
	}
	pump, err := medtronic.Open()
	if err != nil {
		log.Fatal(err)
	}

	signal.Notify(signalChan, os.Interrupt)
	go catchInterrupt(pump)

	for packet := range pump.Radio.Incoming() {
		if verbose {
			log.Printf("raw data: % X (RSSI = %d)\n", packet.Data, packet.Rssi)
		}
		data, err := pump.DecodePacket(packet)
		if err != nil {
			log.Printf("%v\n", err)
			continue
		}
		if verbose {
			log.Printf(" decoded: % X\n", data)
		} else {
			log.Printf("% X (RSSI = %d)\n", data, packet.Rssi)
		}
	}
}

func catchInterrupt(pump *medtronic.Pump) {
	<-signalChan
	pump.PrintStats()
	os.Exit(0)
}

Use high-resolution timestamps in verbose mode only


// +build amd64,arm64,!darwin,!netbsd,!openbsd

package goselect

// darwin, netbsd and openbsd uses uint32 on both amd64 and 386

const (
	// NFDBITS is the amount of bits per mask
	NFDBITS = 8 * 8
)

// +build amd64,arm64 !darwin,!netbsd,!openbsd

package goselect

// darwin, netbsd and openbsd uses uint32 on both amd64 and 386

const (
	// NFDBITS is the amount of bits per mask
	NFDBITS = 8 * 8
)

Fix issue in build tags


package main

import (
	"errors"
	"fmt"
	"log"
	"os/exec"
)

// Iterates over Godep file dependencies and sets
// the specified version on each of them.
func install() error {
	deps, err := readDepFile()
	if err != nil {
		return err
	}

	_, err = exec.LookPath("go")
	if err != nil {
		return errors.New("Go is currently not installed or in your PATH\n")
	}

	for dep, version := range deps {
		log.Printf("Getting %s\n", dep)
		out, err := execCmd(fmt.Sprintf(`go get -d "%s/..."`, dep))
		if err != nil {
			log.Println(out)
			return err
		}

		err = setPackageToVersion(dep, version)
		if err != nil {
			return err
		}
	}

	log.Println("All Done")

	return nil
}

package main

import (
	"errors"
	"fmt"
	"log"
	"os/exec"

	"github.com/mtibben/gogpm/vcs"
)

// Iterates over Godep file dependencies and sets
// the specified version on each of them.
func install() error {
	deps, err := readDepFile()
	if err != nil {
		return err
	}

	_, err = exec.LookPath("go")
	if err != nil {
		return errors.New("Go is currently not installed or in your PATH\n")
	}

	for dep, version := range deps {
		log.Printf("Getting %s\n", dep)

		update := ""
		rr, err := vcs.RepoRootForImportPath(dep)
		if err == nil {
			absoluteVcsPath := installPath(rr.Root)
			cur, _ := rr.Vcs.CurrentTag(absoluteVcsPath)
			if cur != version {
				update = "-u"
			}
		}

		out, err := execCmd(fmt.Sprintf(`go get -d %s "%s/..."`, update, dep))
		if err != nil {
			log.Println(out)
			return err
		}

		err = setPackageToVersion(dep, version)
		if err != nil {
			return err
		}
	}

	log.Println("All Done")

	return nil
}

Check if go get should upate


// +build !openbsd
// +build !windows

package restic

import (
	"restic/errors"
	"syscall"

	"github.com/pkg/xattr"
)

// Getxattr retrieves extended attribute data associated with path.
func Getxattr(path, name string) ([]byte, error) {
	b, e := xattr.Getxattr(path, name)
	if err, ok := e.(*xattr.XAttrError); ok && err.Err == syscall.ENOTSUP {
		return nil, nil
	}
	return b, errors.Wrap(e, "Getxattr")
}

// Listxattr retrieves a list of names of extended attributes associated with the
// given path in the file system.
func Listxattr(path string) ([]string, error) {
	s, e := xattr.Listxattr(path)
	if err, ok := e.(*xattr.XAttrError); ok && err.Err == syscall.ENOTSUP {
		return nil, nil
	}
	return s, errors.Wrap(e, "Listxattr")
}

// Setxattr associates name and data together as an attribute of path.
func Setxattr(path, name string, data []byte) error {
	e := xattr.Setxattr(path, name, data)
	if err, ok := e.(*xattr.XAttrError); ok && err.Err == syscall.ENOTSUP {
		return nil
	}
	return errors.Wrap(e, "Setxattr")
}

// +build !openbsd
// +build !windows

package restic

import (
	"restic/errors"
	"syscall"

	"github.com/pkg/xattr"
)

// Getxattr retrieves extended attribute data associated with path.
func Getxattr(path, name string) ([]byte, error) {
	b, e := xattr.Get(path, name)
	if err, ok := e.(*xattr.Error); ok && err.Err == syscall.ENOTSUP {
		return nil, nil
	}
	return b, errors.Wrap(e, "Getxattr")
}

// Listxattr retrieves a list of names of extended attributes associated with the
// given path in the file system.
func Listxattr(path string) ([]string, error) {
	s, e := xattr.List(path)
	if err, ok := e.(*xattr.Error); ok && err.Err == syscall.ENOTSUP {
		return nil, nil
	}
	return s, errors.Wrap(e, "Listxattr")
}

// Setxattr associates name and data together as an attribute of path.
func Setxattr(path, name string, data []byte) error {
	e := xattr.Set(path, name, data)
	if err, ok := e.(*xattr.Error); ok && err.Err == syscall.ENOTSUP {
		return nil
	}
	return errors.Wrap(e, "Setxattr")
}

Update code for pkg/xattr v0.2.0


package core

import "net/http"

type handlersStack []func(*Context)

var handlers handlersStack

// Use adds a handler to the handlers stack.
func Use(h func(*Context)) {
	handlers = append(handlers, h)
}

func (h handlersStack) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	// Init a new context for the request.
	c := &Context{
		ResponseWriter: w,
		Request:        r,
		index:          -1, // Begin with -1 because the NextWriter will increment index before calling the first handler.
	}

	// Enter the handlers stack.
	// We use a binder to set the c.written flag on first write and break handlers chain.
	c.ResponseWriter = ResponseWriterBinder{
		Writer:         c.ResponseWriter,
		ResponseWriter: c.ResponseWriter,
		BeforeWrite:    func([]byte) { c.written = true },
	}

	// Say to the client to "keep-alive" by default.
	c.ResponseWriter.Header().Set("Connection", "keep-alive")

	c.Next()
}

package core

import "net/http"

type handlersStack []func(*Context)

var handlers handlersStack

// Use adds a handler to the handlers stack.
func Use(h func(*Context)) {
	handlers = append(handlers, h)
}

func (h handlersStack) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	// Init a new context for the request.
	c := &Context{
		ResponseWriter: w,
		Request:        r,
		index:          -1, // Begin with -1 because the NextWriter will increment index before calling the first handler.
	}

	// Enter the handlers stack.
	// We use a binder to set the c.written flag on first write and break handlers chain.
	c.ResponseWriter = ResponseWriterBinder{
		Writer:         c.ResponseWriter,
		ResponseWriter: c.ResponseWriter,
		BeforeWrite:    func([]byte) { c.written = true },
	}

	// Use default headers.
	c.ResponseWriter.Header().Set("Connection", "keep-alive")
	c.ResponseWriter.Header().Set("Vary", "Accept-Encoding")

	c.Next()
}

Use Vary header by default


package main

import (
	"log"
	"os"
	"github.com/timakin/ts/loader"
	"github.com/codegangsta/cli"
)

var Commands = []cli.Command{
	commandAll,
	commandBiz,
	commandHack,
}

var commandAll = cli.Command{
	Name:  "all",
	Usage: "",
	Description: `
`,
	Action: doAll,
}

var commandBiz = cli.Command{
	Name:  "biz",
	Usage: "",
	Description: `
`,
	Action: doBiz,
}

var commandHack = cli.Command{
	Name:  "hack",
	Usage: "",
	Description: `
`,
	Action: doHack,
}

func debug(v ...interface{}) {
	if os.Getenv("DEBUG") != "" {
		log.Println(v...)
	}
}

func assert(err error) {
	if err != nil {
		log.Fatal(err)
	}
}

func doAll(c *cli.Context) {
	hn := make(chan loader.ResultData)
	go loader.GetHNFeed(hn)
	phres := <- hn
	var HNData loader.Feed = &phres
	HNData.Display("Hacker News")
}

func doBiz(c *cli.Context) {
}

func doHack(c *cli.Context) {
}

package main

import (
	"log"
	"os"
	"github.com/timakin/ts/loader"
	"github.com/codegangsta/cli"
)

var Commands = []cli.Command{
	commandAll,
	commandBiz,
	commandHack,
}

var commandAll = cli.Command{
	Name:  "all",
	Usage: "",
	Description: `
`,
	Action: doAll,
}

var commandBiz = cli.Command{
	Name:  "biz",
	Usage: "",
	Description: `
`,
	Action: doBiz,
}

var commandHack = cli.Command{
	Name:  "hack",
	Usage: "",
	Description: `
`,
	Action: doHack,
}

func debug(v ...interface{}) {
	if os.Getenv("DEBUG") != "" {
		log.Println(v...)
	}
}

func assert(err error) {
	if err != nil {
		log.Fatal(err)
	}
}

func doAll(c *cli.Context) {
	hn := make(chan loader.ResultData)
	go loader.GetHNFeed(hn)
	phres := <- hn
	var HNData loader.Feed = &phres
	HNData.Display()
}

func doBiz(c *cli.Context) {
}

func doHack(c *cli.Context) {
}

Change arguement of feed name


// BaruwaAPI Golang bindings for Baruwa REST API
// Copyright (C) 2019 Andrew Colin Kissa <andrew@topdog.za.net>

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.

// Package cmd cmdline client for the Baruwa REST API
package cmd

import (
	cli "github.com/jawher/mow.cli"
)

func systemStatus(cmd *cli.Cmd) {
}

// BaruwaAPI Golang bindings for Baruwa REST API
// Copyright (C) 2019 Andrew Colin Kissa <andrew@topdog.za.net>

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.

// Package cmd cmdline client for the Baruwa REST API
package cmd

import (
	"fmt"
	"log"

	"github.com/baruwa-enterprise/baruwa-go/api"
	prettyjson "github.com/hokaccha/go-prettyjson"
	cli "github.com/jawher/mow.cli"
)

func systemStatus(cmd *cli.Cmd) {
	cmd.Action = func() {
		var b []byte
		var err error
		var c *api.Client
		var s *api.SystemStatus

		if c, err = GetClient(); err != nil {
			log.Fatal(err)
		}

		if s, err = c.GetSystemStatus(); err != nil {
			log.Fatal(err)
		}

		if b, err = prettyjson.Marshal(s); err != nil {
			log.Fatal(err)
		}
		fmt.Printf("%s\n", b)
	}
}

Add system status cmdline implementation


package consul

import (
	"log"

	consulapi "github.com/hashicorp/consul/api"
)

type Config struct {
	Datacenter string `mapstructure:"datacenter"`
	Address    string `mapstructure:"address"`
}

// Client() returns a new client for accessing digital
// ocean.
//
func (c *Config) Client() (*consulapi.Client, error) {
	config := consulapi.DefaultConfig()
	if c.Datacenter != "" {
		config.Datacenter = c.Datacenter
	}
	if c.Address != "" {
		config.Address = c.Address
	}
	client, err := consulapi.NewClient(config)

	log.Printf("[INFO] Consul Client configured with address: '%s', datacenter: '%s'",
		config.Address, config.Datacenter)
	if err != nil {
		return nil, err
	}
	return client, nil
}

package consul

import (
	"log"

	consulapi "github.com/hashicorp/consul/api"
)

type Config struct {
	Datacenter string `mapstructure:"datacenter"`
	Address    string `mapstructure:"address"`
}

// Client() returns a new client for accessing consul.
//
func (c *Config) Client() (*consulapi.Client, error) {
	config := consulapi.DefaultConfig()
	if c.Datacenter != "" {
		config.Datacenter = c.Datacenter
	}
	if c.Address != "" {
		config.Address = c.Address
	}
	client, err := consulapi.NewClient(config)

	log.Printf("[INFO] Consul Client configured with address: '%s', datacenter: '%s'",
		config.Address, config.Datacenter)
	if err != nil {
		return nil, err
	}
	return client, nil
}

Fix comment in consul provider to not mention digitalocean


package config

// The default configuration for windows
var DefaultConfig = &Config{
	Apibase:  "https://mackerel.io",
	Root:     ".",
	Pidfile:  "mackerel-agent.pid",
	Conffile: "mackerel-agent.conf",
	Roles:    []string{},
	Verbose:  false,
	Connection: ConnectionConfig{
		PostMetricsDequeueDelaySeconds: 30,
		PostMetricsRetryDelaySeconds:   60,
		PostMetricsRetryMax:            10,
		PostMetricsBufferSize:          30,
	},
}

package config

import (
	"log"
	"path/filepath"
	"syscall"
	"unsafe"
)

func execdirInit() string {
	var (
		kernel32              = syscall.NewLazyDLL("kernel32")
		procGetModuleFileName = kernel32.NewProc("GetModuleFileNameW")
	)
	var wpath [syscall.MAX_PATH]uint16
	r1, _, err := procGetModuleFileName.Call(0, uintptr(unsafe.Pointer(&wpath[0])), uintptr(len(wpath)))
	if r1 == 0 {
		log.Fatal(err)
	}
	return syscall.UTF16ToString(wpath[:])
}

var execdir = filepath.Dir(execdirInit())

// The default configuration for windows
var DefaultConfig = &Config{
	Apibase:  "https://mackerel.io",
	Root:     execdir,
	Pidfile:  filepath.Join(execdir, "mackerel-agent.pid"),
	Conffile: filepath.Join(execdir, "mackerel-agent.conf"),
	Roles:    []string{},
	Verbose:  false,
	Connection: ConnectionConfig{
		PostMetricsDequeueDelaySeconds: 30,
		PostMetricsRetryDelaySeconds:   60,
		PostMetricsRetryMax:            10,
		PostMetricsBufferSize:          30,
	},
}

Change DefaultConfig pathes on windows


package oci

import (
	"context"
	"fmt"

	"github.com/hashicorp/packer/helper/multistep"
	"github.com/hashicorp/packer/packer"
)

type stepInstanceInfo struct{}

func (s *stepInstanceInfo) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
	var (
		driver = state.Get("driver").(Driver)
		ui     = state.Get("ui").(packer.Ui)
		id     = state.Get("instance_id").(string)
	)

	ip, err := driver.GetInstanceIP(id)
	if err != nil {
		err = fmt.Errorf("Error getting instance's public IP: %s", err)
		ui.Error(err.Error())
		state.Put("error", err)
		return multistep.ActionHalt
	}

	state.Put("instance_ip", ip)

	ui.Say(fmt.Sprintf("Instance has public IP: %s.", ip))

	return multistep.ActionContinue
}

func (s *stepInstanceInfo) Cleanup(state multistep.StateBag) {
	// no cleanup
}

package oci

import (
	"context"
	"fmt"

	"github.com/hashicorp/packer/helper/multistep"
	"github.com/hashicorp/packer/packer"
)

type stepInstanceInfo struct{}

func (s *stepInstanceInfo) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
	var (
		driver = state.Get("driver").(Driver)
		ui     = state.Get("ui").(packer.Ui)
		id     = state.Get("instance_id").(string)
	)

	ip, err := driver.GetInstanceIP(id)
	if err != nil {
		err = fmt.Errorf("Error getting instance's IP: %s", err)
		ui.Error(err.Error())
		state.Put("error", err)
		return multistep.ActionHalt
	}

	state.Put("instance_ip", ip)

	ui.Say(fmt.Sprintf("Instance has IP: %s.", ip))

	return multistep.ActionContinue
}

func (s *stepInstanceInfo) Cleanup(state multistep.StateBag) {
	// no cleanup
}

Update logs to talk about IP instead of public IP


/*
# Issue Checker

Scans your source code for issue links and prints there statuses.

## License

```
The MIT License (MIT)

Copyright (c) 2015 Evecon

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

```
*/
package main

import (
	"fmt"
	"github.com/google/go-github/github"
)

func main() {
	client := github.NewClient(nil)

	for i := 0; i < 100; i++ {
		issue, _, err := client.Issues.Get("go-swagger", "go-swagger", i)
		if err != nil {
			fmt.Errorf("Issues.Get returned error: %v", err)
		} else {
			fmt.Println(*issue.Number, " ", *issue.State)
		}
	}
}

Add licence to source file


package cli

import "sort"

// flagMap is a map of flags with the name as a string key
type flagMap map[string]*Flag

func (fm flagMap) Merge(fm2 flagMap) flagMap {
	for k, v := range fm2 {
		fm[k] = v
	}
	return fm
}

func (fm flagMap) Names() []string {
	var keys []string
	for k := range fm {
		keys = append(keys, k)
	}
	return keys
}

// Sort returns a sorted list of flags
func (fm flagMap) Sort() []*Flag {
	list := make(sort.StringSlice, len(fm))
	i := 0
	for _, f := range fm {
		list[i] = f.Name
		i++
	}
	list.Sort()
	result := make([]*Flag, len(list))
	for i, name := range list {
		result[i] = fm[name]
	}
	return result
}

package cli

import "sort"

// flagMap is a map of flags with the name as a string key
type flagMap map[string]*Flag

func (fm flagMap) Merge(fm2 flagMap) flagMap {
	mergedMap := make(flagMap)
	if fm != nil {
		for k, v := range fm {
			mergedMap[k] = v
		}
	}
	if fm2 != nil {
		for k, v := range fm2 {
			mergedMap[k] = v
		}
	}
	return mergedMap
}

func (fm flagMap) Names() []string {
	var keys []string
	for k := range fm {
		keys = append(keys, k)
	}
	return keys
}

// Sort returns a sorted list of flags
func (fm flagMap) Sort() []*Flag {
	list := make(sort.StringSlice, len(fm))
	i := 0
	for _, f := range fm {
		list[i] = f.Name
		i++
	}
	list.Sort()
	result := make([]*Flag, len(list))
	for i, name := range list {
		result[i] = fm[name]
	}
	return result
}

func (fm flagMap) Without(fm2 flagMap) flagMap {
	diffedMap := make(flagMap)
	if fm == nil {
		return diffedMap
	}
	if fm2 == nil {
		return fm
	}
	for k, v := range fm {
		if _, exist := fm2[k]; !exist {
			diffedMap[k] = v
		}
	}
	return diffedMap
}

Add Merge and Without methods


package controller

import (
	"fmt"
	"net/http"

	"github.com/rs/xid"
)

// RequestID is the unique Request ID for each request
type TraceID struct {
	xid.ID
}

// StandardResponseFields is meant to be included in all response bodies
// and includes "standard" response fields
type StandardResponseFields struct {
	Path    string  `json:"path,omitempty"`
	TraceID TraceID `json:"trace_id,omitempty"`
}

// NewTraceID is an initializer for TraceID
func NewTraceID(id xid.ID) TraceID {
	return TraceID{ID: id}
}

// NewMockTraceID is an initializer for TraceID which returns a
// static "mocked"
func NewMockTraceID() TraceID {
	x, err := xid.FromString("bpa182jipt3b2b78879g")
	if err != nil {
		fmt.Println(err)
	}
	return TraceID{ID: x}
}

// NewStandardResponseFields is an initializer for the StandardResponseFields struct
func NewStandardResponseFields(id TraceID, r *http.Request) StandardResponseFields {
	var sr StandardResponseFields
	sr.TraceID = id
	sr.Path = r.URL.EscapedPath()

	return sr
}

package controller

import (
	"fmt"
	"net/http"

	"github.com/rs/xid"
)

// RequestID is the unique Request ID for each request
type TraceID struct {
	xid.ID
}

// StandardResponseFields is meant to be included in all response bodies
// and includes "standard" response fields
type StandardResponseFields struct {
	Path    string `json:"path,omitempty"`
	TraceID string `json:"trace_id,omitempty"`
}

// NewTraceID is an initializer for TraceID
func NewTraceID(id xid.ID) TraceID {
	return TraceID{ID: id}
}

// NewMockTraceID is an initializer for TraceID which returns a
// static "mocked"
func NewMockTraceID() TraceID {
	x, err := xid.FromString("bpa182jipt3b2b78879g")
	if err != nil {
		fmt.Println(err)
	}
	return TraceID{ID: x}
}

// NewStandardResponseFields is an initializer for the StandardResponseFields struct
func NewStandardResponseFields(id TraceID, r *http.Request) StandardResponseFields {
	var sr StandardResponseFields
	sr.TraceID = id.String()
	sr.Path = r.URL.EscapedPath()

	return sr
}

Make TraceID a string in response, so it can be truly omitted


// Copyright © 2017 shoarai

// The renfls renames files in a directory.
package main

import (
	"flag"
	"fmt"
	"os"
	"os/exec"
	"strings"

	"github.com/shoarai/renfls"
)

const toDir = "toSubDirsName"

// Flag
var ext string
var ignore bool

func main() {
	// DEBUG:
	// createTestDir()

	flag.BoolVar(&ignore, "ignore", false, "bool flag")
	flag.StringVar(&ext, "ext", "", "extensions splited by \".\"")
	flag.Parse()

	exts := strings.Split(ext, ",")
	var err error
	if !ignore {
		// err = renfls.ToSubDirsNameExt(toDir, exts)
	} else {
		err = renfls.ToSubDirsNameIgnoreExt(toDir, exts)
	}

	if err != nil {
		fmt.Println(err)
	}
}

func createTestDir() {
	os.RemoveAll(toDir)
	exec.Command("cp", "-r", "testdata", toDir).Run()
}

// Copyright © 2017 shoarai

// The renfls renames files in a directory.
package main

import (
	"flag"
	"fmt"
	"os"
	"os/exec"
	"strings"

	"github.com/shoarai/renfls"
)

const toDir = "toSubDirsName"

// Flag
var ext string
var ignore bool

func main() {
	// DEBUG:
	// createTestDir()

	flag.BoolVar(&ignore, "ignore", false, "bool flag")
	flag.StringVar(&ext, "ext", "", "extensions splited by \",\"")
	flag.Parse()

	exts := strings.Split(ext, ",")
	var err error
	if !ignore {
		// err = renfls.ToSubDirsNameExt(toDir, exts)
	} else {
		err = renfls.ToSubDirsNameIgnoreExt(toDir, exts)
	}

	if err != nil {
		fmt.Println(err)
	}
}

func createTestDir() {
	os.RemoveAll(toDir)
	exec.Command("cp", "-r", "testdata", toDir).Run()
}

Fix comment for extensions flag


package dotignore

import (
	"bufio"
	"os"
	"path/filepath"
	"strings"
	"sync"

	"github.com/drpotato/dotdot/filesystem"
)

var ignoredFiles map[string]bool
var once sync.Once

func ShouldIgnore(uri string) bool {

	GetIgnoredFiles()

	dotDirURI := filesystem.GetDotDirURI()

	_, fileName := filepath.Split(uri)

	return strings.HasPrefix(uri, dotDirURI) && ignoredFiles[fileName]
}

func GetIgnoredFiles() map[string]bool {
	once.Do(func() {
		ignoredFiles = map[string]bool{
			".gitignore": true,
		}
		loadDotIgnore()
	})
	return ignoredFiles
}

func loadDotIgnore() {

	dotDirURI := filesystem.GetDotDirURI()
	dotIgnoreUri := filepath.Join(dotDirURI, ".dotignore")

	dotIgnoreFile, err := os.Open(dotIgnoreUri)
	if err != nil {
		return
	}
	defer dotIgnoreFile.Close()

	scanner := bufio.NewScanner(dotIgnoreFile)
	for scanner.Scan() {
		line := scanner.Text()
		if strings.HasPrefix(line, "#") {
			continue
		}
		ignoredFiles[line] = true
	}
}

package dotignore

import (
	"bufio"
	"os"
	"path/filepath"
	"strings"
	"sync"

	"github.com/drpotato/dotdot/filesystem"
)

var ignoredFiles map[string]bool
var once sync.Once

func ShouldIgnore(uri string) bool {

	GetIgnoredFiles()

	dotDirURI := filesystem.GetDotDirURI()

	_, fileName := filepath.Split(uri)

	return strings.HasPrefix(uri, dotDirURI) && ignoredFiles[fileName]
}

func GetIgnoredFiles() map[string]bool {
	once.Do(func() {
		ignoredFiles = map[string]bool{
			".git":       true,
			".gitignore": true,
		}
		loadDotIgnore()
	})
	return ignoredFiles
}

func loadDotIgnore() {

	dotDirURI := filesystem.GetDotDirURI()
	dotIgnoreUri := filepath.Join(dotDirURI, ".dotignore")

	dotIgnoreFile, err := os.Open(dotIgnoreUri)
	if err != nil {
		return
	}
	defer dotIgnoreFile.Close()

	scanner := bufio.NewScanner(dotIgnoreFile)
	for scanner.Scan() {
		line := scanner.Text()
		if strings.HasPrefix(line, "#") {
			continue
		}
		ignoredFiles[line] = true
	}
}

Add .git as default ignored file/dir


package cli

import (
	"fmt"

	"github.com/spf13/cobra"
)

// MinRequiredArgs checks if the minimum number of args exists, and returns an
// error if they do not.
func MinRequiredArgs(args []string, min int, cmd *cobra.Command) error {
	if len(args) >= min {
		return nil
	}

	return fmt.Errorf(
		"\"%s\" requires at least %d argument(s).\n\nUsage:  %s\n\n%s",
		cmd.CommandPath(),
		min,
		cmd.UseLine(),
		cmd.Short,
	)
}

// AcceptsNoArgs returns an error message if there are args
func AcceptsNoArgs(args []string, cmd *cobra.Command) error {
	if len(args) == 0 {
		return nil
	}

	return fmt.Errorf(
		"\"%s\" accepts no argument(s).\n\nUsage:  %s\n\n%s",
		cmd.CommandPath(),
		cmd.UseLine(),
		cmd.Short,
	)
}

package cli

import (
	"fmt"
	"strings"

	"github.com/spf13/cobra"
)

// NoArgs validate args and returns an error if there are any args
func NoArgs(cmd *cobra.Command, args []string) error {
	if len(args) == 0 {
		return nil
	}

	if cmd.HasSubCommands() {
		return fmt.Errorf("\n" + strings.TrimRight(cmd.UsageString(), "\n"))
	}

	return fmt.Errorf(
		"\"%s\" accepts no argument(s).\n\nUsage:  %s\n\n%s",
		cmd.CommandPath(),
		cmd.UseLine(),
		cmd.Short,
	)
}

// RequiresMinArgs returns an error if there is not at least min args
func RequiresMinArgs(min int) cobra.PositionalArgs {
	return func(cmd *cobra.Command, args []string) error {
		if len(args) >= min {
			return nil
		}
		return fmt.Errorf(
			"\"%s\" requires at least %d argument(s).\n\nUsage:  %s\n\n%s",
			cmd.CommandPath(),
			min,
			cmd.UseLine(),
			cmd.Short,
		)
	}
}

Use Args in cobra.Command to validate args.


package parser

import (
	"fmt"
	"strconv"
	"strings"
)

type fragment struct {
	pkg        string
	statements int
	covered    bool
}

func parseLine(raw string) fragment {
	output := fragment{
		pkg: extractPackage(raw),
	}

	output.statements, output.covered = extractNumbers(raw)

	return output
}

func extractPackage(raw string) string {
	lastSlash := strings.LastIndex(raw, "/")
	if lastSlash == -1 {
		panic(fmt.Errorf("line skipped due to lack of package '%s'", raw))
	}

	return raw[:(lastSlash + 1)]
}

func extractNumbers(raw string) (int, bool) {
	parts := strings.Split(raw, " ")
	if len(parts) != 3 {
		panic(fmt.Errorf("invalid line format. parts found %d, expected 3", len(parts)))
	}

	lines := extractStatements(parts[1])
	covered := extractCovered(parts[2])

	return lines, covered
}

func extractStatements(raw string) int {
	statements, err := strconv.Atoi(raw)
	if err != nil {
		panic(err)
	}
	return statements
}

func extractCovered(raw string) bool {
	return raw == "1"
}

package parser

import (
	"fmt"
	"strconv"
	"strings"
)

type fragment struct {
	pkg        string
	statements int
	covered    bool
}

func parseLine(raw string) fragment {
	output := fragment{
		pkg: extractPackage(raw),
	}

	output.statements, output.covered = extractNumbers(raw)

	return output
}

func extractPackage(raw string) string {
	lastSlash := strings.LastIndex(raw, "/")
	if lastSlash == -1 {
		panic(fmt.Errorf("line skipped due to lack of package '%s'", raw))
	}

	return raw[:(lastSlash + 1)]
}

func extractNumbers(raw string) (int, bool) {
	parts := strings.Split(raw, " ")
	if len(parts) != 3 {
		panic(fmt.Errorf("invalid line format. parts found %d, expected 3", len(parts)))
	}

	lines := extractStatements(parts[1])
	covered := extractCovered(parts[2])

	return lines, covered
}

func extractStatements(raw string) int {
	statements, err := strconv.Atoi(raw)
	if err != nil {
		panic(err)
	}
	return statements
}

func extractCovered(raw string) bool {
	covered, err := strconv.Atoi(raw)
	if err != nil {
		panic(err)
	}
	return covered > 0
}

Fix support for the `-covermode=count` and `covermode=atomic` options


//+build !windows

package main

func SetupProcessGroup() error {
	// nothing to do on other platforms
}

//+build !windows

package main

func SetupProcessGroup() error {
	// nothing to do on non-windows platforms
	return nil
}

Fix build for non-windows platforms


package flak

import (
	"os"
	"path/filepath"

	"github.com/spacemonkeygo/errors"
	"github.com/spacemonkeygo/errors/try"
)

// Runs a function with a tempdir, cleaning up afterward.
func WithDir(f func(string), dirs ...string) {

	if len(dirs) < 1 {
		panic(errors.ProgrammerError.New("Must have at least one sub-folder for tempdir"))
	}

	tempPath := filepath.Join(dirs...)

	// Tempdir wants parent path to exist
	err := os.MkdirAll(tempPath, 0755)
	if err != nil {
		panic(errors.IOError.Wrap(err))
	}

	try.Do(func() {
		f(tempPath)
	}).Finally(func() {
		err := os.RemoveAll(tempPath)
		if err != nil {
			// TODO: we don't want to panic here, more like a debug log entry, "failed to remove tempdir."
			// Can accomplish once we add logging.
			panic(errors.IOError.Wrap(err))
		}
	}).Done()
}

package flak

import (
	"os"
	"path/filepath"

	"github.com/spacemonkeygo/errors"
)

/*
	Creates a directory, calls the given function, and removes the dir again afterward.
	The created path is handed to the given function (cwd is untouched).

	Multiple parent dirs will be created if necessary.  These will not be
	removed afterwards.

	If the function panics, the dir will *not* be removed.  (We're using this
	in the executor code (n.b. todo refactor it to that package) where mounts
	are flying: we strongly want to avoid a recursive remove in case an error
	was raised from mount cleanup!)
*/
func WithDir(f func(string), dirs ...string) {
	// Mkdirs
	if len(dirs) < 1 {
		panic(errors.ProgrammerError.New("WithDir must have at least one sub-directory"))
	}
	tempPath := filepath.Join(dirs...)
	err := os.MkdirAll(tempPath, 0755)
	if err != nil {
		panic(errors.IOError.Wrap(err))
	}

	// Lambda
	f(tempPath)

	// Cleanup
	//  this is intentionally not in a defer or try/finally -- it's critical we *don't* do this for all errors.
	//  specifically, if there's a placer error?  hooooly shit DO NOT proceed on a bunch of deletes;
	//  in a worst case scenario that placer error might have been failure to remove a bind from the host.
	//  and that would leave a wormhole straight to hell which we should really NOT pump energy into.
	err = os.RemoveAll(tempPath)
	if err != nil {
		// TODO: we don't want to panic here, more like a debug log entry, "failed to remove tempdir."
		// Can accomplish once we add logging.
		panic(errors.IOError.Wrap(err))
	}
}

Clean up more gingerly on errors.