Skip to content

Commit 0b14814

Browse files
authored
refactor: Removed cli from dependency chain activator->cli->stepman. (#367)
1 parent 00fed2c commit 0b14814

4 files changed

Lines changed: 140 additions & 130 deletions

File tree

activator/steplib_ref.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66

77
"github.com/bitrise-io/go-utils/pointers"
88
"github.com/bitrise-io/stepman/activator/steplib"
9-
"github.com/bitrise-io/stepman/cli"
109
"github.com/bitrise-io/stepman/models"
1110
"github.com/bitrise-io/stepman/stepid"
1211
"github.com/bitrise-io/stepman/stepman"
@@ -64,7 +63,7 @@ func prepareStepLibForActivation(
6463
didStepLibUpdateInWorkflow bool,
6564
isOfflineMode bool,
6665
) (stepInfo models.StepInfoModel, didUpdate bool, err error) {
67-
err = cli.Setup(id.SteplibSource, "", log)
66+
err = stepman.SetupLibrary(id.SteplibSource, log)
6867
if err != nil {
6968
return models.StepInfoModel{}, false, fmt.Errorf("setup %s: %s", id.SteplibSource, err)
7069
}
@@ -87,7 +86,7 @@ func prepareStepLibForActivation(
8786
}
8887
}
8988

90-
stepInfo, err = cli.QueryStepInfoFromLibrary(id.SteplibSource, id.IDorURI, id.Version, log)
89+
stepInfo, err = stepman.QueryStepInfoFromLibrary(id.SteplibSource, id.IDorURI, id.Version, log)
9190
if err != nil {
9291
if !canUpdateStepLib(isOfflineMode, didStepLibUpdateInWorkflow) {
9392
return stepInfo, didUpdate, err
@@ -101,7 +100,7 @@ func prepareStepLibForActivation(
101100
didUpdate = true
102101
}
103102

104-
stepInfo, err = cli.QueryStepInfoFromLibrary(id.SteplibSource, id.IDorURI, id.Version, log)
103+
stepInfo, err = stepman.QueryStepInfoFromLibrary(id.SteplibSource, id.IDorURI, id.Version, log)
105104
if err != nil {
106105
return stepInfo, didUpdate, err
107106
}

cli/step_info.go

Lines changed: 3 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,8 @@ package cli
22

33
import (
44
"fmt"
5-
"path/filepath"
6-
"time"
75

8-
"github.com/bitrise-io/go-utils/command/git"
96
"github.com/bitrise-io/go-utils/log"
10-
"github.com/bitrise-io/go-utils/pathutil"
11-
"github.com/bitrise-io/go-utils/retry"
127
"github.com/bitrise-io/stepman/models"
138
"github.com/bitrise-io/stepman/stepman"
149
"github.com/urfave/cli"
@@ -112,124 +107,10 @@ func stepInfo(c *cli.Context) error {
112107
func QueryStepInfo(library, id, version string, log stepman.Logger) (models.StepInfoModel, error) {
113108
switch library {
114109
case "git":
115-
return QueryStepInfoFromGit(id, version)
110+
return stepman.QueryStepInfoFromGit(id, version)
116111
case "path":
117-
return QueryStepInfoFromPath(id)
112+
return stepman.QueryStepInfoFromPath(id)
118113
default: // library step
119-
return QueryStepInfoFromLibrary(library, id, version, log)
114+
return stepman.QueryStepInfoFromLibrary(library, id, version, log)
120115
}
121116
}
122-
123-
// QueryStepInfoFromGit returns step info from git source.
124-
func QueryStepInfoFromGit(gitURL, tagOrBranch string) (models.StepInfoModel, error) {
125-
tmpStepDir, err := pathutil.NormalizedOSTempDirPath("__step__")
126-
if err != nil {
127-
return models.StepInfoModel{}, fmt.Errorf("query git step info: create tmp dir: %s", err)
128-
}
129-
130-
if tagOrBranch == "" {
131-
tagOrBranch = "master"
132-
}
133-
134-
if err := retry.Times(2).Wait(3 * time.Second).Try(func(attempt uint) error {
135-
repo, err := git.New(tmpStepDir)
136-
if err != nil {
137-
return err
138-
}
139-
return repo.CloneTagOrBranch(gitURL, tagOrBranch).Run()
140-
}); err != nil {
141-
return models.StepInfoModel{}, fmt.Errorf("query git step info: clone %s: %s", gitURL, err)
142-
}
143-
144-
stepDefinitionPth := filepath.Join(tmpStepDir, "step.yml")
145-
if exist, err := pathutil.IsPathExists(stepDefinitionPth); err != nil {
146-
return models.StepInfoModel{}, fmt.Errorf("query git step info: check if step.yml exist: %s", err)
147-
} else if !exist {
148-
return models.StepInfoModel{}, fmt.Errorf("query git step info: step.yml does not exist at %s", stepDefinitionPth)
149-
}
150-
151-
step, err := stepman.ParseStepDefinition(stepDefinitionPth, false)
152-
if err != nil {
153-
return models.StepInfoModel{}, fmt.Errorf("query git step info: parse step.yml (%s): %s", stepDefinitionPth, err)
154-
}
155-
156-
return models.StepInfoModel{
157-
Library: "git",
158-
ID: gitURL,
159-
Version: tagOrBranch,
160-
OriginalVersion: "",
161-
LatestVersion: "",
162-
GroupInfo: models.StepGroupInfoModel{},
163-
Step: step,
164-
DefinitionPth: stepDefinitionPth,
165-
}, nil
166-
}
167-
168-
// QueryStepInfoFromPath returns step info from a local path source
169-
func QueryStepInfoFromPath(dir string) (models.StepInfoModel, error) {
170-
stepDefinitionPth := filepath.Join(dir, "step.yml")
171-
if exist, err := pathutil.IsPathExists(stepDefinitionPth); err != nil {
172-
return models.StepInfoModel{}, fmt.Errorf("query local step info: check if step.yml exist: %s", err)
173-
} else if !exist {
174-
return models.StepInfoModel{}, fmt.Errorf("query local step info: step.yml does not exist at %s", stepDefinitionPth)
175-
}
176-
177-
step, err := stepman.ParseStepDefinition(stepDefinitionPth, false)
178-
if err != nil {
179-
return models.StepInfoModel{}, fmt.Errorf("query local step info: parse step.yml (%s): %s", stepDefinitionPth, err)
180-
}
181-
182-
return models.StepInfoModel{
183-
Library: "path",
184-
ID: dir,
185-
Version: "",
186-
OriginalVersion: "",
187-
LatestVersion: "",
188-
GroupInfo: models.StepGroupInfoModel{},
189-
Step: step,
190-
DefinitionPth: stepDefinitionPth,
191-
}, nil
192-
}
193-
194-
// QueryStepInfoFromLibrary returns a step version based on the version string, which can be latest or locked to major or minor versions
195-
func QueryStepInfoFromLibrary(library, id, version string, log stepman.Logger) (models.StepInfoModel, error) {
196-
// Check if setup was done for collection
197-
if exist, err := stepman.RootExistForLibrary(library); err != nil {
198-
return models.StepInfoModel{}, fmt.Errorf("query steplib step info: check if setup was done for %s: %s", library, err)
199-
} else if !exist {
200-
if err := stepman.SetupLibrary(library, log); err != nil {
201-
return models.StepInfoModel{}, fmt.Errorf("query steplib step info: setup %s: %s", library, err)
202-
}
203-
}
204-
205-
stepVersion, err := stepman.ReadStepVersionInfo(library, id, version)
206-
if err != nil {
207-
return models.StepInfoModel{}, fmt.Errorf("query steplib step info: read step information: %s", err)
208-
}
209-
210-
route, found := stepman.ReadRoute(library)
211-
if !found {
212-
return models.StepInfoModel{}, fmt.Errorf("query steplib step info: no route found for %s", library)
213-
}
214-
215-
stepDir := stepman.GetStepCollectionDirPath(route, id, stepVersion.Version)
216-
stepDefinitionPth := filepath.Join(stepDir, "step.yml")
217-
218-
infoPath := stepman.GetStepGlobalInfoPath(route, id)
219-
220-
groupInfo, _, err := stepman.ParseStepGroupInfoModel(infoPath)
221-
if err != nil {
222-
return models.StepInfoModel{}, err
223-
}
224-
225-
return models.StepInfoModel{
226-
Library: library,
227-
ID: id,
228-
Version: stepVersion.Version,
229-
OriginalVersion: "",
230-
LatestVersion: stepVersion.LatestAvailableVersion,
231-
GroupInfo: groupInfo,
232-
Step: stepVersion.Step,
233-
DefinitionPth: stepDefinitionPth,
234-
}, nil
235-
}

stepman/library.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,18 @@ const filePathPrefix = "file://"
1717

1818
// Logger ...
1919
type Logger interface {
20-
Debugf(format string, v ...interface{})
21-
Errorf(format string, v ...interface{})
22-
Warnf(format string, v ...interface{})
23-
Infof(format string, v ...interface{})
20+
Debugf(format string, v ...any)
21+
Errorf(format string, v ...any)
22+
Warnf(format string, v ...any)
23+
Infof(format string, v ...any)
2424
}
2525

2626
// SetupLibrary ...
2727
func SetupLibrary(libraryURI string, log Logger) error {
28+
if libraryURI == "" {
29+
return fmt.Errorf("no step library specified")
30+
}
31+
2832
if exist, err := RootExistForLibrary(libraryURI); err != nil {
2933
return fmt.Errorf("failed to check if routing exist for library (%s), error: %s", libraryURI, err)
3034
} else if exist {

stepman/step_info.go

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package stepman
2+
3+
import (
4+
"fmt"
5+
"path/filepath"
6+
"time"
7+
8+
"github.com/bitrise-io/go-utils/command/git"
9+
"github.com/bitrise-io/go-utils/pathutil"
10+
"github.com/bitrise-io/go-utils/retry"
11+
"github.com/bitrise-io/stepman/models"
12+
)
13+
14+
// QueryStepInfoFromGit returns step info from git source.
15+
func QueryStepInfoFromGit(gitURL, tagOrBranch string) (models.StepInfoModel, error) {
16+
tmpStepDir, err := pathutil.NormalizedOSTempDirPath("__step__")
17+
if err != nil {
18+
return models.StepInfoModel{}, fmt.Errorf("query git step info: create tmp dir: %s", err)
19+
}
20+
21+
if tagOrBranch == "" {
22+
tagOrBranch = "master"
23+
}
24+
25+
if err := retry.Times(2).Wait(3 * time.Second).Try(func(attempt uint) error {
26+
repo, err := git.New(tmpStepDir)
27+
if err != nil {
28+
return err
29+
}
30+
return repo.CloneTagOrBranch(gitURL, tagOrBranch).Run()
31+
}); err != nil {
32+
return models.StepInfoModel{}, fmt.Errorf("query git step info: clone %s: %s", gitURL, err)
33+
}
34+
35+
stepDefinitionPth := filepath.Join(tmpStepDir, "step.yml")
36+
if exist, err := pathutil.IsPathExists(stepDefinitionPth); err != nil {
37+
return models.StepInfoModel{}, fmt.Errorf("query git step info: check if step.yml exist: %s", err)
38+
} else if !exist {
39+
return models.StepInfoModel{}, fmt.Errorf("query git step info: step.yml does not exist at %s", stepDefinitionPth)
40+
}
41+
42+
step, err := ParseStepDefinition(stepDefinitionPth, false)
43+
if err != nil {
44+
return models.StepInfoModel{}, fmt.Errorf("query git step info: parse step.yml (%s): %s", stepDefinitionPth, err)
45+
}
46+
47+
return models.StepInfoModel{
48+
Library: "git",
49+
ID: gitURL,
50+
Version: tagOrBranch,
51+
OriginalVersion: "",
52+
LatestVersion: "",
53+
GroupInfo: models.StepGroupInfoModel{},
54+
Step: step,
55+
DefinitionPth: stepDefinitionPth,
56+
}, nil
57+
}
58+
59+
// QueryStepInfoFromPath returns step info from a local path source
60+
func QueryStepInfoFromPath(dir string) (models.StepInfoModel, error) {
61+
stepDefinitionPth := filepath.Join(dir, "step.yml")
62+
if exist, err := pathutil.IsPathExists(stepDefinitionPth); err != nil {
63+
return models.StepInfoModel{}, fmt.Errorf("query local step info: check if step.yml exist: %s", err)
64+
} else if !exist {
65+
return models.StepInfoModel{}, fmt.Errorf("query local step info: step.yml does not exist at %s", stepDefinitionPth)
66+
}
67+
68+
step, err := ParseStepDefinition(stepDefinitionPth, false)
69+
if err != nil {
70+
return models.StepInfoModel{}, fmt.Errorf("query local step info: parse step.yml (%s): %s", stepDefinitionPth, err)
71+
}
72+
73+
return models.StepInfoModel{
74+
Library: "path",
75+
ID: dir,
76+
Version: "",
77+
OriginalVersion: "",
78+
LatestVersion: "",
79+
GroupInfo: models.StepGroupInfoModel{},
80+
Step: step,
81+
DefinitionPth: stepDefinitionPth,
82+
}, nil
83+
}
84+
85+
// QueryStepInfoFromLibrary returns a step version based on the version string, which can be latest or locked to major or minor versions
86+
func QueryStepInfoFromLibrary(library, id, version string, log Logger) (models.StepInfoModel, error) {
87+
// Check if setup was done for collection
88+
if exist, err := RootExistForLibrary(library); err != nil {
89+
return models.StepInfoModel{}, fmt.Errorf("query steplib step info: check if setup was done for %s: %s", library, err)
90+
} else if !exist {
91+
if err := SetupLibrary(library, log); err != nil {
92+
return models.StepInfoModel{}, fmt.Errorf("query steplib step info: setup %s: %s", library, err)
93+
}
94+
}
95+
96+
stepVersion, err := ReadStepVersionInfo(library, id, version)
97+
if err != nil {
98+
return models.StepInfoModel{}, fmt.Errorf("query steplib step info: read step information: %s", err)
99+
}
100+
101+
route, found := ReadRoute(library)
102+
if !found {
103+
return models.StepInfoModel{}, fmt.Errorf("query steplib step info: no route found for %s", library)
104+
}
105+
106+
stepDir := GetStepCollectionDirPath(route, id, stepVersion.Version)
107+
stepDefinitionPth := filepath.Join(stepDir, "step.yml")
108+
109+
infoPath := GetStepGlobalInfoPath(route, id)
110+
111+
groupInfo, _, err := ParseStepGroupInfoModel(infoPath)
112+
if err != nil {
113+
return models.StepInfoModel{}, err
114+
}
115+
116+
return models.StepInfoModel{
117+
Library: library,
118+
ID: id,
119+
Version: stepVersion.Version,
120+
OriginalVersion: "",
121+
LatestVersion: stepVersion.LatestAvailableVersion,
122+
GroupInfo: groupInfo,
123+
Step: stepVersion.Step,
124+
DefinitionPth: stepDefinitionPth,
125+
}, nil
126+
}

0 commit comments

Comments
 (0)