Skip to content

Commit

Permalink
Change flag to boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
filip-debricked committed Mar 11, 2024
1 parent b50bf80 commit 11fc5d0
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 34 deletions.
19 changes: 7 additions & 12 deletions internal/cmd/scan/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var jsonFilePath string
var exclusions = file.Exclusions()
var verbose bool
var regenerate int
var versionConsolidation int
var versionHint bool
var noResolve bool
var noFingerprint bool
var passOnDowntime bool
Expand All @@ -41,7 +41,7 @@ const (
IntegrationFlag = "integration"
ExclusionFlag = "exclusion"
VerboseFlag = "verbose"
VersionConsolidationFlag = "version-consolidation"
VersionHintFlag = "version-hint"
RegenerateFlag = "regenerate"
NoResolveFlag = "no-resolve"
FingerprintFlag = "fingerprint"
Expand Down Expand Up @@ -112,17 +112,12 @@ $ debricked scan . `+exampleFlags)
"\nExample:\n$ debricked resolve . --regenerate=1",
}, "\n")
cmd.Flags().IntVar(&regenerate, RegenerateFlag, 0, regenerateDoc)
versionConsolidationDoc := strings.Join(
versionHintDoc := strings.Join(
[]string{
"Toggles version consolidation of manifest and manifestless resolition with 3 modes:\n",
"Consolidation Level | Meaning",
"------------------- | -------",
"0 | No consolidation",
"1 (default) | Use manifest version hints",
"2 (not implemented) | Combine version and package hints",
"\nExample:\n$ debricked scan . --version-consolidation=1",
"Toggles version hinting, i.e using manifest versions to help manifestless resolution.\n",
"\nExample:\n$ debricked scan . --version-hint=False",
}, "\n")
cmd.Flags().IntVar(&versionConsolidation, VersionConsolidationFlag, 1, versionConsolidationDoc)
cmd.Flags().BoolVar(&versionHint, VersionHintFlag, true, versionHintDoc)
verboseDoc := strings.Join(
[]string{
"This flag allows you to reduce error output for resolution.",
Expand Down Expand Up @@ -169,7 +164,7 @@ func RunE(s *scan.IScanner) func(_ *cobra.Command, args []string) error {
Exclusions: viper.GetStringSlice(ExclusionFlag),
Verbose: viper.GetBool(VerboseFlag),
Regenerate: viper.GetInt(RegenerateFlag),
VersionConsolidation: viper.GetInt(VersionConsolidationFlag),
VersionHint: viper.GetBool(VersionHintFlag),
RepositoryName: viper.GetString(RepositoryFlag),
CommitName: viper.GetString(CommitFlag),
BranchName: viper.GetString(BranchFlag),
Expand Down
4 changes: 2 additions & 2 deletions internal/scan/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type DebrickedOptions struct {
Exclusions []string
Verbose bool
Regenerate int
VersionConsolidation int
VersionHint bool
RepositoryName string
CommitName string
BranchName string
Expand Down Expand Up @@ -207,7 +207,7 @@ func (dScanner *DebrickedScanner) scan(options DebrickedOptions, gitMetaObject g
GitMetaObject: gitMetaObject,
IntegrationsName: options.IntegrationName,
CallGraphUploadTimeout: options.CallGraphUploadTimeout,
VersionConsolidation: options.VersionConsolidation,
VersionHint: options.VersionHint,
}
result, err := (*dScanner.uploader).Upload(uploaderOptions)
if err != nil {
Expand Down
24 changes: 11 additions & 13 deletions internal/upload/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ var (
const callgraphName = "debricked-call-graph"

type uploadBatch struct {
client *client.IDebClient
fileGroups file.Groups
gitMetaObject *git.MetaObject
integrationName string
ciUploadId int
callGraphTimeout int
versionConsolidation int
client *client.IDebClient
fileGroups file.Groups
gitMetaObject *git.MetaObject
integrationName string
ciUploadId int
callGraphTimeout int
versionHint bool
}

func newUploadBatch(client *client.IDebClient, fileGroups file.Groups, gitMetaObject *git.MetaObject, integrationName string, callGraphTimeout int, versionConsolidation int) *uploadBatch {
return &uploadBatch{client: client, fileGroups: fileGroups, gitMetaObject: gitMetaObject, integrationName: integrationName, ciUploadId: 0, callGraphTimeout: callGraphTimeout, versionConsolidation: versionConsolidation}
func newUploadBatch(client *client.IDebClient, fileGroups file.Groups, gitMetaObject *git.MetaObject, integrationName string, callGraphTimeout int, versionHint bool) *uploadBatch {
return &uploadBatch{client: client, fileGroups: fileGroups, gitMetaObject: gitMetaObject, integrationName: integrationName, ciUploadId: 0, callGraphTimeout: callGraphTimeout, versionHint: versionHint}
}

// upload concurrently posts all file groups to Debricked
Expand Down Expand Up @@ -124,11 +124,10 @@ func (uploadBatch *uploadBatch) uploadFile(filePath string, timeout int) error {
_ = writer.WriteField("commitName", uploadBatch.gitMetaObject.CommitName)
_ = writer.WriteField("repositoryUrl", uploadBatch.gitMetaObject.RepositoryUrl)
_ = writer.WriteField("branchName", uploadBatch.gitMetaObject.BranchName)
_ = writer.WriteField("versionConsolidation", strconv.Itoa(uploadBatch.versionConsolidation))
_ = writer.WriteField("versionHint", strconv.FormatBool(uploadBatch.versionHint))
if uploadBatch.initialized() {
_ = writer.WriteField("ciUploadId", strconv.Itoa(uploadBatch.ciUploadId))
}

response, err := (*uploadBatch.client).Post(
"/api/1.0/open/uploads/dependencies/files",
writer.FormDataContentType(),
Expand All @@ -138,11 +137,9 @@ func (uploadBatch *uploadBatch) uploadFile(filePath string, timeout int) error {
if err != nil {
return err
}

if !uploadBatch.initialized() {
data, _ := io.ReadAll(response.Body)
defer response.Body.Close()

uFile := uploadedFile{}
_ = json.Unmarshal(data, &uFile)
if uFile.CiUploadId == 0 {
Expand Down Expand Up @@ -171,6 +168,7 @@ func (uploadBatch *uploadBatch) initAnalysis() error {
if err != nil {
return err
}
print(body) //TODO: REMOVE!
response, err := (*uploadBatch.client).Post(
"/api/1.0/open/finishes/dependencies/files/uploads",
"application/json",
Expand Down
10 changes: 5 additions & 5 deletions internal/upload/batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestUploadWithBadFiles(t *testing.T) {
clientMock.AddMockResponse(mockRes)
clientMock.AddMockResponse(mockRes)
c = clientMock
batch := newUploadBatch(&c, groups, metaObj, "CLI", 10*60, 1)
batch := newUploadBatch(&c, groups, metaObj, "CLI", 10*60, true)
var buf bytes.Buffer
log.SetOutput(&buf)
err = batch.upload()
Expand All @@ -48,7 +48,7 @@ func TestUploadWithBadFiles(t *testing.T) {
}

func TestInitAnalysisWithoutAnyFiles(t *testing.T) {
batch := newUploadBatch(nil, file.Groups{}, nil, "CLI", 10*60, 1)
batch := newUploadBatch(nil, file.Groups{}, nil, "CLI", 10*60, true)
err := batch.initAnalysis()

assert.ErrorContains(t, err, "failed to find dependency files")
Expand All @@ -71,7 +71,7 @@ func TestWaitWithPollingTerminatedError(t *testing.T) {
}
clientMock.AddMockResponse(mockRes)
c = clientMock
batch := newUploadBatch(&c, groups, metaObj, "CLI", 10*60, 1)
batch := newUploadBatch(&c, groups, metaObj, "CLI", 10*60, true)

uploadResult, err := batch.wait()

Expand All @@ -96,7 +96,7 @@ func TestInitUploadBadFile(t *testing.T) {
clientMock.AddMockResponse(mockRes)

var c client.IDebClient = clientMock
batch := newUploadBatch(&c, groups, metaObj, "CLI", 10*60, 1)
batch := newUploadBatch(&c, groups, metaObj, "CLI", 10*60, true)

files, err := batch.initUpload()

Expand All @@ -123,7 +123,7 @@ func TestInitUpload(t *testing.T) {
clientMock.AddMockResponse(mockRes)

var c client.IDebClient = clientMock
batch := newUploadBatch(&c, groups, metaObj, "CLI", 10*60, 1)
batch := newUploadBatch(&c, groups, metaObj, "CLI", 10*60, true)

files, err := batch.initUpload()

Expand Down
4 changes: 2 additions & 2 deletions internal/upload/uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type DebrickedOptions struct {
GitMetaObject git.MetaObject
IntegrationsName string
CallGraphUploadTimeout int
VersionConsolidation int
VersionHint bool
}

type IUploader interface {
Expand All @@ -36,7 +36,7 @@ func NewUploader(c client.IDebClient) (*Uploader, error) {

func (uploader *Uploader) Upload(o IOptions) (*UploadResult, error) {
dOptions := o.(DebrickedOptions)
batch := newUploadBatch(uploader.client, dOptions.FileGroups, &dOptions.GitMetaObject, dOptions.IntegrationsName, dOptions.CallGraphUploadTimeout, dOptions.VersionConsolidation)
batch := newUploadBatch(uploader.client, dOptions.FileGroups, &dOptions.GitMetaObject, dOptions.IntegrationsName, dOptions.CallGraphUploadTimeout, dOptions.VersionHint)

err := batch.upload()
if err != nil {
Expand Down

0 comments on commit 11fc5d0

Please sign in to comment.