<commit_before>package runstats

import "os"
import "fmt"
import "flag"
import "time"
import "strconv"
import "runtime"

import "github.com/bmhatfield/g2s"

var statsd *string = flag.String("statsd", "localhost:8125", "Statsd host:port pair")
var user_prefix *string = flag.String("metric-prefix", "default", "Metric prefix path; detects the local hostname by default")
var pause *int = flag.Int("pause", 10, "Collection pause interval")

var CPU *bool = flag.Bool("cpu", true, "Collect CPU Statistics")
var MEM *bool = flag.Bool("mem", true, "Collect Memory Statistics")
var GC *bool = flag.Bool("gc", true, "Collect GC Statistics (requires Memory be enabled)")

var prefix string
var s g2s.Statter

var err error

func init() {
	go collector()
}

func collector() {
	if !flag.Parsed() {
		flag.Parse()
	}

	s, err = g2s.Dial("udp", *statsd)

	if err != nil {
		panic(fmt.Sprintf("Unable to connect to Statsd on %s - %s", *statsd, err))
	}

	if *user_prefix == "default" {
		prefix, err = os.Hostname()

		if err != nil {
			prefix = "unknown_host.go"
		} else {
			prefix = fmt.Sprintf("%s.go", prefix)
		}
	} else {
		prefix = *user_prefix
	}

	for {
		if *CPU {
			// Goroutines
			s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "cpu.goroutines"), strconv.Itoa(runtime.NumGoroutine()))

			// CGo calls
			s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "cpu.cgo_calls"), strconv.FormatUint(uint64(runtime.NumCgoCall()), 10))
		}

		if *MEM {
			m := new(runtime.MemStats)
			runtime.ReadMemStats(m)

			// General
			s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.alloc"), strconv.FormatUint(m.Alloc, 10))
			s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.total"), strconv.FormatUint(m.TotalAlloc, 10))
			s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.sys"), strconv.FormatUint(m.Sys, 10))
			s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.lookups"), strconv.FormatUint(m.Lookups, 10))
			s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.malloc"), strconv.FormatUint(m.Mallocs, 10))
			s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.frees"), strconv.FormatUint(m.Frees, 10))

			// Heap
			s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.heap.alloc"), strconv.FormatUint(m.HeapAlloc, 10))
			s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.heap.sys"), strconv.FormatUint(m.HeapSys, 10))
			s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.heap.idle"), strconv.FormatUint(m.HeapIdle, 10))
			s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.heap.inuse"), strconv.FormatUint(m.HeapInuse, 10))
			s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.heap.released"), strconv.FormatUint(m.HeapReleased, 10))
			s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.heap.objects"), strconv.FormatUint(m.HeapObjects, 10))

			// Stack
			s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.stack.inuse"), strconv.FormatUint(m.StackInuse, 10))
			s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.stack.sys"), strconv.FormatUint(m.StackSys, 10))
			s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.stack.mspan_inuse"), strconv.FormatUint(m.MSpanInuse, 10))
			s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.stack.mspan_sys"), strconv.FormatUint(m.MSpanSys, 10))
			s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.stack.mcache_inuse"), strconv.FormatUint(m.MCacheInuse, 10))
			s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.stack.mcache_sys"), strconv.FormatUint(m.MCacheSys, 10))

			s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.othersys"), strconv.FormatUint(m.OtherSys, 10))

			if *GC {
				// GC
				s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.gc.sys"), strconv.FormatUint(m.GCSys, 10))
				s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.gc.next"), strconv.FormatUint(m.NextGC, 10))
				s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.gc.last"), strconv.FormatUint(m.LastGC, 10))
				s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.gc.pause_total"), strconv.FormatUint(m.PauseTotalNs, 10))
				s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.gc.pause"), strconv.FormatUint(m.PauseNs[(m.NumGC+255)%256], 10))
				s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.gc.count"), strconv.FormatUint(uint64(m.NumGC), 10))
			}
		}

		// Gauges are a 'snapshot' rather than a histogram. Pausing for some interval
		// aims to get a 'recent' snapshot out before statsd flushes metrics.
		time.Sleep(time.Duration(*pause) * time.Second)
	}
}
<commit_msg>Defer first collector run<commit_after>package runstats

import "os"
import "fmt"
import "flag"
import "time"
import "strconv"
import "runtime"

import "github.com/bmhatfield/g2s"

var statsd *string = flag.String("statsd", "localhost:8125", "Statsd host:port pair")
var user_prefix *string = flag.String("metric-prefix", "default", "Metric prefix path; detects the local hostname by default")
var pause *int = flag.Int("pause", 10, "Collection pause interval")

var CPU *bool = flag.Bool("cpu", true, "Collect CPU Statistics")
var MEM *bool = flag.Bool("mem", true, "Collect Memory Statistics")
var GC *bool = flag.Bool("gc", true, "Collect GC Statistics (requires Memory be enabled)")

var prefix string
var s g2s.Statter

var err error

func init() {
	go collector()
}

func collector() {
	// Defer the first execution of this goroutine until the program
	// finishes initializing.
	runtime.Gosched()

	if !flag.Parsed() {
		flag.Parse()
	}

	s, err = g2s.Dial("udp", *statsd)

	if err != nil {
		panic(fmt.Sprintf("Unable to connect to Statsd on %s - %s", *statsd, err))
	}

	if *user_prefix == "default" {
		prefix, err = os.Hostname()

		if err != nil {
			prefix = "unknown_host.go"
		} else {
			prefix = fmt.Sprintf("%s.go", prefix)
		}
	} else {
		prefix = *user_prefix
	}

	for {
		if *CPU {
			// Goroutines
			s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "cpu.goroutines"), strconv.Itoa(runtime.NumGoroutine()))

			// CGo calls
			s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "cpu.cgo_calls"), strconv.FormatUint(uint64(runtime.NumCgoCall()), 10))
		}

		if *MEM {
			m := new(runtime.MemStats)
			runtime.ReadMemStats(m)

			// General
			s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.alloc"), strconv.FormatUint(m.Alloc, 10))
			s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.total"), strconv.FormatUint(m.TotalAlloc, 10))
			s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.sys"), strconv.FormatUint(m.Sys, 10))
			s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.lookups"), strconv.FormatUint(m.Lookups, 10))
			s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.malloc"), strconv.FormatUint(m.Mallocs, 10))
			s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.frees"), strconv.FormatUint(m.Frees, 10))

			// Heap
			s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.heap.alloc"), strconv.FormatUint(m.HeapAlloc, 10))
			s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.heap.sys"), strconv.FormatUint(m.HeapSys, 10))
			s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.heap.idle"), strconv.FormatUint(m.HeapIdle, 10))
			s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.heap.inuse"), strconv.FormatUint(m.HeapInuse, 10))
			s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.heap.released"), strconv.FormatUint(m.HeapReleased, 10))
			s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.heap.objects"), strconv.FormatUint(m.HeapObjects, 10))

			// Stack
			s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.stack.inuse"), strconv.FormatUint(m.StackInuse, 10))
			s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.stack.sys"), strconv.FormatUint(m.StackSys, 10))
			s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.stack.mspan_inuse"), strconv.FormatUint(m.MSpanInuse, 10))
			s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.stack.mspan_sys"), strconv.FormatUint(m.MSpanSys, 10))
			s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.stack.mcache_inuse"), strconv.FormatUint(m.MCacheInuse, 10))
			s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.stack.mcache_sys"), strconv.FormatUint(m.MCacheSys, 10))

			s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.othersys"), strconv.FormatUint(m.OtherSys, 10))

			if *GC {
				// GC
				s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.gc.sys"), strconv.FormatUint(m.GCSys, 10))
				s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.gc.next"), strconv.FormatUint(m.NextGC, 10))
				s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.gc.last"), strconv.FormatUint(m.LastGC, 10))
				s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.gc.pause_total"), strconv.FormatUint(m.PauseTotalNs, 10))
				s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.gc.pause"), strconv.FormatUint(m.PauseNs[(m.NumGC+255)%256], 10))
				s.Gauge(1.0, fmt.Sprintf("%s.%s", prefix, "mem.gc.count"), strconv.FormatUint(uint64(m.NumGC), 10))
			}
		}

		// Gauges are a 'snapshot' rather than a histogram. Pausing for some interval
		// aims to get a 'recent' snapshot out before statsd flushes metrics.
		time.Sleep(time.Duration(*pause) * time.Second)
	}
}
<|endoftext|>


<commit_before>package templates

var LoggedInHome = `
<!doctype html>
  <html lang="en">
  <head>
    {{template "header" . }}

    <link rel="stylesheet" href="/a/css/kd.css?{{.Version}}" />
    <link rel="stylesheet" href="/a/css/koding.css?{{.Version}}" />
  </head>

  <body class='logged-in'>
    <!--[if IE]><script>(function(){window.location.href='/unsupported.html'})();</script><![endif]-->

    <script>var KD={}</script>
    <script>var KD={"config":{{.Runtime}}}</script>

    <script>KD.isLoggedInOnLoad=true;</script>

    <script>KD.userAccount={{.User.GetWithDefault "Account" "null" }};</script>

    <script>KD.userMachines={{.User.GetWithDefault "Machines" "null"}};</script>

    <script>KD.userWorkspaces={{.User.GetWithDefault "Workspaces" "null"}};</script>

    <script>KD.currentGroup={{.User.GetWithDefault "Group" "null"}};</script>

    <script>
      (function(d) {
        var config = {
          kitId: 'rbd0tum',
          scriptTimeout: 3000
        },
        h=d.documentElement,t=setTimeout(function(){h.className=h.className.replace(/\bwf-loading\b/g,"")+" wf-inactive";},config.scriptTimeout),tk=d.createElement("script"),f=false,s=d.getElementsByTagName("script")[0],a;h.className+=" wf-loading";tk.src='//use.typekit.net/'+config.kitId+'.js';tk.async=true;tk.onload=tk.onreadystatechange=function(){a=this.readyState;if(f||a&&a!="complete"&&a!="loaded")return;f=true;clearTimeout(t);try{Typekit.load(config)}catch(e){}};s.parentNode.insertBefore(tk,s)
      })(document);
    </script>

    {{template "analytics" }}

    <script>KD.socialApiData={{.User.GetWithDefault "SocialApiData" "null"}};</script>

    <script src='/a/js/kd.libs.js?{{.Version}}'></script>
    <script src='/a/js/kd.js?{{.Version}}'></script>
    <script src='/a/js/koding.js?{{.Version}}'></script>

    <script>
      KD.utils.defer(function () {
        KD.currentGroup = KD.remote.revive(KD.currentGroup);
        KD.userAccount = KD.remote.revive(KD.userAccount);
      });
    </script>

    {{template "analytics" }}

    {{if not .Impersonating }}
      <script type="text/javascript">
        var _user_id = '{{.User.GetWithDefault "Username" ""}}'; var _session_id = '{{.User.GetWithDefault "SessionId" ""}}'; var _sift = _sift || []; _sift.push(['_setAccount', 'f270274999']); _sift.push(['_setUserId', _user_id]); _sift.push(['_setSessionId', _session_id]); _sift.push(['_trackPageview']); (function() { function ls() { var e = document.createElement('script'); e.type = 'text/javascript'; e.async = true; e.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'cdn.siftscience.com/s.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(e, s); } if (window.attachEvent) { window.attachEvent('onload', ls); } else { window.addEventListener('load', ls, false); } })();
      </script>
    {{end}}
</body>
</html>
`
<commit_msg>go-webserver: remove duplicate loading for segementio<commit_after>package templates

var LoggedInHome = `
<!doctype html>
  <html lang="en">
  <head>
    {{template "header" . }}

    <link rel="stylesheet" href="/a/css/kd.css?{{.Version}}" />
    <link rel="stylesheet" href="/a/css/koding.css?{{.Version}}" />
  </head>

  <body class='logged-in'>
    <!--[if IE]><script>(function(){window.location.href='/unsupported.html'})();</script><![endif]-->

    <script>var KD={}</script>
    <script>var KD={"config":{{.Runtime}}}</script>

    <script>KD.isLoggedInOnLoad=true;</script>

    <script>KD.userAccount={{.User.GetWithDefault "Account" "null" }};</script>

    <script>KD.userMachines={{.User.GetWithDefault "Machines" "null"}};</script>

    <script>KD.userWorkspaces={{.User.GetWithDefault "Workspaces" "null"}};</script>

    <script>KD.currentGroup={{.User.GetWithDefault "Group" "null"}};</script>

    <script>
      (function(d) {
        var config = {
          kitId: 'rbd0tum',
          scriptTimeout: 3000
        },
        h=d.documentElement,t=setTimeout(function(){h.className=h.className.replace(/\bwf-loading\b/g,"")+" wf-inactive";},config.scriptTimeout),tk=d.createElement("script"),f=false,s=d.getElementsByTagName("script")[0],a;h.className+=" wf-loading";tk.src='//use.typekit.net/'+config.kitId+'.js';tk.async=true;tk.onload=tk.onreadystatechange=function(){a=this.readyState;if(f||a&&a!="complete"&&a!="loaded")return;f=true;clearTimeout(t);try{Typekit.load(config)}catch(e){}};s.parentNode.insertBefore(tk,s)
      })(document);
    </script>

    <script>KD.socialApiData={{.User.GetWithDefault "SocialApiData" "null"}};</script>

    <script src='/a/js/kd.libs.js?{{.Version}}'></script>
    <script src='/a/js/kd.js?{{.Version}}'></script>
    <script src='/a/js/koding.js?{{.Version}}'></script>

    <script>
      KD.utils.defer(function () {
        KD.currentGroup = KD.remote.revive(KD.currentGroup);
        KD.userAccount = KD.remote.revive(KD.userAccount);
      });
    </script>

    {{template "analytics" }}

    {{if not .Impersonating }}
      <script type="text/javascript">
        var _user_id = '{{.User.GetWithDefault "Username" ""}}'; var _session_id = '{{.User.GetWithDefault "SessionId" ""}}'; var _sift = _sift || []; _sift.push(['_setAccount', 'f270274999']); _sift.push(['_setUserId', _user_id]); _sift.push(['_setSessionId', _session_id]); _sift.push(['_trackPageview']); (function() { function ls() { var e = document.createElement('script'); e.type = 'text/javascript'; e.async = true; e.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'cdn.siftscience.com/s.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(e, s); } if (window.attachEvent) { window.attachEvent('onload', ls); } else { window.addEventListener('load', ls, false); } })();
      </script>
    {{end}}
</body>
</html>
`
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"socialapi/workers/common/runner"
	"socialapi/workers/email/sender"

	"github.com/sendgrid/sendgrid-go"
)

