Skip to content

Commit

Permalink
Add an option to open a browser with the package's web url (#390)
Browse files Browse the repository at this point in the history
  • Loading branch information
lizrabuya authored Oct 30, 2024
1 parent 87cce00 commit 08b24e2
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 18 deletions.
13 changes: 13 additions & 0 deletions internal/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"encoding/base64"
"fmt"
"strings"

"github.com/pkg/browser"
)

func GenerateGraphQLID(prefix, uuid string) string {
Expand All @@ -14,3 +16,14 @@ func GenerateGraphQLID(prefix, uuid string) string {

return graphqlID.String()
}

func OpenInWebBrowser(openInWeb bool, webUrl string) error {
if openInWeb {
err := browser.OpenURL(webUrl)
if err != nil {
fmt.Println("Error opening browser: ", err)
return err
}
}
return nil
}
14 changes: 0 additions & 14 deletions pkg/cmd/build/build.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package build

import (
"fmt"

"github.com/MakeNowJust/heredoc"
"github.com/buildkite/cli/v3/pkg/cmd/factory"
"github.com/buildkite/cli/v3/pkg/cmd/validation"
"github.com/charmbracelet/lipgloss"
"github.com/pkg/browser"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -40,17 +37,6 @@ func NewCmdBuild(f *factory.Factory) *cobra.Command {
return &cmd
}

func openBuildInBrowser(openInWeb bool, webUrl string) error {
if openInWeb {
err := browser.OpenURL(webUrl)
if err != nil {
fmt.Println("Error opening browser: ", err)
return err
}
}
return nil
}

func renderResult(result string) string {
return lipgloss.JoinVertical(lipgloss.Top,
lipgloss.NewStyle().Padding(0, 0).Render(result))
Expand Down
3 changes: 2 additions & 1 deletion pkg/cmd/build/cancel.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
buildResolver "github.com/buildkite/cli/v3/internal/build/resolver"
"github.com/buildkite/cli/v3/internal/io"
pipelineResolver "github.com/buildkite/cli/v3/internal/pipeline/resolver"
"github.com/buildkite/cli/v3/internal/util"
"github.com/buildkite/cli/v3/pkg/cmd/factory"
"github.com/buildkite/go-buildkite/v4"
"github.com/charmbracelet/huh/spinner"
Expand Down Expand Up @@ -84,5 +85,5 @@ func cancelBuild(ctx context.Context, org string, pipeline string, buildId strin

fmt.Printf("%s\n", renderResult(fmt.Sprintf("Build canceled: %s", build.WebURL)))

return openBuildInBrowser(web, build.WebURL)
return util.OpenInWebBrowser(web, build.WebURL)
}
3 changes: 2 additions & 1 deletion pkg/cmd/build/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/MakeNowJust/heredoc"
"github.com/buildkite/cli/v3/internal/io"
"github.com/buildkite/cli/v3/internal/pipeline/resolver"
"github.com/buildkite/cli/v3/internal/util"
"github.com/buildkite/cli/v3/pkg/cmd/factory"
"github.com/buildkite/go-buildkite/v4"
"github.com/charmbracelet/huh/spinner"
Expand Down Expand Up @@ -142,5 +143,5 @@ func newBuild(ctx context.Context, org string, pipeline string, f *factory.Facto

fmt.Printf("%s\n", renderResult(fmt.Sprintf("Build created: %s", build.WebURL)))

return openBuildInBrowser(web, build.WebURL)
return util.OpenInWebBrowser(web, build.WebURL)
}
3 changes: 2 additions & 1 deletion pkg/cmd/build/rebuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
buildResolver "github.com/buildkite/cli/v3/internal/build/resolver"
"github.com/buildkite/cli/v3/internal/build/resolver/options"
pipelineResolver "github.com/buildkite/cli/v3/internal/pipeline/resolver"
"github.com/buildkite/cli/v3/internal/util"
"github.com/buildkite/cli/v3/pkg/cmd/factory"
"github.com/buildkite/go-buildkite/v4"
"github.com/charmbracelet/huh/spinner"
Expand Down Expand Up @@ -102,5 +103,5 @@ func rebuild(ctx context.Context, org string, pipeline string, buildId string, w

fmt.Printf("%s\n", renderResult(fmt.Sprintf("Build created: %s", build.WebURL)))

return openBuildInBrowser(web, build.WebURL)
return util.OpenInWebBrowser(web, build.WebURL)
}
12 changes: 11 additions & 1 deletion pkg/cmd/pkg/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"

"github.com/MakeNowJust/heredoc"
"github.com/buildkite/cli/v3/internal/util"
"github.com/buildkite/cli/v3/pkg/cmd/factory"
"github.com/buildkite/go-buildkite/v4"
"github.com/charmbracelet/huh/spinner"
Expand All @@ -23,6 +24,8 @@ type pushPackageConfig struct {
const stdinFileNameFlag = "stdin-file-name"

func NewCmdPackagePush(f *factory.Factory) *cobra.Command {
var web bool

cmd := cobra.Command{
Use: "push registry-name {path/to/file | --stdin-file-name filename -}",
Short: "Push a new package to Buildkite Packages",
Expand All @@ -31,8 +34,14 @@ func NewCmdPackagePush(f *factory.Factory) *cobra.Command {
or via stdin. If passed via stdin, the filename must be provided with the --stdin-file-name flag, as Buildkite
Packages requires a filename for the package.`),
Example: heredoc.Doc(`
Push a package to a Buildkite registry
The web URL of the uploaded package will be printed to stdout.
$ bk package push my-registry my-package.tar.gz
$ cat my-package.tar.gz | bk package push my-registry --stdin-file-name my-package.tar.gz - # Pass package via stdin, note hyphen as the argument
# add -w to open the build in your web browser
$ bk package push my-registry my-package.tar.gz -w
`),
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := loadAndValidateConfig(cmd.Flags(), args)
Expand Down Expand Up @@ -83,11 +92,12 @@ func NewCmdPackagePush(f *factory.Factory) *cobra.Command {

fmt.Fprintf(cmd.OutOrStdout(), "Created package: %s\n", pkg.Name)
fmt.Fprintf(cmd.OutOrStdout(), "View it on the web at: %s\n", pkg.WebURL)
return nil
return util.OpenInWebBrowser(web, pkg.WebURL)
},
}

cmd.Flags().StringP(stdinFileNameFlag, "n", "", "The filename to use for the package, if it's passed via stdin. Invalid otherwise.")
cmd.Flags().BoolVarP(&web, "web", "w", false, "Open in a web browser the package information after it has been uploaded.")

return &cmd
}
Expand Down

0 comments on commit 08b24e2

Please sign in to comment.