Skip to content

Commit

Permalink
feat: adds get module cli command
Browse files Browse the repository at this point in the history
chore: clean up s3 initialization for env var compatibility
  • Loading branch information
nadilas committed Nov 17, 2021
1 parent c522cbe commit 67f3ce5
Show file tree
Hide file tree
Showing 15 changed files with 251 additions and 188 deletions.
81 changes: 0 additions & 81 deletions .github/workflows/codesee-arch-diagram.yml

This file was deleted.

40 changes: 40 additions & 0 deletions cli/cmd/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"

"github.com/dotindustries/moar/moarpb"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/spf13/cobra"
)

Expand All @@ -15,6 +16,42 @@ var moduleCmd = &cobra.Command{
Aliases: []string{"m"},
}

var getAll bool
var getModuleCmd = &cobra.Command{
Use: "get",
Short: "Get module details",
Aliases: []string{"g", "read", "r"},
Run: func(cmd *cobra.Command, args []string) {
client := protobufClient()

request := &moarpb.GetModuleRequest{}

if !getAll {
if len(args) < 1 {
fmt.Println("ERROR: module name not provided")
os.Exit(1)
}
request.ModuleName = args[0]
}
response, err := client.GetModule(context.Background(), request)
if err != nil {
fmt.Println("ERROR: ", err)
os.Exit(1)
}
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"Module", "Author", "Language", "Version"})
rowConfigAutoMerge := table.RowConfig{AutoMerge: true}
for _, module := range response.Module {
for _, v := range module.Versions {
t.AppendRow(table.Row{module.Name, module.Author, module.Language, v.Name}, rowConfigAutoMerge)
// t.AppendSeparator()
}
}
t.Render()
},
}

var author, language string

var newModuleCmd = &cobra.Command{
Expand Down Expand Up @@ -59,5 +96,8 @@ func init() {
panic(err)
}
moduleCmd.AddCommand(newModuleCmd)

getModuleCmd.Flags().BoolVarP(&getAll, "all", "a", false, "Gets all modules within the system ignoring provided module name arguments")
moduleCmd.AddCommand(getModuleCmd)
rootCmd.AddCommand(moduleCmd)
}
6 changes: 3 additions & 3 deletions cli/cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"net/http"
"os"

