Skip to content

Commit

Permalink
Merge pull request #197 from rebuy-de/cdnmirror-dir
Browse files Browse the repository at this point in the history
make cdnmirror create dirs
  • Loading branch information
svenwltr committed Mar 22, 2024
2 parents b05ceae + eb9193f commit 50d026a
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions cmd/cdnmirror/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path"
Expand Down Expand Up @@ -53,21 +52,26 @@ func (g *Generate) Bind(cmd *cobra.Command) error {

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

err = writeGitignore()
if err != nil {
return err
}

writeGitignore()
return g.download()
}

func writeGitignore() {
func writeGitignore() error {
filename := path.Join(targetPathPrefix, ".gitignore")

buf := new(bytes.Buffer)
fmt.Fprintln(buf, "*")
fmt.Fprintln(buf, "!.gitignore")

err := ioutil.WriteFile(filename, buf.Bytes(), 0644)
cmdutil.Must(err)
return os.WriteFile(filename, buf.Bytes(), 0644)
}

func (g *Generate) download() error {
Expand Down Expand Up @@ -98,7 +102,7 @@ func (g *Generate) download() error {
MinifySyntax: true,
})
if len(result.Errors) != 0 {
cmdutil.Must(errors.Errorf("%#v", result.Errors))
return errors.Errorf("%#v", result.Errors)
}
code = string(result.Code)

Expand All @@ -108,6 +112,11 @@ func (g *Generate) download() error {
return fmt.Errorf("invalid minify option %q", g.Minify)
}

err = os.MkdirAll(path.Dir(targetFile), 0755)
if err != nil {
return fmt.Errorf("create target dir: %w", err)
}

f, err := os.Create(targetFile)
if err != nil {
return fmt.Errorf("create target file: %w", err)
Expand Down

0 comments on commit 50d026a

Please sign in to comment.