var (
	Name = "MailSender"
)

func main() {
	r := runner.New(Name)
	if err := r.Init(); err != nil {
		fmt.Println(err)
		return
	}

	sg := sendgrid.NewSendGridClient(r.Conf.Email.Username, r.Conf.Email.Password)

	constructor := sender.New(r.Log, sg)
	r.SetContext(constructor)
	r.Register(sender.Mail{}).On("send").Handle((*sender.Controller).Send)
	r.Listen()
	r.Wait()
}
<commit_msg>mailsender: main file is rewritten according to new struct<commit_after>package main

import (
	"fmt"
	"socialapi/workers/common/runner"
	"socialapi/workers/email/sender"

	"github.com/sendgrid/sendgrid-go"
)

var (
	Name = "MailSender"
)

func main() {
	r := runner.New(Name)
	if err := r.Init(); err != nil {
		fmt.Println(err)
		return
	}

	sg := sendgrid.NewSendGridClient(r.Conf.Email.Username, r.Conf.Email.Password)
	sgm := &sender.SendGridMail{
		Sendgrid: sg,
	}

	constructor := sender.New(r.Log, sgm)
	r.SetContext(constructor)
	r.Register(sender.Mail{}).On("send").Handle((*sender.Controller).Process)
	r.Listen()
	r.Wait()
}
<|endoftext|>


<commit_before>package raftor

import "golang.org/x/net/context"

// ClusterEventType is an enum describing how the cluster is changing.
type ClusterEventType uint8

const (
	// AddMember is used to describe a cluster change when a node is added.
	AddMember ClusterEventType = iota

	// RemoveMember is used to describe a cluster change when a node is removed.
	RemoveMember

	// UpdateMember is used to describe a cluster change when a node is updated.
	UpdateMember
)

// ClusterChangeEvent is used to store details about a cluster change. It is sent when a new node is detected and after the change has been applied to a raft log.
type ClusterChangeEvent struct {
	Type   ClusterEventType
	Member Member
}

// Cluster maintains an active list of nodes in the cluster. Cluster is also responsible for reporting and responding to changes in cluster membership.
type Cluster interface {

	// ID represents the cluster ID.
	ID() uint64

	// Name returns the Cluster's name
	Name() string

	// GetMember returns a Member instance based on it's ID.
	GetMember(uint64) Member

	// IsBanished checks whether the given ID has been removed from this
	// cluster at some point in the past
	IsBanished(id uint64) bool

	// NotifyChange sends ClusterChangeEvents over the given channel when a node joins, leaves or is updated in the cluster.
	NotifyChange() <-chan ClusterChangeEvent

	// ApplyChange is called after the ClusterChangeEvent has been processed and stored by Raft.
	ApplyChange() chan ClusterChangeEvent

	// Stop stops the cluster and triggers the context when finished
	Stop(context.Context)
}
<commit_msg>Add Notifier and Applier methods to Cluster and updated docs<commit_after>package raftor

import "golang.org/x/net/context"

// ClusterEventType is an enum describing how the cluster is changing.
type ClusterEventType uint8

const (
	// AddMember is used to describe a cluster change when a node is added.
	AddMember ClusterEventType = iota

	// RemoveMember is used to describe a cluster change when a node is removed.
	RemoveMember

	// UpdateMember is used to describe a cluster change when a node is updated.
	UpdateMember
)

// ClusterChangeEvent is used to store details about a cluster change. It is sent when a new node is detected and after the change has been applied to a raft log.
type ClusterChangeEvent struct {
	Type   ClusterEventType
	Member Member
}

// Cluster maintains an active list of nodes in the cluster. Cluster is also responsible for reporting and responding to changes in cluster membership.
type Cluster interface {

	// ID represents the cluster ID.
	ID() uint64

	// Name returns the Cluster's name.
	Name() string

	// GetMember returns a Member instance based on it's ID.
	GetMember(uint64) Member

	// IsBanished checks whether the given ID has been removed from this
	// cluster at some point in the past.
	IsBanished(id uint64) bool

	// Notifier returns a ClusterChangeNotifier and is used to notify the Raft node of cluster changes.
	Notifier() ClusterChangeNotifier

	// Applier returns an Applier which processes Raft log events.
	Applier() Applier

	// ApplyChange is called after the ClusterChangeEvent has been processed and stored by Raft.
	ApplyChange() chan ClusterChangeEvent

	// LocalNode returns the RaftNode which represents the local node of the cluster.
	LocalNode() RaftNode

	// Stop stops the cluster and triggers the context when finished.
	Stop(context.Context)
}
<|endoftext|>


