Skip to content

Commit

Permalink
Add --list flag support for open cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
manumilou committed Jun 18, 2018
1 parent a472e3b commit 8774738
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pkg/cmd/open.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"os"

"github.com/spf13/cobra"

"github.com/devbuddy/devbuddy/pkg/helpers/open"
Expand All @@ -14,6 +16,10 @@ var openCmd = &cobra.Command{
Args: zeroOrOneArg,
}

func init() {
openCmd.Flags().Bool("list", false, "List available project's URLs")
}

func openRun(cmd *cobra.Command, args []string) {
linkName := ""
if len(args) == 1 {
Expand All @@ -23,6 +29,12 @@ func openRun(cmd *cobra.Command, args []string) {
proj, err := project.FindCurrent()
checkError(err)

if GetFlagBool(cmd, "list") {
err = open.PrintLinks(proj)
checkError(err)
os.Exit(0)
}

url, err := open.FindLink(proj, linkName)
checkError(err)

Expand Down
13 changes: 13 additions & 0 deletions pkg/helpers/open/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (

"github.com/devbuddy/devbuddy/pkg/helpers"
"github.com/devbuddy/devbuddy/pkg/project"

color "github.com/logrusorgru/aurora"
)

// Open a file or URL with the default application, return immediately.
Expand Down Expand Up @@ -51,3 +53,14 @@ func FindLink(proj *project.Project, linkName string) (url string, err error) {

return
}

func PrintLinks(proj *project.Project) (err error) {
if len(proj.Manifest.Open) == 0 {
return fmt.Errorf("no links found in the project")
}
for title, url := range proj.Manifest.Open {
fmt.Println(color.Green(title), "\t", url)
}

return nil
}
14 changes: 14 additions & 0 deletions pkg/helpers/open/open_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,17 @@ func TestFindLinkGithub(t *testing.T) {
require.Equal(t, expectedURL, url)
}
}

func TestPrintLinks(t *testing.T) {
open := map[string]string{}
proj := &project.Project{Manifest: &manifest.Manifest{Open: open}}

err := PrintLinks(proj)
require.Error(t, err)

open = map[string]string{"doc": "http://doc.com"}
proj = &project.Project{Manifest: &manifest.Manifest{Open: open}}

err = PrintLinks(proj)
require.NoError(t, err)
}

0 comments on commit 8774738

Please sign in to comment.