Skip to content

Commit

Permalink
feat: Add more code
Browse files Browse the repository at this point in the history
  • Loading branch information
dadav committed Feb 25, 2024
1 parent eca4ffc commit 121d81b
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 59 deletions.
1 change: 1 addition & 0 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,6 @@ func init() {
serveCmd.Flags().StringVar(&config.ApiVersion, "api-version", "v3", "the forge api version to use")
serveCmd.Flags().IntVar(&config.Port, "port", 8080, "the port to listen to")
serveCmd.Flags().StringVar(&config.Bind, "bind", "", "host to listen to")
serveCmd.Flags().StringVar(&config.ModulesDir, "modulesdir", "/opt/gorge/modules", "directory containing all the modules")
serveCmd.Flags().BoolVar(&config.Dev, "dev", false, "enables dev mode")
}
30 changes: 13 additions & 17 deletions internal/api/v3/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,34 +64,30 @@ func (s *ModuleOperationsApi) DeprecateModule(ctx context.Context, moduleSlug st
return gen.Response(http.StatusNotImplemented, nil), errors.New("DeprecateModule method not implemented")
}

type GetModule500Response struct {
Message string `json:"message,omitempty"`
Errors []string `json:"errors,omitempty"`
}

// GetModule - Fetch module
func (s *ModuleOperationsApi) GetModule(ctx context.Context, moduleSlug string, withHtml bool, includeFields []string, excludeFields []string, ifModifiedSince string) (gen.ImplResponse, error) {
// TODO - update GetModule with the required logic for this service method.
// Add api_module_operations_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation.

metadata, err := backend.ReadMetadata("/tmp/modules/puppetlabs-haproxy-8.0.0.tar.gz")
metadata, err := backend.ReadReleaseMetadata("/tmp/modules/puppetlabs-haproxy-8.0.0.tar.gz")

Check failure on line 74 in internal/api/v3/module.go

View workflow job for this annotation

GitHub Actions / goreleaser

assignment mismatch: 2 variables but backend.ReadReleaseMetadata returns 3 values
if err != nil {
log.Log.Fatal(err)
log.Log.Error(err)
return gen.Response(
http.StatusInternalServerError,
GetModule500Response{
Message: http.StatusText(http.StatusInternalServerError),
Errors: []string{"There was some error while reading the metadata"},
}), err
}

// TODO: Uncomment the next line to return response Response(200, Module{}) or use other options such as http.Ok ...
return gen.Response(http.StatusOK, gen.Module{
Uri: "/foo",
Slug: moduleSlug,
Name: metadata.Name,
HomepageUrl: metadata.ProjectUrl,
}), nil

// TODO: Uncomment the next line to return response Response(304, {}) or use other options such as http.Ok ...
// return Response(304, nil),nil

// TODO: Uncomment the next line to return response Response(400, GetFile400Response{}) or use other options such as http.Ok ...
// return Response(400, GetFile400Response{}), nil

// TODO: Uncomment the next line to return response Response(404, GetFile404Response{}) or use other options such as http.Ok ...
// return Response(404, GetFile404Response{}), nil

// return gen.Response(http.StatusNotImplemented, nil), errors.New("GetModule method not implemented")
}

// GetModules - List modules
Expand Down
52 changes: 31 additions & 21 deletions internal/api/v3/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import (
"errors"
"net/http"
"os"
"path/filepath"
"strings"

"github.com/dadav/gorge/internal/backend"
"github.com/dadav/gorge/internal/config"
gen "github.com/dadav/gorge/pkg/gen/v3/openapi"
)

Expand Down Expand Up @@ -63,12 +67,13 @@ func (s *ReleaseOperationsApi) DeleteRelease(ctx context.Context, releaseSlug st
return gen.Response(http.StatusNotImplemented, nil), errors.New("DeleteRelease method not implemented")
}

func releaseToModule(releaseSlug string) string {
return releaseSlug[:strings.LastIndex(releaseSlug, "-")]
}

