Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
Created auth and utils package
Browse files Browse the repository at this point in the history
  • Loading branch information
pksunkara committed Apr 30, 2016
1 parent 4645e1a commit f0cfb6a
Show file tree
Hide file tree
Showing 17 changed files with 145 additions and 120 deletions.
7 changes: 0 additions & 7 deletions auth.go

This file was deleted.

16 changes: 16 additions & 0 deletions auth/destroy.go
@@ -0,0 +1,16 @@
package auth

import (
"github.com/pksunkara/hub/utils"
)

type DestroyCommand struct{}

func (a *DestroyCommand) Execute(args []string) error {
Config("user", "")
Config("token", "")

utils.HandleInfo("You are now in `public` mode")

return nil
}
7 changes: 7 additions & 0 deletions auth/index.go
@@ -0,0 +1,7 @@
package auth

type Command struct {
User UserCommand `command:"user" alias:"u" description:"Set an username to use their public data"`
Private PrivateCommand `command:"private" alias:"p" description:"Give access to your private data"`
Destroy DestroyCommand `command:"destroy" alias:"d" description:"Destroy authorization and delete username"`
}
16 changes: 16 additions & 0 deletions auth/private.go
@@ -0,0 +1,16 @@
package auth

// import (
// "fmt"
// "github.com/howeyc/gopass"
// )

type PrivateCommand struct{}

func (a *PrivateCommand) Execute(args []string) error {
return nil
}

func (a *PrivateCommand) Usage() string {
return "<user>"
}
26 changes: 26 additions & 0 deletions auth/user.go
@@ -0,0 +1,26 @@
package auth

import (
"github.com/pksunkara/hub/utils"
)

type UserCommand struct{}

func (a *UserCommand) Execute(args []string) error {
if Config("token") != "" {
return &utils.ErrModes{}
}

if len(args) == 1 {
Config("user", args[0])
utils.HandleInfo("You are now in `user` mode")
} else {
return &utils.ErrArgument{}
}

return nil
}

func (a *UserCommand) Usage() string {
return "<user>"
}
12 changes: 0 additions & 12 deletions auth_destroy.go

This file was deleted.

16 changes: 0 additions & 16 deletions auth_private.go

This file was deleted.

22 changes: 0 additions & 22 deletions auth_user.go

This file was deleted.

9 changes: 5 additions & 4 deletions clone.go
@@ -1,6 +1,7 @@
package main

import (
"github.com/pksunkara/hub/utils"
"strings"
)

