Skip to content

Commit

Permalink
(bug) broke clone command when adding git passthrough, added promptin…
Browse files Browse the repository at this point in the history
…g for gitlab.* config values when not set
  • Loading branch information
zaquestion committed Aug 28, 2017
1 parent 4877963 commit 503913e
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 16 deletions.
2 changes: 1 addition & 1 deletion cmd/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
)

// cloneCmd represents the clone command
// NOTE: There is special handling for "clone" in cmd/root.go
var cloneCmd = &cobra.Command{
Use: "clone",
Short: "",
Long: `Clone supports these shorthands
- repo
- namespace/repo`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
path, err := gitlab.ClonePath(args[0])
if err == gitlab.ErrProjectNotFound {
Expand Down
16 changes: 14 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import (
"github.com/zaquestion/lab/internal/git"
)

var cfgFile string

// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "lab",
Expand Down Expand Up @@ -76,6 +74,20 @@ func labUsage(c *cobra.Command) string {
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if cmd, _, err := RootCmd.Find(os.Args[1:]); err != nil || cmd.Use == "clone" {
// Passthough clone if any flags are in use
if cmd.Use == "clone" {
// This will fail if their are in a flags because no
// flags are defined on the command
err = cmd.ParseFlags(os.Args[1:])
if err == nil {
if err := RootCmd.Execute(); err != nil {
// Execute has already logged the error
os.Exit(1)
}
}
}

// Passthrough to git for any unrecognised commands
git := git.New(os.Args[1:]...)
err = git.Run()
if exiterr, ok := err.(*exec.ExitError); ok {
Expand Down
88 changes: 75 additions & 13 deletions internal/gitlab/gitlab.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package gitlab

import (
"bufio"
"fmt"
"log"
"net/http"
"os"
Expand All @@ -10,6 +12,7 @@ import (
"github.com/pkg/errors"
"github.com/tcnksm/go-gitconfig"
"github.com/xanzy/go-gitlab"
"github.com/zaquestion/lab/internal/git"
)

var (
Expand All @@ -23,31 +26,90 @@ var (
User string
)

const defaultGitLabHost = "https://gitlab.com"

func init() {
reader := bufio.NewReader(os.Stdin)
var err error
var errt error
host, err = gitconfig.Entire("gitlab.host")
if err != nil {
log.Fatal("git config gitlab.host must be set")
}
token, err = gitconfig.Entire("gitlab.token")
if err != nil {
log.Fatal("git config gitlab.token must be set")
fmt.Printf("Enter default GitLab host (default: %s): ", defaultGitLabHost)
host, err = reader.ReadString('\n')
host = host[:len(host)-1]
if err != nil {
log.Fatal(err)
}
if host == "" {
host = defaultGitLabHost
}
cmd := git.New("config", "--global", "gitlab.host", host)
err = cmd.Run()
if err != nil {
log.Fatal(err)
}

}
User, err = gitconfig.Entire("gitlab.user")
token, errt = gitconfig.Entire("gitlab.token")
if err != nil {
log.Fatal("git config gitlab.user must be set")
fmt.Print("Enter default GitLab user: ")
User, err = reader.ReadString('\n')
User = User[:len(User)-1]
if err != nil {
log.Fatal(err)
}
if User == "" {
log.Fatal("git config gitlab.user must be set")
}
cmd := git.New("config", "--global", "gitlab.user", User)
err = cmd.Run()
if err != nil {
log.Fatal(err)
}

// If the default user is being set this is the first time lab
// is being run. Check if they want to provide an API key for
// private repos
if errt != nil {
fmt.Print("Enter default GitLab token (default: empty): ")
token, err = reader.ReadString('\n')
token = token[:len(token)-1]
if err != nil {
log.Fatal(err)
}
// Its okay for the key to be empty, since you can still call public repos
if token != "" {
cmd := git.New("config", "--global", "gitlab.token", token)
err = cmd.Run()
if err != nil {
log.Fatal(err)
}
}
}
}

lab = gitlab.NewClient(nil, token)
lab.SetBaseURL(host + "/api/v4")

if os.Getenv("DEBUG") != "" {
log.Println("gitlab.host:", host)
log.Println("gitlab.token:", "************"+token[12:])
if len(token) > 12 {
log.Println("gitlab.token:", "************"+token[12:])
} else {
log.Println("This token looks invalid due to it's length")
log.Println("gitlab.token:", token)
}
log.Println("gitlab.user:", User)
}
lab = gitlab.NewClient(nil, token)
lab.SetBaseURL(host + "/api/v4")
_, _, err = lab.Projects.ListProjects(&gitlab.ListProjectsOptions{})
if err != nil {
log.Fatal("Error: ", err)

// Test listing projects
projects, _, err := lab.Projects.ListProjects(&gitlab.ListProjectsOptions{})
if err != nil {
log.Fatal("Error: ", err)
}
if len(projects) > 0 {
spew.Dump(projects[0])
}
}
}

Expand Down

0 comments on commit 503913e

Please sign in to comment.