-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds
open
command for quickly opening Svix's hosted docs (#15)
* 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
1 parent
40474bc
commit 4ef6409
Showing
5 changed files
with
64 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters