diff --git a/cmd/open.go b/cmd/open.go new file mode 100644 index 0000000..696c7e0 --- /dev/null +++ b/cmd/open.go @@ -0,0 +1,57 @@ +package cmd + +import ( + "fmt" + "os" + "strings" + + "github.com/skratchdot/open-golang/open" + "github.com/spf13/cobra" + "github.com/svixhq/svix-cli/pretty" + "github.com/svixhq/svix-cli/validators" +) + +var openableURLs = map[string]string{ + "docs": "https://docs.svix.com/", + "api": "https://api.svix.com/docs", +} + +type openCmd struct { + cmd *cobra.Command +} + +func keys(m map[string]string) []string { + keys := make([]string, 0, len(m)) + for key := range m { + keys = append(keys, key) + } + return keys +} + +func newOpenCmd() *openCmd { + keys := keys(openableURLs) + oc := &openCmd{ + cmd: &cobra.Command{ + Use: fmt.Sprintf("open [%s]", strings.Join(keys, "|")), + ValidArgs: keys, + Args: validators.ExactValidArgs(1), + Short: "Quickly open Svix pages in your browser", + Long: `Quickly open Svix pages in your browser: +docs - opens the Svix documentation +api - opens the Svix API documentation + `, + Run: func(cmd *cobra.Command, args []string) { + url := openableURLs[args[0]] + err := open.Run(url) + if err != nil { + fmt.Fprintf(os.Stderr, `Failed to open %s in your default browser +To open it manually navigate to: +%s +`, args[0], pretty.MakeTerminalLink(url, url)) + os.Exit(1) + } + }, + }, + } + return oc +} diff --git a/cmd/root.go b/cmd/root.go index 16b7dda..1ab913b 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -52,6 +52,7 @@ func init() { rootCmd.AddCommand(newMessageAttemptCmd().cmd) rootCmd.AddCommand(newVerifyCmd().cmd) rootCmd.AddCommand(newCompletionCmd().cmd) + rootCmd.AddCommand(newOpenCmd().cmd) } // initConfig reads in config file and ENV variables if set. diff --git a/go.mod b/go.mod index 3eeb9ff..2d0857c 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/BurntSushi/toml v0.3.1 github.com/manifoldco/promptui v0.8.0 github.com/mitchellh/go-homedir v1.1.0 + github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 github.com/spf13/cobra v1.1.3 github.com/spf13/viper v1.7.1 github.com/svixhq/svix-libs v0.14.1-0.20210531145855-ff10b4b07c16 diff --git a/go.sum b/go.sum index 4b31f2c..efd14b1 100644 --- a/go.sum +++ b/go.sum @@ -231,6 +231,8 @@ github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= +github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 h1:JIAuq3EEf9cgbU6AtGPK4CTG3Zf6CKMNqf0MHTggAUA= +github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966/go.mod h1:sUM3LWHvSMaG192sy56D9F7CNvL7jUJVXoqM1QKLnog= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s= diff --git a/pretty/print.go b/pretty/print.go index 9916eb0..08a74fc 100644 --- a/pretty/print.go +++ b/pretty/print.go @@ -23,6 +23,6 @@ func Print(v interface{}, opts *PrintOptions) { fmt.Println(string(b)) } -// func makeTerminalHyperlink(name, url string) string { -// return fmt.Sprintf("\u001B]8;;%s\a%s\u001B]8;;\a", url, name) -// } +func MakeTerminalLink(name, url string) string { + return fmt.Sprintf("\u001B]8;;%s\a%s\u001B]8;;\a", url, name) +}