fix package name parsing and add sourceref to http resource
Some checks are pending
Lint / golangci-lint (push) Waiting to run
Declarative Tests / test (push) Waiting to run
Declarative Tests / build-fedora (push) Waiting to run
Declarative Tests / build-ubuntu-focal (push) Waiting to run

This commit is contained in:
Matthew Rich 2024-09-24 19:25:53 +00:00
parent 55fd39f09d
commit 4caed45f96
2 changed files with 22 additions and 8 deletions

View File

@ -4,7 +4,7 @@ package resource
import ( import (
"context" "context"
_ "errors" "errors"
"fmt" "fmt"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"io" "io"
@ -28,6 +28,10 @@ const (
HTTPTypeName TypeName = "http" HTTPTypeName TypeName = "http"
) )
var (
ErrUndefinedContentSource error = errors.New("Cannot create reader: no content defined")
)
func init() { func init() {
ResourceTypes.Register([]string{"http", "https"}, HTTPFactory) ResourceTypes.Register([]string{"http", "https"}, HTTPFactory)
} }
@ -256,6 +260,7 @@ func (h *HTTP) Create(ctx context.Context) (err error) {
slog.Error("HTTP.Create() content", "http", h) slog.Error("HTTP.Create() content", "http", h)
// create should post to the named resource using the resource content or sourceref.
contentReader, err = h.contentSourceReader() contentReader, err = h.contentSourceReader()
if err != nil { if err != nil {
return return
@ -418,6 +423,10 @@ func (h *HTTP) writeThru(ctx context.Context) (contentWriter io.WriteCloser, err
} }
*/ */
func (h *HTTP) hasContentSource() bool {
return len(h.ContentSourceRef) > 0 || len(h.Content) > 0
}
func (h *HTTP) contentSourceReader() (contentReader io.ReadCloser, err error) { func (h *HTTP) contentSourceReader() (contentReader io.ReadCloser, err error) {
if len(h.ContentSourceRef) != 0 { if len(h.ContentSourceRef) != 0 {
contentReader, err = h.ContentSourceRef.Lookup(nil).ContentReaderStream() contentReader, err = h.ContentSourceRef.Lookup(nil).ContentReaderStream()
@ -426,7 +435,7 @@ func (h *HTTP) contentSourceReader() (contentReader io.ReadCloser, err error) {
if len(h.Content) != 0 { if len(h.Content) != 0 {
contentReader = io.NopCloser(strings.NewReader(h.Content)) contentReader = io.NopCloser(strings.NewReader(h.Content))
} else { } else {
err = fmt.Errorf("Cannot create reader: no content defined") err = ErrUndefinedContentSource
} }
} }
return return
@ -434,7 +443,9 @@ func (h *HTTP) contentSourceReader() (contentReader io.ReadCloser, err error) {
// set up reader for source content // set up reader for source content
func (h *HTTP) readThru(ctx context.Context) (contentReader io.ReadCloser, err error) { func (h *HTTP) readThru(ctx context.Context) (contentReader io.ReadCloser, err error) {
if contentReader, err = h.contentSourceReader(); err != nil { if h.hasContentSource() {
contentReader, err = h.contentSourceReader()
} else {
if h.reader == nil { if h.reader == nil {
h.reader, err = transport.NewReaderWithContext(h.parsedURI, ctx) h.reader, err = transport.NewReaderWithContext(h.parsedURI, ctx)
h.reader.SetGzip(false) h.reader.SetGzip(false)

View File

@ -498,9 +498,12 @@ func NewApkReadCommand() *command.Command {
c.Extractor = func(out []byte, target any) error { c.Extractor = func(out []byte, target any) error {
p := target.(*Package) p := target.(*Package)
pkg := strings.Split(string(out), "-") pkg := strings.Split(string(out), "-")
if pkg[0] == p.Name { numberOfFields := len(pkg)
p.Name = pkg[0] packageName := strings.Join(pkg[0:numberOfFields - 3], "-")
p.Version = pkg[1] packageVersion := strings.Join(pkg[numberOfFields - 2:numberOfFields - 1], "-")
if packageName == p.Name {
p.Name = packageName
p.Version = packageVersion
p.State = "present" p.State = "present"
} else { } else {
p.State = "absent" p.State = "absent"
@ -543,7 +546,7 @@ func NewApkReadPackagesCommand() *command.Command {
lines := strings.Split(strings.TrimSpace(string(out)), "\n") lines := strings.Split(strings.TrimSpace(string(out)), "\n")
numberOfLines := len(lines) numberOfLines := len(lines)
diff := (numberOfLines - 1) - numberOfPackages diff := (numberOfLines) - numberOfPackages
if diff > 0 { if diff > 0 {
for i := 0; i < diff; i++ { for i := 0; i < diff; i++ {
*Packages = append(*Packages, NewPackage()) *Packages = append(*Packages, NewPackage())
@ -556,7 +559,7 @@ func NewApkReadPackagesCommand() *command.Command {
packageFields := strings.Split(installedPackage[0], "-") packageFields := strings.Split(installedPackage[0], "-")
numberOfFields := len(packageFields) numberOfFields := len(packageFields)
if numberOfFields > 2 { if numberOfFields > 2 {
packageName := strings.Join(packageFields[:numberOfFields - 3], "-") packageName := strings.Join(packageFields[0:numberOfFields - 2], "-")
packageVersion := strings.Join(packageFields[numberOfFields - 2:numberOfFields - 1], "-") packageVersion := strings.Join(packageFields[numberOfFields - 2:numberOfFields - 1], "-")
p.Name = packageName p.Name = packageName
p.State = "present" p.State = "present"