Skip to content

Commit

Permalink
Merge pull request #7 from michelleN/update-dep
Browse files Browse the repository at this point in the history
fix: home is current dir when homepath empty
  • Loading branch information
Michelle Noorali authored Jan 2, 2018
2 parents e9fae4c + aa0f25c commit d4f4e27
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 3 deletions.
20 changes: 19 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@

[[constraint]]
name = "github.com/spf13/cobra"

[[constraint]]
name = "github.com/Sirupsen/logrus"
13 changes: 12 additions & 1 deletion cmd/pack-repo/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"io"
"os"

"github.com/Azure/draft/pkg/draft/draftpath"
"github.com/Azure/draft/pkg/draft/pack/repo"
Expand Down Expand Up @@ -40,7 +41,17 @@ func (a *addCmd) complete(args []string) error {
return err
}
a.source = args[0]
a.home = draftpath.Home(homePath())
home := homePath()
if home == "" {
path, err := os.Getwd()
if err != nil {
return err
}
home = path
}

a.home = draftpath.Home(home)
debug("home path: %s", a.home)
return nil
}

Expand Down
27 changes: 27 additions & 0 deletions cmd/pack-repo/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,33 @@ func TestAddReturnsPackRepoExistsErr(t *testing.T) {
}
}

func TestAddComplete(t *testing.T) {

add := &addCmd{
out: ioutil.Discard,
err: ioutil.Discard,
}

source := "testdata/packrepo"
args := []string{source}
if err := add.complete(args); err != nil {
t.Errorf("unexpected error: %s", err)
}
if add.source != source {
t.Errorf("Incorrect source. Expected %s, got %s", source, add.source)
}

home, err := os.Getwd()
if err != nil {
t.Fatal(err)
}

if string(add.home) != home {
t.Errorf("Incorrect home. Expected %s, got %s", home, add.home)
}

}

// tempDir creates and returns the path as well as a function to clean the temporary directory
func tempDir(t *testing.T) (string, func()) {
t.Helper()
Expand Down
21 changes: 20 additions & 1 deletion cmd/pack-repo/pack-repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,34 @@ import (
"os"
"strings"

log "github.com/Sirupsen/logrus"
"github.com/spf13/cobra"
)

const homeEnvVar = "DRAFT_HOME"

var globalUsage = `The Draft pack repository plugin.
var (
flagDebug bool
globalUsage = `The Draft pack repository plugin.
`
)

func newRootCmd(out io.Writer, in io.Reader) *cobra.Command {
cmd := &cobra.Command{
Use: "pack-repo",
Short: "the Draft pack repository plugin",
Long: globalUsage,
SilenceUsage: true,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if flagDebug {
log.SetLevel(log.DebugLevel)
}
},
}

p := cmd.PersistentFlags()
p.BoolVar(&flagDebug, "debug", false, "enable verbose output")

cmd.AddCommand(
newAddCmd(out),
newListCmd(out),
Expand All @@ -41,6 +53,13 @@ func homePath() string {
return os.Getenv(homeEnvVar)
}

func debug(format string, args ...interface{}) {
if flagDebug {
format = fmt.Sprintf("[debug] %s\n", format)
fmt.Printf(format, args...)
}
}

func main() {
cmd := newRootCmd(os.Stdout, os.Stdin)
if err := cmd.Execute(); err != nil {
Expand Down

0 comments on commit d4f4e27

Please sign in to comment.