Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Copyright (c) 2019, Saleem Mirza
- All rights reserved.
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions are met:
- * Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
- * Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
- * Neither the name of the <organization> nor the
- names of its contributors may be used to endorse or promote products
- derived from this software without specific prior written permission.
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- package main
- import (
- "flag"
- "fmt"
- "io/ioutil"
- "log"
- "os"
- "path"
- "path/filepath"
- "regexp"
- "strings"
- "time"
- )
- var (
- homeDirectory string
- workDirectory string
- filter string
- rxString = `(?msi)<script[^>]+?src=\"~?\/(.+?)(\?.+?)?\".*?<\/script>`
- rx = regexp.MustCompile(rxString)
- scriptInfoMap = make(map[string]int64)
- extensionsMap = make(map[string]bool)
- )
- func init() {
- currentDirectory, _ := os.Getwd()
- flag.StringVar(&homeDirectory, "homeDirectory", currentDirectory, "set home directory")
- flag.StringVar(&workDirectory, "workDirectory", currentDirectory, "set working directory")
- flag.StringVar(&filter, "filter", ".html", "file types to apply changes.")
- }
- func main() {
- var logoText = `
- oo oo oo oo
- oo oo oo oo ooo
- ooooo ooooooo
- ooooo ooooooo
- ooooo ooooo
- oooo ooo
- oo ooo
- o oooo
- oo ooo oo ooooooo
- o oooo ooooo oooooo o
- o ooooo oooooooo oooooo o
- oo ooooo ooooooo ooooooo o
- o ooo oooo ooooooooo o
- oo oooooooooo ooo
- ooo oooooo ooooooooooo oooo
- ooooo oooooo ooooooooooooo oooooo
- ooooooo ooooooooooooooo oooooooo
- oooooooo ooo ooooooooooooooo ooooooooo
- oooooooooooo oooooooooooooooooooooooooo
- ooooooooooooooooooooooooooooo oooooooooo
- ooooooooo ooooooo ooooooooooo oooooooo
- ooooooooooo ooooooooooo ooooo
- oooooooooo ooooooooooo
- ooooooo oooooooooooo
- ooooooooooo
- oooooooooo
- `
- fmt.Println(logoText)
- flag.Usage = func() {
- fmt.Println("\nUsage:")
- flag.PrintDefaults()
- fmt.Println()
- }
- flag.Parse()
- for _, v := range strings.Split(filter, ";") {
- extensionsMap[v] = true
- }
- fileCounter := 0
- var startTime = time.Now()
- err := filepath.Walk(workDirectory, func(path string, info os.FileInfo, err error) error {
- if err != nil {
- return err
- }
- if info.IsDir() {
- return nil
- }
- fileExt := filepath.Ext(info.Name())
- if _, ok := extensionsMap[fileExt]; ok == true {
- fileCounter++
- _ = processFile(path)
- }
- return nil
- })
- fmt.Printf("\n\n%d files processed in %0.3f seconds\n", fileCounter, time.Since(startTime).Seconds())
- if err != nil {
- println(err)
- }
- }
- func processFile(filePath string) error {
- data, e := ioutil.ReadFile(filePath)
- if e == nil {
- fileContents := string(data)
- matchIndices := rx.FindAllStringSubmatchIndex(fileContents, -1)
- if matchIndices != nil {
- file, err := os.OpenFile(filePath, os.O_TRUNC|os.O_WRONLY, 0777)
- if err != nil {
- log.Println(err)
- return err
- }
- defer file.Close()
- _ = file.Truncate(0)
- lastIndex := 0
- for _, v := range matchIndices {
- _, _ = file.WriteString(fileContents[lastIndex:v[2]])
- script := fileContents[v[2]:v[3]]
- scriptPath := path.Join(homeDirectory, script)
- fileInfo, err := os.Stat(scriptPath)
- if err == nil {
- _, _ = file.WriteString(fmt.Sprintf("%s?v=%v", script, fileInfo.ModTime().Unix()))
- } else {
- log.Printf("[ NOT FOUND: %s ] %s\n", script, filePath)
- _, _ = file.WriteString(script)
- }
- if v[5] < 1 {
- lastIndex = v[3]
- } else {
- lastIndex = v[5]
- }
- }
- _, _ = file.WriteString(fileContents[lastIndex:])
- }
- return nil
- } else {
- return e
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement