From be9c1020f7c70e830bd43e84e9dec2ac1d63c4d1 Mon Sep 17 00:00:00 2001 From: Krisztian Goedrei Date: Thu, 3 Sep 2015 14:05:51 +0200 Subject: [PATCH] step_info --- cli/commands.go | 11 +++++++ cli/download.go | 2 -- cli/step_info.go | 83 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 cli/step_info.go diff --git a/cli/commands.go b/cli/commands.go index 53fccaec..2a5158dc 100644 --- a/cli/commands.go +++ b/cli/commands.go @@ -24,6 +24,17 @@ var ( flCollection, }, }, + { + Name: "step-info", + Aliases: []string{"i"}, + Usage: "Provides information (step ID, last version, given version) about specified step.", + Action: stepInfo, + Flags: []cli.Flag{ + flCollection, + flID, + flVersion, + }, + }, { Name: "download", Aliases: []string{"d"}, diff --git a/cli/download.go b/cli/download.go index 954296d9..119e379d 100644 --- a/cli/download.go +++ b/cli/download.go @@ -7,8 +7,6 @@ import ( ) func download(c *cli.Context) { - log.Info("[STEPMAN] - Download") - // Input validation collectionURI := c.String(CollectionKey) if collectionURI == "" { diff --git a/cli/step_info.go b/cli/step_info.go new file mode 100644 index 00000000..9c8dec66 --- /dev/null +++ b/cli/step_info.go @@ -0,0 +1,83 @@ +package cli + +import ( + "encoding/json" + "fmt" + + log "github.com/Sirupsen/logrus" + "github.com/bitrise-io/stepman/stepman" + "github.com/codegangsta/cli" +) + +// StepInfoModel ... +type StepInfoModel struct { + StepID string `json:"step_id,omitempty" yaml:"step_id,omitempty"` + StepVersion string `json:"step_version,omitempty" yaml:"step_version,omitempty"` + LatestVersion string `json:"latest_version,omitempty" yaml:"latest_version,omitempty"` +} + +func getStepInfoString(id, version, latest string) (string, error) { + stepInfo := StepInfoModel{ + StepID: id, + StepVersion: version, + LatestVersion: latest, + } + + bytes, err := json.Marshal(stepInfo) + if err != nil { + return "", err + } + return string(bytes), nil +} + +func stepInfo(c *cli.Context) { + // Input validation + collectionURI := c.String(CollectionKey) + if collectionURI == "" { + log.Fatalln("[STEPMAN] - No step collection specified") + } + + id := c.String(IDKey) + if id == "" { + log.Fatal("[STEPMAN] - Missing step id") + } + + version := c.String(VersionKey) + + // Check if step exist in collection + collection, err := stepman.ReadStepSpec(collectionURI) + if err != nil { + log.Fatalln("[STEPMAN] - Failed to read steps spec (spec.json)") + } + + stepFound := false + if version == "" { + stepFound = collection.IsStepExist(id) + } else { + _, stepFound = collection.GetStep(id, version) + } + + if !stepFound { + if version == "" { + log.Fatalf("[STEPMAN] - Collection doesn't contain any version of step (id:%s)", id) + } else { + log.Fatalf("[STEPMAN] - Collection doesn't contain step (id:%s) (version:%s)", id, version) + } + } + + latest, err := collection.GetLatestStepVersion(id) + if err != nil { + log.Fatalf("[STEPMAN] - Failed to get latest version of step (id:%s)", id) + } + + if version == "" { + version = latest + } + + stepInfoString, err := getStepInfoString(id, version, latest) + if err != nil { + log.Fatal("Failed to generate step info, err:", err) + } + + fmt.Println(stepInfoString) +}