Skip to content

Commit

Permalink
added uploader functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Sjoerd Riemersma committed Apr 30, 2021
1 parent 51940a1 commit 0c76e88
Show file tree
Hide file tree
Showing 8 changed files with 384 additions and 22 deletions.
2 changes: 2 additions & 0 deletions cmd/afosto/afosto.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"github.com/afosto/cli/cmd/afosto/template"
"github.com/afosto/cli/cmd/afosto/upload"
"github.com/spf13/cobra"
"os"
)
Expand All @@ -12,6 +13,7 @@ var (

func init() {
rootCmd.AddCommand(template.GetCommands()...)
rootCmd.AddCommand(upload.GetCommands()...)
}

func main() {
Expand Down
18 changes: 17 additions & 1 deletion cmd/afosto/template/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,23 @@ func GetCommands() []*cobra.Command {
func Render(cmd *cobra.Command, args []string) {
user := auth.GetUser()
if user == nil {
user = auth.GetImplicitUser()
user = auth.GetImplicitUser([]string{
"openid",
"email",
"profile",
"cnt:index:read",
"iam:users:read",
"iam:roles:read",
"iam:tenants:read",
"lcs:locations:read",
"lcs:handling:read",
"lcs:shipments:read",
"odr:orders:read",
"odr:coupons:read",
"odr:invoices:read",
"rel:contacts:read",
"rel:identity:read",
})
}
port, err := cmd.Flags().GetInt("port")
if err != nil {
Expand Down
144 changes: 144 additions & 0 deletions cmd/afosto/upload/upload.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package upload

import (
"context"
"github.com/afosto/cli/pkg/auth"
"github.com/afosto/cli/pkg/client"
"github.com/afosto/cli/pkg/logging"
"github.com/spf13/cobra"
"io"
"log"
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
"sync"
)

var _ io.Reader = (*os.File)(nil)

func GetCommands() []*cobra.Command {

dir, _ := os.Getwd()

renderCmd := &cobra.Command{
Use: "upload",
Short: "Upload template",

Long: `Start the local preview service`,
Run: func(cmd *cobra.Command, args []string) {
upload(cmd, args)
}}

renderCmd.Flags().StringP("source", "s", dir, "")
renderCmd.Flags().StringP("destination", "d", "/uploads/", "")

return []*cobra.Command{renderCmd}
}

func upload(cmd *cobra.Command, args []string) {
source, err := cmd.Flags().GetString("source")

if err != nil {
log.Fatal(err)
}

destination, err := cmd.Flags().GetString("destination")

if err != nil {
log.Fatal(err)
}

user := auth.GetUser()
if user == nil {
user = auth.GetImplicitUser([]string{
"openid",
"email",
"profile",
"cnt:files:read",
"cnt:files:write",
})
}
ctx := context.WithValue(context.Background(), client.Jwt, user.GetAccessToken())

fileClient := client.NewFileClient()
queue := make(chan string, 25)
uploader := sync.WaitGroup{}
matcher := regexp.MustCompile(".*(jpe?g|png|svg|css|csv|js|txt|doc|eot|json|xls|xlsx|pdf|xml|mp4|mov|zip|md)$")
uploader.Add(1)
for i := 0; i < runtime.NumCPU(); i++ {
go func() {
for path := range queue {
file1, err := os.Open(path)
if err != nil {
logging.Log.Warnf("✗ could not open `%s`", path)
}

filePath := path

if idx := strings.LastIndex(filePath, "/"); idx != -1 {
filePath = filePath[0 : idx+1]
}

relativePath := strings.TrimLeft(filePath, source)

replacer := strings.NewReplacer("//", "/")

destinationDir := replacer.Replace(strings.Join([]string{destination, relativePath}, "/"))

//
finfo, err := file1.Stat()
if err != nil {
logging.Log.Warnf("✗ could not get stats of `%s`", path)
}

signature, err := fileClient.GetPublicSignature(ctx, destinationDir, "upsert")
if err != nil {
logging.Log.Warnf("✗ failed to get a signature url for `%s`", destinationDir)
}

file, err := fileClient.Upload(ctx, file1, finfo.Name(), signature)
if err != nil {
logging.Log.Errorf("✗ failed to upload `%s`", path)
} else {
logging.Log.Infof("✔ Uploaded `%s` on url `%s`", file.Filename, file.Url)
}
uploader.Done()
file1.Close()
}

}()
}

err = filepath.Walk(source,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

if info.IsDir() {
return nil
}

if !matcher.MatchString(path) {
logging.Log.Warnf("✗ invalid suffix for `%s`", path)
}

uploader.Add(1)
queue <- path
logging.Log.Infof("✔ added to queue `%s` ", path)

//fmt.Println(path)

return nil
})

if err != nil {
log.Fatal(err)
}

uploader.Wait()

logging.Log.Info("✔ Finished uploading all files")
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ module github.com/afosto/cli
go 1.13

require (
github.com/dghubble/sling v1.3.0
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/flosch/pongo2/v4 v4.0.2
github.com/google/uuid v1.2.0
github.com/gorilla/mux v1.8.0
github.com/leekchan/accounting v1.0.0
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/pkg/browser v0.0.0-20210115035449-ce105d075bb4
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.1.1
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsr
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dghubble/sling v1.3.0 h1:pZHjCJq4zJvc6qVQ5wN1jo5oNZlNE0+8T/h0XeXBUKU=
github.com/dghubble/sling v1.3.0/go.mod h1:XXShWaBWKzNLhu2OxikSNFrlsvowtz4kyRuXUG7oQKY=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
Expand Down Expand Up @@ -63,6 +65,8 @@ github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Z
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
Expand Down Expand Up @@ -115,6 +119,7 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/leekchan/accounting v1.0.0 h1:+Wd7dJ//dFPa28rc1hjyy+qzCbXPMR91Fb6F1VGTQHg=
github.com/leekchan/accounting v1.0.0/go.mod h1:3timm6YPhY3YDaGxl0q3eaflX0eoSx3FXn7ckHe4tO0=
github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
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=
Expand All @@ -136,6 +141,8 @@ github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWb
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pkg/browser v0.0.0-20210115035449-ce105d075bb4 h1:Qj1ukM4GlMWXNdMBuXcXfz/Kw9s1qm0CLY32QxuSImI=
github.com/pkg/browser v0.0.0-20210115035449-ce105d075bb4/go.mod h1:N6UoU20jOqggOuDwUaBQpluzLNDqif3kq9z2wpdYEfQ=
Expand Down
4 changes: 2 additions & 2 deletions pkg/auth/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ func GetUser() *data.User {
return user
}

func GetImplicitUser() *data.User {
err := browser.OpenURL(client.GetAuthorizationURL())
func GetImplicitUser(permissions []string) *data.User {
err := browser.OpenURL(client.GetAuthorizationURL(permissions))
if err != nil {
logging.Log.Fatal("could not call browser")
}
Expand Down
20 changes: 1 addition & 19 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,7 @@ type QueryError struct {
Path []string `json:"path"`
}

func GetAuthorizationURL() string {
scopes := []string{
"openid",
"email",
"profile",
"cnt:index:read",
"iam:users:read",
"iam:roles:read",
"iam:tenants:read",
"lcs:locations:read",
"lcs:handling:read",
"lcs:shipments:read",
"odr:orders:read",
"odr:coupons:read",
"odr:invoices:read",
"rel:contacts:read",
"rel:identity:read",
}

func GetAuthorizationURL(scopes []string) string {
return fmt.Sprintf("%s?client_id=%s&redirect_uri=%s&response_type=token+id_token&scope=%s",
BaseAuthorizationURL, OauthClientID, url.QueryEscape(RedirectURL), url.QueryEscape(strings.Join(scopes, " ")))
}
Expand Down
Loading

0 comments on commit 0c76e88

Please sign in to comment.