diff --git a/cli/activate.go b/cli/activate.go index ddda5b1d..9a6461ea 100644 --- a/cli/activate.go +++ b/cli/activate.go @@ -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") @@ -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 { @@ -51,14 +60,32 @@ 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) } else if !exist { log.Info("[STEPMAN] - Step does not exist, download it") if err := stepman.DownloadStep(collection, id, version); err != nil { + if update { + + } log.Fatal("[STEPMAN] - Failed to download step:", err) } } diff --git a/cli/commands.go b/cli/commands.go index 4b8b0235..d3a46f3f 100644 --- a/cli/commands.go +++ b/cli/commands.go @@ -31,6 +31,7 @@ var ( flCollection, flID, flVersion, + flUpdate, }, }, { @@ -44,6 +45,7 @@ var ( flVersion, flPath, flCopyYML, + flUpdate, }, }, } diff --git a/cli/download.go b/cli/download.go index b3725ef8..a25a0d24 100644 --- a/cli/download.go +++ b/cli/download.go @@ -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) @@ -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") } diff --git a/cli/flags.go b/cli/flags.go index 510ef4bc..c2e0bb84 100644 --- a/cli/flags.go +++ b/cli/flags.go @@ -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 ... @@ -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" @@ -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 ( @@ -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() { diff --git a/cli/update.go b/cli/update.go index 98d73f77..f45cbb4f 100644 --- a/cli/update.go +++ b/cli/update.go @@ -1,6 +1,7 @@ package cli import ( + "errors" "os" log "github.com/Sirupsen/logrus" @@ -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") @@ -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) } }