// GetFile - Download module release
func (s *ReleaseOperationsApi) GetFile(ctx context.Context, filename string) (gen.ImplResponse, error) {
// TODO - update GetFile with the required logic for this service method.
// Add api_release_operations_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation.

f, err := os.Open("/tmp/modules/" + filename)
f, err := os.Open(filepath.Join(config.ModulesDir, releaseToModule(filename), filename))
if err != nil {
if os.IsNotExist(err) {
return gen.Response(http.StatusNotFound, gen.GetFile404Response{
Expand All @@ -79,29 +84,34 @@ func (s *ReleaseOperationsApi) GetFile(ctx context.Context, filename string) (ge
}

return gen.Response(http.StatusOK, f), nil
}

// TODO: Uncomment the next line to return response Response(400, GetFile400Response{}) or use other options such as http.Ok ...
// return gen.Response(400, gen.GetFile400Response{}), nil
type GetRelease500Response struct {
Message string `json:"message,omitempty"`
Errors []string `json:"errors,omitempty"`
}

// GetRelease - Fetch module release
func (s *ReleaseOperationsApi) GetRelease(ctx context.Context, releaseSlug string, withHtml bool, includeFields []string, excludeFields []string, ifModifiedSince string) (gen.ImplResponse, error) {
// TODO - update GetRelease with the required logic for this service method.
// Add api_release_operations_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation.

// TODO: Uncomment the next line to return response Response(200, Release{}) or use other options such as http.Ok ...
// return Response(200, Release{}), nil

// TODO: Uncomment the next line to return response Response(304, {}) or use other options such as http.Ok ...
// return Response(304, nil),nil

// TODO: Uncomment the next line to return response Response(400, GetFile400Response{}) or use other options such as http.Ok ...
// return Response(400, GetFile400Response{}), nil

// TODO: Uncomment the next line to return response Response(404, GetFile404Response{}) or use other options such as http.Ok ...
// return Response(404, GetFile404Response{}), nil
metadata, readme, err := backend.ReadReleaseMetadata(releaseSlug)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return gen.Response(http.StatusNotFound, gen.GetFile404Response{
Message: http.StatusText(http.StatusNotFound),
Errors: []string{"release not found"},
}), err
}
return gen.Response(http.StatusInternalServerError, GetRelease500Response{
Message: http.StatusText(http.StatusInternalServerError),
Errors: []string{"error while reading release metadata"},
}), err
}

return gen.Response(http.StatusNotImplemented, nil), errors.New("GetRelease method not implemented")
return gen.Response(http.StatusOK, gen.Release{
Slug: releaseSlug,
Module: gen.ReleaseModule{Name: metadata.Name},
Readme: readme,
}), nil
}

// GetReleasePlan - Fetch module release plan
Expand Down
50 changes: 30 additions & 20 deletions internal/backend/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,30 @@ import (
"bytes"
"compress/gzip"
"encoding/json"
"errors"
"io"
"os"
"path/filepath"
"strings"

"github.com/dadav/gorge/internal/log"
"github.com/dadav/gorge/internal/config"
"github.com/dadav/gorge/internal/model"
)

func ReadMetadata(path string) (*model.ModuleMetadata, error) {
func ReadReleaseMetadata(releaseSlug string) (*model.ReleaseMetadata, string, error) {
var jsonData bytes.Buffer
var releaseMetadata model.ReleaseMetadata
readme := new(strings.Builder)
path := filepath.Join(config.ModulesDir, releaseSlug, ".tar.gz")

f, err := os.Open(path)
if err != nil {
log.Log.Panic(err)
return nil, readme.String(), err
}
defer f.Close()

g, err := gzip.NewReader(f)
if err != nil {
log.Log.Panic(err)
return nil, readme.String(), err
}

tarReader := tar.NewReader(g)
Expand All @@ -35,27 +40,32 @@ func ReadMetadata(path string) (*model.ModuleMetadata, error) {
}

if err != nil {
log.Log.Fatal(err)
return nil, readme.String(), err
}

if header.Typeflag != tar.TypeReg || filepath.Base(header.Name) != "metadata.json" {
if header.Typeflag != tar.TypeReg {
continue
}

var jsonData bytes.Buffer
_, err = io.Copy(&jsonData, tarReader)
if err != nil {
log.Log.Fatal(err)
}

var data model.ModuleMetadata
if err := json.Unmarshal(jsonData.Bytes(), &data); err != nil {
log.Log.Fatal(err)
}
switch filepath.Base(header.Name) {
case "metadata.json":
_, err = io.Copy(&jsonData, tarReader)
if err != nil {
return nil, readme.String(), err
}

return &data, nil
if err := json.Unmarshal(jsonData.Bytes(), &releaseMetadata); err != nil {
return nil, readme.String(), err
}

case "README.md":
_, err = io.Copy(readme, tarReader)
if err != nil {
return nil, readme.String(), err
}
default:
continue
}
}

return nil, errors.New("metadata.json is missing")
return &releaseMetadata, readme.String(), nil
}
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ var (
Port int
Bind string
Dev bool
ModulesDir string
)
2 changes: 1 addition & 1 deletion internal/model/module_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type ModuleDependency struct {

type ModuleRequirement ModuleDependency

type ModuleMetadata struct {
type ReleaseMetadata struct {
Name string `json:"name"`
Version string `json:"version"`
Author string `json:"author"`
Expand Down

0 comments on commit 121d81b

Please sign in to comment.