-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fd44423
commit a0c6401
Showing
5 changed files
with
362 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
// Copyright 2024 Interlynk.io | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package edit | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
type SearchParams struct { | ||
subject string | ||
name string | ||
version string | ||
missing bool | ||
append bool | ||
} | ||
|
||
type paramTuple struct { | ||
name string | ||
value string | ||
} | ||
|
||
type configParams struct { | ||
ctx *context.Context | ||
|
||
inputFilePath string | ||
outputFilePath string | ||
|
||
search SearchParams | ||
|
||
name string | ||
version string | ||
supplier paramTuple | ||
authors []paramTuple | ||
purl string | ||
cpe string | ||
licenses []paramTuple | ||
hashes []paramTuple | ||
tools []paramTuple | ||
copyright string | ||
lifecycles []string | ||
description string | ||
repository string | ||
typ string | ||
} | ||
|
||
var supportedSubjects map[string]bool = map[string]bool{ | ||
"document": true, | ||
"primary-component": true, | ||
"component-name-version": true, | ||
} | ||
|
||
func convertToConfigParams(eParams *EditParams) (*configParams, error) { | ||
p := &configParams{} | ||
|
||
//log := logger.FromContext(*eParams.Ctx) | ||
|
||
if err := validatePath(eParams.Input); err != nil { | ||
return nil, err | ||
} | ||
|
||
p.inputFilePath = eParams.Input | ||
|
||
if eParams.Output != "" { | ||
p.outputFilePath = eParams.Output | ||
} | ||
|
||
p.search = SearchParams{} | ||
|
||
if eParams.Subject != "" { | ||
p.search.subject = eParams.Subject | ||
} | ||
|
||
p.search = SearchParams{} | ||
|
||
if eParams.Subject != "" && supportedSubjects[strings.ToLower(eParams.Subject)] { | ||
p.search.subject = strings.ToLower(eParams.Subject) | ||
} else { | ||
return nil, fmt.Errorf("unsupported subject %s", eParams.Subject) | ||
} | ||
|
||
if p.search.subject == "component-name-version" { | ||
name, version := parseInputFormat(eParams.Search) | ||
if name == "" || version == "" { | ||
return nil, fmt.Errorf("invalid component-name-version format both name and version must be provided") | ||
} | ||
p.search.name = name | ||
p.search.version = version | ||
} | ||
|
||
p.search.missing = eParams.Missing | ||
p.search.append = eParams.Append | ||
|
||
p.name = eParams.Name | ||
p.version = eParams.Version | ||
|
||
if eParams.Supplier != "" { | ||
name, email := parseInputFormat(eParams.Supplier) | ||
|
||
p.supplier = paramTuple{ | ||
name: name, | ||
value: email, | ||
} | ||
} | ||
|
||
for _, author := range eParams.Authors { | ||
name, email := parseInputFormat(author) | ||
p.authors = append(p.authors, paramTuple{ | ||
name: name, | ||
value: email, | ||
}) | ||
} | ||
|
||
p.purl = eParams.Purl | ||
p.cpe = eParams.Cpe | ||
|
||
for _, license := range eParams.Licenses { | ||
name, url := parseInputFormat(license) | ||
p.licenses = append(p.licenses, paramTuple{ | ||
name: name, | ||
value: url, | ||
}) | ||
} | ||
|
||
for _, hash := range eParams.Hashes { | ||
algorithm, value := parseInputFormat(hash) | ||
p.hashes = append(p.hashes, paramTuple{ | ||
name: algorithm, | ||
value: value, | ||
}) | ||
} | ||
|
||
for _, tool := range eParams.Tools { | ||
name, version := parseInputFormat(tool) | ||
p.tools = append(p.tools, paramTuple{ | ||
name: name, | ||
value: version, | ||
}) | ||
} | ||
|
||
p.copyright = eParams.CopyRight | ||
p.lifecycles = eParams.Lifecycles | ||
p.description = eParams.Description | ||
p.repository = eParams.Repository | ||
p.typ = eParams.Type | ||
|
||
return p, nil | ||
} | ||
func parseInputFormat(s string) (name string, version string) { | ||
// Trim any leading/trailing whitespace | ||
s = strings.TrimSpace(s) | ||
|
||
// Regular expression to match the pattern | ||
re := regexp.MustCompile(`^(.+?)\s*(?:\(([^)]+)\))?$`) | ||
|
||
matches := re.FindStringSubmatch(s) | ||
if len(matches) > 1 { | ||
name = strings.TrimSpace(matches[1]) | ||
if len(matches) > 2 { | ||
version = strings.TrimSpace(matches[2]) | ||
} | ||
} else { | ||
name = s | ||
} | ||
|
||
return name, version | ||
} | ||
func validatePath(path string) error { | ||
stat, err := os.Stat(path) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
if stat.IsDir() { | ||
return fmt.Errorf("path %s is a directory include only files", path) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.