diff --git a/cmd/commands/api/api.go b/cmd/commands/api/api.go index 65aaa5b..417f004 100644 --- a/cmd/commands/api/api.go +++ b/cmd/commands/api/api.go @@ -23,6 +23,9 @@ func Add(rootCmd *cobra.Command, globalOpts *options.GlobalOptions) { Use: "api []", 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) @@ -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) diff --git a/cmd/commands/downloads/download/download.go b/cmd/commands/downloads/download/download.go index 3a675c5..e061e2d 100644 --- a/cmd/commands/downloads/download/download.go +++ b/cmd/commands/downloads/download/download.go @@ -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" @@ -18,22 +17,16 @@ func Add(prCmd *cobra.Command, globalOpts *options.GlobalOptions) { downloadCmd := &cobra.Command{ Use: "download []", 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) diff --git a/cmd/commands/downloads/list/list.go b/cmd/commands/downloads/list/list.go index cd68745..eb5c121 100644 --- a/cmd/commands/downloads/list/list.go +++ b/cmd/commands/downloads/list/list.go @@ -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" @@ -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 { diff --git a/cmd/commands/downloads/upload/upload.go b/cmd/commands/downloads/upload/upload.go index 6707ecf..c002347 100644 --- a/cmd/commands/downloads/upload/upload.go +++ b/cmd/commands/downloads/upload/upload.go @@ -6,8 +6,6 @@ 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" ) @@ -15,21 +13,13 @@ import ( 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") @@ -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 diff --git a/cmd/commands/pr/create/create.go b/cmd/commands/pr/create/create.go index daa1e5a..a000f16 100644 --- a/cmd/commands/pr/create/create.go +++ b/cmd/commands/pr/create/create.go @@ -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" ) @@ -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 { diff --git a/cmd/commands/pr/list/list.go b/cmd/commands/pr/list/list.go index 2488aac..afa88b0 100644 --- a/cmd/commands/pr/list/list.go +++ b/cmd/commands/pr/list/list.go @@ -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" @@ -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) diff --git a/cmd/commands/pr/merge/merge.go b/cmd/commands/pr/merge/merge.go index b6a8a24..d787572 100644 --- a/cmd/commands/pr/merge/merge.go +++ b/cmd/commands/pr/merge/merge.go @@ -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" ) @@ -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]) diff --git a/cmd/commands/pr/statuses/statuses.go b/cmd/commands/pr/statuses/statuses.go index 7e29a1e..4bbc1aa 100644 --- a/cmd/commands/pr/statuses/statuses.go +++ b/cmd/commands/pr/statuses/statuses.go @@ -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" ) @@ -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]) diff --git a/cmd/commands/pr/view/view.go b/cmd/commands/pr/view/view.go index 86faf54..784b0d8 100644 --- a/cmd/commands/pr/view/view.go +++ b/cmd/commands/pr/view/view.go @@ -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" @@ -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]) diff --git a/cmd/options/global_options.go b/cmd/options/global_options.go index 97f8d0f..34450eb 100644 --- a/cmd/options/global_options.go +++ b/cmd/options/global_options.go @@ -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 } diff --git a/cmd/root.go b/cmd/root.go index cddba9c..1f9ca38 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -10,6 +10,8 @@ import ( "github.com/craftamap/bb/cmd/commands/downloads" "github.com/craftamap/bb/cmd/commands/pr" "github.com/craftamap/bb/cmd/options" + bbgit "github.com/craftamap/bb/git" + "github.com/craftamap/bb/internal" "github.com/kirsle/configdir" "github.com/logrusorgru/aurora" "github.com/spf13/cobra" @@ -23,14 +25,32 @@ var ( Long: "Work seamlessly with Bitbucket.org from the command line.", Example: `$ bb pr list`, PersistentPreRun: func(cmd *cobra.Command, args []string) { - err := viper.Unmarshal(&globalOpts) - if err != nil { - fmt.Printf("%s%s%s\n", aurora.Red(":: "), aurora.Bold("An error occured: "), err) - return + username := viper.GetString("username") + password := viper.GetString("password") + + if _, ok := cmd.Annotations["RequiresRepository"]; ok { + bbrepo, err := bbgit.GetBitbucketRepo() + if err != nil { + fmt.Printf("%s%s%s\n", aurora.Red(":: "), aurora.Bold("An error occured: "), err) + os.Exit(1) + } + if !bbrepo.IsBitbucketOrg() { + fmt.Printf("%s%s%s\n", aurora.Red(":: "), aurora.Bold("An error occured: "), "Are you sure this is a bitbucket repo?") + os.Exit(1) + } + + globalOpts.BitbucketRepo = bbrepo + } + + if _, ok := cmd.Annotations["RequiresClient"]; ok { + globalOpts.Client = &internal.Client{ + Username: username, + Password: password, + } } if cmd.Name() != "login" { - if globalOpts.Password == "" { + if password == "" { fmt.Println(aurora.Yellow("::"), aurora.Bold("Warning:"), "Look's like you have not set up bb yet.") fmt.Println(aurora.Yellow("::"), aurora.Bold("Warning:"), "Run", aurora.BgWhite(aurora.Black(" bb auth login ")), "to set up bb.") }