Skip to content

Commit

Permalink
Adds open command for quickly opening Svix's hosted docs (#15)
Browse files Browse the repository at this point in the history
* Added a docs command

* Remove lint issues - now produces error

* Updated to an open command - open docs/api

* Delete docs.go

* update usage strings & organize in map

Co-authored-by: Frank <84723448+svix-frank@users.noreply.github.com>
Co-authored-by: Frank Chiarulli Jr <frank@svix.com>
  • Loading branch information
3 people authored Jun 4, 2021
1 parent 40474bc commit 4ef6409
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 3 deletions.
57 changes: 57 additions & 0 deletions cmd/open.go
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
6 changes: 3 additions & 3 deletions pretty/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 4ef6409

Please sign in to comment.