Skip to content

Commit

Permalink
Merge pull request #151 from rebuy-de/simplify-cdnmirror
Browse files Browse the repository at this point in the history
simplify cdnmirror by moving definitions to actual apps
  • Loading branch information
svenwltr authored Apr 21, 2023
2 parents 9dc6c84 + 49f3786 commit 62ef139
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 79 deletions.
79 changes: 43 additions & 36 deletions cmd/cdnmirror/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/evanw/esbuild/pkg/api"
"github.com/pkg/errors"
"github.com/rebuy-de/rebuy-go-sdk/v5/pkg/cmdutil"
"github.com/rebuy-de/rebuy-go-sdk/v5/pkg/webutil"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand All @@ -30,22 +29,34 @@ func main() {

func NewRootCommand() *cobra.Command {
return cmdutil.New(
"cdnmirror SOURCE_NAME..", "Downloads assets from CDNs so the server can serve them directly.",
"cdnmirror", "Downloads assets from CDNs so the server can serve them directly.",
cmdutil.WithLogVerboseFlag(),
cmdutil.WithRun(Generate),
cmdutil.WithRunner(new(Generate)),
)
}

func Generate(ctx context.Context, cmd *cobra.Command, args []string) {
type Generate struct {
Source string
Target string
Minify string
}

func (g *Generate) Bind(cmd *cobra.Command) error {
cmd.PersistentFlags().StringVar(
&g.Source, "source", "", `URL to the original CDN.`)
cmd.PersistentFlags().StringVar(
&g.Target, "target", "", `Name of the target file in assets/cdnmirror`)
cmd.PersistentFlags().StringVar(
&g.Minify, "minify", "", `Minify file with given type; allowed values: js`)
return nil
}

func (g *Generate) Run(ctx context.Context) error {
err := os.MkdirAll(targetPathPrefix, 0755)
cmdutil.Must(err)

writeGitignore()

for _, name := range args {
source := resolve(name)
download(source)
}
return g.download()
}

func writeGitignore() {
Expand All @@ -59,40 +70,28 @@ func writeGitignore() {
cmdutil.Must(err)
}

func resolve(name string) webutil.CDNMirrorSource {
switch name {
case "@hotwired/turbo":
return webutil.CDNMirrorSourceHotwiredTurbo()
case "bootstrap":
return webutil.CDNMirrorSourceBootstrap()
case "font-awesome-sprites":
return webutil.CDNMirrorSourceFontAwesomeSprites()
case "bulma":
return webutil.CDNMirrorSourceBulma()
default:
cmdutil.Must(errors.Errorf("invalid source name"))
return webutil.CDNMirrorSource{}
}
}
func (g *Generate) download() error {
targetFile := filepath.FromSlash(path.Join(targetPathPrefix, g.Target))

func download(source webutil.CDNMirrorSource) {
targetFile := filepath.FromSlash(path.Join(targetPathPrefix, source.Target))

resp, err := http.Get(source.URL)
cmdutil.Must(err)
resp, err := http.Get(g.Source)
if err != nil {
return fmt.Errorf("request source: %w", err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
cmdutil.Must(fmt.Errorf(resp.Status))
return fmt.Errorf(resp.Status)
}

body, err := io.ReadAll(resp.Body)
cmdutil.Must(err)
if err != nil {
return fmt.Errorf("read source: %w", err)
}

var code string

switch source.Minify {
case webutil.CDNMirrorMinifyJS:
switch g.Minify {
case "js":
result := api.Transform(string(body), api.TransformOptions{
MinifyWhitespace: true,
MinifyIdentifiers: true,
Expand All @@ -103,14 +102,22 @@ func download(source webutil.CDNMirrorSource) {
}
code = string(result.Code)

default:
case "":
code = string(body)
default:
return fmt.Errorf("invalid minify option %q", g.Minify)
}

f, err := os.Create(targetFile)
cmdutil.Must(err)
if err != nil {
return fmt.Errorf("create target file: %w", err)
}
defer f.Close()

_, err = io.WriteString(f, code)
cmdutil.Must(err)
if err != nil {
return fmt.Errorf("write file: %w", err)
}

return nil
}
43 changes: 0 additions & 43 deletions pkg/webutil/cdnmirror.go

This file was deleted.

0 comments on commit 62ef139

Please sign in to comment.