s32 "github.com/dotindustries/moar/internal/storage/s3"
"github.com/dotindustries/moar/internal/storage/s3"
"github.com/gorilla/handlers"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -32,7 +32,7 @@ var upCmd = &cobra.Command{
// case "etcd":
// moduleStorage = internal.NewDatabase()
case "s3":
moduleStorage = s32.New(storageAddress)
moduleStorage = s3.New(storageAddress)
default:
logrus.Fatalf("invalid module storage type: '%s'", moduleStorageType)
}
Expand All @@ -56,7 +56,7 @@ var upCmd = &cobra.Command{

func init() {
upCmd.Flags().StringVar(&moduleStorageType, "storage_type", "s3", "Defines what storage type to use. Possible values: s3")
upCmd.Flags().StringVar(&storageAddress, "storage_addr", "localhost:9000", "The address to reach the storage")
upCmd.Flags().StringVar(&storageAddress, "storage_addr", "", "The address to reach the storage")
upCmd.Flags().StringVar(&host, "host", ":8000", "The address to bind the server to")
upCmd.Flags().BoolVar(&versionOverwriteEnabled, "overwrite", false, "Toggles whether version overwrite is enabled")
rootCmd.AddCommand(upCmd)
Expand Down
1 change: 1 addition & 0 deletions docker/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
COMPOSE_PROJECT_NAME=moar
1 change: 1 addition & 0 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ services:
image: dotindustries/moar-registry
pull_policy: always
hostname: moar
restart: on-failure
command:
- ./moar
- up
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.16
require (
github.com/Masterminds/semver v1.5.0
github.com/gorilla/handlers v1.5.1
github.com/jedib0t/go-pretty/v6 v6.2.4
github.com/minio/minio-go/v7 v7.0.14
github.com/mitchellh/go-homedir v1.1.0
github.com/sirupsen/logrus v1.8.1
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ github.com/felixge/httpsnoop v1.0.1 h1:lvB5Jl89CsZtGIWuTcDM1E/vkVs49/Ml7JJe07l8S
github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fzipp/gocyclo v0.3.1/go.mod h1:DJHO6AUmbdqj2ET4Z9iArSuwWgYDRryYt2wASxc7x3E=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
Expand Down Expand Up @@ -160,6 +161,8 @@ github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NH
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jcchavezs/porto v0.1.0 h1:Xmxxn25zQMmgE7/yHYmh19KcItG81hIwfbEEFnd6w/Q=
github.com/jcchavezs/porto v0.1.0/go.mod h1:fESH0gzDHiutHRdX2hv27ojnOVFco37hg1W6E9EZF4A=
github.com/jedib0t/go-pretty/v6 v6.2.4 h1:wdaj2KHD2W+mz8JgJ/Q6L/T5dB7kyqEFI16eLq7GEmk=
github.com/jedib0t/go-pretty/v6 v6.2.4/go.mod h1:+nE9fyyHGil+PuISTCrp7avEdo6bqoMwqZnuiK2r2a0=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/joeshaw/multierror v0.0.0-20140124173710-69b34d4ec901 h1:rp+c0RAYOWj8l6qbCUTSiRLG/iKnW3K3/QfPPuSsBt4=
github.com/joeshaw/multierror v0.0.0-20140124173710-69b34d4ec901/go.mod h1:Z86h9688Y0wesXCyonoVr47MasHilkuLMqGhRZ4Hpak=
Expand Down Expand Up @@ -192,6 +195,8 @@ github.com/magiconair/properties v1.8.1 h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzR
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
github.com/minio/md5-simd v1.1.0 h1:QPfiOqlZH+Cj9teu0t9b1nTBfPbyTl16Of5MeuShdK4=
Expand Down Expand Up @@ -226,6 +231,7 @@ github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/profile v1.2.1/go.mod h1:hJw3o1OdXxsrSjjVksARp5W95eeEaEfptyVZyv6JUPA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
Expand Down Expand Up @@ -398,6 +404,7 @@ golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180816055513-1c9583448a9c/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down
9 changes: 9 additions & 0 deletions internal/registry/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
type Reader interface {
ModuleResources(ctx context.Context, module string, version string, data bool) ([]internal.File, error)
GetModule(ctx context.Context, name string, loadData bool) (*internal.Module, error)
GetModules(ctx context.Context, loadData bool) ([]*internal.Module, error)
Close() error
}

Expand Down Expand Up @@ -97,6 +98,14 @@ func (s *Service) GetModule(ctx context.Context, name string, loadData bool) (*i
return m, err
}

func (s *Service) GetAllModules(ctx context.Context, loadData bool) ([]*internal.Module, error) {
modules, err := s.storage.GetModules(ctx, loadData)
if err != nil {
return nil, err
}
return modules, nil
}

func (s *Service) Close() error {
return s.storage.Close()
}
65 changes: 65 additions & 0 deletions internal/storage/s3/setup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package s3

import (
"os"

"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"github.com/sirupsen/logrus"
)

const defaultBucket = "modules"
const defaultEndpoint = "localhost:9000"

func New(endpoint string) *Storage {
logger := logrus.WithField("op", "storage")
endpoint = validateEndpoint(endpoint)
creds := collectCredentials()
useSSL := false
if os.Getenv("S3_USE_SSL") != "" {
useSSL = true
}
minioClient, err := minio.New(endpoint,
&minio.Options{
Creds: creds,
Secure: useSSL,
},
)
if err != nil {
logger.Fatalln(err)
}

s := &Storage{
minioClient: minioClient,
logger: logger,
bucket: bucket(),
}
s.setup()
return s
}

func collectCredentials() *credentials.Credentials {
accessKeyID := os.Getenv("S3_ACCESS_KEY_ID")
secretAccessKey := os.Getenv("S3_SECRET_ACCESS_KEY")
sessionToken := os.Getenv("AWS_SESSION_TOKEN")
return credentials.NewStaticV4(accessKeyID, secretAccessKey, sessionToken)
}

func bucket() string {
bucket := os.Getenv("S3_BUCKET")
if bucket == "" {
bucket = defaultBucket
}
return bucket
}

func validateEndpoint(endpoint string) string {
if endpoint == "" {
endpoint = os.Getenv("S3_ENDPOINT_URL")
if endpoint == "" {
endpoint = defaultEndpoint
}
}
logrus.Infof("Using S3 at %s", endpoint)
return endpoint
}
Loading

0 comments on commit 67f3ce5

Please sign in to comment.