Browse Source

lint fixed

bugtaker 5 years ago
parent
commit
3c9acc4eb4
15 changed files with 97 additions and 43 deletions
  1. 1 0
      .gitignore
  2. 5 0
      .golangci.yml
  3. 5 0
      .goreleaser.yml
  4. 31 0
      Makefile
  5. 1 0
      cmd/add.go
  6. 3 2
      cmd/delete.go
  7. 1 0
      cmd/list.go
  8. 3 2
      cmd/show.go
  9. 3 2
      cmd/update.go
  10. 3 2
      cmd/use.go
  11. 5 4
      git/git.go
  12. 5 4
      main.go
  13. 7 4
      ssh/ssh.go
  14. 16 13
      store/store.go
  15. 8 10
      util/util.go

+ 1 - 0
.gitignore

@@ -22,3 +22,4 @@ Thumbs.db
 vendor/
 giter
 dist/
+bin/

+ 5 - 0
.golangci.yml

@@ -0,0 +1,5 @@
+linters:
+  enable-all: true
+  disable:
+    - gosec
+    - gochecknoglobals

+ 5 - 0
.goreleaser.yml

@@ -1,4 +1,9 @@
+before:
+  hooks:
+  - go mod download
 builds:
+  - env:
+    - CGO_ENABLED=0
   -
     main: ./main.go
     binary: giter

+ 31 - 0
Makefile

@@ -0,0 +1,31 @@
+export PATH := ./bin:$(PATH)
+export GO111MODULE := on
+
+# Install all the build and lint dependencies
+setup:
+	curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh
+	go mod download
+.PHONY: setup
+
+# Build all files.
+build:
+	@echo "==> Building"
+	@go build -o bin/giter
+.PHONY: build
+
+# Run all the linters
+lint:
+	@./bin/golangci-lint run
+.PHONY: lint
+
+# Release binaries to GitHub.
+release: build
+	@echo "==> Releasing"
+	@goreleaser --rm-dist
+	@echo "==> Complete"
+.PHONY: release
+
+# Clean.
+clean:
+	@rm -rf dist
+.PHONY: clean

+ 1 - 0
cmd/add.go

