-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Auto-generated Pull Request for feat/content-loader #352
Merged
Merged
Changes from 14 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
74f6109
refactor(logger.go): replace ioutil.TempFile with os.CreateTemp for c…
cybersiddhu f2ee56a
feat(keys.go): add ContentClientKey and ContentClient constants
cybersiddhu 6fc5bb2
feat(stockcenter.go): add content API client to stockcenter registry
cybersiddhu 12f0cb7
feat(client.go): add SetContentAPIClient function to establish grpc c…
cybersiddhu 53fe3a7
feat(client.go): add NewCliS3Client function to support urfave/cli co…
cybersiddhu e88045e
feat(client.go): add SetS3Client function to initialize S3 client
cybersiddhu 8a128a0
refactor(main.go, logger.go): move setupCliLogger function from main.…
cybersiddhu 8779261
feat(client.go): add new client.go file with SetS3Client and SetConte…
cybersiddhu 2367e79
feat(cli/flag.go): add ContentLoaderFlags function to handle CLI flags
cybersiddhu 878c0c8
feat(action.go): add LoadContent function to handle S3 objects
cybersiddhu b2b740f
feat(client.go): add CliSetup function to initialize S3 and ContentAP…
cybersiddhu cf9a445
feat(main.go): add new loader command line application
cybersiddhu 8976a48
chore(go.mod): update dependencies for better functionality and security
cybersiddhu 2c23c16
feat(action.go): add comprehensive content processing from S3 bucket
cybersiddhu 010e4f2
refactor(plasmid.go): replace ioutil with os and fs for directory rea…
cybersiddhu 58bc5e2
chore(lint.yml): update Go version from 1.20 to 1.21 and linter versi…
cybersiddhu fdf63d0
refactor(action.go): remove unused variable sct in storeOrUpdateConte…
cybersiddhu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
contentcli "github.com/dictyBase/modware-import/internal/content/cli" | ||
"github.com/dictyBase/modware-import/internal/content/client" | ||
"github.com/dictyBase/modware-import/internal/logger" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
func main() { | ||
app := &cli.App{ | ||
Name: "loader", | ||
Usage: "A command line application for loading data", | ||
Before: logger.SetupCliLogger, | ||
Flags: []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: "log-level", | ||
Usage: "Logging level, should be one of debug,warn,info or error", | ||
Value: "error", | ||
}, | ||
&cli.StringFlag{ | ||
Name: "log-format", | ||
Usage: "Format of log, either of json or text", | ||
Value: "json", | ||
}, | ||
&cli.StringFlag{ | ||
Name: "log-file", | ||
Usage: "log file for output in addition to stderr", | ||
}, | ||
}, | ||
Commands: []*cli.Command{ | ||
{ | ||
Name: "content-data", | ||
Usage: "load content data from s3 storage", | ||
Action: contentcli.LoadContent, | ||
Flags: contentcli.ContentLoaderFlags(), | ||
Before: client.CliSetup, | ||
}, | ||
}, | ||
} | ||
|
||
err := app.Run(os.Args) | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
os.Exit(1) | ||
} | ||
} |
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,216 @@ | ||
package cli | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"regexp" | ||
"strings" | ||
|
||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
|
||
A "github.com/IBM/fp-go/array" | ||
Fn "github.com/IBM/fp-go/function" | ||
O "github.com/IBM/fp-go/option" | ||
"github.com/dictyBase/go-genproto/dictybaseapis/content" | ||
"github.com/dictyBase/modware-import/internal/registry" | ||
regsc "github.com/dictyBase/modware-import/internal/registry/stockcenter" | ||
"github.com/minio/minio-go/v6" | ||
"github.com/sirupsen/logrus" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
var noncharReg = regexp.MustCompile("[^a-z0-9]+") | ||
|
||
func Slugify(name string) string { | ||
return strings.Trim( | ||
noncharReg.ReplaceAllString(strings.ToLower(name), "-"), | ||
"-", | ||
) | ||
} | ||
|
||
func LoadContent(cltx *cli.Context) error { | ||
logger := registry.GetLogger() | ||
s3Client := registry.GetS3Client() | ||
client := regsc.GetContentAPIClient() | ||
|
||
doneCh := make(chan struct{}) | ||
defer close(doneCh) | ||
s3Objects := listS3Objects(cltx, s3Client, doneCh) | ||
|
||
for cinfo := range s3Objects { | ||
err := processS3Object(cltx, logger, s3Client, client, cinfo) | ||
if err != nil { | ||
return cli.Exit(err.Error(), 2) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func listS3Objects( | ||
cltx *cli.Context, | ||
s3Client *minio.Client, | ||
doneCh chan struct{}, | ||
) <-chan minio.ObjectInfo { | ||
return s3Client.ListObjects( | ||
cltx.String("s3-bucket"), | ||
cltx.String("s3-bucket-path"), | ||
true, | ||
doneCh, | ||
) | ||
} | ||
|
||
func processS3Object( | ||
cltx *cli.Context, | ||
logger *logrus.Entry, | ||
s3Client *minio.Client, | ||
client content.ContentServiceClient, | ||
cinfo minio.ObjectInfo, | ||
) error { | ||
sinfo, err := s3Client.StatObject( | ||
cltx.String("s3-bucket"), cinfo.Key, minio.StatObjectOptions{}, | ||
) | ||
if err != nil { | ||
return fmt.Errorf( | ||
"error in getting information for object %s %s", | ||
sinfo.Key, | ||
err, | ||
) | ||
} | ||
logger.Infof("read content file %s", sinfo.Key) | ||
|
||
jsonContent, err := getContent(cltx, s3Client, sinfo) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
name, namespace := nameAndNamespace(sinfo.Key) | ||
slug := Slugify(fmt.Sprintf("%s %s", name, namespace)) | ||
|
||
err = storeOrUpdateContent( | ||
client, | ||
slug, | ||
name, | ||
namespace, | ||
jsonContent, | ||
logger, | ||
sinfo, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func getContent( | ||
cltx *cli.Context, | ||
s3Client *minio.Client, | ||
sinfo minio.ObjectInfo, | ||
) ([]byte, error) { | ||
obj, err := s3Client.GetObject( | ||
cltx.String("s3-bucket"), sinfo.Key, minio.GetObjectOptions{}, | ||
) | ||
if err != nil { | ||
return nil, fmt.Errorf("error in getting object %s", err) | ||
} | ||
jsonContent, err := io.ReadAll(obj) | ||
if err != nil { | ||
return nil, fmt.Errorf( | ||
"error in reading content for file %s %s", | ||
sinfo.Key, | ||
err, | ||
) | ||
} | ||
|
||
return jsonContent, nil | ||
} | ||
|
||
func storeOrUpdateContent( | ||
client content.ContentServiceClient, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function |
||
slug, name, namespace string, | ||
jsonContent []byte, | ||
logger *logrus.Entry, | ||
sinfo minio.ObjectInfo, | ||
) error { | ||
sct, err := client.GetContentBySlug( | ||
context.Background(), | ||
&content.ContentRequest{ | ||
Slug: slug, | ||
}, | ||
) | ||
if err != nil { | ||
if status.Code(err) == codes.NotFound { | ||
return createStoreContent( | ||
client, | ||
name, | ||
namespace, | ||
string(jsonContent), | ||
slug, | ||
logger, | ||
sinfo, | ||
) | ||
} | ||
return fmt.Errorf( | ||
"error in fetching content %s %s %s", | ||
sinfo.Key, | ||
sct.Data.Attributes.Name, | ||
sct.Data.Attributes.Namespace, | ||
) | ||
} | ||
logger.Infof("found existing content %s %s %s", sinfo.Key, name, namespace) | ||
|
||
return nil | ||
} | ||
|
||
func createStoreContent( | ||
client content.ContentServiceClient, | ||
name, namespace, jsonContent, slug string, | ||
logger *logrus.Entry, | ||
sinfo minio.ObjectInfo, | ||
) error { | ||
nct, err := client.StoreContent( | ||
context.Background(), | ||
&content.StoreContentRequest{ | ||
Data: &content.StoreContentRequest_Data{ | ||
Attributes: &content.NewContentAttributes{ | ||
Name: name, | ||
Namespace: namespace, | ||
CreatedBy: "[email protected]", | ||
Content: jsonContent, | ||
Slug: slug, | ||
}, | ||
}, | ||
}, | ||
) | ||
if err != nil { | ||
return fmt.Errorf( | ||
"error in creating content %s %s %s", | ||
sinfo.Key, | ||
name, | ||
namespace, | ||
) | ||
} | ||
logger.Infof( | ||
"created content %s %s %s", | ||
sinfo.Key, | ||
nct.Data.Attributes.Name, | ||
nct.Data.Attributes.Namespace, | ||
) | ||
|
||
return nil | ||
} | ||
|
||
func nameAndNamespace(input string) (string, string) { | ||
output := Fn.Pipe4( | ||
strings.Split(input, "/"), | ||
A.Last, | ||
O.Map(func(val string) []string { return strings.Split(val, ".") }), | ||
O.Map(func(val []string) string { return val[0] }), | ||
O.Map(func(val string) []string { return strings.Split(val, "-") }), | ||
) | ||
data, _ := O.Unwrap(output) | ||
return data[1], data[0] | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function
processS3Object
has 5 arguments (exceeds 4 allowed). Consider refactoring.