Skip to content

Commit

Permalink
Moved Client and bitbucket into root
Browse files Browse the repository at this point in the history
  • Loading branch information
craftamap committed Nov 24, 2020
1 parent 8472e6a commit b5cd563
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 142 deletions.
5 changes: 4 additions & 1 deletion cmd/commands/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ func Add(rootCmd *cobra.Command, globalOpts *options.GlobalOptions) {
Use: "api <url> [<body>]",
Short: "Make an authenticated api.bitbucket.org request to the rest 2.0 api",
Long: "Make an authenticated api.bitbucket.org request to the rest 2.0 api",
Annotations: map[string]string{
"RequiresClient": "true",
},
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(Headers)

Expand All @@ -44,7 +47,7 @@ func Add(rootCmd *cobra.Command, globalOpts *options.GlobalOptions) {
fmt.Printf("%s%s%s\n", aurora.Red(":: "), aurora.Bold("An error occured: "), err)
return
}
req.SetBasicAuth(globalOpts.Username, globalOpts.Password)
req.SetBasicAuth(globalOpts.Client.Username, globalOpts.Client.Username)

for _, header := range Headers {
splitted := strings.SplitN(header, "=", 2)
Expand Down
21 changes: 7 additions & 14 deletions cmd/commands/downloads/download/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"

"github.com/craftamap/bb/cmd/options"
bbgit "github.com/craftamap/bb/git"
"github.com/craftamap/bb/internal"
"github.com/logrusorgru/aurora"
"github.com/spf13/cobra"
Expand All @@ -18,22 +17,16 @@ func Add(prCmd *cobra.Command, globalOpts *options.GlobalOptions) {
downloadCmd := &cobra.Command{
Use: "download <filename> [<target path>]",
Short: "download files from bitbucket",
Annotations: map[string]string{
"RequiresClient": "true",
"RequiresRepository": "true",
},
Run: func(cmd *cobra.Command, args []string) {
// TODO: Get rid of this "list" workaround
c := internal.Client{
Username: globalOpts.Username,
Password: globalOpts.Password,
}

bbrepo, err := bbgit.GetBitbucketRepo()
if err != nil {
fmt.Printf("%s%s%s\n", aurora.Red(":: "), aurora.Bold("An error occured: "), err)
return
}
if !bbrepo.IsBitbucketOrg() {
fmt.Printf("%s%s%s\n", aurora.Yellow(":: "), aurora.Bold("Warning: "), "Are you sure this is a bitbucket repo?")
return
}
c := globalOpts.Client
bbrepo := globalOpts.BitbucketRepo

downloads, err := c.GetDownloads(bbrepo.RepoOrga, bbrepo.RepoSlug)
if err != nil {
fmt.Printf("%s%s%s\n", aurora.Red(":: "), aurora.Bold("An error occured: "), err)
Expand Down
22 changes: 7 additions & 15 deletions cmd/commands/downloads/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"fmt"

"github.com/craftamap/bb/cmd/options"
bbgit "github.com/craftamap/bb/git"
"github.com/craftamap/bb/internal"
"github.com/dustin/go-humanize"
"github.com/logrusorgru/aurora"
"github.com/pkg/browser"
Expand All @@ -19,21 +17,15 @@ var (
func Add(downloadsCmd *cobra.Command, globalOpts *options.GlobalOptions) {
listCmd := &cobra.Command{
Use: "list",
Annotations: map[string]string{
"RequiresClient": "true",
"RequiresRepository": "true",
},
Run: func(cmd *cobra.Command, args []string) {
c := internal.Client{
Username: globalOpts.Username,
Password: globalOpts.Password,
}

bbrepo, err := bbgit.GetBitbucketRepo()
if err != nil {
fmt.Printf("%s%s%s\n", aurora.Red(":: "), aurora.Bold("An error occured: "), err)
return
}
if !bbrepo.IsBitbucketOrg() {
fmt.Printf("%s%s%s\n", aurora.Yellow(":: "), aurora.Bold("Warning: "), "Are you sure this is a bitbucket repo?")
return
}
c := globalOpts.Client
bbrepo := globalOpts.BitbucketRepo

if Web {
err := browser.OpenURL(fmt.Sprintf("https://bitbucket.org/%s/%s/downloads", bbrepo.RepoOrga, bbrepo.RepoSlug))
if err != nil {
Expand Down
24 changes: 7 additions & 17 deletions cmd/commands/downloads/upload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,20 @@ import (
"path/filepath"

"github.com/craftamap/bb/cmd/options"
bbgit "github.com/craftamap/bb/git"
"github.com/craftamap/bb/internal"
"github.com/logrusorgru/aurora"
"github.com/spf13/cobra"
)

func Add(downloadsCmd *cobra.Command, globalOpts *options.GlobalOptions) {
uploadCmd := &cobra.Command{
Use: "upload",
Annotations: map[string]string{
"RequiresClient": "true",
"RequiresRepository": "true",
},
Run: func(cmd *cobra.Command, args []string) {
c := internal.Client{
Username: globalOpts.Username,
Password: globalOpts.Password,
}

bbrepo, err := bbgit.GetBitbucketRepo()
if err != nil {
fmt.Printf("%s%s%s\n", aurora.Red(":: "), aurora.Bold("An error occured: "), err)
return
}
if !bbrepo.IsBitbucketOrg() {
fmt.Printf("%s%s%s\n", aurora.Yellow(":: "), aurora.Bold("Warning: "), "Are you sure this is a bitbucket repo?")
return
}
c := globalOpts.Client
bbrepo := globalOpts.BitbucketRepo

if len(args) == 0 {
fmt.Printf("%s%s%s\n", aurora.Yellow(":: "), aurora.Bold("Warning: "), "No file specified")
Expand All @@ -45,7 +35,7 @@ func Add(downloadsCmd *cobra.Command, globalOpts *options.GlobalOptions) {
}
fmt.Printf("%s Uploading file %s\n", aurora.Green(":: "), filepath.Base(fpath))

_, err = c.UploadDownload(bbrepo.RepoOrga, bbrepo.RepoSlug, fpath)
_, err := c.UploadDownload(bbrepo.RepoOrga, bbrepo.RepoSlug, fpath)
if err != nil {
fmt.Printf("%s%s%s\n", aurora.Red(":: "), aurora.Bold("An error occured: "), err)
return
Expand Down
40 changes: 14 additions & 26 deletions cmd/commands/pr/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"github.com/cli/cli/git"
"github.com/cli/cli/pkg/surveyext"
"github.com/craftamap/bb/cmd/options"
bbgit "github.com/craftamap/bb/git"
"github.com/craftamap/bb/internal"
"github.com/logrusorgru/aurora"
"github.com/spf13/cobra"
)
Expand All @@ -26,41 +24,31 @@ func Add(prCmd *cobra.Command, globalOpts *options.GlobalOptions) {
createCmd := &cobra.Command{
Use: "create",
Short: "Create a pull request",
Annotations: map[string]string{
"RequiresClient": "true",
"RequiresRepository": "true",
},
Run: func(cmd *cobra.Command, args []string) {
var (
sourceBranch string
)
// Initialisation
c := internal.Client{
Username: globalOpts.Username,
Password: globalOpts.Password,
}

bbrepo, err := bbgit.GetBitbucketRepo()
if err != nil {
fmt.Printf("%s%s%s\n", aurora.Red(":: "), aurora.Bold("An error occured: "), err)
return
}
if !bbrepo.IsBitbucketOrg() {
fmt.Printf("%s%s%s\n", aurora.Yellow(":: "), aurora.Bold("Warning: "), "Are you sure this is a bitbucket repo?")
return
}

// Get Current Branch
sourceBranch, err = git.CurrentBranch()
if err != nil {
fmt.Printf("%s%s%s\n", aurora.Red(":: "), aurora.Bold("An error occured: "), err)
return
}
c := globalOpts.Client
bbrepo := globalOpts.BitbucketRepo

// Prepare required data
var (
sourceBranch string
targetBranch string
title string
body string
defaultBody string
reviewers []string
)
sourceBranch, err := git.CurrentBranch()
if err != nil {
fmt.Printf("%s%s%s\n", aurora.Red(":: "), aurora.Bold("An error occured: "), err)
return
}

// Prepare required data
// First, init default data
repo, err := c.RepositoryGet(bbrepo.RepoOrga, bbrepo.RepoSlug)
if err != nil {
Expand Down
21 changes: 6 additions & 15 deletions cmd/commands/pr/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"strings"

"github.com/craftamap/bb/cmd/options"
bbgit "github.com/craftamap/bb/git"
"github.com/craftamap/bb/internal"
"github.com/logrusorgru/aurora"
"github.com/pkg/browser"
"github.com/spf13/cobra"
Expand All @@ -23,20 +21,13 @@ func Add(prCmd *cobra.Command, globalOpts *options.GlobalOptions) {
Use: "list",
Short: "List and filter pull requests in this repository",
Long: "List and filter pull requests in this repository",
Annotations: map[string]string{
"RequiresClient": "true",
"RequiresRepository": "true",
},
Run: func(cmd *cobra.Command, args []string) {
c := internal.Client{
Username: globalOpts.Username,
Password: globalOpts.Password,
}
bbrepo, err := bbgit.GetBitbucketRepo()
if err != nil {
fmt.Printf("%s%s%s\n", aurora.Red(":: "), aurora.Bold("An error occured: "), err)
return
}
if !bbrepo.IsBitbucketOrg() {
fmt.Printf("%s%s%s\n", aurora.Yellow(":: "), aurora.Bold("Warning: "), "Are you sure this is a bitbucket repo?")
return
}
c := globalOpts.Client
bbrepo := globalOpts.BitbucketRepo

if Web {
repo, err := c.RepositoryGet(bbrepo.RepoOrga, bbrepo.RepoSlug)
Expand Down
22 changes: 6 additions & 16 deletions cmd/commands/pr/merge/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"github.com/cli/cli/git"
"github.com/craftamap/bb/cmd/commands/pr/view"
"github.com/craftamap/bb/cmd/options"
bbgit "github.com/craftamap/bb/git"
"github.com/craftamap/bb/internal"
"github.com/logrusorgru/aurora"
"github.com/spf13/cobra"
)
Expand All @@ -18,24 +16,16 @@ func Add(prCmd *cobra.Command, globalOpts *options.GlobalOptions) {
Use: "merge",
Long: "Merge a pull request on Bitbucket.org",
Short: "Merge a pull request",
Annotations: map[string]string{
"RequiresClient": "true",
"RequiresRepository": "true",
},
Run: func(cmd *cobra.Command, args []string) {
var id int
var err error

c := internal.Client{
Username: globalOpts.Username,
Password: globalOpts.Password,
}

bbrepo, err := bbgit.GetBitbucketRepo()
if err != nil {
fmt.Printf("%s%s%s\n", aurora.Red(":: "), aurora.Bold("An error occured: "), err)
return
}
if !bbrepo.IsBitbucketOrg() {
fmt.Printf("%s%s%s\n", aurora.Yellow(":: "), aurora.Bold("Warning: "), "Are you sure this is a bitbucket repo?")
return
}
c := globalOpts.Client
bbrepo := globalOpts.BitbucketRepo

if len(args) > 0 {
id, err = strconv.Atoi(args[0])
Expand Down
22 changes: 6 additions & 16 deletions cmd/commands/pr/statuses/statuses.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (

"github.com/cli/cli/git"
"github.com/craftamap/bb/cmd/options"
bbgit "github.com/craftamap/bb/git"
"github.com/craftamap/bb/internal"
"github.com/logrusorgru/aurora"
"github.com/spf13/cobra"
)
Expand All @@ -17,24 +15,16 @@ func Add(prCmd *cobra.Command, globalOpts *options.GlobalOptions) {
Use: "statuses",
Short: "Show CI status for a single pull request",
Long: "Show CI status for a single pull request",
Annotations: map[string]string{
"RequiresClient": "true",
"RequiresRepository": "true",
},
Run: func(cmd *cobra.Command, args []string) {
var id int
var err error

c := internal.Client{
Username: globalOpts.Username,
Password: globalOpts.Password,
}

bbrepo, err := bbgit.GetBitbucketRepo()
if err != nil {
fmt.Printf("%s%s%s\n", aurora.Red(":: "), aurora.Bold("An error occured: "), err)
return
}
if !bbrepo.IsBitbucketOrg() {
fmt.Printf("%s%s%s\n", aurora.Yellow(":: "), aurora.Bold("Warning: "), "Are you sure this is a bitbucket repo?")
return
}
c := globalOpts.Client
bbrepo := globalOpts.BitbucketRepo

if len(args) > 0 {
id, err = strconv.Atoi(args[0])
Expand Down
21 changes: 6 additions & 15 deletions cmd/commands/pr/view/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/charmbracelet/glamour"
"github.com/cli/cli/git"
"github.com/craftamap/bb/cmd/options"
bbgit "github.com/craftamap/bb/git"
"github.com/craftamap/bb/internal"
"github.com/logrusorgru/aurora"
"github.com/pkg/browser"
Expand All @@ -23,24 +22,16 @@ func Add(prCmd *cobra.Command, globalOpts *options.GlobalOptions) {
Use: "view",
Short: "View a pull request",
Long: "Display the title, body, and other information about a pull request.",
Annotations: map[string]string{
"RequiresClient": "true",
"RequiresRepository": "true",
},
Run: func(cmd *cobra.Command, args []string) {
var id int
var err error
c := internal.Client{
Username: globalOpts.Username,
Password: globalOpts.Password,
}

bbrepo, err := bbgit.GetBitbucketRepo()

if err != nil {
fmt.Printf("%s%s%s\n", aurora.Red(":: "), aurora.Bold("An error occured: "), err)
return
}
if !bbrepo.IsBitbucketOrg() {
fmt.Printf("%s%s%s\n", aurora.Yellow(":: "), aurora.Bold("Warning: "), "Are you sure this is a bitbucket repo?")
return
}
c := globalOpts.Client
bbrepo := globalOpts.BitbucketRepo

if len(args) > 0 {
id, err = strconv.Atoi(args[0])
Expand Down
9 changes: 7 additions & 2 deletions cmd/options/global_options.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package options

import (
bbgit "github.com/craftamap/bb/git"
"github.com/craftamap/bb/internal"
)

type GlobalOptions struct {
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
BitbucketRepo *bbgit.BitbucketRepo
Client *internal.Client
}
Loading

0 comments on commit b5cd563

Please # to comment.