@@ -2,6 +2,7 @@ package cmd
 
 import (
 	"fmt"
+
 	"github.com/jsmartx/giter/git"
 	"github.com/jsmartx/giter/store"
 	"github.com/jsmartx/giter/util"

+ 3 - 2
cmd/delete.go

@@ -3,10 +3,11 @@ package cmd
 import (
 	"errors"
 	"fmt"
+	"strconv"
+
 	"github.com/jsmartx/giter/store"
 	"github.com/jsmartx/giter/util"
 	"github.com/urfave/cli"
-	"strconv"
 )
 
 func Delete(c *cli.Context) error {
@@ -19,7 +20,7 @@ func Delete(c *cli.Context) error {
 	s := store.New()
 	users := s.List(name, true)
 	if len(users) == 0 {
-		return errors.New("User not found!")
+		return errors.New("user not found")
 	}
 	u := users[0]
 	if len(users) > 1 {

+ 1 - 0
cmd/list.go

@@ -2,6 +2,7 @@ package cmd
 
 import (
 	"fmt"
+
 	"github.com/jsmartx/giter/git"
 	"github.com/jsmartx/giter/store"
 	"github.com/urfave/cli"

+ 3 - 2
cmd/show.go

@@ -3,11 +3,12 @@ package cmd
 import (
 	"errors"
 	"fmt"
+	"strconv"
+
 	"github.com/jsmartx/giter/git"
 	"github.com/jsmartx/giter/store"
 	"github.com/jsmartx/giter/util"
 	"github.com/urfave/cli"
-	"strconv"
 )
 
 func Show(c *cli.Context) error {
@@ -29,7 +30,7 @@ func Show(c *cli.Context) error {
 	s := store.New()
 	users := s.List(name, true)
 	if len(users) == 0 {
-		return errors.New("User not found!")
+		return errors.New("user not found")
 	}
 	u := users[0]
 	if len(users) > 1 {

+ 3 - 2
cmd/update.go

@@ -3,10 +3,11 @@ package cmd
 import (
 	"errors"
 	"fmt"
+	"strconv"
+
 	"github.com/jsmartx/giter/store"
 	"github.com/jsmartx/giter/util"
 	"github.com/urfave/cli"
-	"strconv"
 )
 
 func Update(c *cli.Context) error {
@@ -19,7 +20,7 @@ func Update(c *cli.Context) error {
 	s := store.New()
 	users := s.List(name, true)
 	if len(users) == 0 {
-		return errors.New("User not found!")
+		return errors.New("user not found")
 	}
 	u := users[0]
 	if len(users) > 1 {

+ 3 - 2
cmd/use.go

@@ -3,12 +3,13 @@ package cmd
 import (
 	"errors"
 	"fmt"
+	"strconv"
+
 	"github.com/jsmartx/giter/git"
 	"github.com/jsmartx/giter/ssh"
 	"github.com/jsmartx/giter/store"
 	"github.com/jsmartx/giter/util"
 	"github.com/urfave/cli"
-	"strconv"
 )
 
 func Use(c *cli.Context) error {
@@ -30,7 +31,7 @@ func Use(c *cli.Context) error {
 	s := store.New()
 	users := s.List(name, true)
 	if len(users) == 0 {
-		return errors.New("User not found!")
+		return errors.New("user not found")
 	}
 	u := users[0]
 	if len(users) > 1 {

+ 5 - 4
git/git.go

@@ -1,12 +1,13 @@
 package git
 
 import (
-	"github.com/jsmartx/giter/util"
-	"github.com/mitchellh/go-homedir"
-	"gopkg.in/src-d/go-git.v4"
-	"gopkg.in/src-d/go-git.v4/config"
 	fs "io/ioutil"
 	"net/url"
+
+	"github.com/jsmartx/giter/util"
+	homedir "github.com/mitchellh/go-homedir"
+	git "gopkg.in/src-d/go-git.v4"
+	config "gopkg.in/src-d/go-git.v4/config"
 )
 
 type Git struct {

+ 5 - 4
main.go

@@ -1,18 +1,19 @@
 package main
 
 import (
-	"github.com/jsmartx/giter/cmd"
-	"github.com/urfave/cli"
 	"log"
 	"os"
+
+	"github.com/jsmartx/giter/cmd"
+	"github.com/urfave/cli"
 )
 
-const version = "0.0.1"
+var version = "dev"
 
 func main() {
 	app := cli.NewApp()
 	app.Usage = "Git users manager"
-	app.Version = version
+	app.Version = Version
 	app.Commands = []cli.Command{
 		{
 			Name:    "list",

+ 7 - 4
ssh/ssh.go

@@ -1,11 +1,12 @@
 package ssh
 
 import (
-	"github.com/jsmartx/giter/util"
-	"github.com/kevinburke/ssh_config"
 	fs "io/ioutil"
 	"os"
 	"path/filepath"
+
+	"github.com/jsmartx/giter/util"
+	"github.com/kevinburke/ssh_config"
 )
 
 type Config struct {
@@ -18,10 +19,10 @@ func loadConfig() *ssh_config.Config {
 		return nil
 	}
 	f, err := os.Open(p)
-	defer f.Close()
 	if err != nil {
 		return nil
 	}
+	defer f.Close()
 	cfg, err := ssh_config.Decode(f)
 	if err != nil {
 		return nil
@@ -51,7 +52,9 @@ func New() *Config {
 	cfg = &ssh_config.Config{
 		Hosts: make([]*ssh_config.Host, 0),
 	}
-	saveConfig(cfg)
+	if err := saveConfig(cfg); err != nil {
+		panic(err)
+	}
 	return &Config{cfg: cfg}
 }
 

+ 16 - 13
store/store.go

@@ -5,12 +5,13 @@ import (
 	"encoding/json"
 	"errors"
 	"fmt"
-	"github.com/jsmartx/giter/util"
 	fs "io/ioutil"
 	"net/url"
 	"os"
 	"path/filepath"
 	"strings"
+
+	"github.com/jsmartx/giter/util"
 )
 
 const ROOT = "~/.giter/"
@@ -31,18 +32,16 @@ type Options struct {
 func (u *User) Hash() string {
 	host := util.JoinHostPort(u.Host, u.Port)
 	url := fmt.Sprintf("%s://%s@%s", u.Scheme, u.Name, host)
-	hash := md5.New()
-	hash.Write([]byte(url))
-	return fmt.Sprintf("%x", hash.Sum(nil))
+	return fmt.Sprintf("%x", md5.Sum([]byte(url)))
 }
 
-func (u *User) Url(pwd string) string {
-	fullUrl := &url.URL{
+func (u *User) URL(pwd string) string {
+	fullURL := &url.URL{
 		Scheme: u.Scheme,
 		User:   url.UserPassword(u.Name, pwd),
 		Host:   util.JoinHostPort(u.Host, u.Port),
 	}
-	return fullUrl.String()
+	return fullURL.String()
 }
 
 func (u *User) KeyPath() (string, error) {
@@ -79,13 +78,15 @@ func loadConfig() *Config {
 		return nil
 	}
 	f, err := os.Open(cfgPath)
-	defer f.Close()
 	if err != nil {
 		return nil
 	}
+	defer f.Close()
 	var cfg Config
 	parser := json.NewDecoder(f)
-	parser.Decode(&cfg)
+	if err := parser.Decode(&cfg); err != nil {
+		panic(err)
+	}
 	return &cfg
 }
 
@@ -111,14 +112,16 @@ func New() *Store {
 	cfg = &Config{
 		Users: make([]*User, 0),
 	}
-	saveConfig(cfg)
+	if err := saveConfig(cfg); err != nil {
+		panic(err)
+	}
 	return &Store{c: cfg}
 }
 
 func (s *Store) check(user *User) error {
 	for _, u := range s.c.Users {
 		if u.Hash() == user.Hash() {
-			return errors.New("User already exist!")
+			return errors.New("user already exist")
 		}
 	}
 	return nil
@@ -149,7 +152,7 @@ func (s *Store) Add(u *User, opts *Options) error {
 		}
 	} else {
 		pwdPath := filepath.Join(keysDir, u.Hash()+".credential")
-		data := []byte(u.Url(opts.Password))
+		data := []byte(u.URL(opts.Password))
 		if err := fs.WriteFile(pwdPath, data, 0755); err != nil {
 			return err
 		}
@@ -186,7 +189,7 @@ func (s *Store) Update(hash string, u *User, opts *Options) error {
 	} else {
 		pwdPath := filepath.Join(keysDir, u.Hash()+".credential")
 		if opts.Password != "" {
-			data := []byte(u.Url(opts.Password))
+			data := []byte(u.URL(opts.Password))
 			if err := fs.WriteFile(pwdPath, data, 0600); err != nil {
 				return err
 			}

+ 8 - 10
util/util.go

@@ -7,9 +7,6 @@ import (
 	"encoding/pem"
 	"errors"
 	"fmt"
-	"github.com/chzyer/readline"
-	"github.com/mitchellh/go-homedir"
-	"golang.org/x/crypto/ssh"
 	"io"
 	fs "io/ioutil"
 	"net"
@@ -17,6 +14,10 @@ import (
 	"os"
 	"path/filepath"
 	"regexp"
+
+	"github.com/chzyer/readline"
+	homedir "github.com/mitchellh/go-homedir"
+	"golang.org/x/crypto/ssh"
 )
 
 func Keygen(publicPath, privatePath string, bits int) error {
@@ -26,10 +27,10 @@ func Keygen(publicPath, privatePath string, bits int) error {
 	}
 	// generate and write private key as PEM
 	privateFile, err := os.OpenFile(privatePath, os.O_RDWR|os.O_CREATE, 0600)
-	defer privateFile.Close()
 	if err != nil {
 		return err
 	}
+	defer privateFile.Close()
 	privatePEM := &pem.Block{
 		Type:  "RSA PRIVATE KEY",
 		Bytes: x509.MarshalPKCS1PrivateKey(privateKey),
@@ -48,18 +49,16 @@ func Keygen(publicPath, privatePath string, bits int) error {
 func JoinHostPort(host, port string) string {
 	if port != "" {
 		return fmt.Sprintf("%s:%s", host, port)
-	} else {
-		return host
 	}
+	return host
 }
 
 func SplitHostPort(host string) (string, string) {
 	h, p, err := net.SplitHostPort(host)
 	if err != nil {
 		return host, ""
-	} else {
-		return h, p
 	}
+	return h, p
 }
 
 var ScpRe = regexp.MustCompile(`^([a-zA-Z0-9_]+)@([a-zA-Z0-9._-]+):(.*)$`)
@@ -98,9 +97,8 @@ type PromptConfig struct {
 func (c *PromptConfig) String() string {
 	if c.Default != "" {
 		return fmt.Sprintf("%s(%s) ", c.Prompt, c.Default)
-	} else {
-		return c.Prompt
 	}
+	return c.Prompt
 }
 
 func CheckError(err error) {