Skip to content

Commit

Permalink
Remove activate subcommand, move code out of cli package (#333)
Browse files Browse the repository at this point in the history
* Remove activate subcommand, move code out of `cli` package

* Remove integration test
  • Loading branch information
ofalvai authored Jul 31, 2024
1 parent ec8abbe commit 5b7e38a
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 208 deletions.
91 changes: 0 additions & 91 deletions _tests/integration/activate_test.go

This file was deleted.

142 changes: 35 additions & 107 deletions cli/activate.go → activator/steplib/activate.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cli
package steplib

import (
"fmt"
Expand All @@ -7,92 +7,28 @@ import (
"slices"

"github.com/bitrise-io/go-utils/command"
"github.com/bitrise-io/go-utils/log"
"github.com/bitrise-io/go-utils/pathutil"
"github.com/bitrise-io/stepman/models"
"github.com/bitrise-io/stepman/stepman"
"github.com/urfave/cli"
)

var activateCommand = cli.Command{
Name: "activate",
Usage: "Copy the step with specified --id, and --version, into provided path. If --version flag is not set, the latest version of the step will be used. If --copyyml flag is set, step.yml will be copied to the given path.",
Flags: []cli.Flag{
cli.StringFlag{
Name: CollectionKey + ", " + collectionKeyShort,
Usage: "Collection of step.",
EnvVar: CollectionPathEnvKey,
},
cli.StringFlag{
Name: IDKey + ", " + idKeyShort,
Usage: "Step id.",
},
cli.StringFlag{
Name: VersionKey + ", " + versionKeyShort,
Usage: "Step version.",
},
cli.StringFlag{
Name: PathKey + ", " + pathKeyShort,
Usage: "Path where the step will copied.",
},
cli.StringFlag{
Name: CopyYMLKey + ", " + copyYMLKeyShort,
Usage: "Path where the activated step's step.yml will be copied.",
},
cli.BoolFlag{
Name: UpdateKey + ", " + updateKeyShort,
Usage: "If flag is set, and collection doesn't contains the specified step, the collection will updated.",
},
},
Action: func(c *cli.Context) error {
if err := activate(c); err != nil {
failf("Command failed: %s", err)
}
return nil
},
}

func activate(c *cli.Context) error {
stepLibURI := c.String(CollectionKey)
if stepLibURI == "" {
return fmt.Errorf("no steplib specified")
}

id := c.String(IDKey)
if id == "" {
return fmt.Errorf("no step ID specified")
}

path := c.String(PathKey)
if path == "" {
return fmt.Errorf("no destination path specified")
}

version := c.String(VersionKey)
copyYML := c.String(CopyYMLKey)
update := c.Bool(UpdateKey)
logger := log.NewDefaultLogger(false)
isOfflineMode := false
var errStepNotAvailableOfflineMode error = fmt.Errorf("step not available in offline mode")

return Activate(stepLibURI, id, version, path, copyYML, update, logger, isOfflineMode)
}

// Activate ...
func Activate(stepLibURI, id, version, destination, destinationStepYML string, updateLibrary bool, log stepman.Logger, isOfflineMode bool) error {
stepLib, err := stepman.ReadStepSpec(stepLibURI)
func ActivateStep(stepLibURI, id, version, destination, destinationStepYML string, log stepman.Logger, isOfflineMode bool) error {
stepCollection, err := stepman.ReadStepSpec(stepLibURI)
if err != nil {
return fmt.Errorf("failed to read %s steplib: %s", stepLibURI, err)
}

step, version, err := queryStep(stepLib, stepLibURI, id, version, updateLibrary, log)
step, version, err := queryStep(stepCollection, stepLibURI, id, version)
if err != nil {
return fmt.Errorf("failed to find step: %s", err)
}

srcFolder, err := activateStep(stepLib, stepLibURI, id, version, step, log, isOfflineMode)
srcFolder, err := activateStep(stepCollection, stepLibURI, id, version, step, log, isOfflineMode)
if err != nil {
if err == errStepNotAvailableOfflineMode {
availableVersions := listCachedStepVersion(log, stepLib, stepLibURI, id)
availableVersions := ListCachedStepVersions(log, stepCollection, stepLibURI, id)
versionList := "Other versions available in the local cache:"
for _, version := range availableVersions {
versionList = versionList + fmt.Sprintf("\n- %s", version)
Expand All @@ -118,17 +54,9 @@ func Activate(stepLibURI, id, version, destination, destinationStepYML string, u
return nil
}

func queryStep(stepLib models.StepCollectionModel, stepLibURI string, id, version string, updateLibrary bool, log stepman.Logger) (models.StepModel, string, error) {
func queryStep(stepLib models.StepCollectionModel, stepLibURI string, id, version string) (models.StepModel, string, error) {
step, stepFound, versionFound := stepLib.GetStep(id, version)
if (!stepFound || !versionFound) && updateLibrary {
var err error
stepLib, err = stepman.UpdateLibrary(stepLibURI, log)
if err != nil {
return models.StepModel{}, "", fmt.Errorf("failed to update %s steplib: %s", stepLibURI, err)
}

step, stepFound, versionFound = stepLib.GetStep(id, version)
}
if !stepFound {
return models.StepModel{}, "", fmt.Errorf("%s steplib does not contain %s step", stepLibURI, id)
}
Expand Down Expand Up @@ -172,33 +100,6 @@ func activateStep(stepLib models.StepCollectionModel, stepLibURI, id, version st
return stepCacheDir, nil
}

func listCachedStepVersion(log stepman.Logger, stepLib models.StepCollectionModel, stepLibURI, stepID string) []string {
versions := []models.Semver{}

for version, step := range stepLib.Steps[stepID].Versions {
_, err := activateStep(stepLib, stepLibURI, stepID, version, step, log, true)
if err != nil {
continue
}

v, err := models.ParseSemver(version)
if err != nil {
log.Warnf("failed to parse version (%s): %s", version, err)
}

versions = append(versions, v)
}

slices.SortFunc(versions, models.CmpSemver)

versionsStr := make([]string, len(versions))
for i, v := range versions {
versionsStr[i] = v.String()
}

return versionsStr
}

func copyStep(src, dst string) error {
if exist, err := pathutil.IsPathExists(dst); err != nil {
return fmt.Errorf("failed to check if %s path exist: %s", dst, err)
Expand Down Expand Up @@ -233,3 +134,30 @@ func copyStepYML(libraryURL, id, version, dest string) error {
}
return nil
}

func ListCachedStepVersions(log stepman.Logger, stepLib models.StepCollectionModel, stepLibURI, stepID string) []string {
versions := []models.Semver{}

for version, step := range stepLib.Steps[stepID].Versions {
_, err := activateStep(stepLib, stepLibURI, stepID, version, step, log, true)
if err != nil {
continue
}

v, err := models.ParseSemver(version)
if err != nil {
log.Warnf("failed to parse version (%s): %s", version, err)
}

versions = append(versions, v)
}

slices.SortFunc(versions, models.CmpSemver)

versionsStr := make([]string, len(versions))
for i, v := range versions {
versionsStr[i] = v.String()
}

return versionsStr
}
3 changes: 2 additions & 1 deletion activator/steplib_ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"path/filepath"

"github.com/bitrise-io/go-utils/pointers"
"github.com/bitrise-io/stepman/activator/steplib"
"github.com/bitrise-io/stepman/cli"
"github.com/bitrise-io/stepman/models"
"github.com/bitrise-io/stepman/stepid"
Expand Down Expand Up @@ -32,7 +33,7 @@ func ActivateSteplibRefStep(
return activationResult, err
}

err = cli.Activate(id.SteplibSource, id.IDorURI, stepInfo.Version, activatedStepDir, stepYMLPath, false, log, isOfflineMode)
err = steplib.ActivateStep(id.SteplibSource, id.IDorURI, stepInfo.Version, activatedStepDir, stepYMLPath, log, isOfflineMode)
if err != nil {
return activationResult, err
}
Expand Down
1 change: 0 additions & 1 deletion cli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ var (
flUpdate,
},
},
activateCommand,
{
Name: "audit",
Usage: "Validates Step or Step Collection.",
Expand Down
5 changes: 0 additions & 5 deletions cli/errors.go

This file was deleted.

3 changes: 2 additions & 1 deletion cli/step_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/bitrise-io/go-utils/log"
"github.com/bitrise-io/go-utils/pointers"
"github.com/bitrise-io/go-utils/stringutil"
"github.com/bitrise-io/stepman/activator/steplib"
"github.com/bitrise-io/stepman/models"
"github.com/bitrise-io/stepman/stepman"
"github.com/urfave/cli"
Expand Down Expand Up @@ -62,7 +63,7 @@ func printRawStepList(log stepman.Logger, stepLibURI string, maintaner string, s
}

if isShort { // print only step IDs and cached versions
cachedVersions := listCachedStepVersion(log, stepLib, stepLibURI, stepID)
cachedVersions := steplib.ListCachedStepVersions(log, stepLib, stepLibURI, stepID)
id := fmt.Sprintf("%s (%s)", stepID, stepGroupInfo.Info.Maintainer)
fmt.Printf("%s cached versions: %s\n", printInMaxNChars(id, 55), strings.Join(cachedVersions, ", "))

Expand Down
4 changes: 2 additions & 2 deletions toolkits/golang_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"testing"

"github.com/bitrise-io/go-utils/command"
"github.com/bitrise-io/stepman/cli"
"github.com/bitrise-io/stepman/activator/steplib"
"github.com/bitrise-io/stepman/stepid"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -219,7 +219,7 @@ func Benchmark_goBuildStep(b *testing.B) {
require.NoError(b, err)
}()

err = cli.Activate("https://github.com/bitrise-io/bitrise-steplib", "xcode-test", "5.1.1", stepDir, "", true, logger, false)
err = steplib.ActivateStep("https://github.com/bitrise-io/bitrise-steplib", "xcode-test", "5.1.1", stepDir, "", logger, false)
require.NoError(b, err)

packageName := "github.com/bitrise-steplib/steps-xcode-test"
Expand Down

0 comments on commit 5b7e38a

Please sign in to comment.