Expand All @@ -10,11 +11,11 @@ type CloneCommand struct {

func (c *CloneCommand) Execute(args []string) error {
if len(args) == 0 {
return &ErrArgument{}
return &utils.ErrArgument{}
}

if strings.Index(args[0], ":") != -1 {
return &ErrProxy{}
return &utils.ErrProxy{}
}

var repo string
Expand All @@ -29,7 +30,7 @@ func (c *CloneCommand) Execute(args []string) error {

if len(path) == 1 {
if Config("user") == "" {
return &ErrUserMode{}
return &utils.ErrUserMode{}
}

if Config("token") != "" {
Expand All @@ -43,7 +44,7 @@ func (c *CloneCommand) Execute(args []string) error {
return &ErrProxy{}
}

return Git([]string{"clone", "--progress", repo}...)
return utils.Git([]string{"clone", "--progress", repo}...)
}

func (c *CloneCommand) Usage() string {
Expand Down
13 changes: 7 additions & 6 deletions fetch.go
@@ -1,24 +1,25 @@
package main

import (
"github.com/pksunkara/hub/utils"
"strings"
)

type FetchCommand struct{}

func (f *FetchCommand) Execute(args []string) error {
if len(args) == 0 {
return &ErrArgument{}
return &utils.ErrArgument{}
} else if len(args) > 1 {
return &ErrProxy{}
return &utils.ErrProxy{}
}

if len(strings.Split(args[0], "/")) != 1 {
return &ErrProxy{}
return &utils.ErrProxy{}
}

users := strings.Split(args[0], ",")
remotes, err := Remotes()
remotes, err := utils.Remotes()

remoteAdd := &RemoteAddCommand{}

Expand All @@ -32,13 +33,13 @@ func (f *FetchCommand) Execute(args []string) error {
}
}

err = Git(append([]string{"fetch", "--multiple"}, users...)...)
err = utils.Git(append([]string{"fetch", "--multiple"}, users...)...)

if err != nil {
return err
}

HandleInfo("Fetched from remotes " + args[0])
utils.HandleInfo("Fetched from remotes " + args[0])

return nil
}
Expand Down
55 changes: 14 additions & 41 deletions main.go
Expand Up @@ -3,6 +3,8 @@ package main
import (
"errors"
"github.com/jessevdk/go-flags"
"github.com/pksunkara/hub/auth"
"github.com/pksunkara/hub/utils"
"github.com/robfig/config"
"github.com/wsxiaoys/terminal"
"os"
Expand All @@ -24,13 +26,15 @@ var (
var Options struct {
Verbose bool `short:"v" long:"verbose" description:"Show verbose debug information"`

Auth AuthCommand `command:"auth" alias:"a" description:"Manage github access modes"`
Clone CloneCommand `command:"clone" alias:"c" description:"Clone github repos easily"`
Fetch FetchCommand `command:"fetch" description:"Fetch multiple users repo updates"`
Fork ForkCommand `command:"fork" description:"Fork a github repo"`
Push PushCommand `command:"push" description:"Push to multiple github repos"`
Remote RemoteCommand `command:"remote" alias:"r" description:"Manage remotes of repos" subcommands-optional:"1"`
Version VersionCommand `command:"version" description:"Display program version"`
Auth auth.Command `command:"auth" alias:"a" description:"Manage github access modes"`
Clone CloneCommand `command:"clone" alias:"c" description:"Clone github repos easily"`
Config ConfigCommand `command:"config" description:"Manage hub's configuration"`
Fetch FetchCommand `command:"fetch" description:"Fetch multiple users repo updates"`
Fork ForkCommand `command:"fork" description:"Fork a github repo"`
Generate GenerateCommand `command:"generate" alias:"g" description:"Generate something in the repo"`
Push PushCommand `command:"push" description:"Push to multiple github repos"`
Remote RemoteCommand `command:"remote" alias:"r" description:"Manage remotes of repos" subcommands-optional:"1"`
Version VersionCommand `command:"version" description:"Display program version"`
}

func main() {
Expand Down Expand Up @@ -82,7 +86,7 @@ func main() {

if err != nil {
if Config("combine") == "1" {
err := Git(os.Args[1:]...)
err := utils.Git(os.Args[1:]...)

if err != nil {
os.Exit(1)
Expand All @@ -92,7 +96,7 @@ func main() {
}

if _, ok := err.(*exec.ExitError); ok {
HandleError(errors.New("Running git command is unsuccessful"))
utils.HandleError(errors.New("Running git command is unsuccessful"))
os.Exit(1)
}

Expand All @@ -108,40 +112,9 @@ func main() {
}
}

HandleError(err)
utils.HandleError(err)

terminal.Stderr.Nl()
parser.WriteHelp(os.Stderr)
}
}

func HandleError(err error) {
if err != nil {
terminal.Stderr.Color("r").Print("errs").Color("w!").Print(": ", err).Reset().Nl()
}
}

func HandleInfo(str string) {
terminal.Stderr.Color("g").Print("info").Color("w!").Print(": ", str).Reset().Nl()
}

func HandleDebug(str string) {
if Options.Verbose {
terminal.Stderr.Color("y").Print("logs").Color("w!").Print(": ", str).Reset().Nl()
}
}

func Git(args ...string) error {
CheckGit()

cmd := exec.Command("git", args...)

cmd.Stdin = os.Stdin

cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

HandleDebug("git " + strings.Join(args, " "))

return cmd.Run()
}
9 changes: 5 additions & 4 deletions push.go
@@ -1,30 +1,31 @@
package main

import (
"github.com/pksunkara/hub/utils"
"strings"
)

type PushCommand struct{}

func (p *PushCommand) Execute(args []string) error {
if len(args) != 2 {
return &ErrProxy{}
return &utils.ErrProxy{}
}

remotes := strings.Split(args[0], ",")

if len(remotes) == 1 {
return &ErrProxy{}
return &utils.ErrProxy{}
}

for _, remote := range remotes {
err := Git(append([]string{"push", remote}, args[1:]...)...)
err := utils.Git(append([]string{"push", remote}, args[1:]...)...)

if err != nil {
return err
}

HandleInfo("Pushed to `" + remote + "` remote")
utils.HandleInfo("Pushed to `" + remote + "` remote")
}

return nil
Expand Down
6 changes: 5 additions & 1 deletion remote.go
@@ -1,9 +1,13 @@
package main

import (
"github.com/pksunkara/hub/utils"
)

type RemoteCommand struct {
RemoteAdd RemoteAddCommand `command:"add" description:"Add a github remote easily"`
}

func (r *RemoteCommand) Execute(args []string) error {
return &ErrProxy{}
return &utils.ErrProxy{}
}

0 comments on commit f0cfb6a

Please sign in to comment.