From f0cfb6a20ea91013565b9e86d1744482d276e5a5 Mon Sep 17 00:00:00 2001 From: Pavan Kumar Sunkara Date: Sat, 30 Apr 2016 20:00:08 +0200 Subject: [PATCH] Created auth and utils package --- auth.go | 7 ----- auth/destroy.go | 16 +++++++++++ auth/index.go | 7 +++++ auth/private.go | 16 +++++++++++ auth/user.go | 26 ++++++++++++++++++ auth_destroy.go | 12 --------- auth_private.go | 16 ----------- auth_user.go | 22 --------------- clone.go | 9 ++++--- fetch.go | 13 ++++----- main.go | 55 ++++++++++---------------------------- push.go | 9 ++++--- remote.go | 6 ++++- remote_add.go | 11 ++++---- error.go => utils/error.go | 2 +- utils/log.go | 21 +++++++++++++++ utils.go => utils/utils.go | 17 +++++++++++- 17 files changed, 145 insertions(+), 120 deletions(-) delete mode 100644 auth.go create mode 100644 auth/destroy.go create mode 100644 auth/index.go create mode 100644 auth/private.go create mode 100644 auth/user.go delete mode 100644 auth_destroy.go delete mode 100644 auth_private.go delete mode 100644 auth_user.go rename error.go => utils/error.go (97%) create mode 100644 utils/log.go rename utils.go => utils/utils.go (75%) diff --git a/auth.go b/auth.go deleted file mode 100644 index 4788655..0000000 --- a/auth.go +++ /dev/null @@ -1,7 +0,0 @@ -package main - -type AuthCommand struct { - AuthUser AuthUserCommand `command:"user" alias:"u" description:"Set an username to use their public data"` - AuthPrivate AuthPrivateCommand `command:"private" alias:"p" description:"Give access to your private data"` - AuthDestroy AuthDestroyCommand `command:"destroy" alias:"d" description:"Destroy authorization and delete username"` -} diff --git a/auth/destroy.go b/auth/destroy.go new file mode 100644 index 0000000..d1faf1d --- /dev/null +++ b/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 +} diff --git a/auth/index.go b/auth/index.go new file mode 100644 index 0000000..fb6571c --- /dev/null +++ b/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"` +} diff --git a/auth/private.go b/auth/private.go new file mode 100644 index 0000000..a97d401 --- /dev/null +++ b/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 "" +} diff --git a/auth/user.go b/auth/user.go new file mode 100644 index 0000000..e8ccf18 --- /dev/null +++ b/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 "" +} diff --git a/auth_destroy.go b/auth_destroy.go deleted file mode 100644 index 978c40c..0000000 --- a/auth_destroy.go +++ /dev/null @@ -1,12 +0,0 @@ -package main - -type AuthDestroyCommand struct{} - -func (a *AuthDestroyCommand) Execute(args []string) error { - Config("user", "") - Config("token", "") - - HandleInfo("You are now in `public` mode") - - return nil -} diff --git a/auth_private.go b/auth_private.go deleted file mode 100644 index 070debd..0000000 --- a/auth_private.go +++ /dev/null @@ -1,16 +0,0 @@ -package main - -// import ( -// "fmt" -// "github.com/howeyc/gopass" -// ) - -type AuthPrivateCommand struct{} - -func (a *AuthPrivateCommand) Execute(args []string) error { - return nil -} - -func (a *AuthPrivateCommand) Usage() string { - return "" -} diff --git a/auth_user.go b/auth_user.go deleted file mode 100644 index 71a18f6..0000000 --- a/auth_user.go +++ /dev/null @@ -1,22 +0,0 @@ -package main - -type AuthUserCommand struct{} - -func (a *AuthUserCommand) Execute(args []string) error { - if Config("token") != "" { - return &ErrModes{} - } - - if len(args) == 1 { - Config("user", args[0]) - HandleInfo("You are now in `user` mode") - } else { - return &ErrArgument{} - } - - return nil -} - -func (a *AuthUserCommand) Usage() string { - return "" -} diff --git a/clone.go b/clone.go index e59dff0..fab1e70 100644 --- a/clone.go +++ b/clone.go @@ -1,6 +1,7 @@ package main import ( + "github.com/pksunkara/hub/utils" "strings" ) @@ -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 @@ -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") != "" { @@ -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 { diff --git a/fetch.go b/fetch.go index b85bfd1..8f6695f 100644 --- a/fetch.go +++ b/fetch.go @@ -1,6 +1,7 @@ package main import ( + "github.com/pksunkara/hub/utils" "strings" ) @@ -8,17 +9,17 @@ 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{} @@ -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 } diff --git a/main.go b/main.go index 0b3e4a2..33d4436 100644 --- a/main.go +++ b/main.go @@ -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" @@ -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() { @@ -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) @@ -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) } @@ -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() -} diff --git a/push.go b/push.go index 92acaf3..60567b1 100644 --- a/push.go +++ b/push.go @@ -1,6 +1,7 @@ package main import ( + "github.com/pksunkara/hub/utils" "strings" ) @@ -8,23 +9,23 @@ 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 diff --git a/remote.go b/remote.go index faeee91..632a532 100644 --- a/remote.go +++ b/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{} } diff --git a/remote_add.go b/remote_add.go index 07f433f..6551373 100644 --- a/remote_add.go +++ b/remote_add.go @@ -1,6 +1,7 @@ package main import ( + "github.com/pksunkara/hub/utils" "strings" ) @@ -10,9 +11,9 @@ type RemoteAddCommand struct { func (r *RemoteAddCommand) Execute(args []string) error { if len(args) == 0 { - return &ErrArgument{} + return &utils.ErrArgument{} } else if len(args) > 1 { - return &ErrProxy{} + return &utils.ErrProxy{} } var user, repo string @@ -25,7 +26,7 @@ func (r *RemoteAddCommand) Execute(args []string) error { if args[0] == "origin" { if Config("user") == "" { - return &ErrUserMode{} + return &utils.ErrUserMode{} } user = Config("user") @@ -38,10 +39,10 @@ func (r *RemoteAddCommand) Execute(args []string) error { if len(path) == 1 { repo = repo + user + "/" + Repo() } else { - return &ErrProxy{} + return &utils.ErrProxy{} } - err := Git([]string{"remote", "add", args[0], repo}...) + err := utils.Git([]string{"remote", "add", args[0], repo}...) if err != nil { return err diff --git a/error.go b/utils/error.go similarity index 97% rename from error.go rename to utils/error.go index eae28ce..dd0e576 100644 --- a/error.go +++ b/utils/error.go @@ -1,4 +1,4 @@ -package main +package utils type ErrProxy struct{} diff --git a/utils/log.go b/utils/log.go new file mode 100644 index 0000000..773cfc3 --- /dev/null +++ b/utils/log.go @@ -0,0 +1,21 @@ +package utils + +import ( + "github.com/wsxiaoys/terminal" +) + +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, verbose bool) { + if verbose { + terminal.Stderr.Color("y").Print("logs").Color("w!").Print(": ", str).Reset().Nl() + } +} diff --git a/utils.go b/utils/utils.go similarity index 75% rename from utils.go rename to utils/utils.go index 7cdfc61..3e47d46 100644 --- a/utils.go +++ b/utils/utils.go @@ -1,4 +1,4 @@ -package main +package utils import ( "errors" @@ -46,3 +46,18 @@ func Remotes() (remotes map[string]string, err error) { return } + +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() +}