Browse Source

Merge pull request #5008 from gyuho/gosimple_unused

clean up with gosimple and unused
Gyu-Ho Lee 9 years ago
parent
commit
4041bbe571

+ 2 - 0
.travis.yml

@@ -22,6 +22,8 @@ env:
 
 
 before_install:
 before_install:
  - go get -v github.com/chzchzchz/goword
  - go get -v github.com/chzchzchz/goword
+ - go get -v honnef.co/go/simple/cmd/gosimple
+ - go get -v honnef.co/go/unused/cmd/unused
 
 
 # disable godep restore override
 # disable godep restore override
 install:
 install:

+ 0 - 10
clientv3/concurrency/key.go

@@ -45,16 +45,6 @@ func NewUniqueKV(ctx context.Context, kv v3.KV, pfx, val string, opts ...v3.OpOp
 	}
 	}
 }
 }
 
 
-func waitUpdate(ctx context.Context, client *v3.Client, key string, opts ...v3.OpOption) error {
-	cctx, cancel := context.WithCancel(ctx)
-	defer cancel()
-	wresp, ok := <-client.Watch(cctx, key, opts...)
-	if !ok {
-		return ctx.Err()
-	}
-	return wresp.Err()
-}
-
 func waitDelete(ctx context.Context, client *v3.Client, key string, rev int64) error {
 func waitDelete(ctx context.Context, client *v3.Client, key string, rev int64) error {
 	cctx, cancel := context.WithCancel(ctx)
 	cctx, cancel := context.WithCancel(ctx)
 	defer cancel()
 	defer cancel()

+ 5 - 3
contrib/systemd/etcd2-backup-coreos/etcd2-restore.go

@@ -81,9 +81,11 @@ func restoreEtcd() error {
 	return runCommands(10, 2*time.Second)
 	return runCommands(10, 2*time.Second)
 }
 }
 
 
-var clusterHealthRegex = regexp.MustCompile(".*cluster is healthy.*")
-var lineSplit = regexp.MustCompile("\n+")
-var colonSplit = regexp.MustCompile("\\:")
+var (
+	clusterHealthRegex = regexp.MustCompile(".*cluster is healthy.*")
+	lineSplit          = regexp.MustCompile("\n+")
+	colonSplit         = regexp.MustCompile(`\:`)
+)
 
 
 func runCommands(maxRetry int, interval time.Duration) error {
 func runCommands(maxRetry int, interval time.Duration) error {
 	var retryCnt int
 	var retryCnt int

+ 1 - 1
etcdctl/ctlv2/command/cluster_health.go

@@ -104,7 +104,7 @@ func handleClusterHealth(c *cli.Context) {
 				}
 				}
 
 
 				checked = true
 				checked = true
-				if result.Health == "true" || nresult.Health == true {
+				if result.Health == "true" || nresult.Health {
 					health = true
 					health = true
 					fmt.Printf("member %s is healthy: got healthy result from %s\n", m.ID, url)
 					fmt.Printf("member %s is healthy: got healthy result from %s\n", m.ID, url)
 				} else {
 				} else {

+ 0 - 12
etcdctl/ctlv2/command/util.go

@@ -42,18 +42,6 @@ var (
 	defaultDialTimeout = 30 * time.Second
 	defaultDialTimeout = 30 * time.Second
 )
 )
 
 
-// trimsplit slices s into all substrings separated by sep and returns a
-// slice of the substrings between the separator with all leading and trailing
-// white space removed, as defined by Unicode.
-func trimsplit(s, sep string) []string {
-	raw := strings.Split(s, ",")
-	trimmed := make([]string, 0)
-	for _, r := range raw {
-		trimmed = append(trimmed, strings.TrimSpace(r))
-	}
-	return trimmed
-}
-
 func argOrStdin(args []string, stdin io.Reader, i int) (string, error) {
 func argOrStdin(args []string, stdin io.Reader, i int) (string, error) {
 	if i < len(args) {
 	if i < len(args) {
 		return args[i], nil
 		return args[i], nil

+ 1 - 4
etcdctl/ctlv3/command/member_command.go

@@ -22,10 +22,7 @@ import (
 	"github.com/spf13/cobra"
 	"github.com/spf13/cobra"
 )
 )
 
 
-var (
-	memberID       uint64
-	memberPeerURLs string
-)
+var memberPeerURLs string
 
 
 // NewMemberCommand returns the cobra command for "member".
 // NewMemberCommand returns the cobra command for "member".
 func NewMemberCommand() *cobra.Command {
 func NewMemberCommand() *cobra.Command {

+ 3 - 2
etcdserver/auth/auth_test.go

@@ -416,7 +416,7 @@ func TestCreateAndUpdateUser(t *testing.T) {
 
 
 	s := store{server: d, timeout: testTimeout, ensuredOnce: true, PasswordStore: fastPasswordStore{}}
 	s := store{server: d, timeout: testTimeout, ensuredOnce: true, PasswordStore: fastPasswordStore{}}
 	out, created, err := s.CreateOrUpdateUser(user)
 	out, created, err := s.CreateOrUpdateUser(user)
-	if created == false {
+	if !created {
 		t.Error("Should have created user, instead updated?")
 		t.Error("Should have created user, instead updated?")
 	}
 	}
 	if err != nil {
 	if err != nil {
@@ -427,7 +427,7 @@ func TestCreateAndUpdateUser(t *testing.T) {
 		t.Error("UpdateUser doesn't match given update. Got", out, "expected", expected)
 		t.Error("UpdateUser doesn't match given update. Got", out, "expected", expected)
 	}
 	}
 	out, created, err = s.CreateOrUpdateUser(update)
 	out, created, err = s.CreateOrUpdateUser(update)
-	if created == true {
+	if created {
 		t.Error("Should have updated user, instead created?")
 		t.Error("Should have updated user, instead created?")
 	}
 	}
 	if err != nil {
 	if err != nil {
@@ -572,6 +572,7 @@ func TestEnableAuth(t *testing.T) {
 		t.Error("Unexpected error", err)
 		t.Error("Unexpected error", err)
 	}
 	}
 }
 }
+
 func TestDisableAuth(t *testing.T) {
 func TestDisableAuth(t *testing.T) {
 	trueval := "true"
 	trueval := "true"
 	falseval := "false"
 	falseval := "false"

+ 2 - 6
etcdserver/membership/cluster.go

@@ -144,9 +144,7 @@ func (c *RaftCluster) PeerURLs() []string {
 	defer c.Unlock()
 	defer c.Unlock()
 	urls := make([]string, 0)
 	urls := make([]string, 0)
 	for _, p := range c.members {
 	for _, p := range c.members {
-		for _, addr := range p.PeerURLs {
-			urls = append(urls, addr)
-		}
+		urls = append(urls, p.PeerURLs...)
 	}
 	}
 	sort.Strings(urls)
 	sort.Strings(urls)
 	return urls
 	return urls
@@ -159,9 +157,7 @@ func (c *RaftCluster) ClientURLs() []string {
 	defer c.Unlock()
 	defer c.Unlock()
 	urls := make([]string, 0)
 	urls := make([]string, 0)
 	for _, p := range c.members {
 	for _, p := range c.members {
-		for _, url := range p.ClientURLs {
-			urls = append(urls, url)
-		}
+		urls = append(urls, p.ClientURLs...)
 	}
 	}
 	sort.Strings(urls)
 	sort.Strings(urls)
 	return urls
 	return urls

+ 1 - 1
etcdserver/stats/queue.go

@@ -85,7 +85,7 @@ func (q *statsQueue) Rate() (float64, float64) {
 		return 0, 0
 		return 0, 0
 	}
 	}
 
 
-	if time.Now().Sub(back.SendingTime) > time.Second {
+	if time.Since(back.SendingTime) > time.Second {
 		q.Clear()
 		q.Clear()
 		return 0, 0
 		return 0, 0
 	}
 	}

+ 1 - 1
etcdserver/stats/server.go

@@ -57,7 +57,7 @@ func (ss *ServerStats) JSON() []byte {
 	ss.Lock()
 	ss.Lock()
 	stats := *ss
 	stats := *ss
 	ss.Unlock()
 	ss.Unlock()
-	stats.LeaderInfo.Uptime = time.Now().Sub(stats.LeaderInfo.StartTime).String()
+	stats.LeaderInfo.Uptime = time.Since(stats.LeaderInfo.StartTime).String()
 	stats.SendingPkgRate, stats.SendingBandwidthRate = stats.SendRates()
 	stats.SendingPkgRate, stats.SendingBandwidthRate = stats.SendRates()
 	stats.RecvingPkgRate, stats.RecvingBandwidthRate = stats.RecvRates()
 	stats.RecvingPkgRate, stats.RecvingBandwidthRate = stats.RecvRates()
 	b, err := json.Marshal(stats)
 	b, err := json.Marshal(stats)

+ 1 - 1
pkg/testutil/leak.go

@@ -48,7 +48,7 @@ func CheckLeakedGoroutine() bool {
 	}
 	}
 
 
 	stackCount := make(map[string]int)
 	stackCount := make(map[string]int)
-	re := regexp.MustCompile("\\(0[0-9a-fx, ]*\\)")
+	re := regexp.MustCompile(`\(0[0-9a-fx, ]*\)`)
 	for _, g := range gs {
 	for _, g := range gs {
 		// strip out pointer arguments in first function of stack dump
 		// strip out pointer arguments in first function of stack dump
 		normalized := string(re.ReplaceAll([]byte(g), []byte("(...)")))
 		normalized := string(re.ReplaceAll([]byte(g), []byte("(...)")))

+ 1 - 1
storage/kvstore.go

@@ -289,7 +289,7 @@ func (s *store) Compact(rev int64) (<-chan struct{}, error) {
 
 
 	s.fifoSched.Schedule(j)
 	s.fifoSched.Schedule(j)
 
 
-	indexCompactionPauseDurations.Observe(float64(time.Now().Sub(start) / time.Millisecond))
+	indexCompactionPauseDurations.Observe(float64(time.Since(start) / time.Millisecond))
 	return ch, nil
 	return ch, nil
 }
 }
 
 

+ 2 - 2
storage/kvstore_compaction.go

@@ -21,7 +21,7 @@ import (
 
 
 func (s *store) scheduleCompaction(compactMainRev int64, keep map[revision]struct{}) bool {
 func (s *store) scheduleCompaction(compactMainRev int64, keep map[revision]struct{}) bool {
 	totalStart := time.Now()
 	totalStart := time.Now()
-	defer dbCompactionTotalDurations.Observe(float64(time.Now().Sub(totalStart) / time.Millisecond))
+	defer dbCompactionTotalDurations.Observe(float64(time.Since(totalStart) / time.Millisecond))
 
 
 	end := make([]byte, 8)
 	end := make([]byte, 8)
 	binary.BigEndian.PutUint64(end, uint64(compactMainRev+1))
 	binary.BigEndian.PutUint64(end, uint64(compactMainRev+1))
@@ -54,7 +54,7 @@ func (s *store) scheduleCompaction(compactMainRev int64, keep map[revision]struc
 		// update last
 		// update last
 		revToBytes(revision{main: rev.main, sub: rev.sub + 1}, last)
 		revToBytes(revision{main: rev.main, sub: rev.sub + 1}, last)
 		tx.Unlock()
 		tx.Unlock()
-		dbCompactionPauseDurations.Observe(float64(time.Now().Sub(start) / time.Millisecond))
+		dbCompactionPauseDurations.Observe(float64(time.Since(start) / time.Millisecond))
 
 
 		select {
 		select {
 		case <-time.After(100 * time.Millisecond):
 		case <-time.After(100 * time.Millisecond):

+ 1 - 4
store/event_history.go

@@ -117,10 +117,7 @@ func (eh *EventHistory) clone() *EventHistory {
 		Back:     eh.Queue.Back,
 		Back:     eh.Queue.Back,
 	}
 	}
 
 
-	for i, e := range eh.Queue.Events {
-		clonedQueue.Events[i] = e
-	}
-
+	copy(clonedQueue.Events, eh.Queue.Events)
 	return &EventHistory{
 	return &EventHistory{
 		StartIndex: eh.StartIndex,
 		StartIndex: eh.StartIndex,
 		Queue:      clonedQueue,
 		Queue:      clonedQueue,

+ 1 - 1
store/store.go

@@ -345,7 +345,7 @@ func (s *store) Delete(nodePath string, dir, recursive bool) (*Event, error) {
 	}
 	}
 
 
 	// recursive implies dir
 	// recursive implies dir
-	if recursive == true {
+	if recursive {
 		dir = true
 		dir = true
 	}
 	}
 
 

+ 46 - 9
test

@@ -10,6 +10,10 @@
 # PKG=snap ./test
 # PKG=snap ./test
 set -e
 set -e
 
 
+# TODO: 'client' pkg fails with gosimple from generated files
+# TODO: 'rafttest' is failing with unused
+GOSIMPLE_UNUSED_PATHS=$(go list ./... | sed -e 's/github.com\/coreos\/etcd\///g' | grep -vE 'cmd|vendor|rafttest|github.com/coreos/etcd$|client$')
+
 # Invoke ./cover for HTML output
 # Invoke ./cover for HTML output
 COVER=${COVER:-"-cover"}
 COVER=${COVER:-"-cover"}
 
 
@@ -96,15 +100,48 @@ function fmt_tests {
 	done
 	done
 
 
 	echo "Checking goword..."
 	echo "Checking goword..."
-	# get all go files to process
-	gofiles=`find $FMT -iname '*.go' 2>/dev/null`
-	# ignore tests and protobuf files
-	gofiles=`echo ${gofiles} | sort | uniq | sed "s/ /\n/g" | egrep -v "(\\_test.go|\\.pb\\.go)"`
-	# only check for broken exported godocs
-	gowordRes=`goword -use-spell=false ${gofiles} | grep godoc-export | sort`
-	if [ ! -z "$gowordRes" ]; then
-		echo -e "goword checking failed:\n${gowordRes}"
-		exit 255
+	if which goword >/dev/null; then
+		echo "goword is installed..."
+		# get all go files to process
+		gofiles=`find $FMT -iname '*.go' 2>/dev/null`
+		# ignore tests and protobuf files
+		gofiles=`echo ${gofiles} | sort | uniq | sed "s/ /\n/g" | egrep -v "(\\_test.go|\\.pb\\.go)"`
+		# only check for broken exported godocs
+		gowordRes=`goword -use-spell=false ${gofiles} | grep godoc-export | sort`
+		if [ ! -z "$gowordRes" ]; then
+			echo -e "goword checking failed:\n${gowordRes}"
+			exit 255
+		fi
+	else
+		echo "gowrod does not exist... skipping..."
+	fi
+
+	echo "Checking gosimple"
+	if which gosimple >/dev/null; then
+		echo "gosimple is installed..."
+		for path in $GOSIMPLE_UNUSED_PATHS; do
+			simplResult=$(gosimple $REPO_PATH/${path})
+			if [ -n "${simplResult}" ]; then
+				echo -e "gosimple checking ${path} failed:\n${simplResult}"
+				exit 255
+			fi
+		done
+	else
+		echo "gosimple does not exist... skipping..."
+	fi
+
+	echo "Checking unused"
+	if which unused >/dev/null; then
+		echo "unused is installed..."
+		for path in $GOSIMPLE_UNUSED_PATHS; do
+			unusedResult=$(unused  $REPO_PATH/${path})
+			if [ -n "${unusedResult}" ]; then
+				echo -e "unused checking ${path} failed:\n${unusedResult}"
+				exit 255
+			fi
+		done
+	else
+		echo "unused does not exist... skipping..."
 	fi
 	fi
 
 
 	echo "Checking for license header..."
 	echo "Checking for license header..."