Skip to content

Commit

Permalink
Exported func (#311)
Browse files Browse the repository at this point in the history
* Update setup.go

* Migrate logging from logrus to go-utils/log

* Update activate_test.go

* Add UpdateLibrary

* Use failf in cli package

* Pull envman's master version
  • Loading branch information
godrei authored Oct 10, 2022
1 parent 6e12d27 commit a88e9a9
Show file tree
Hide file tree
Showing 56 changed files with 189 additions and 3,315 deletions.
6 changes: 4 additions & 2 deletions _tests/integration/activate_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package integration

import (
"errors"
"fmt"
"os"
"os/exec"
"testing"

"github.com/bitrise-io/go-utils/command"
"github.com/bitrise-io/go-utils/command/git"
"github.com/bitrise-io/go-utils/errorutil"
"github.com/bitrise-io/go-utils/pathutil"
"github.com/bitrise-io/stepman/stepman"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -51,7 +52,8 @@ func cloneDeafultStepLib() (string, error) {

cloneCmd := repo.Clone(defaultLibraryURI)
if out, err := cloneCmd.RunAndReturnTrimmedCombinedOutput(); err != nil {
if errorutil.IsExitStatusError(err) {
var exerr *exec.ExitError
if errors.As(err, &exerr) {
return "", fmt.Errorf("%s failed: %s", cloneCmd.PrintableCommandArgs(), out)
}
return "", fmt.Errorf("%s failed: %s", cloneCmd.PrintableCommandArgs(), err)
Expand Down
22 changes: 10 additions & 12 deletions cli/activate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"path/filepath"

"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"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
)

Expand Down Expand Up @@ -45,7 +45,7 @@ var activateCommand = cli.Command{
},
Action: func(c *cli.Context) error {
if err := activate(c); err != nil {
log.Fatalf("Command failed: %s", err)
failf("Command failed: %s", err)
}
return nil
},
Expand All @@ -71,22 +71,22 @@ func activate(c *cli.Context) error {
copyYML := c.String(CopyYMLKey)
update := c.Bool(UpdateKey)

return Activate(stepLibURI, id, version, path, copyYML, update)
return Activate(stepLibURI, id, version, path, copyYML, update, log.NewDefaultLogger(false))
}

// Activate ...
func Activate(stepLibURI, id, version, destination, destinationStepYML string, updateLibrary bool) error {
func Activate(stepLibURI, id, version, destination, destinationStepYML string, updateLibrary bool, log stepman.Logger) error {
stepLib, 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)
step, version, err := queryStep(stepLib, stepLibURI, id, version, updateLibrary, log)
if err != nil {
return fmt.Errorf("failed to find step: %s", err)
}

srcFolder, err := downloadStep(stepLib, stepLibURI, id, version, step)
srcFolder, err := downloadStep(stepLib, stepLibURI, id, version, step, log)
if err != nil {
return fmt.Errorf("failed to download step: %s", err)
}
Expand All @@ -104,13 +104,11 @@ func Activate(stepLibURI, id, version, destination, destinationStepYML string, u
return nil
}

func queryStep(stepLib models.StepCollectionModel, stepLibURI string, id, version string, updateLibrary bool) (models.StepModel, string, error) {
func queryStep(stepLib models.StepCollectionModel, stepLibURI string, id, version string, updateLibrary bool, log stepman.Logger) (models.StepModel, string, error) {
step, stepFound, versionFound := stepLib.GetStep(id, version)
if (!stepFound || !versionFound) && updateLibrary {
log.Infof("StepLib doesn't contain step (%s) with version: %s -- Updating StepLib", id, version)

var err error
stepLib, err = stepman.UpdateLibrary(stepLibURI)
stepLib, err = stepman.UpdateLibrary(stepLibURI, log)
if err != nil {
return models.StepModel{}, "", fmt.Errorf("failed to update %s steplib: %s", stepLibURI, err)
}
Expand All @@ -135,7 +133,7 @@ func queryStep(stepLib models.StepCollectionModel, stepLibURI string, id, versio
return step, version, nil
}

func downloadStep(stepLib models.StepCollectionModel, stepLibURI, id, version string, step models.StepModel) (string, error) {
func downloadStep(stepLib models.StepCollectionModel, stepLibURI, id, version string, step models.StepModel, log stepman.Logger) (string, error) {
route, found := stepman.ReadRoute(stepLibURI)
if !found {
return "", fmt.Errorf("no route found for %s steplib", stepLibURI)
Expand All @@ -145,7 +143,7 @@ func downloadStep(stepLib models.StepCollectionModel, stepLibURI, id, version st
if exist, err := pathutil.IsPathExists(stepCacheDir); err != nil {
return "", fmt.Errorf("failed to check if %s path exist: %s", stepCacheDir, err)
} else if !exist {
if err := stepman.DownloadStep(stepLibURI, stepLib, id, version, step.Source.Commit); err != nil {
if err := stepman.DownloadStep(stepLibURI, stepLib, id, version, step.Source.Commit, log); err != nil {
return "", fmt.Errorf("download failed: %s", err)
}
}
Expand Down
16 changes: 8 additions & 8 deletions cli/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"strings"
"time"

log "github.com/sirupsen/logrus"
"github.com/bitrise-io/go-utils/colorstring"
"github.com/bitrise-io/go-utils/command/git"
"github.com/bitrise-io/go-utils/log"
"github.com/bitrise-io/go-utils/pathutil"
"github.com/bitrise-io/go-utils/retry"
"github.com/bitrise-io/stepman/models"
Expand Down Expand Up @@ -129,34 +129,34 @@ func audit(c *cli.Context) error {
collectionURI := c.String("collection")
if collectionURI != "" {
if beforePR {
log.Warnln("before-pr flag is used only for Step audit")
log.Warnf("before-pr flag is used only for Step audit")
}

if err := auditStepLibBeforeSharePullRequest(collectionURI); err != nil {
log.Fatalf("Audit Step Collection failed, err: %s", err)
failf("Audit Step Collection failed, err: %s", err)
}
} else {
stepYMLPath := c.String("step-yml")
if stepYMLPath != "" {
if exist, err := pathutil.IsPathExists(stepYMLPath); err != nil {
log.Fatalf("Failed to check path (%s), err: %s", stepYMLPath, err)
failf("Failed to check path (%s), err: %s", stepYMLPath, err)
} else if !exist {
log.Fatalf("step.yml doesn't exist at: %s", stepYMLPath)
failf("step.yml doesn't exist at: %s", stepYMLPath)
}

if beforePR {
if err := auditStepBeforeSharePullRequest(stepYMLPath); err != nil {
log.Fatalf("Step audit failed, err: %s", err)
failf("Step audit failed, err: %s", err)
}
} else {
if err := auditStepBeforeShare(stepYMLPath); err != nil {
log.Fatalf("Step audit failed, err: %s", err)
failf("Step audit failed, err: %s", err)
}
}

log.Infof(" * "+colorstring.Greenf("[OK] ")+"Success audit (%s)", stepYMLPath)
} else {
log.Fatalln("'stepman audit' command needs --collection or --step-yml flag")
failf("'stepman audit' command needs --collection or --step-yml flag")
}
}

Expand Down
26 changes: 10 additions & 16 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,23 @@ import (
"os"
"path"

"github.com/bitrise-io/go-utils/log"
"github.com/bitrise-io/stepman/stepman"
"github.com/bitrise-io/stepman/version"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
)

func initLogFormatter() {
log.SetFormatter(&log.TextFormatter{
ForceColors: true,
FullTimestamp: true,
TimestampFormat: "15:04:05",
})
}

func before(c *cli.Context) error {
initLogFormatter()
initHelpAndVersionFlags()
initAppHelpTemplate()

// Log level
logLevel, err := log.ParseLevel(c.String(LogLevelKey))
if err != nil {
return fmt.Errorf("Failed to parse log level, error: %s", err)
if c.String(LogLevelKey) == "debug" {
log.SetEnableDebugLog(true)
}
log.SetLevel(logLevel)

// Setup
err = stepman.CreateStepManDirIfNeeded()
err := stepman.CreateStepManDirIfNeeded()
if err != nil {
return err
}
Expand Down Expand Up @@ -62,6 +51,11 @@ func Run() {
app.Commands = commands

if err := app.Run(os.Args); err != nil {
log.Fatal(err)
failf(err.Error())
}
}

func failf(format string, v ...interface{}) {
log.Errorf(format, v...)
os.Exit(1)
}
11 changes: 6 additions & 5 deletions cli/collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cli
import (
"encoding/json"
"fmt"
"os"

"github.com/bitrise-io/go-utils/colorstring"
flog "github.com/bitrise-io/go-utils/log"
Expand Down Expand Up @@ -80,17 +79,19 @@ func collections(c *cli.Context) error {
} else if format == OutputFormatJSON {
log = flog.NewDefaultJSONLoger()
} else {
fmt.Printf("%s: invalid format: %s\n", colorstring.Red("Error"), format)
os.Exit(1)
failf("invalid format: %s", format)
}

steplibInfos := []models.SteplibInfoModel{}
stepLibURIs := stepman.GetAllStepCollectionPath()
for _, steplibURI := range stepLibURIs {
route, found := stepman.ReadRoute(steplibURI)
if !found {
log.Print(NewErrorOutput("No routing found for steplib: %s", steplibURI))
os.Exit(1)
out := NewErrorOutput("No routing found for steplib: %s", steplibURI)
if format == OutputFormatJSON {
failf(out.JSON())
}
failf(out.String())
}

specPth := stepman.GetStepSpecPath(route)
Expand Down
2 changes: 1 addition & 1 deletion cli/delete_steplib.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cli
import (
"fmt"

log "github.com/sirupsen/logrus"
"github.com/bitrise-io/go-utils/log"
"github.com/bitrise-io/stepman/stepman"
"github.com/urfave/cli"
)
Expand Down
28 changes: 14 additions & 14 deletions cli/download.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cli

import (
log "github.com/sirupsen/logrus"
"github.com/bitrise-io/go-utils/log"
"github.com/bitrise-io/stepman/stepman"
"github.com/urfave/cli"
)
Expand All @@ -10,28 +10,28 @@ func download(c *cli.Context) error {
// Input validation
collectionURI := c.String(CollectionKey)
if collectionURI == "" {
log.Fatalf("No step collection specified")
failf("No step collection specified")
}
route, found := stepman.ReadRoute(collectionURI)
if !found {
log.Fatalf("No route found for lib: %s", collectionURI)
failf("No route found for lib: %s", collectionURI)
}

id := c.String(IDKey)
if id == "" {
log.Fatalf("Missing step id")
failf("Missing step id")
}

collection, err := stepman.ReadStepSpec(collectionURI)
if err != nil {
log.Fatalf("Failed to read step spec, error: %s", err)
failf("Failed to read step spec, error: %s", err)
}

version := c.String(VersionKey)
if version == "" {
latest, err := collection.GetLatestStepVersion(id)
if err != nil {
log.Fatalf("Failed to get step latest version, error: %s", err)
failf("Failed to get step latest version, error: %s", err)
}
version = latest
}
Expand All @@ -49,31 +49,31 @@ func download(c *cli.Context) error {
}

if err := stepman.ReGenerateLibrarySpec(route); err != nil {
log.Fatalf("Failed to update collection:%s error:%v", collectionURI, err)
failf("Failed to update collection:%s error:%v", collectionURI, err)
}

if _, stepFound, versionFound := collection.GetStep(id, version); !stepFound || !versionFound {
if !stepFound {
log.Fatalf("Even the updated collection doesn't contain step with id: %s", id)
failf("Even the updated collection doesn't contain step with id: %s", id)
} else if !versionFound {
log.Fatalf("Even the updated collection doesn't contain step (%s) with version: %s", id, version)
failf("Even the updated collection doesn't contain step (%s) with version: %s", id, version)
}
}
} else {
if !stepFound {
log.Fatalf("Collection doesn't contain step with id: %s -- Updating StepLib", id)
failf("Collection doesn't contain step with id: %s -- Updating StepLib", id)
} else if !versionFound {
log.Fatalf("Collection doesn't contain step (%s) with version: %s -- Updating StepLib", id, version)
failf("Collection doesn't contain step (%s) with version: %s -- Updating StepLib", id, version)
}
}
}

if step.Source == nil {
log.Fatalf("Missing step's (%s) Source property", id)
failf("Missing step's (%s) Source property", id)
}

if err := stepman.DownloadStep(collectionURI, collection, id, version, step.Source.Commit); err != nil {
log.Fatalf("Failed to download step, error: %s", err)
if err := stepman.DownloadStep(collectionURI, collection, id, version, step.Source.Commit, log.NewDefaultLogger(false)); err != nil {
failf("Failed to download step, error: %s", err)
}

return nil
Expand Down
6 changes: 3 additions & 3 deletions cli/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"os"
"path/filepath"

log "github.com/sirupsen/logrus"
"github.com/bitrise-io/go-utils/fileutil"
"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"
Expand Down Expand Up @@ -101,15 +101,15 @@ func export(c *cli.Context) error {
return fmt.Errorf("Failed to check if setup was done for StepLib, error: %s", err)
} else if !exist {
log.Infof("StepLib does not exist, setup...")
if err := stepman.SetupLibrary(steplibURI); err != nil {
if err := stepman.SetupLibrary(steplibURI, log.NewDefaultLogger(false)); err != nil {
return fmt.Errorf("Failed to setup StepLib, error: %s", err)
}
}

// Prepare spec
stepLibSpec, err := stepman.ReadStepSpec(steplibURI)
if err != nil {
log.Fatalf("Failed to read StepLib spec, error: %s", err)
failf("Failed to read StepLib spec, error: %s", err)
}

switch exportType {
Expand Down
Loading

0 comments on commit a88e9a9

Please sign in to comment.