Skip to content

Commit

Permalink
Merge pull request #38 from gkiki90/update_if_needed
Browse files Browse the repository at this point in the history
update flag
  • Loading branch information
viktorbenei committed Jul 22, 2015
2 parents 5be0c4c + bed8ad2 commit b741ecf
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 28 deletions.
26 changes: 25 additions & 1 deletion cli/activate.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ import (
"github.com/codegangsta/cli"
)

func parseUpdate(c *cli.Context) bool {
if c.IsSet(UpdateKey) {
return c.Bool(UpdateKey)
}
return false
}

func activate(c *cli.Context) {
log.Debugln("[STEPMAN] - Activate")

Expand All @@ -33,6 +40,8 @@ func activate(c *cli.Context) {

copyYML := c.String(CopyYMLKey)

update := parseUpdate(c)

// Get step
collection, err := stepman.ReadStepSpec(collectionURI)
if err != nil {
Expand All @@ -51,8 +60,23 @@ func activate(c *cli.Context) {
version = latest
}

log.Debugf(" (id:%#v) (version:%#v) (path:%#v) (copyYML:%#v)\n", id, version, path, copyYML)
// Check step exist in collection
if _, found := collection.GetStep(id, version); !found {
if update {
log.Infof("[STEPMAN] - Collection doesn't contain step (id:%s) (version:%s) -- Updating collection", id, version)
if err := updateCollection(collectionURI); err != nil {
log.Fatalf("[STEPMAN] - Failed to update collection:%s error:%v", collectionURI, err)
}

if _, found := collection.GetStep(id, version); !found {
log.Fatalf("[STEPMAN] - Even the updated collection doesn't contain step (id:%s) (version:%s)", id, version)
}
} else {
log.Fatalf("[STEPMAN] - Collection doesn't contain step (id:%s) (version:%s)", id, version)
}
}

// Check step exist in local cache
stepCacheDir := stepman.GetStepCacheDirPath(collectionURI, id, version)
if exist, err := pathutil.IsPathExists(stepCacheDir); err != nil {
log.Fatal("[STEPMAN] - Failed to check path:", err)
Expand Down
2 changes: 2 additions & 0 deletions cli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var (
flCollection,
flID,
flVersion,
flUpdate,
},
},
{
Expand All @@ -44,6 +45,7 @@ var (
flVersion,
flPath,
flCopyYML,
flUpdate,
},
},
}
Expand Down
20 changes: 19 additions & 1 deletion cli/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func download(c *cli.Context) {
log.Info("[STEPMAN] - Download")

// StepSpec collection path
// Input validation
collectionURI := c.String(CollectionKey)
if collectionURI == "" {
collectionURI = os.Getenv(CollectionPathEnvKey)
Expand Down Expand Up @@ -42,6 +42,24 @@ func download(c *cli.Context) {
version = latest
}

update := parseUpdate(c)

// Check step exist in collection
if _, found := collection.GetStep(id, version); !found {
if update {
log.Infof("[STEPMAN] - Collection doesn't contain step (id:%s) (version:%s) -- Updating collection", id, version)
if err := updateCollection(collectionURI); err != nil {
log.Fatalf("[STEPMAN] - Failed to update collection:%s error:%v", collectionURI, err)
}

if _, found := collection.GetStep(id, version); !found {
log.Fatalf("[STEPMAN] - Even the updated collection doesn't contain step (id:%s) (version:%s)", id, version)
}
} else {
log.Fatalf("[STEPMAN] - Collection doesn't contain step (id:%s) (version:%s)", id, version)
}
}

if err := stepman.DownloadStep(collection, id, version); err != nil {
log.Fatal("[STEPMAN] - Failed to download step")
}
Expand Down
22 changes: 15 additions & 7 deletions cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ package cli
import "github.com/codegangsta/cli"

const (
// HelpKey ...
HelpKey string = "help"
helpKeyShort string = "h"

// VersionKey ...
VersionKey string = "version"
versionKeyShort string = "v"

// CollectionPathEnvKey ...
CollectionPathEnvKey string = "STEPMAN_COLLECTION"
// CollectionKey ...
Expand All @@ -25,10 +33,6 @@ const (
IDKey string = "id"
idKeyShort string = "i"

// VersionKey ...
VersionKey string = "version"
versionKeyShort string = "v"

// PathKey ...
PathKey string = "path"
pathKeyShort string = "p"
Expand All @@ -37,9 +41,9 @@ const (
CopyYMLKey string = "copyyml"
copyYMLKeyShort string = "y"

// HelpKey ...
HelpKey string = "help"
helpKeyShort string = "h"
// UpdateKey ...
UpdateKey string = "update"
updateKeyShort string = "u"
)

var (
Expand Down Expand Up @@ -86,6 +90,10 @@ var (
Value: "",
Usage: "",
}
flUpdate = cli.BoolFlag{
Name: UpdateKey + ", " + updateKeyShort,
Usage: "If flag is set, and collection doesn't contains the specified step, the collection will updated.",
}
)

func init() {
Expand Down
46 changes: 27 additions & 19 deletions cli/update.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"errors"
"os"

log "github.com/Sirupsen/logrus"
Expand All @@ -9,6 +10,30 @@ import (
"github.com/codegangsta/cli"
)

func updateCollection(collectionURI string) error {
pth := stepman.GetCollectionBaseDirPath(collectionURI)
if exists, err := pathutil.IsPathExists(pth); err != nil {
return err
} else if !exists {
return errors.New("[STEPMAN] - Not initialized")
}

if err := stepman.DoGitPull(pth); err != nil {
return err
}

specPth := pth + "/steplib.yml"
collection, err := stepman.ParseStepCollection(specPth)
if err != nil {
return err
}

if err := stepman.WriteStepSpecToFile(collectionURI, collection); err != nil {
return err
}
return nil
}

func update(c *cli.Context) {
log.Info("[STEPMAN] - Update")

Expand All @@ -28,25 +53,8 @@ func update(c *cli.Context) {
}

for _, URI := range collectionURIs {
pth := stepman.GetCollectionBaseDirPath(URI)
if exists, err := pathutil.IsPathExists(pth); err != nil {
log.Fatal("[STEPMAN] - Failed to check path:", err)
} else if !exists {
log.Fatal("[STEPMAN] - Not initialized")
}

if err := stepman.DoGitPull(pth); err != nil {
log.Fatal("[STEPMAN] - Failed to do git update:", err)
}

specPth := pth + "/steplib.yml"
collection, err := stepman.ParseStepCollection(specPth)
if err != nil {
log.Fatal("[STEPMAN] - Failed to read step spec:", err)
}

if err := stepman.WriteStepSpecToFile(URI, collection); err != nil {
log.Fatal("[STEPMAN] - Failed to save step spec:", err)
if err := updateCollection(URI); err != nil {
log.Fatalf("Failed to update collection:%s error:%v", URI, err)
}
}

Expand Down

0 comments on commit b741ecf

Please sign in to comment.