<commit_before>/*
Copyright 2017 The Kubernetes Authors.

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

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

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

package testing

import (
	"fmt"
	"testing"

	"k8s.io/apiserver/pkg/util/feature"
)

// SetFeatureGateDuringTest sets the specified gate to the specified value, and returns a function that restores the original value.
// Failures to set or restore cause the test to fail.
//
// Example use:
//
// defer utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.<FeatureName>, true)()
func SetFeatureGateDuringTest(t *testing.T, gate feature.FeatureGate, f feature.Feature, value bool) func() {
	originalValue := gate.Enabled(f)

	if err := gate.(feature.MutableFeatureGate).Set(fmt.Sprintf("%s=%v", f, value)); err != nil {
		t.Errorf("error setting %s=%v: %v", f, value, err)
	}

	return func() {
		if err := gate.(feature.MutableFeatureGate).Set(fmt.Sprintf("%s=%v", f, originalValue)); err != nil {
			t.Errorf("error restoring %s=%v: %v", f, originalValue, err)
		}
	}
}
<commit_msg>Allow FeatureGate values to be overridden in benchmarks.<commit_after>/*
Copyright 2017 The Kubernetes Authors.

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

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

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

package testing

import (
	"fmt"
	"testing"

	"k8s.io/apiserver/pkg/util/feature"
)

// SetFeatureGateDuringTest sets the specified gate to the specified value, and returns a function that restores the original value.
// Failures to set or restore cause the test to fail.
//
// Example use:
//
// defer utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.<FeatureName>, true)()
func SetFeatureGateDuringTest(tb testing.TB, gate feature.FeatureGate, f feature.Feature, value bool) func() {
	originalValue := gate.Enabled(f)

	if err := gate.(feature.MutableFeatureGate).Set(fmt.Sprintf("%s=%v", f, value)); err != nil {
		tb.Errorf("error setting %s=%v: %v", f, value, err)
	}

	return func() {
		if err := gate.(feature.MutableFeatureGate).Set(fmt.Sprintf("%s=%v", f, originalValue)); err != nil {
			tb.Errorf("error restoring %s=%v: %v", f, originalValue, err)
		}
	}
}
<|endoftext|>


<commit_before>package cmd

import (
	"fmt"
	"os"
	"os/exec"
	"runtime"
	"strings"
	"syscall"

	"github.com/github/hub/Godeps/_workspace/src/github.com/kballard/go-shellquote"
	"github.com/github/hub/utils"
)

type Cmd struct {
	Name string
	Args []string
}

func (cmd Cmd) String() string {
	return fmt.Sprintf("%s %s", cmd.Name, strings.Join(cmd.Args, " "))
}

func (cmd *Cmd) WithArg(arg string) *Cmd {
	cmd.Args = append(cmd.Args, arg)

	return cmd
}

func (cmd *Cmd) WithArgs(args ...string) *Cmd {
	for _, arg := range args {
		cmd.WithArg(arg)
	}

	return cmd
}

func (cmd *Cmd) CombinedOutput() (string, error) {
	output, err := exec.Command(cmd.Name, cmd.Args...).CombinedOutput()

	return string(output), err
}

// Run runs command with `Exec` on platforms except Windows
// which only supports `Spawn`
func (cmd *Cmd) Run() error {
	if runtime.GOOS == "windows" {
		return cmd.Spawn()
	} else {
		return cmd.Exec()
	}
}

// Spawn runs command with spawn(3)
func (cmd *Cmd) Spawn() error {
	c := exec.Command(cmd.Name, cmd.Args...)
	c.Stdin = os.Stdin
	c.Stdout = os.Stdout
	c.Stderr = os.Stderr

	return c.Run()
}

// Exec runs command with exec(3)
// Note that Windows doesn't support exec(3): http://golang.org/src/pkg/syscall/exec_windows.go#L339
func (cmd *Cmd) Exec() error {
	binary, err := exec.LookPath(cmd.Name)
	if err != nil {
		return fmt.Errorf("command not found: %s", cmd.Name)
	}

	args := []string{binary}
	args = append(args, cmd.Args...)

	return syscall.Exec(binary, args, os.Environ())
}

func New(cmd string) *Cmd {
	cmds, err := shellquote.Split(cmd)
	utils.Check(err)

	name := cmds[0]
	args := make([]string, 0)
	for _, arg := range cmds[1:] {
		args = append(args, arg)
	}
	return &Cmd{Name: name, Args: args}
}

func NewWithArray(cmd []string) *Cmd {
	return &Cmd{Name: cmd[0], Args: cmd[1:]}
}
<commit_msg>Have `HUB_VERBOSE` also log externally run commands<commit_after>package cmd

import (
	"fmt"
	"os"
	"os/exec"
	"runtime"
	"strings"
	"syscall"

	"github.com/github/hub/Godeps/_workspace/src/github.com/kballard/go-shellquote"
	"github.com/github/hub/ui"
	"github.com/github/hub/utils"
)

type Cmd struct {
	Name string
	Args []string
}

func (cmd Cmd) String() string {
	return fmt.Sprintf("%s %s", cmd.Name, strings.Join(cmd.Args, " "))
}

func (cmd *Cmd) WithArg(arg string) *Cmd {
	cmd.Args = append(cmd.Args, arg)

	return cmd
}

func (cmd *Cmd) WithArgs(args ...string) *Cmd {
	for _, arg := range args {
		cmd.WithArg(arg)
	}

	return cmd
}

func (cmd *Cmd) CombinedOutput() (string, error) {
	verboseLog(cmd)
	output, err := exec.Command(cmd.Name, cmd.Args...).CombinedOutput()

	return string(output), err
}

// Run runs command with `Exec` on platforms except Windows
// which only supports `Spawn`
func (cmd *Cmd) Run() error {
	if runtime.GOOS == "windows" {
		return cmd.Spawn()
	} else {
		return cmd.Exec()
	}
}

// Spawn runs command with spawn(3)
func (cmd *Cmd) Spawn() error {
	verboseLog(cmd)
	c := exec.Command(cmd.Name, cmd.Args...)
	c.Stdin = os.Stdin
	c.Stdout = os.Stdout
	c.Stderr = os.Stderr

	return c.Run()
}

// Exec runs command with exec(3)
// Note that Windows doesn't support exec(3): http://golang.org/src/pkg/syscall/exec_windows.go#L339
func (cmd *Cmd) Exec() error {
	binary, err := exec.LookPath(cmd.Name)
	if err != nil {
		return fmt.Errorf("command not found: %s", cmd.Name)
	}

	args := []string{binary}
	args = append(args, cmd.Args...)

	verboseLog(cmd)
	return syscall.Exec(binary, args, os.Environ())
}

func New(cmd string) *Cmd {
	cmds, err := shellquote.Split(cmd)
	utils.Check(err)

	name := cmds[0]
	args := make([]string, 0)
	for _, arg := range cmds[1:] {
		args = append(args, arg)
	}
	return &Cmd{Name: name, Args: args}
}

func NewWithArray(cmd []string) *Cmd {
	return &Cmd{Name: cmd[0], Args: cmd[1:]}
}

func verboseLog(cmd *Cmd) {
	if os.Getenv("HUB_VERBOSE") != "" {
		msg := fmt.Sprintf("$ %s %s", cmd.Name, strings.Join(cmd.Args, " "))
		if ui.IsTerminal(os.Stderr) {
			msg = fmt.Sprintf("\033[35m%s\033[0m", msg)
		}
		ui.Errorln(msg)
	}
}
<|endoftext|>


<commit_before>package cmd

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"os"
	"strings"
)

type NotDefinedError string

func (suffix NotDefinedError) Error() string {
	return fmt.Sprintf("metadata: GCE metadata %q not defined", string(suffix))
}

//func Get(suffix string) (string, error) {
//	val, _, err := getETag(suffix)
//	return val, err
//}

func Get(suffix string) (value, err error) {
	client := &http.Client{}
	host = "169.254.169.254"
	url := "http://" + host + "/computeMetadata/v1/" + suffix
	req, _ := http.NewRequest("GET", url, nil)
	req.Header.Set("Metadata-Flavor", "Google")
	res, err := client.Do(req)
	if err != nil {
		return "", "", err
	}
	defer res.Body.Close()
	if res.StatusCode == http.StatusNotFound {
		return "", "", NotDefinedError(suffix)
	}
	if res.StatusCode != 200 {
		return "", "", fmt.Errorf("status code %d trying to fetch %s", res.StatusCode, url)
	}
	all, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return "", "", err
	}
	return string(all), nil
}

func getTrimmed(suffix string) (s string, err error) {
	s, err = Get(suffix)
	s = strings.TrimSpace(s)
	return
}

func lines(suffix string) ([]string, error) {
	j, err := Get(suffix)
	if err != nil {
		return nil, err
	}
	s := strings.Split(strings.TrimSpace(j), "\n")
	for i := range s {
		s[i] = strings.TrimSpace(s[i])
	}
	return s, nil
}

// ProjectID returns the current instance's project ID string.
func ProjectID() (string, error) {
	return getTrimmed("project/project-id")
}

// NumericProjectID returns the current instance's numeric project ID.
func NumericProjectID() (string, error) {
	return getTrimmed("project/numeric-project-id")
}

// InternalIP returns the instance's primary internal IP address.
func InternalIP() (string, error) {
	return getTrimmed("instance/network-interfaces/0/ip")
}

// ExternalIP returns the instance's primary external (public) IP address.
func ExternalIP() (string, error) {
	return getTrimmed("instance/network-interfaces/0/access-configs/0/external-ip")
}

// Hostname returns the instance's hostname. This will be of the form
// "<instanceID>.c.<projID>.internal".
func Hostname() (string, error) {
	return getTrimmed("instance/hostname")
}

// MachineType returns the instance's machine type.
func MachineType() (string, error) {
	machine, err := getTrimmed("instance/machine-type")
	// machine-type is of the form "projects/<projNum>/machineTypes/<machine-typeName>
	if err != nil {
		return "", err
	}
	return machine[strings.LastIndex(machine, "/")+1:], nil
}

// Description returns the instance's description.
func Description() (string, error) {
	return getTrimmed("instance/description")
}

// InstanceTags returns the list of user-defined instance tags,
// assigned when initially creating a GCE instance.
func InstanceTags() ([]string, error) {
	var s []string
	j, err := Get("instance/tags")
	if err != nil {
		return nil, err
	}
	if err := json.NewDecoder(strings.NewReader(j)).Decode(&s); err != nil {
		return nil, err
	}
	return s, nil
}

// InstanceID returns the current VM's numeric instance ID.
func InstanceID() (string, error) {
	return getTrimmed("instance/id")
}

// InstanceName returns the current VM's instance ID string.
func InstanceName() (string, error) {
	host, err := Hostname()
	if err != nil {
		return "", err
	}
	return strings.Split(host, ".")[0], nil
}

// Zone returns the current VM's zone, such as "us-central1-b".
func Zone() (string, error) {
	zone, err := getTrimmed("instance/zone")
	// zone is of the form "projects/<projNum>/zones/<zoneName>".
	if err != nil {
		return "", err
	}
	return zone[strings.LastIndex(zone, "/")+1:], nil
}

// InstanceAttributes returns the list of user-defined attributes,
// assigned when initially creating a GCE VM instance. The value of an
// attribute can be obtained with InstanceAttributeValue.
func InstanceAttributes() ([]string, error) { return lines("instance/attributes/") }

// InstanceAttributeValue returns the value of the provided VM
// instance attribute.
//
// If the requested attribute is not defined, the returned error will
// be of type NotDefinedError.
//
// InstanceAttributeValue may return ("", nil) if the attribute was
// defined to be the empty string.
func InstanceAttributeValue(attr string) (string, error) {
	return Get("instance/attributes/" + attr)
}
<commit_msg>Fixes<commit_after>package cmd

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"
)

type NotDefinedError string

func (suffix NotDefinedError) Error() string {
	return fmt.Sprintf("metadata: GCE metadata %q not defined", string(suffix))
}

//func Get(suffix string) (string, error) {
//	val, _, err := getETag(suffix)
//	return val, err
//}

func Get(suffix string) (value, err error) {
	client := &http.Client{}
	host := "169.254.169.254"
	url := "http://" + host + "/computeMetadata/v1/" + suffix
	req, _ := http.NewRequest("GET", url, nil)
	req.Header.Set("Metadata-Flavor", "Google")
	res, err := client.Do(req)
	if err != nil {
		return "", err
	}
	defer res.Body.Close()
	if res.StatusCode == http.StatusNotFound {
		return "", NotDefinedError(suffix)
	}
	if res.StatusCode != 200 {
		return "", fmt.Errorf("status code %d trying to fetch %s", res.StatusCode, url)
	}
	all, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return "", err
	}
	return string(all), nil
}

func getTrimmed(suffix string) (s string, err error) {
	s, err = Get(suffix)
	s = strings.TrimSpace(s)
	return
}

func lines(suffix string) ([]string, error) {
	j, err := Get(suffix)
	if err != nil {
		return nil, err
	}
	s := strings.Split(strings.TrimSpace(j), "\n")
	for i := range s {
		s[i] = strings.TrimSpace(s[i])
	}
	return s, nil
}

// ProjectID returns the current instance's project ID string.
func ProjectID() (string, error) {
	return getTrimmed("project/project-id")
}

// NumericProjectID returns the current instance's numeric project ID.
func NumericProjectID() (string, error) {
	return getTrimmed("project/numeric-project-id")
}

// InternalIP returns the instance's primary internal IP address.
func InternalIP() (string, error) {
	return getTrimmed("instance/network-interfaces/0/ip")
}

// ExternalIP returns the instance's primary external (public) IP address.
func ExternalIP() (string, error) {
	return getTrimmed("instance/network-interfaces/0/access-configs/0/external-ip")
}

// Hostname returns the instance's hostname. This will be of the form
// "<instanceID>.c.<projID>.internal".
func Hostname() (string, error) {
	return getTrimmed("instance/hostname")
}

// MachineType returns the instance's machine type.
func MachineType() (string, error) {
	machine, err := getTrimmed("instance/machine-type")
	// machine-type is of the form "projects/<projNum>/machineTypes/<machine-typeName>
	if err != nil {
		return "", err
	}
	return machine[strings.LastIndex(machine, "/")+1:], nil
}

// Description returns the instance's description.
func Description() (string, error) {
	return getTrimmed("instance/description")
}

// InstanceTags returns the list of user-defined instance tags,
// assigned when initially creating a GCE instance.
func InstanceTags() ([]string, error) {
	var s []string
	j, err := Get("instance/tags")
	if err != nil {
		return nil, err
	}
	if err := json.NewDecoder(strings.NewReader(j)).Decode(&s); err != nil {
		return nil, err
	}
	return s, nil
}

// InstanceID returns the current VM's numeric instance ID.
func InstanceID() (string, error) {
	return getTrimmed("instance/id")
}

// InstanceName returns the current VM's instance ID string.
func InstanceName() (string, error) {
	host, err := Hostname()
	if err != nil {
		return "", err
	}
	return strings.Split(host, ".")[0], nil
}

// Zone returns the current VM's zone, such as "us-central1-b".
func Zone() (string, error) {
	zone, err := getTrimmed("instance/zone")
	// zone is of the form "projects/<projNum>/zones/<zoneName>".
	if err != nil {
		return "", err
	}
	return zone[strings.LastIndex(zone, "/")+1:], nil
}

// InstanceAttributes returns the list of user-defined attributes,
// assigned when initially creating a GCE VM instance. The value of an
// attribute can be obtained with InstanceAttributeValue.
func InstanceAttributes() ([]string, error) { return lines("instance/attributes/") }

// InstanceAttributeValue returns the value of the provided VM
// instance attribute.
//
// If the requested attribute is not defined, the returned error will
// be of type NotDefinedError.
//
// InstanceAttributeValue may return ("", nil) if the attribute was
// defined to be the empty string.
func InstanceAttributeValue(attr string) (string, error) {
	return Get("instance/attributes/" + attr)
}
<|endoftext|>


<commit_before>package cmd

import (
	"fmt"
	"os"

	"github.com/epy0n0ff/ssh-config-gen/sshconfig"
	"github.com/epy0n0ff/ssh-config-gen/util"
	"github.com/spf13/cobra"
)

func NewGenCommand() *cobra.Command {
	var tomlPath string
	var templatePath string
	var err error

	cmd := &cobra.Command{
		Use:   "gen",
		Short: "A brief description of your application",
		Run: func(cmd *cobra.Command, args []string) {
			g := &gen{
				tomlPath:     tomlPath,
				templatePath: templatePath,
			}

			err = util.ValidateGenCommandArguments(cmd.Flags())
			util.CheckErr(err)
			util.CheckErr(g.Run())
		},
	}

	cmd.PersistentFlags().StringVar(&tomlPath, "toml", "", "hosts.toml file path")
	cmd.PersistentFlags().StringVar(&templatePath, "tpl", "", "ssh configuration template file path")

	return cmd
}

type gen struct {
	tomlPath     string
	templatePath string
}

func (m *gen) Run() error {
	tml, err := sshconfig.LoadToml(m.tomlPath)
	if err != nil {
		fmt.Fprintf(os.Stderr, "load toml file failed: %v", err)
	}
	config, err := sshconfig.GetSshConfig(m.templatePath, tml)
	if err != nil {
		fmt.Fprintf(os.Stderr, "load template file failed: %v", err)
	}

	fmt.Fprintf(os.Stderr, "%s", config)
	return nil
}
<commit_msg>Fix stdout<commit_after>package cmd

import (
	"fmt"
	"os"

	"github.com/epy0n0ff/ssh-config-gen/sshconfig"
	"github.com/epy0n0ff/ssh-config-gen/util"
	"github.com/spf13/cobra"
)

func NewGenCommand() *cobra.Command {
	var tomlPath string
	var templatePath string
	var err error

	cmd := &cobra.Command{
		Use:   "gen",
		Short: "A brief description of your application",
		Run: func(cmd *cobra.Command, args []string) {
			g := &gen{
				tomlPath:     tomlPath,
				templatePath: templatePath,
			}

			err = util.ValidateGenCommandArguments(cmd.Flags())
			util.CheckErr(err)
			util.CheckErr(g.Run())
		},
	}

	cmd.PersistentFlags().StringVar(&tomlPath, "toml", "", "hosts.toml file path")
	cmd.PersistentFlags().StringVar(&templatePath, "tpl", "", "ssh configuration template file path")

	return cmd
}

type gen struct {
	tomlPath     string
	templatePath string
}

func (m *gen) Run() error {
	tml, err := sshconfig.LoadToml(m.tomlPath)
	if err != nil {
		fmt.Fprintf(os.Stderr, "load toml file failed: %v", err)
	}
	config, err := sshconfig.GetSshConfig(m.templatePath, tml)
	if err != nil {
		fmt.Fprintf(os.Stderr, "load template file failed: %v", err)
	}

	fmt.Fprintf(os.Stdout, "%s", config)
	return nil
}
<|endoftext|>


<commit_before>package command

import (
	"net/http"
	"os"
	"runtime"
	"runtime/pprof"
	"strconv"
	"strings"
	"sync"
	"time"

	"github.com/chrislusf/seaweedfs/weed/glog"
	"github.com/chrislusf/seaweedfs/weed/pb/master_pb"
	"github.com/chrislusf/seaweedfs/weed/server"
	"github.com/chrislusf/seaweedfs/weed/storage"
	"github.com/chrislusf/seaweedfs/weed/util"
	"github.com/gorilla/mux"
	"github.com/soheilhy/cmux"
	"google.golang.org/grpc/reflection"
)

type ServerOptions struct {
	cpuprofile *string
}

var (
	serverOptions ServerOptions
	filerOptions  FilerOptions
)

func init() {
	cmdServer.Run = runServer // break init cycle
}

var cmdServer = &Command{
	UsageLine: "server -port=8080 -dir=/tmp -volume.max=5 -ip=server_name",
	Short:     "start a server, including volume server, and automatically elect a master server",
	Long: `start both a volume server to provide storage spaces
  and a master server to provide volume=>location mapping service and sequence number of file ids

  This is provided as a convenient way to start both volume server and master server.
  The servers are exactly the same as starting them separately.

  So other volume servers can use this embedded master server also.

  Optionally, one filer server can be started. Logically, filer servers should not be in a cluster.
  They run with meta data on disk, not shared. So each filer server is different.

  `,
}

var (
	serverIp                      = cmdServer.Flag.String("ip", "localhost", "ip or server name")
	serverBindIp                  = cmdServer.Flag.String("ip.bind", "0.0.0.0", "ip address to bind to")
	serverMaxCpu                  = cmdServer.Flag.Int("maxCpu", 0, "maximum number of CPUs. 0 means all available CPUs")
	serverTimeout                 = cmdServer.Flag.Int("idleTimeout", 30, "connection idle seconds")
	serverDataCenter              = cmdServer.Flag.String("dataCenter", "", "current volume server's data center name")
	serverRack                    = cmdServer.Flag.String("rack", "", "current volume server's rack name")
	serverWhiteListOption         = cmdServer.Flag.String("whiteList", "", "comma separated Ip addresses having write permission. No limit if empty.")
	serverPeers                   = cmdServer.Flag.String("master.peers", "", "other master nodes in comma separated ip:masterPort list")
	serverSecureKey               = cmdServer.Flag.String("secure.secret", "", "secret to encrypt Json Web Token(JWT)")
	serverGarbageThreshold        = cmdServer.Flag.String("garbageThreshold", "0.3", "threshold to vacuum and reclaim spaces")
	masterPort                    = cmdServer.Flag.Int("master.port", 9333, "master server http listen port")
	masterMetaFolder              = cmdServer.Flag.String("master.dir", "", "data directory to store meta data, default to same as -dir specified")
	masterVolumeSizeLimitMB       = cmdServer.Flag.Uint("master.volumeSizeLimitMB", 30*1000, "Master stops directing writes to oversized volumes.")
	masterVolumePreallocate       = cmdServer.Flag.Bool("master.volumePreallocate", false, "Preallocate disk space for volumes.")
	masterDefaultReplicaPlacement = cmdServer.Flag.String("master.defaultReplicaPlacement", "000", "Default replication type if not specified.")
	volumePort                    = cmdServer.Flag.Int("volume.port", 8080, "volume server http listen port")
	volumePublicPort              = cmdServer.Flag.Int("volume.port.public", 0, "volume server public port")
	volumeDataFolders             = cmdServer.Flag.String("dir", os.TempDir(), "directories to store data files. dir[,dir]...")
	volumeMaxDataVolumeCounts     = cmdServer.Flag.String("volume.max", "7", "maximum numbers of volumes, count[,count]...")
	volumePulse                   = cmdServer.Flag.Int("pulseSeconds", 5, "number of seconds between heartbeats")
	volumeIndexType               = cmdServer.Flag.String("volume.index", "memory", "Choose [memory|leveldb|boltdb|btree] mode for memory~performance balance.")
	volumeFixJpgOrientation       = cmdServer.Flag.Bool("volume.images.fix.orientation", true, "Adjust jpg orientation when uploading.")
	volumeReadRedirect            = cmdServer.Flag.Bool("volume.read.redirect", true, "Redirect moved or non-local volumes.")
	volumeServerPublicUrl         = cmdServer.Flag.String("volume.publicUrl", "", "publicly accessible address")
	isStartingFiler               = cmdServer.Flag.Bool("filer", false, "whether to start filer")

	serverWhiteList []string
)

func init() {
	serverOptions.cpuprofile = cmdServer.Flag.String("cpuprofile", "", "cpu profile output file")
	filerOptions.collection = cmdServer.Flag.String("filer.collection", "", "all data will be stored in this collection")
	filerOptions.port = cmdServer.Flag.Int("filer.port", 8888, "filer server http listen port")
	filerOptions.publicPort = cmdServer.Flag.Int("filer.port.public", 0, "filer server public http listen port")
	filerOptions.defaultReplicaPlacement = cmdServer.Flag.String("filer.defaultReplicaPlacement", "", "Default replication type if not specified during runtime.")
	filerOptions.redirectOnRead = cmdServer.Flag.Bool("filer.redirectOnRead", false, "whether proxy or redirect to volume server during file GET request")
	filerOptions.disableDirListing = cmdServer.Flag.Bool("filer.disableDirListing", false, "turn off directory listing")
	filerOptions.maxMB = cmdServer.Flag.Int("filer.maxMB", 32, "split files larger than the limit")
}

func runServer(cmd *Command, args []string) bool {
	filerOptions.secretKey = serverSecureKey
	if *serverOptions.cpuprofile != "" {
		f, err := os.Create(*serverOptions.cpuprofile)
		if err != nil {
			glog.Fatal(err)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}

	if *filerOptions.redirectOnRead {
		*isStartingFiler = true
	}

	master := *serverIp + ":" + strconv.Itoa(*masterPort)
	filerOptions.ip = serverIp

	if *filerOptions.defaultReplicaPlacement == "" {
		*filerOptions.defaultReplicaPlacement = *masterDefaultReplicaPlacement
	}

	if *volumePublicPort == 0 {
		*volumePublicPort = *volumePort
	}

	if *serverMaxCpu < 1 {
		*serverMaxCpu = runtime.NumCPU()
	}
	runtime.GOMAXPROCS(*serverMaxCpu)

	folders := strings.Split(*volumeDataFolders, ",")
	maxCountStrings := strings.Split(*volumeMaxDataVolumeCounts, ",")
	var maxCounts []int
	for _, maxString := range maxCountStrings {
		if max, e := strconv.Atoi(maxString); e == nil {
			maxCounts = append(maxCounts, max)
		} else {
			glog.Fatalf("The max specified in -max not a valid number %s", maxString)
		}
	}
	if len(folders) != len(maxCounts) {
		glog.Fatalf("%d directories by -dir, but only %d max is set by -max", len(folders), len(maxCounts))
	}
	for _, folder := range folders {
		if err := util.TestFolderWritable(folder); err != nil {
			glog.Fatalf("Check Data Folder(-dir) Writable %s : %s", folder, err)
		}
	}

	if *masterVolumeSizeLimitMB > 30*1000 {
		glog.Fatalf("masterVolumeSizeLimitMB should be less than 30000")
	}

	if *masterMetaFolder == "" {
		*masterMetaFolder = folders[0]
	}
	if err := util.TestFolderWritable(*masterMetaFolder); err != nil {
		glog.Fatalf("Check Meta Folder (-mdir=\"%s\") Writable: %s", *masterMetaFolder, err)
	}

	if *serverWhiteListOption != "" {
		serverWhiteList = strings.Split(*serverWhiteListOption, ",")
	}

	if *isStartingFiler {
		go func() {
			time.Sleep(1 * time.Second)

			filerOptions.start()

		}()
	}

	var raftWaitForMaster sync.WaitGroup
	var volumeWait sync.WaitGroup

	raftWaitForMaster.Add(1)
	volumeWait.Add(1)

	go func() {
		r := mux.NewRouter()
		ms := weed_server.NewMasterServer(r, *masterPort, *masterMetaFolder,
			*masterVolumeSizeLimitMB, *masterVolumePreallocate,
			*volumePulse, *masterDefaultReplicaPlacement, *serverGarbageThreshold,
			serverWhiteList, *serverSecureKey,
		)

		glog.V(0).Infoln("Start Seaweed Master", util.VERSION, "at", *serverIp+":"+strconv.Itoa(*masterPort))
		masterListener, e := util.NewListener(*serverBindIp+":"+strconv.Itoa(*masterPort), 0)
		if e != nil {
			glog.Fatalf("Master startup error: %v", e)
		}

		go func() {
			raftWaitForMaster.Wait()
			time.Sleep(100 * time.Millisecond)
			myAddress := *serverIp + ":" + strconv.Itoa(*masterPort)
			var peers []string
			if *serverPeers != "" {
				peers = strings.Split(*serverPeers, ",")
			}
			raftServer := weed_server.NewRaftServer(r, peers, myAddress, *masterMetaFolder, ms.Topo, *volumePulse)
			ms.SetRaftServer(raftServer)
			volumeWait.Done()
		}()

		raftWaitForMaster.Done()

		// start grpc and http server
		m := cmux.New(masterListener)

		grpcL := m.Match(cmux.HTTP2HeaderField("content-type", "application/grpc"))
		httpL := m.Match(cmux.Any())

		// Create your protocol servers.
		grpcS := util.NewGrpcServer()
		master_pb.RegisterSeaweedServer(grpcS, ms)
		reflection.Register(grpcS)

		httpS := &http.Server{Handler: r}

		go grpcS.Serve(grpcL)
		go httpS.Serve(httpL)

		if err := m.Serve(); err != nil {
			glog.Fatalf("master server failed to serve: %v", err)
		}

	}()

	volumeWait.Wait()
	time.Sleep(100 * time.Millisecond)
	if *volumePublicPort == 0 {
		*volumePublicPort = *volumePort
	}
	if *volumeServerPublicUrl == "" {
		*volumeServerPublicUrl = *serverIp + ":" + strconv.Itoa(*volumePublicPort)
	}
	isSeperatedPublicPort := *volumePublicPort != *volumePort
	volumeMux := http.NewServeMux()
	publicVolumeMux := volumeMux
	if isSeperatedPublicPort {
		publicVolumeMux = http.NewServeMux()
	}
	volumeNeedleMapKind := storage.NeedleMapInMemory
	switch *volumeIndexType {
	case "leveldb":
		volumeNeedleMapKind = storage.NeedleMapLevelDb
	case "boltdb":
		volumeNeedleMapKind = storage.NeedleMapBoltDb
	case "btree":
		volumeNeedleMapKind = storage.NeedleMapBtree
	}
	volumeServer := weed_server.NewVolumeServer(volumeMux, publicVolumeMux,
		*serverIp, *volumePort, *volumeServerPublicUrl,
		folders, maxCounts,
		volumeNeedleMapKind,
		[]string{master}, *volumePulse, *serverDataCenter, *serverRack,
		serverWhiteList, *volumeFixJpgOrientation, *volumeReadRedirect,
	)

	glog.V(0).Infoln("Start Seaweed volume server", util.VERSION, "at", *serverIp+":"+strconv.Itoa(*volumePort))
	volumeListener, eListen := util.NewListener(
		*serverBindIp+":"+strconv.Itoa(*volumePort),
		time.Duration(*serverTimeout)*time.Second,
	)
	if eListen != nil {
		glog.Fatalf("Volume server listener error: %v", eListen)
	}
	if isSeperatedPublicPort {
		publicListeningAddress := *serverIp + ":" + strconv.Itoa(*volumePublicPort)
		glog.V(0).Infoln("Start Seaweed volume server", util.VERSION, "public at", publicListeningAddress)
		publicListener, e := util.NewListener(publicListeningAddress, time.Duration(*serverTimeout)*time.Second)
		if e != nil {
			glog.Fatalf("Volume server listener error:%v", e)
		}
		go func() {
			if e := http.Serve(publicListener, publicVolumeMux); e != nil {
				glog.Fatalf("Volume server fail to serve public: %v", e)
			}
		}()
	}

	util.OnInterrupt(func() {
		volumeServer.Shutdown()
		pprof.StopCPUProfile()
	})

	if e := http.Serve(volumeListener, volumeMux); e != nil {
		glog.Fatalf("Volume server fail to serve:%v", e)
	}

	return true
}
<commit_msg>add filer sub dir list size limit to "weed server"<commit_after>package command

import (
	"net/http"
	"os"
	"runtime"
	"runtime/pprof"
	"strconv"
	"strings"
	"sync"
	"time"

	"github.com/chrislusf/seaweedfs/weed/glog"
	"github.com/chrislusf/seaweedfs/weed/pb/master_pb"
	"github.com/chrislusf/seaweedfs/weed/server"
	"github.com/chrislusf/seaweedfs/weed/storage"
	"github.com/chrislusf/seaweedfs/weed/util"
	"github.com/gorilla/mux"
	"github.com/soheilhy/cmux"
	"google.golang.org/grpc/reflection"
)

type ServerOptions struct {
	cpuprofile *string
}

var (
	serverOptions ServerOptions
	filerOptions  FilerOptions
)

func init() {
	cmdServer.Run = runServer // break init cycle
}

var cmdServer = &Command{
	UsageLine: "server -port=8080 -dir=/tmp -volume.max=5 -ip=server_name",
	Short:     "start a server, including volume server, and automatically elect a master server",
	Long: `start both a volume server to provide storage spaces
  and a master server to provide volume=>location mapping service and sequence number of file ids

  This is provided as a convenient way to start both volume server and master server.
  The servers are exactly the same as starting them separately.

  So other volume servers can use this embedded master server also.

  Optionally, one filer server can be started. Logically, filer servers should not be in a cluster.
  They run with meta data on disk, not shared. So each filer server is different.

  `,
}

var (
	serverIp                      = cmdServer.Flag.String("ip", "localhost", "ip or server name")
	serverBindIp                  = cmdServer.Flag.String("ip.bind", "0.0.0.0", "ip address to bind to")
	serverMaxCpu                  = cmdServer.Flag.Int("maxCpu", 0, "maximum number of CPUs. 0 means all available CPUs")
	serverTimeout                 = cmdServer.Flag.Int("idleTimeout", 30, "connection idle seconds")
	serverDataCenter              = cmdServer.Flag.String("dataCenter", "", "current volume server's data center name")
	serverRack                    = cmdServer.Flag.String("rack", "", "current volume server's rack name")
	serverWhiteListOption         = cmdServer.Flag.String("whiteList", "", "comma separated Ip addresses having write permission. No limit if empty.")
	serverPeers                   = cmdServer.Flag.String("master.peers", "", "other master nodes in comma separated ip:masterPort list")
	serverSecureKey               = cmdServer.Flag.String("secure.secret", "", "secret to encrypt Json Web Token(JWT)")
	serverGarbageThreshold        = cmdServer.Flag.String("garbageThreshold", "0.3", "threshold to vacuum and reclaim spaces")
	masterPort                    = cmdServer.Flag.Int("master.port", 9333, "master server http listen port")
	masterMetaFolder              = cmdServer.Flag.String("master.dir", "", "data directory to store meta data, default to same as -dir specified")
	masterVolumeSizeLimitMB       = cmdServer.Flag.Uint("master.volumeSizeLimitMB", 30*1000, "Master stops directing writes to oversized volumes.")
	masterVolumePreallocate       = cmdServer.Flag.Bool("master.volumePreallocate", false, "Preallocate disk space for volumes.")
	masterDefaultReplicaPlacement = cmdServer.Flag.String("master.defaultReplicaPlacement", "000", "Default replication type if not specified.")
	volumePort                    = cmdServer.Flag.Int("volume.port", 8080, "volume server http listen port")
	volumePublicPort              = cmdServer.Flag.Int("volume.port.public", 0, "volume server public port")
	volumeDataFolders             = cmdServer.Flag.String("dir", os.TempDir(), "directories to store data files. dir[,dir]...")
	volumeMaxDataVolumeCounts     = cmdServer.Flag.String("volume.max", "7", "maximum numbers of volumes, count[,count]...")
	volumePulse                   = cmdServer.Flag.Int("pulseSeconds", 5, "number of seconds between heartbeats")
	volumeIndexType               = cmdServer.Flag.String("volume.index", "memory", "Choose [memory|leveldb|boltdb|btree] mode for memory~performance balance.")
	volumeFixJpgOrientation       = cmdServer.Flag.Bool("volume.images.fix.orientation", true, "Adjust jpg orientation when uploading.")
	volumeReadRedirect            = cmdServer.Flag.Bool("volume.read.redirect", true, "Redirect moved or non-local volumes.")
	volumeServerPublicUrl         = cmdServer.Flag.String("volume.publicUrl", "", "publicly accessible address")
	isStartingFiler               = cmdServer.Flag.Bool("filer", false, "whether to start filer")

	serverWhiteList []string
)

func init() {
	serverOptions.cpuprofile = cmdServer.Flag.String("cpuprofile", "", "cpu profile output file")
	filerOptions.collection = cmdServer.Flag.String("filer.collection", "", "all data will be stored in this collection")
	filerOptions.port = cmdServer.Flag.Int("filer.port", 8888, "filer server http listen port")
	filerOptions.publicPort = cmdServer.Flag.Int("filer.port.public", 0, "filer server public http listen port")
	filerOptions.defaultReplicaPlacement = cmdServer.Flag.String("filer.defaultReplicaPlacement", "", "Default replication type if not specified during runtime.")
	filerOptions.redirectOnRead = cmdServer.Flag.Bool("filer.redirectOnRead", false, "whether proxy or redirect to volume server during file GET request")
	filerOptions.disableDirListing = cmdServer.Flag.Bool("filer.disableDirListing", false, "turn off directory listing")
	filerOptions.maxMB = cmdServer.Flag.Int("filer.maxMB", 32, "split files larger than the limit")
	filerOptions.dirListingLimit = cmdServer.Flag.Int("filer.dirListLimit", 1000, "limit sub dir listing size")
}

func runServer(cmd *Command, args []string) bool {
	filerOptions.secretKey = serverSecureKey
	if *serverOptions.cpuprofile != "" {
		f, err := os.Create(*serverOptions.cpuprofile)
		if err != nil {
			glog.Fatal(err)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}

	if *filerOptions.redirectOnRead {
		*isStartingFiler = true
	}

	master := *serverIp + ":" + strconv.Itoa(*masterPort)
	filerOptions.ip = serverIp

	if *filerOptions.defaultReplicaPlacement == "" {
		*filerOptions.defaultReplicaPlacement = *masterDefaultReplicaPlacement
	}

	if *volumePublicPort == 0 {
		*volumePublicPort = *volumePort
	}

	if *serverMaxCpu < 1 {
		*serverMaxCpu = runtime.NumCPU()
	}
	runtime.GOMAXPROCS(*serverMaxCpu)

	folders := strings.Split(*volumeDataFolders, ",")
	maxCountStrings := strings.Split(*volumeMaxDataVolumeCounts, ",")
	var maxCounts []int
	for _, maxString := range maxCountStrings {
		if max, e := strconv.Atoi(maxString); e == nil {
			maxCounts = append(maxCounts, max)
		} else {
			glog.Fatalf("The max specified in -max not a valid number %s", maxString)
		}
	}
	if len(folders) != len(maxCounts) {
		glog.Fatalf("%d directories by -dir, but only %d max is set by -max", len(folders), len(maxCounts))
	}
	for _, folder := range folders {
		if err := util.TestFolderWritable(folder); err != nil {
			glog.Fatalf("Check Data Folder(-dir) Writable %s : %s", folder, err)
		}
	}

	if *masterVolumeSizeLimitMB > 30*1000 {
		glog.Fatalf("masterVolumeSizeLimitMB should be less than 30000")
	}

	if *masterMetaFolder == "" {
		*masterMetaFolder = folders[0]
	}
	if err := util.TestFolderWritable(*masterMetaFolder); err != nil {
		glog.Fatalf("Check Meta Folder (-mdir=\"%s\") Writable: %s", *masterMetaFolder, err)
	}

	if *serverWhiteListOption != "" {
		serverWhiteList = strings.Split(*serverWhiteListOption, ",")
	}

	if *isStartingFiler {
		go func() {
			time.Sleep(1 * time.Second)

			filerOptions.start()

		}()
	}

	var raftWaitForMaster sync.WaitGroup
	var volumeWait sync.WaitGroup

	raftWaitForMaster.Add(1)
	volumeWait.Add(1)

	go func() {
		r := mux.NewRouter()
		ms := weed_server.NewMasterServer(r, *masterPort, *masterMetaFolder,
			*masterVolumeSizeLimitMB, *masterVolumePreallocate,
			*volumePulse, *masterDefaultReplicaPlacement, *serverGarbageThreshold,
			serverWhiteList, *serverSecureKey,
		)

		glog.V(0).Infoln("Start Seaweed Master", util.VERSION, "at", *serverIp+":"+strconv.Itoa(*masterPort))
		masterListener, e := util.NewListener(*serverBindIp+":"+strconv.Itoa(*masterPort), 0)
		if e != nil {
			glog.Fatalf("Master startup error: %v", e)
		}

		go func() {
			raftWaitForMaster.Wait()
			time.Sleep(100 * time.Millisecond)
			myAddress := *serverIp + ":" + strconv.Itoa(*masterPort)
			var peers []string
			if *serverPeers != "" {
				peers = strings.Split(*serverPeers, ",")
			}
			raftServer := weed_server.NewRaftServer(r, peers, myAddress, *masterMetaFolder, ms.Topo, *volumePulse)
			ms.SetRaftServer(raftServer)
			volumeWait.Done()
		}()

		raftWaitForMaster.Done()

		// start grpc and http server
		m := cmux.New(masterListener)

		grpcL := m.Match(cmux.HTTP2HeaderField("content-type", "application/grpc"))
		httpL := m.Match(cmux.Any())

		// Create your protocol servers.
		grpcS := util.NewGrpcServer()
		master_pb.RegisterSeaweedServer(grpcS, ms)
		reflection.Register(grpcS)

		httpS := &http.Server{Handler: r}

		go grpcS.Serve(grpcL)
		go httpS.Serve(httpL)

		if err := m.Serve(); err != nil {
			glog.Fatalf("master server failed to serve: %v", err)
		}

	}()

	volumeWait.Wait()
	time.Sleep(100 * time.Millisecond)
	if *volumePublicPort == 0 {
		*volumePublicPort = *volumePort
	}
	if *volumeServerPublicUrl == "" {
		*volumeServerPublicUrl = *serverIp + ":" + strconv.Itoa(*volumePublicPort)
	}
	isSeperatedPublicPort := *volumePublicPort != *volumePort
	volumeMux := http.NewServeMux()
	publicVolumeMux := volumeMux
	if isSeperatedPublicPort {
		publicVolumeMux = http.NewServeMux()
	}
	volumeNeedleMapKind := storage.NeedleMapInMemory
	switch *volumeIndexType {
	case "leveldb":
		volumeNeedleMapKind = storage.NeedleMapLevelDb
	case "boltdb":
		volumeNeedleMapKind = storage.NeedleMapBoltDb
	case "btree":
		volumeNeedleMapKind = storage.NeedleMapBtree
	}
	volumeServer := weed_server.NewVolumeServer(volumeMux, publicVolumeMux,
		*serverIp, *volumePort, *volumeServerPublicUrl,
		folders, maxCounts,
		volumeNeedleMapKind,
		[]string{master}, *volumePulse, *serverDataCenter, *serverRack,
		serverWhiteList, *volumeFixJpgOrientation, *volumeReadRedirect,
	)

	glog.V(0).Infoln("Start Seaweed volume server", util.VERSION, "at", *serverIp+":"+strconv.Itoa(*volumePort))
	volumeListener, eListen := util.NewListener(
		*serverBindIp+":"+strconv.Itoa(*volumePort),
		time.Duration(*serverTimeout)*time.Second,
	)
	if eListen != nil {
		glog.Fatalf("Volume server listener error: %v", eListen)
	}
	if isSeperatedPublicPort {
		publicListeningAddress := *serverIp + ":" + strconv.Itoa(*volumePublicPort)
		glog.V(0).Infoln("Start Seaweed volume server", util.VERSION, "public at", publicListeningAddress)
		publicListener, e := util.NewListener(publicListeningAddress, time.Duration(*serverTimeout)*time.Second)
		if e != nil {
			glog.Fatalf("Volume server listener error:%v", e)
		}
		go func() {
			if e := http.Serve(publicListener, publicVolumeMux); e != nil {
				glog.Fatalf("Volume server fail to serve public: %v", e)
			}
		}()
	}

	util.OnInterrupt(func() {
		volumeServer.Shutdown()
		pprof.StopCPUProfile()
	})

	if e := http.Serve(volumeListener, volumeMux); e != nil {
		glog.Fatalf("Volume server fail to serve:%v", e)
	}

	return true
}
<|endoftext|>


<commit_before>package cmd

import (
	"os"

	"chkit-v2/chlib"
	"chkit-v2/chlib/requestresults"
	"chkit-v2/helpers"
	"strings"

	"fmt"

	"regexp"

	"github.com/spf13/cobra"
)

var getCmdFile, getCmdKind, getCmdName string

var validOutputFormats = []string{"json", "yaml", "pretty"}

var getCmd = &cobra.Command{
	Use:        "get (KIND [NAME]| --file -f FILE)",
	Short:      "Show info about pod(s), service(s), namespace(s), deployment(s)",
	ValidArgs:  []string{chlib.KindPods, chlib.KindDeployments, chlib.KindNamespaces, chlib.KindService, "--file", "-f"},
	ArgAliases: []string{"po", "pods", "pod", "deployments", "deployment", "deploy", "service", "services", "svc", "ns", "namespaces", "namespace"},
	PreRun: func(cmd *cobra.Command, args []string) {
		if cmd.Flag("output").Changed {
			switch val, _ := cmd.Flags().GetString("output"); val {
			case "json", "yaml", "pretty", "list":
			default:
				np.FEEDBACK.Printf("Invalid output format. Choose from (%s)", strings.Join(validOutputFormats, ", "))
				cmd.Usage()
				os.Exit(1)
			}
		}
		if len(args) == 0 {
			np.FEEDBACK.Println("KIND or file not specified")
			cmd.Usage()
			os.Exit(1)
		}
		switch args[0] {
		case "po", "pods", "pod":
			getCmdKind = chlib.KindPods
		case "deployments", "deployment", "deploy":
			getCmdKind = chlib.KindDeployments
		case "service", "services", "svc":
			getCmdKind = chlib.KindService
		case "ns", "namespaces", "namespace":
			getCmdKind = chlib.KindNamespaces
		default:
			if cmd.Flag("file").Changed {
				getCmdFile, _ = cmd.Flags().GetString("file")
			} else {
				np.FEEDBACK.Printf("Invalid KIND. Choose from (%s)\n", strings.Join(cmd.ArgAliases, ", "))
				cmd.Usage()
				os.Exit(1)
			}
		}
		if len(args) >= 2 && getCmdFile == "" && regexp.MustCompile(chlib.ObjectNameRegex).MatchString(args[1]) {
			getCmdName = args[1]
		} else {
			np.FEEDBACK.Println("NAME is not specified or invalid")
			cmd.Usage()
			os.Exit(1)
		}
	},
	Run: func(cmd *cobra.Command, args []string) {
		client, err := chlib.NewClient(db, helpers.CurrentClientVersion, helpers.UuidV4(), np)
		if err != nil {
			np.ERROR.Println(err)
			return
		}
		var jsonContent []chlib.GenericJson
		if getCmdFile != "" {
			err = chlib.LoadJsonFromFile(getCmdFile, &jsonContent)
		} else {
			nameSpace, _ := cmd.Flags().GetString("namespace")
			jsonContent, err = chlib.GetCmdRequestJson(client, getCmdKind, getCmdName, nameSpace)
		}
		if err != nil {
			np.ERROR.Printf("json receive error: %s\n", err)
			return
		}
		switch format, _ := cmd.Flags().GetString("output"); format {
		case "pretty":
			fieldToSort, _ := cmd.Flags().GetString("sort-by")
			p, err := requestresults.ProcessResponse(jsonContent, strings.ToUpper(fieldToSort), np)
			if err != nil {
				break
			}
			err = p.Print()
		case "json":
			err = chlib.JsonPrettyPrint(jsonContent, np)
		case "yaml":
			err = chlib.YamlPrint(jsonContent, np)
		}
		if err != nil {
			np.ERROR.Println(err)
		}
	},
}

func init() {
	getCmd.PersistentFlags().StringP("output", "o", "pretty", fmt.Sprintf("Output format: %s", strings.Join(validOutputFormats, ", ")))
	cobra.MarkFlagCustom(getCmd.PersistentFlags(), "output", "__chkit_get_outformat")
	getCmd.PersistentFlags().StringP("sort-by", "s", "NAME", "Sort by field. Used only if list printed")
	cobra.MarkFlagCustom(getCmd.PersistentFlags(), "sort-by", "__chkit_get_sort_columns")
	getCmd.PersistentFlags().StringP("namespace", "n", "", "Namespace")
	cobra.MarkFlagCustom(getCmd.PersistentFlags(), "namespace", "__chkit_namespaces_list")
	getCmd.PersistentFlags().StringP("file", "f", "", "JSON file generated on object creation")
	cobra.MarkFlagFilename(getCmd.PersistentFlags(), "file", "json")
	RootCmd.AddCommand(getCmd)
}
<commit_msg>Fix get parameter parsing<commit_after>package cmd

import (
	"os"

	"chkit-v2/chlib"
	"chkit-v2/chlib/requestresults"
	"chkit-v2/helpers"
	"strings"

	"fmt"

	"regexp"

	"github.com/spf13/cobra"
)

var getCmdFile, getCmdKind, getCmdName string

var validOutputFormats = []string{"json", "yaml", "pretty"}

var getCmd = &cobra.Command{
	Use:        "get (KIND [NAME]| --file -f FILE)",
	Short:      "Show info about pod(s), service(s), namespace(s), deployment(s)",
	ValidArgs:  []string{chlib.KindPods, chlib.KindDeployments, chlib.KindNamespaces, chlib.KindService, "--file", "-f"},
	ArgAliases: []string{"po", "pods", "pod", "deployments", "deployment", "deploy", "service", "services", "svc", "ns", "namespaces", "namespace"},
	PreRun: func(cmd *cobra.Command, args []string) {
		if cmd.Flag("output").Changed {
			switch val, _ := cmd.Flags().GetString("output"); val {
			case "json", "yaml", "pretty", "list":
			default:
				np.FEEDBACK.Printf("Invalid output format. Choose from (%s)", strings.Join(validOutputFormats, ", "))
				cmd.Usage()
				os.Exit(1)
			}
		}
		if len(args) == 0 {
			np.FEEDBACK.Println("KIND or file not specified")
			cmd.Usage()
			os.Exit(1)
		}
		switch args[0] {
		case "po", "pods", "pod":
			getCmdKind = chlib.KindPods
		case "deployments", "deployment", "deploy":
			getCmdKind = chlib.KindDeployments
		case "service", "services", "svc":
			getCmdKind = chlib.KindService
		case "ns", "namespaces", "namespace":
			getCmdKind = chlib.KindNamespaces
		default:
			if cmd.Flag("file").Changed {
				getCmdFile, _ = cmd.Flags().GetString("file")
			} else {
				np.FEEDBACK.Printf("Invalid KIND. Choose from (%s)\n", strings.Join(cmd.ArgAliases, ", "))
				cmd.Usage()
				os.Exit(1)
			}
		}
		if len(args) >= 1 && getCmdFile == "" {
			if len(args) >= 2 {
				getCmdName = args[1]
			}
		} else {
			np.FEEDBACK.Println("KIND or FILE is not specified")
			cmd.Usage()
			os.Exit(1)
		}
		if getCmdName != "" && !regexp.MustCompile(chlib.ObjectNameRegex).MatchString(getCmdName) {
			np.FEEDBACK.Println("NAME is invalid")
			cmd.Usage()
			os.Exit(1)
		}
	},
	Run: func(cmd *cobra.Command, args []string) {
		client, err := chlib.NewClient(db, helpers.CurrentClientVersion, helpers.UuidV4(), np)
		if err != nil {
			np.ERROR.Println(err)
			return
		}
		var jsonContent []chlib.GenericJson
		if getCmdFile != "" {
			err = chlib.LoadJsonFromFile(getCmdFile, &jsonContent)
		} else {
			nameSpace, _ := cmd.Flags().GetString("namespace")
			jsonContent, err = chlib.GetCmdRequestJson(client, getCmdKind, getCmdName, nameSpace)
		}
		if err != nil {
			np.ERROR.Printf("json receive error: %s\n", err)
			return
		}
		switch format, _ := cmd.Flags().GetString("output"); format {
		case "pretty":
			fieldToSort, _ := cmd.Flags().GetString("sort-by")
			p, err := requestresults.ProcessResponse(jsonContent, strings.ToUpper(fieldToSort), np)
			if err != nil {
				break
			}
			err = p.Print()
		case "json":
			err = chlib.JsonPrettyPrint(jsonContent, np)
		case "yaml":
			err = chlib.YamlPrint(jsonContent, np)
		}
		if err != nil {
			np.ERROR.Println(err)
		}
	},
}

func init() {
	getCmd.PersistentFlags().StringP("output", "o", "pretty", fmt.Sprintf("Output format: %s", strings.Join(validOutputFormats, ", ")))
	cobra.MarkFlagCustom(getCmd.PersistentFlags(), "output", "__chkit_get_outformat")
	getCmd.PersistentFlags().StringP("sort-by", "s", "NAME", "Sort by field. Used only if list printed")
	cobra.MarkFlagCustom(getCmd.PersistentFlags(), "sort-by", "__chkit_get_sort_columns")
	getCmd.PersistentFlags().StringP("namespace", "n", "", "Namespace")
	cobra.MarkFlagCustom(getCmd.PersistentFlags(), "namespace", "__chkit_namespaces_list")
	getCmd.PersistentFlags().StringP("file", "f", "", "JSON file generated on object creation")
	cobra.MarkFlagFilename(getCmd.PersistentFlags(), "file", "json")
	RootCmd.AddCommand(getCmd)
}
<|endoftext|>


<commit_before>package command

import (
	"fmt"
	"net/http"
	"os/user"
	"strconv"
	"time"

	"github.com/chrislusf/seaweedfs/weed/glog"
	"github.com/chrislusf/seaweedfs/weed/security"
	"github.com/chrislusf/seaweedfs/weed/server"
	"github.com/chrislusf/seaweedfs/weed/util"
	"github.com/spf13/viper"
)

var (
	webDavStandaloneOptions WebDavOption
)

type WebDavOption struct {
	filer          *string
	port           *int
	collection     *string
	tlsPrivateKey  *string
	tlsCertificate *string
}

func init() {
	cmdWebDav.Run = runWebDav // break init cycle
	webDavStandaloneOptions.filer = cmdWebDav.Flag.String("filer", "localhost:8888", "filer server address")
	webDavStandaloneOptions.port = cmdWebDav.Flag.Int("port", 7333, "webdav server http listen port")
	webDavStandaloneOptions.collection = cmdWebDav.Flag.String("collection", "", "collection to create the files")
	webDavStandaloneOptions.tlsPrivateKey = cmdWebDav.Flag.String("key.file", "", "path to the TLS private key file")
	webDavStandaloneOptions.tlsCertificate = cmdWebDav.Flag.String("cert.file", "", "path to the TLS certificate file")
}

var cmdWebDav = &Command{
	UsageLine: "webdav -port=7333 -filer=<ip:port>",
	Short:     "start a webdav server that is backed by a filer",
	Long: `start a webdav server that is backed by a filer.

`,
}

func runWebDav(cmd *Command, args []string) bool {

	weed_server.LoadConfiguration("security", false)

	glog.V(0).Infof("Starting Seaweed WebDav Server %s at https port %d", util.VERSION, *webDavStandaloneOptions.port)

	return webDavStandaloneOptions.startWebDav()

}

func (wo *WebDavOption) startWebDav() bool {

	filerGrpcAddress, err := parseFilerGrpcAddress(*wo.filer)
	if err != nil {
		glog.Fatal(err)
		return false
	}

	// detect current user
	uid, gid := uint32(0), uint32(0)
	if u, err := user.Current(); err == nil {
		if parsedId, pe := strconv.ParseUint(u.Uid, 10, 32); pe == nil {
			uid = uint32(parsedId)
		}
		if parsedId, pe := strconv.ParseUint(u.Gid, 10, 32); pe == nil {
			gid = uint32(parsedId)
		}
	}

	ws, webdavServer_err := weed_server.NewWebDavServer(&weed_server.WebDavOption{
		Filer:            *wo.filer,
		FilerGrpcAddress: filerGrpcAddress,
		GrpcDialOption:   security.LoadClientTLS(viper.Sub("grpc"), "client"),
		Collection:       *wo.collection,
		Uid:              uid,
		Gid:              gid,
	})
	if webdavServer_err != nil {
		glog.Fatalf("WebDav Server startup error: %v", webdavServer_err)
	}

	httpS := &http.Server{Handler: ws.Handler}

	listenAddress := fmt.Sprintf(":%d", *wo.port)
	webDavListener, err := util.NewListener(listenAddress, time.Duration(10)*time.Second)
	if err != nil {
		glog.Fatalf("WebDav Server listener on %s error: %v", listenAddress, err)
	}

	if *wo.tlsPrivateKey != "" {
		glog.V(0).Infof("Start Seaweed WebDav Server %s at https port %d", util.VERSION, *wo.port)
		if err = httpS.ServeTLS(webDavListener, *wo.tlsCertificate, *wo.tlsPrivateKey); err != nil {
			glog.Fatalf("WebDav Server Fail to serve: %v", err)
		}
	} else {
		glog.V(0).Infof("Start Seaweed WebDav Server %s at http port %d", util.VERSION, *wo.port)
		if err = httpS.Serve(webDavListener); err != nil {
			glog.Fatalf("WebDav Server Fail to serve: %v", err)
		}
	}

	return true

}
<commit_msg>mark the webdav as unstable<commit_after>package command

import (
	"fmt"
	"net/http"
	"os/user"
	"strconv"
	"time"

	"github.com/chrislusf/seaweedfs/weed/glog"
	"github.com/chrislusf/seaweedfs/weed/security"
	"github.com/chrislusf/seaweedfs/weed/server"
	"github.com/chrislusf/seaweedfs/weed/util"
	"github.com/spf13/viper"
)

var (
	webDavStandaloneOptions WebDavOption
)

type WebDavOption struct {
	filer          *string
	port           *int
	collection     *string
	tlsPrivateKey  *string
	tlsCertificate *string
}

func init() {
	cmdWebDav.Run = runWebDav // break init cycle
	webDavStandaloneOptions.filer = cmdWebDav.Flag.String("filer", "localhost:8888", "filer server address")
	webDavStandaloneOptions.port = cmdWebDav.Flag.Int("port", 7333, "webdav server http listen port")
	webDavStandaloneOptions.collection = cmdWebDav.Flag.String("collection", "", "collection to create the files")
	webDavStandaloneOptions.tlsPrivateKey = cmdWebDav.Flag.String("key.file", "", "path to the TLS private key file")
	webDavStandaloneOptions.tlsCertificate = cmdWebDav.Flag.String("cert.file", "", "path to the TLS certificate file")
}

var cmdWebDav = &Command{
	UsageLine: "webdav -port=7333 -filer=<ip:port>",
	Short:     "<unstable> start a webdav server that is backed by a filer",
	Long: `start a webdav server that is backed by a filer.

`,
}

func runWebDav(cmd *Command, args []string) bool {

	weed_server.LoadConfiguration("security", false)

	glog.V(0).Infof("Starting Seaweed WebDav Server %s at https port %d", util.VERSION, *webDavStandaloneOptions.port)

	return webDavStandaloneOptions.startWebDav()

}

func (wo *WebDavOption) startWebDav() bool {

	filerGrpcAddress, err := parseFilerGrpcAddress(*wo.filer)
	if err != nil {
		glog.Fatal(err)
		return false
	}

	// detect current user
	uid, gid := uint32(0), uint32(0)
	if u, err := user.Current(); err == nil {
		if parsedId, pe := strconv.ParseUint(u.Uid, 10, 32); pe == nil {
			uid = uint32(parsedId)
		}
		if parsedId, pe := strconv.ParseUint(u.Gid, 10, 32); pe == nil {
			gid = uint32(parsedId)
		}
	}

	ws, webdavServer_err := weed_server.NewWebDavServer(&weed_server.WebDavOption{
		Filer:            *wo.filer,
		FilerGrpcAddress: filerGrpcAddress,
		GrpcDialOption:   security.LoadClientTLS(viper.Sub("grpc"), "client"),
		Collection:       *wo.collection,
		Uid:              uid,
		Gid:              gid,
	})
	if webdavServer_err != nil {
		glog.Fatalf("WebDav Server startup error: %v", webdavServer_err)
	}

	httpS := &http.Server{Handler: ws.Handler}

	listenAddress := fmt.Sprintf(":%d", *wo.port)
	webDavListener, err := util.NewListener(listenAddress, time.Duration(10)*time.Second)
	if err != nil {
		glog.Fatalf("WebDav Server listener on %s error: %v", listenAddress, err)
	}

	if *wo.tlsPrivateKey != "" {
		glog.V(0).Infof("Start Seaweed WebDav Server %s at https port %d", util.VERSION, *wo.port)
		if err = httpS.ServeTLS(webDavListener, *wo.tlsCertificate, *wo.tlsPrivateKey); err != nil {
			glog.Fatalf("WebDav Server Fail to serve: %v", err)
		}
	} else {
		glog.V(0).Infof("Start Seaweed WebDav Server %s at http port %d", util.VERSION, *wo.port)
		if err = httpS.Serve(webDavListener); err != nil {
			glog.Fatalf("WebDav Server Fail to serve: %v", err)
		}
	}

	return true

}
<|endoftext|>


<commit_before>/*
 * Copyright 2016 Frank Wessels <fwessels@xs4all.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 cmd

import (
	"fmt"
	"github.com/s3git/s3git-go"
	"github.com/spf13/cobra"
	"github.com/fatih/color"
)

var oneline bool
var snapshots bool

// logCmd represents the log command
var logCmd = &cobra.Command{
	Use:   "log",
	Short: "Show commit log",
	Long: "Show commit log for the repository",
	Run: func(cmd *cobra.Command, args []string) {

		repo, err := s3git.OpenRepository(".")
		if err != nil {
			er(err)
		}

		options := []s3git.ListCommitOptions{}
		options = append(options, s3git.ListCommitOptionSetOnlySnapshots(true))

		list, err := repo.ListCommits("", options...)
		if err != nil {
			er(err)
		}

		for commit := range list {
			if oneline {
				color.Set(color.FgYellow)
				fmt.Print(commit.Hash)
				color.Unset()
				fmt.Print(" ")
				fmt.Println(commit.Message)

			} else {
				color.Set(color.FgYellow)
				fmt.Println("commit", commit.Hash)
				color.Unset()

				fmt.Println("Date:", commit.TimeStamp)
				fmt.Println()
				fmt.Println("   ", commit.Message)
				fmt.Println()
			}
		}
	},
}

func init() {
	RootCmd.AddCommand(logCmd)

	// Add local message flags
	logCmd.Flags().BoolVarP(&oneline, "pretty", "p", false, "Pretty format")
	logCmd.Flags().BoolVar(&snapshots, "snapshots", false, "Just show snapshot commits")
}
<commit_msg>Pass in value of snapshots flag for commit logging<commit_after>/*
 * Copyright 2016 Frank Wessels <fwessels@xs4all.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 cmd

import (
	"fmt"
	"github.com/s3git/s3git-go"
	"github.com/spf13/cobra"
	"github.com/fatih/color"
)

var oneline bool
var snapshots bool

// logCmd represents the log command
var logCmd = &cobra.Command{
	Use:   "log",
	Short: "Show commit log",
	Long: "Show commit log for the repository",
	Run: func(cmd *cobra.Command, args []string) {

		repo, err := s3git.OpenRepository(".")
		if err != nil {
			er(err)
		}

		options := []s3git.ListCommitOptions{}
		options = append(options, s3git.ListCommitOptionSetOnlySnapshots(snapshots))

		list, err := repo.ListCommits("", options...)
		if err != nil {
			er(err)
		}

		for commit := range list {
			if oneline {
				color.Set(color.FgYellow)
				fmt.Print(commit.Hash)
				color.Unset()
				fmt.Print(" ")
				fmt.Println(commit.Message)

			} else {
				color.Set(color.FgYellow)
				fmt.Println("commit", commit.Hash)
				color.Unset()

				fmt.Println("Date:", commit.TimeStamp)
				fmt.Println()
				fmt.Println("   ", commit.Message)
				fmt.Println()
			}
		}
	},
}

func init() {
	RootCmd.AddCommand(logCmd)

	// Add local message flags
	logCmd.Flags().BoolVarP(&oneline, "pretty", "p", false, "Pretty format")
	logCmd.Flags().BoolVar(&snapshots, "snapshots", false, "Just show snapshot commits")
}
<|endoftext|>


<commit_before>// Copyright © 2017 NAME HERE <EMAIL ADDRESS>
//
// 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 cmd

import (
	"encoding/json"
	"fmt"
	"log"
	"net/http"
	"os"
	"text/tabwriter"

	"github.com/spf13/cobra"
	"github.com/spf13/viper"
)

type RedmineGetTimeEntriesStruct struct {
	TimeEntries []RedmineTimeEntryStruct `json:"time_entries"`
}

type RedmineTimeEntryStruct struct {
	ID        int                   `json:"id"`
	Time      float64               `json:"hours"`
	Comment   string                `json:"comments"`
	Date      string                `json:"spent_on"`
	CreatedOn string                `json:"created_on"`
	UpdatedOn string                `json:"updated_on"`
	Project   RedmineProjectSctruct `json:"project"`
	Issue     RedmineIssueStruct    `json:"issue"`
	User      RedmineUserStruct     `json:"user"`
	Activity  RedmineActivityStruct `json:"activity"`
}

type RedmineProjectSctruct struct {
	ID   int    `json:"id"`
	Name string `json:"name"`
}

type RedmineIssueStruct struct {
	ID int `json:"id"`
}

type RedmineUserStruct struct {
	ID   int    `json:"id"`
	Name string `json:"name"`
}

type RedmineActivityStruct struct {
	ID   int    `json:"id"`
	Name string `json:"name"`
}

func logRun(cmd *cobra.Command, args []string) {
	issueID = getIssueID()

	url := fmt.Sprintf("http://%s/issues/%d/time_entries.json", viper.Get("redmine.url"), issueID)
	request, err := http.NewRequest(http.MethodGet, url, nil)

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

	request.Header.Add("X-Redmine-API-Key", viper.GetString("redmine.access_key"))

	client := &http.Client{}

	response, err := client.Do(request)

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

	defer response.Body.Close()

	payload := new(RedmineGetTimeEntriesStruct)
	err = json.NewDecoder(response.Body).Decode(payload)

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

	w := new(tabwriter.Writer)
	w.Init(os.Stdout, 0, 8, 0, '\t', 0)

	fmt.Fprintln(w, "Date\tTime\tUser Name\tAct. ID\tActivity Name\tComment")
	for _, timeEntry := range payload.TimeEntries {
		fmt.Fprintf(w, "%s", timeEntry.Date)
		fmt.Fprint(w, "\t")
		fmt.Fprintf(w, "%.2f", timeEntry.Time)
		fmt.Fprint(w, "\t")
		fmt.Fprintf(w, "%s", timeEntry.User.Name)
		fmt.Fprint(w, "\t")
		fmt.Fprintf(w, "%d", timeEntry.Activity.ID)
		fmt.Fprint(w, "\t")
		fmt.Fprintf(w, "%s", timeEntry.Activity.Name)
		fmt.Fprintf(w, "%q", timeEntry.Comment)
		fmt.Fprintln(w)
	}
	w.Flush()

}

// logCmd represents the log command
var logCmd = &cobra.Command{
	Use:   "log",
	Short: "Show the appointments of a specific issue",
	Long:  ``,
	Run:   logRun,
}

func init() {
	RootCmd.AddCommand(logCmd)
}
<commit_msg>Fix log list<commit_after>// Copyright © 2017 NAME HERE <EMAIL ADDRESS>
//
// 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 cmd

import (
	"encoding/json"
	"fmt"
	"log"
	"net/http"
	"os"
	"text/tabwriter"

	"github.com/spf13/cobra"
	"github.com/spf13/viper"
)

type RedmineGetTimeEntriesStruct struct {
	TimeEntries []RedmineTimeEntryStruct `json:"time_entries"`
}

type RedmineTimeEntryStruct struct {
	ID        int                   `json:"id"`
	Time      float64               `json:"hours"`
	Comment   string                `json:"comments"`
	Date      string                `json:"spent_on"`
	CreatedOn string                `json:"created_on"`
	UpdatedOn string                `json:"updated_on"`
	Project   RedmineProjectSctruct `json:"project"`
	Issue     RedmineIssueStruct    `json:"issue"`
	User      RedmineUserStruct     `json:"user"`
	Activity  RedmineActivityStruct `json:"activity"`
}

type RedmineProjectSctruct struct {
	ID   int    `json:"id"`
	Name string `json:"name"`
}

type RedmineIssueStruct struct {
	ID int `json:"id"`
}

type RedmineUserStruct struct {
	ID   int    `json:"id"`
	Name string `json:"name"`
}

type RedmineActivityStruct struct {
	ID   int    `json:"id"`
	Name string `json:"name"`
}

func logRun(cmd *cobra.Command, args []string) {
	issueID = getIssueID()

	url := fmt.Sprintf("http://%s/issues/%d/time_entries.json", viper.Get("redmine.url"), issueID)
	request, err := http.NewRequest(http.MethodGet, url, nil)

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

	request.Header.Add("X-Redmine-API-Key", viper.GetString("redmine.access_key"))

	client := &http.Client{}

	response, err := client.Do(request)

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

	defer response.Body.Close()

	payload := new(RedmineGetTimeEntriesStruct)
	err = json.NewDecoder(response.Body).Decode(payload)

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

	w := new(tabwriter.Writer)
	w.Init(os.Stdout, 0, 8, 0, '\t', 0)

	fmt.Fprintln(w, "Date\tTime\tUser Name\tAct. ID\tActivity Name\tComment")
	for _, timeEntry := range payload.TimeEntries {
		fmt.Fprintf(w, "%s", timeEntry.Date)
		fmt.Fprint(w, "\t")
		fmt.Fprintf(w, "%.2f", timeEntry.Time)
		fmt.Fprint(w, "\t")
		fmt.Fprintf(w, "%s", timeEntry.User.Name)
		fmt.Fprint(w, "\t")
		fmt.Fprintf(w, "%d", timeEntry.Activity.ID)
		fmt.Fprint(w, "\t")
		fmt.Fprintf(w, "%s", timeEntry.Activity.Name)
		fmt.Fprint(w, "\t")
		fmt.Fprintf(w, "%q", timeEntry.Comment)
		fmt.Fprintln(w)
	}
	w.Flush()

}

// logCmd represents the log command
var logCmd = &cobra.Command{
	Use:   "log",
	Short: "Show the appointments of a specific issue",
	Long:  ``,
	Run:   logRun,
}

func init() {
	RootCmd.AddCommand(logCmd)
}
<|endoftext|>


<commit_before>package cmd

import (
	"fmt"
	ctx "github.com/smira/aptly/context"
	"github.com/smira/commander"
)

// Run runs single command starting from root cmd with args, optionally initializing context
func Run(cmd *commander.Command, cmdArgs []string, initContext bool) (returnCode int) {
	defer func() {
		if r := recover(); r != nil {
			fatal, ok := r.(*ctx.FatalError)
			if !ok {
				panic(r)
			}
			fmt.Println("ERROR:", fatal.Message)
			returnCode = fatal.ReturnCode
		}
	}()

	returnCode = 0

	flags, args, err := cmd.ParseFlags(cmdArgs)
	if err != nil {
		ctx.Fatal(err)
	}

	if initContext {
		err = InitContext(flags)
		if err != nil {
			ctx.Fatal(err)
		}
		defer ShutdownContext()
	}

	context.UpdateFlags(flags)

	err = cmd.Dispatch(args)
	if err != nil {
		ctx.Fatal(err)
	}

	return
}
<commit_msg>Send error messages to stderr. #249<commit_after>package cmd

import (
	"fmt"
	ctx "github.com/smira/aptly/context"
	"github.com/smira/commander"
	"os"
)

// Run runs single command starting from root cmd with args, optionally initializing context
func Run(cmd *commander.Command, cmdArgs []string, initContext bool) (returnCode int) {
	defer func() {
		if r := recover(); r != nil {
			fatal, ok := r.(*ctx.FatalError)
			if !ok {
				panic(r)
			}
			fmt.Fprintln(os.Stderr, "ERROR:", fatal.Message)
			returnCode = fatal.ReturnCode
		}
	}()

	returnCode = 0

	flags, args, err := cmd.ParseFlags(cmdArgs)
	if err != nil {
		ctx.Fatal(err)
	}

	if initContext {
		err = InitContext(flags)
		if err != nil {
			ctx.Fatal(err)
		}
		defer ShutdownContext()
	}

	context.UpdateFlags(flags)

	err = cmd.Dispatch(args)
	if err != nil {
		ctx.Fatal(err)
	}

	return
}
<|endoftext|>


<commit_before>package main

import (
	"bufio"
	"errors"
	"fmt"
	"log"
	"os"
	"path"
	"strings"

	"github.com/mijia/modelq/drivers"
)

type CodeResult struct {
	name string
	err  error
}

type CodeConfig struct {
	packageName    string
	touchTimestamp bool
}

func generateModels(dbName string, dbSchema drivers.DbSchema, config CodeConfig) {
	if fs, err := os.Stat(config.packageName); err != nil || !fs.IsDir() {
		os.Mkdir(config.packageName, os.ModeDir|os.ModePerm)
	}

	jobs := make(chan CodeResult)
	for tbl, cols := range dbSchema {
		go func(tableName string, schema drivers.TableSchema) {
			err := generateModel(dbName, tableName, schema, config)
			jobs <- CodeResult{tableName, err}
		}(tbl, cols)
	}

	for i := 0; i < len(dbSchema); i++ {
		result := <-jobs
		if result.err != nil {
			log.Printf("Error when generating code for %s, %s", result.name, result.err)
		} else {
			log.Printf("Code generated for table %s, into package %s/%s.go", result.name, config.packageName, result.name)
		}
	}
	close(jobs)
}

func generateModel(dbName, tName string, schema drivers.TableSchema, config CodeConfig) error {
	file, err := os.Create(path.Join(config.packageName, tName+".go"))
	if err != nil {
		return err
	}
	w := bufio.NewWriter(file)

	defer func() {
		w.Flush()
		file.Close()
	}()

	model := ModelMeta{
		Name:      toCapitalCase(tName),
		DbName:    dbName,
		TableName: tName,
		Fields:    make([]ModelField, len(schema)),
		config:    config,
	}
	needTime := false
	for i, col := range schema {
		field := ModelField{
			Name:            toCapitalCase(col.ColumnName),
			ColumnName:      col.ColumnName,
			Type:            col.DataType,
			JsonMeta:        fmt.Sprintf("`json:\"%s\"`", col.ColumnName),
			IsPrimaryKey:    strings.ToUpper(col.ColumnKey) == "PRI",
			IsAutoIncrement: strings.ToUpper(col.Extra) == "AUTO_INCREMENT",
			DefaultValue:    col.DefaultValue,
			Extra:           col.Extra,
			Comment:         col.Comment,
		}
		if field.Type == "time.Time" {
			needTime = true
		}
		if field.IsPrimaryKey {
			model.PrimaryField = &field
		}
		model.Fields[i] = field
	}

	if err := model.GenHeader(w, needTime); err != nil {
		return fmt.Errorf("[%s] Fail to gen model header, %s", tName, err)
	}
	if err := model.GenStruct(w); err != nil {
		return fmt.Errorf("[%s] Fail to gen model struct, %s", tName, err)
	}
	if err := model.GenObjectApi(w); err != nil {
		return fmt.Errorf("[%s] Fail to gen model object api, %s", tName, err)
	}
	if err := model.GenQueryApi(w); err != nil {
		return fmt.Errorf("[%s] Fail to gen model query api, %s", tName, err)
	}
	if err := model.GenManagedObjApi(w); err != nil {
		return fmt.Errorf("[%s] Fail to gen model managed objects api, %s", tName, err)
	}

	return nil
}

type ModelField struct {
	Name            string
	ColumnName      string
	Type            string
	JsonMeta        string
	IsPrimaryKey    bool
	IsAutoIncrement bool
	DefaultValue    string
	Extra           string
	Comment         string
}

func (f ModelField) ConverterFuncName() string {
	convertors := map[string]string{
		"int64":     "AsInt64",
		"int":       "AsInt",
		"string":    "AsString",
		"time.Time": "AsTime",
		"float64":   "AsFloat64",
		"bool":      "AsBool",
	}
	if c, ok := convertors[f.Type]; ok {
		return c
	}
	return "AsString"
}

type ModelMeta struct {
	Name         string
	DbName       string
	TableName    string
	PrimaryField *ModelField
	Fields       []ModelField
	config       CodeConfig
}

func (m ModelMeta) HasAutoIncrementPrimaryKey() bool {
	return m.PrimaryField != nil && m.PrimaryField.IsAutoIncrement
}

func (m ModelMeta) AllFields() string {
	fields := make([]string, len(m.Fields))
	for i, f := range m.Fields {
		fields[i] = fmt.Sprintf("\"%s\"", f.Name)
	}
	return strings.Join(fields, ", ")
}

func (m ModelMeta) InsertableFields() string {
	fields := make([]string, 0, len(m.Fields))
	for _, f := range m.Fields {
		if f.IsPrimaryKey && f.IsAutoIncrement {
			continue
		}
		autoTimestamp := strings.ToUpper(f.DefaultValue) == "CURRENT_TIMESTAMP" ||
			strings.ToUpper(f.DefaultValue) == "NOW()"
		if f.Type == "time.Time" && autoTimestamp && !m.config.touchTimestamp {
			continue
		}
		fields = append(fields, fmt.Sprintf("\"%s\"", f.Name))
	}
	return strings.Join(fields, ", ")
}

func (m ModelMeta) UpdatableFields() string {
	fields := make([]string, 0, len(m.Fields))
	for _, f := range m.Fields {
		if f.IsPrimaryKey {
			continue
		}
		autoUpdateTime := strings.ToUpper(f.Extra) == "ON UPDATE CURRENT_TIMESTAMP"
		if autoUpdateTime && !m.config.touchTimestamp {
			continue
		}
		fields = append(fields, fmt.Sprintf("\"%s\"", f.Name))
	}
	return strings.Join(fields, ", ")
}

func (m ModelMeta) GenHeader(w *bufio.Writer, importTime bool) error {
	return tmHeader.Execute(w, map[string]interface{}{
		"DbName":     m.DbName,
		"TableName":  m.TableName,
		"PkgName":    m.config.packageName,
		"ImportTime": importTime,
	})
}

func (m ModelMeta) GenStruct(w *bufio.Writer) error {
	return tmStruct.Execute(w, m)
}

func (m ModelMeta) GenObjectApi(w *bufio.Writer) error {
	return tmObjApi.Execute(w, m)
}

func (m ModelMeta) GenQueryApi(w *bufio.Writer) error {
	return tmQueryApi.Execute(w, m)
}

func (m ModelMeta) GenManagedObjApi(w *bufio.Writer) error {
	return tmManagedObjApi.Execute(w, m)
}

func toCapitalCase(name string) string {
	// cp___hello_12jiu -> CpHello12Jiu
	data := []byte(name)
	segStart := true
	endPos := 0
	for i := 0; i < len(data); i++ {
		ch := data[i]
		if (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') {
			if segStart {
				if ch >= 'a' && ch <= 'z' {
					ch = ch - 'a' + 'A'
				}
				segStart = false
			} else {
				if ch >= 'A' && ch <= 'Z' {
					ch = ch - 'A' + 'a'
				}
			}
			data[endPos] = ch
			endPos++
		} else if ch >= '0' && ch <= '9' {
			data[endPos] = ch
			endPos++
			segStart = true
		} else {
			segStart = true
		}
	}
	return string(data[:endPos])
}
<commit_msg>Fix the dummy importion<commit_after>package main

import (
	"bufio"
	"fmt"
	"log"
	"os"
	"path"
	"strings"

	"github.com/mijia/modelq/drivers"
)

type CodeResult struct {
	name string
	err  error
}

type CodeConfig struct {
	packageName    string
	touchTimestamp bool
}

func generateModels(dbName string, dbSchema drivers.DbSchema, config CodeConfig) {
	if fs, err := os.Stat(config.packageName); err != nil || !fs.IsDir() {
		os.Mkdir(config.packageName, os.ModeDir|os.ModePerm)
	}

	jobs := make(chan CodeResult)
	for tbl, cols := range dbSchema {
		go func(tableName string, schema drivers.TableSchema) {
			err := generateModel(dbName, tableName, schema, config)
			jobs <- CodeResult{tableName, err}
		}(tbl, cols)
	}

	for i := 0; i < len(dbSchema); i++ {
		result := <-jobs
		if result.err != nil {
			log.Printf("Error when generating code for %s, %s", result.name, result.err)
		} else {
			log.Printf("Code generated for table %s, into package %s/%s.go", result.name, config.packageName, result.name)
		}
	}
	close(jobs)
}

func generateModel(dbName, tName string, schema drivers.TableSchema, config CodeConfig) error {
	file, err := os.Create(path.Join(config.packageName, tName+".go"))
	if err != nil {
		return err
	}
	w := bufio.NewWriter(file)

	defer func() {
		w.Flush()
		file.Close()
	}()

	model := ModelMeta{
		Name:      toCapitalCase(tName),
		DbName:    dbName,
		TableName: tName,
		Fields:    make([]ModelField, len(schema)),
		config:    config,
	}
	needTime := false
	for i, col := range schema {
		field := ModelField{
			Name:            toCapitalCase(col.ColumnName),
			ColumnName:      col.ColumnName,
			Type:            col.DataType,
			JsonMeta:        fmt.Sprintf("`json:\"%s\"`", col.ColumnName),
			IsPrimaryKey:    strings.ToUpper(col.ColumnKey) == "PRI",
			IsAutoIncrement: strings.ToUpper(col.Extra) == "AUTO_INCREMENT",
			DefaultValue:    col.DefaultValue,
			Extra:           col.Extra,
			Comment:         col.Comment,
		}
		if field.Type == "time.Time" {
			needTime = true
		}
		if field.IsPrimaryKey {
			model.PrimaryField = &field
		}
		model.Fields[i] = field
	}

	if err := model.GenHeader(w, needTime); err != nil {
		return fmt.Errorf("[%s] Fail to gen model header, %s", tName, err)
	}
	if err := model.GenStruct(w); err != nil {
		return fmt.Errorf("[%s] Fail to gen model struct, %s", tName, err)
	}
	if err := model.GenObjectApi(w); err != nil {
		return fmt.Errorf("[%s] Fail to gen model object api, %s", tName, err)
	}
	if err := model.GenQueryApi(w); err != nil {
		return fmt.Errorf("[%s] Fail to gen model query api, %s", tName, err)
	}
	if err := model.GenManagedObjApi(w); err != nil {
		return fmt.Errorf("[%s] Fail to gen model managed objects api, %s", tName, err)
	}

	return nil
}

type ModelField struct {
	Name            string
	ColumnName      string
	Type            string
	JsonMeta        string
	IsPrimaryKey    bool
	IsAutoIncrement bool
	DefaultValue    string
	Extra           string
	Comment         string
}

func (f ModelField) ConverterFuncName() string {
	convertors := map[string]string{
		"int64":     "AsInt64",
		"int":       "AsInt",
		"string":    "AsString",
		"time.Time": "AsTime",
		"float64":   "AsFloat64",
		"bool":      "AsBool",
	}
	if c, ok := convertors[f.Type]; ok {
		return c
	}
	return "AsString"
}

type ModelMeta struct {
	Name         string
	DbName       string
	TableName    string
	PrimaryField *ModelField
	Fields       []ModelField
	config       CodeConfig
}

func (m ModelMeta) HasAutoIncrementPrimaryKey() bool {
	return m.PrimaryField != nil && m.PrimaryField.IsAutoIncrement
}

func (m ModelMeta) AllFields() string {
	fields := make([]string, len(m.Fields))
	for i, f := range m.Fields {
		fields[i] = fmt.Sprintf("\"%s\"", f.Name)
	}
	return strings.Join(fields, ", ")
}

func (m ModelMeta) InsertableFields() string {
	fields := make([]string, 0, len(m.Fields))
	for _, f := range m.Fields {
		if f.IsPrimaryKey && f.IsAutoIncrement {
			continue
		}
		autoTimestamp := strings.ToUpper(f.DefaultValue) == "CURRENT_TIMESTAMP" ||
			strings.ToUpper(f.DefaultValue) == "NOW()"
		if f.Type == "time.Time" && autoTimestamp && !m.config.touchTimestamp {
			continue
		}
		fields = append(fields, fmt.Sprintf("\"%s\"", f.Name))
	}
	return strings.Join(fields, ", ")
}

func (m ModelMeta) UpdatableFields() string {
	fields := make([]string, 0, len(m.Fields))
	for _, f := range m.Fields {
		if f.IsPrimaryKey {
			continue
		}
		autoUpdateTime := strings.ToUpper(f.Extra) == "ON UPDATE CURRENT_TIMESTAMP"
		if autoUpdateTime && !m.config.touchTimestamp {
			continue
		}
		fields = append(fields, fmt.Sprintf("\"%s\"", f.Name))
	}
	return strings.Join(fields, ", ")
}

func (m ModelMeta) GenHeader(w *bufio.Writer, importTime bool) error {
	return tmHeader.Execute(w, map[string]interface{}{
		"DbName":     m.DbName,
		"TableName":  m.TableName,
		"PkgName":    m.config.packageName,
		"ImportTime": importTime,
	})
}

func (m ModelMeta) GenStruct(w *bufio.Writer) error {
	return tmStruct.Execute(w, m)
}

func (m ModelMeta) GenObjectApi(w *bufio.Writer) error {
	return tmObjApi.Execute(w, m)
}

func (m ModelMeta) GenQueryApi(w *bufio.Writer) error {
	return tmQueryApi.Execute(w, m)
}

func (m ModelMeta) GenManagedObjApi(w *bufio.Writer) error {
	return tmManagedObjApi.Execute(w, m)
}

func toCapitalCase(name string) string {
	// cp___hello_12jiu -> CpHello12Jiu
	data := []byte(name)
	segStart := true
	endPos := 0
	for i := 0; i < len(data); i++ {
		ch := data[i]
		if (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') {
			if segStart {
				if ch >= 'a' && ch <= 'z' {
					ch = ch - 'a' + 'A'
				}
				segStart = false
			} else {
				if ch >= 'A' && ch <= 'Z' {
					ch = ch - 'A' + 'a'
				}
			}
			data[endPos] = ch
			endPos++
		} else if ch >= '0' && ch <= '9' {
			data[endPos] = ch
			endPos++
			segStart = true
		} else {
			segStart = true
		}
	}
	return string(data[:endPos])
}
<|endoftext|>