Skip to content

Commit

Permalink
lint fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
bugtaker committed Jan 14, 2019
1 parent f8d95df commit 3c9acc4
Show file tree
Hide file tree
Showing 15 changed files with 97 additions and 43 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ Thumbs.db
vendor/
giter
dist/
bin/
5 changes: 5 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
linters:
enable-all: true
disable:
- gosec
- gochecknoglobals
5 changes: 5 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
before:
hooks:
- go mod download
builds:
- env:
- CGO_ENABLED=0
-
main: ./main.go
binary: giter
Expand Down
31 changes: 31 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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 change: 1 addition & 0 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"

"github.com/jsmartx/giter/git"
"github.com/jsmartx/giter/store"
"github.com/jsmartx/giter/util"
Expand Down
5 changes: 3 additions & 2 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"

"github.com/jsmartx/giter/git"
"github.com/jsmartx/giter/store"
"github.com/urfave/cli"
Expand Down
5 changes: 3 additions & 2 deletions cmd/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions cmd/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
9 changes: 5 additions & 4 deletions git/git.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
9 changes: 5 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
11 changes: 7 additions & 4 deletions ssh/ssh.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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}
}

Expand Down
29 changes: 16 additions & 13 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
Expand All @@ -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) {
Expand Down Expand Up @@ -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
}

Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
18 changes: 8 additions & 10 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ import (
"encoding/pem"
"errors"
"fmt"
"github.com/chzyer/readline"
"github.com/mitchellh/go-homedir"
"golang.org/x/crypto/ssh"
"io"
fs "io/ioutil"
"net"
"net/url"
"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 {
Expand All @@ -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),
Expand All @@ -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._-]+):(.*)$`)
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 3c9acc4

Please sign in to comment.