Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
bugtaker committed Jan 12, 2019
0 parents commit 7d821a9
Show file tree
Hide file tree
Showing 16 changed files with 1,163 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# http://editorconfig.org
root = true

# Unix-style newlines with a newline ending every file
[*]
indent_style = space
indent_size = 2
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

# Tab indentation (no size specified)
[Makefile]
indent_style = tab

# Golang uses tabs by default
[*.go]
indent_style = tab
23 changes: 23 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
*.iml
.idea/
.ipr
.iws
*~
~*
*.diff
*.patch
*.bak
.DS_Store
Thumbs.db
.project
.*proj
.svn/
*.swp
*.swo
*.log
*.sublime-project
*.sublime-workspace
.buildpath
.settings
vendor/
giter
21 changes: 21 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
builds:
-
main: ./main.go
binary: giter
ldflags: -s -w -X main.version={{.Version}}
goos:
- darwin
- linux
- windows
- freebsd
- netbsd
- openbsd
goarch:
- amd64
- 386
changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Giter - Git users manager
======

> Giter is a git users manager.
83 changes: 83 additions & 0 deletions cmd/add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package cmd

import (
"fmt"
"github.com/jsmartx/giter/git"
"github.com/jsmartx/giter/store"
"github.com/jsmartx/giter/util"
"github.com/urfave/cli"
)

func getUser() *git.User {
g, err := git.New(".")
if err != nil {
return git.GlobalUser()
}
return g.GetUser()
}

func getHost() string {
g, err := git.New(".")
if err != nil {
return "ssh://github.com"
}
urls, err := g.Remotes()
if err != nil || len(urls) == 0 {
return "ssh://github.com"
}
return fmt.Sprintf("%s://%s", urls[0].Scheme, urls[0].Host)
}

func Add(c *cli.Context) error {
u := getUser()
nameCfg := &util.PromptConfig{
Prompt: "user name: ",
}
emailCfg := &util.PromptConfig{
Prompt: "user email: ",
}
urlCfg := &util.PromptConfig{
Prompt: "git server: ",
Default: getHost(),
}
if u != nil {
nameCfg.Default = u.Name
emailCfg.Default = u.Email
}
name := util.Prompt(nameCfg)
email := util.Prompt(emailCfg)
urlStr := util.Prompt(urlCfg)

url, err := util.ParseURL(urlStr)
util.CheckError(err)

host, port := util.SplitHostPort(url.Host)
user := &store.User{
Name: name,
Email: email,
Scheme: url.Scheme,
Host: host,
Port: port,
}
options := &store.Options{}
if user.IsSSH() {
pvtPath, err := util.SysSSHConfig()
if err == nil {
fmt.Printf("There is a SSH key: %s\nYou can use this key or generate a new SSH key.\n", pvtPath)
txt := util.Prompt(&util.PromptConfig{
Prompt: fmt.Sprintf("Do you want to use '%s' [Y/n]? ", pvtPath),
})
if txt == "y" || txt == "Y" {
options.KeyPath = pvtPath
}
}
} else {
pwd := util.Prompt(&util.PromptConfig{
Prompt: "user password: ",
Silent: true,
})
options.Password = pwd
}
s := store.New()
return s.Add(user, options)
}
49 changes: 49 additions & 0 deletions cmd/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package cmd

import (
"errors"
"fmt"
"github.com/jsmartx/giter/store"
"github.com/jsmartx/giter/util"
"github.com/urfave/cli"
"strconv"
)

func Delete(c *cli.Context) error {
name := c.Args().First()
if name == "" {
name = util.Prompt(&util.PromptConfig{
Prompt: "user name: ",
})
}
s := store.New()
users := s.List(name, true)
if len(users) == 0 {
return errors.New("User not found!")
}
u := users[0]
if len(users) > 1 {
fmt.Printf("There are %d users:\n", len(users))
for i, item := range users {
fmt.Printf("%4d) %s\n", i+1, item.String())
}
str := util.Prompt(&util.PromptConfig{
Prompt: "Enter number to select user: ",
})
i, err := strconv.Atoi(str)
if err != nil {
return err
}
if i < 1 || i > len(users) {
return errors.New("Out of range")
}
u = users[i-1]
}
txt := util.Prompt(&util.PromptConfig{
Prompt: fmt.Sprintf("Are you sure to delete '%s' [Y/n]? ", u.String()),
})
if txt != "y" && txt != "Y" {
return nil
}
return s.Delete(u)
}
27 changes: 27 additions & 0 deletions cmd/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package cmd

import (
"fmt"
"github.com/jsmartx/giter/git"
"github.com/jsmartx/giter/store"
"github.com/urfave/cli"
)

func List(c *cli.Context) error {
g, err := git.New(".")
var cur *git.User
if err == nil {
cur = g.GetUser()
}
filter := c.Args().First()
s := store.New()
users := s.List(filter, false)
for _, u := range users {
if cur != nil && cur.Name == u.Name && cur.Email == u.Email {
fmt.Printf(" * %s\n", u.String())
} else {
fmt.Printf(" %s\n", u.String())
}
}
return nil
}
97 changes: 97 additions & 0 deletions cmd/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package cmd

import (
"errors"
"fmt"
"github.com/jsmartx/giter/store"
"github.com/jsmartx/giter/util"
"github.com/urfave/cli"
"strconv"
)

func Update(c *cli.Context) error {
name := c.Args().First()
if name == "" {
name = util.Prompt(&util.PromptConfig{
Prompt: "user name: ",
})
}
s := store.New()
users := s.List(name, true)
if len(users) == 0 {
return errors.New("User not found!")
}
u := users[0]
if len(users) > 1 {
fmt.Printf("There are %d users:\n", len(users))
for i, item := range users {
fmt.Printf("%4d) %s\n", i+1, item.String())
}
str := util.Prompt(&util.PromptConfig{
Prompt: "Enter number to select user: ",
})
i, err := strconv.Atoi(str)
if err != nil {
return err
}
if i < 1 || i > len(users) {
return errors.New("Out of range")
}
u = users[i-1]
}
email := util.Prompt(&util.PromptConfig{
Prompt: "user email: ",
Default: u.Email,
})
urlStr := util.Prompt(&util.PromptConfig{
Prompt: "git server: ",
Default: u.FullHost(),
})

url, err := util.ParseURL(urlStr)
util.CheckError(err)

host, port := util.SplitHostPort(url.Host)
user := &store.User{
Name: name,
Email: email,
Scheme: url.Scheme,
Host: host,
Port: port,
}
options := &store.Options{}
if user.IsSSH() {
if u.IsSSH() {
txt := util.Prompt(&util.PromptConfig{
Prompt: fmt.Sprintf("Regenerate the SSH key [Y/n]? "),
})
if txt != "y" && txt != "Y" {
p, err := u.KeyPath()
if err != nil {
return err
}
options.KeyPath = p
}
}
} else {
if !u.IsSSH() {
txt := util.Prompt(&util.PromptConfig{
Prompt: fmt.Sprintf("Reset password [Y/n]? "),
})
if txt == "y" || txt == "Y" {
pwd := util.Prompt(&util.PromptConfig{
Prompt: "user password: ",
Silent: true,
})
options.Password = pwd
}
} else {
pwd := util.Prompt(&util.PromptConfig{
Prompt: "user password: ",
Silent: true,
})
options.Password = pwd
}
}
return s.Update(u.Hash(), user, options)
}
65 changes: 65 additions & 0 deletions cmd/use.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package cmd

import (
"errors"
"fmt"
"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 {
g, err := git.New(".")
if err != nil {
return err
}
name := c.Args().First()
if name == "" {
name = util.Prompt(&util.PromptConfig{
Prompt: "user name: ",
})
}
s := store.New()
users := s.List(name, true)
if len(users) == 0 {
return errors.New("User not found!")
}
u := users[0]
if len(users) > 1 {
fmt.Printf("There are %d users:\n", len(users))
for i, item := range users {
fmt.Printf("%4d) %s\n", i+1, item.String())
}
str := util.Prompt(&util.PromptConfig{
Prompt: "Enter number to select user: ",
})
i, err := strconv.Atoi(str)
if err != nil {
return err
}
if i < 1 || i > len(users) {
return errors.New("Out of range")
}
u = users[i-1]
}
if u.IsSSH() {
keyPath, err := u.KeyPath()
if err != nil {
fmt.Println(err)
}
s := ssh.New()
err = s.SetHost(&ssh.Host{
Key: u.Host,
Hostname: u.Host,
Port: u.Port,
IdentityFile: keyPath,
})
if err != nil {
fmt.Println(err)
}
}
return g.SetUser(u.Name, u.Email)
}
Loading

0 comments on commit 7d821a9

Please sign in to comment.