Skip to content

Commit

Permalink
Format via go fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
malscent committed Feb 10, 2021
1 parent a845dd5 commit 9e3cac6
Showing 1 changed file with 57 additions and 59 deletions.
116 changes: 57 additions & 59 deletions bundler.go
Original file line number Diff line number Diff line change
@@ -1,80 +1,79 @@
package main

import (
"bytes"
"fmt"
"github.com/thatisuday/commando"
"io"
"io/ioutil"
"mvdan.cc/sh/v3/syntax"
"os"
"io"
"fmt"
"bytes"
"path/filepath"
"strings"
"time"
"github.com/thatisuday/commando"
"path/filepath"
"io/ioutil"
)

func main() {
// configure commando
commando.SetExecutableName("bash_bundler").
SetVersion("1.0.0").
SetDescription("This simple tool bundles bash files into a single bash file.")
SetVersion("1.0.0").
SetDescription("This simple tool bundles bash files into a single bash file.")

commando.Register(nil).
SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
fmt.Printf("Printing options of the `info` command...\n\n")
SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
fmt.Printf("Printing options of the `info` command...\n\n")

// print arguments
for k, v := range args {
fmt.Printf("arg -> %v: %v(%T)\n", k, v.Value, v.Value)
}
// print arguments
for k, v := range args {
fmt.Printf("arg -> %v: %v(%T)\n", k, v.Value, v.Value)
}

// print flags
for k, v := range flags {
fmt.Printf("flag -> %v: %v(%T)\n", k, v.Value, v.Value)
}
})

// print flags
for k, v := range flags {
fmt.Printf("flag -> %v: %v(%T)\n", k, v.Value, v.Value)
}
})

commando.Register("bundle").
SetShortDescription("bundle a bash script").
SetDescription("Takes an entry bash script and bundles it and all its sources into a single output file.").
AddFlag("entry,e", "The entrypoint to the bash script to bundle.", commando.String, nil).
AddFlag("output,o", "The output file to write to", commando.String, nil).
AddFlag("minify,m", "Minify the output file", commando.Bool, false).
SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
fmt.Printf("Performing bundling on entrypoint: %v\n", flags["entry"].Value)
fmt.Printf("Bundling output to: %v\n", flags["output"].Value)
entry, err := flags["entry"].GetString()
output, err := flags["output"].GetString()
min, err := flags["minify"].GetBool()
if (err != nil) {
fmt.Println("Error reading parameters.")
}
content, err := bundle(entry, true)
if err != nil {
fmt.Println("ERROR: " + err.Error())
return
}
if min {
content, err = minify(content)
if err != nil {
fmt.Println("ERROR: " + err.Error())
return
}
}
err = writeToFile(output, content)
SetShortDescription("bundle a bash script").
SetDescription("Takes an entry bash script and bundles it and all its sources into a single output file.").
AddFlag("entry,e", "The entrypoint to the bash script to bundle.", commando.String, nil).
AddFlag("output,o", "The output file to write to", commando.String, nil).
AddFlag("minify,m", "Minify the output file", commando.Bool, false).
SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
fmt.Printf("Performing bundling on entrypoint: %v\n", flags["entry"].Value)
fmt.Printf("Bundling output to: %v\n", flags["output"].Value)
entry, err := flags["entry"].GetString()
output, err := flags["output"].GetString()
min, err := flags["minify"].GetBool()
if err != nil {
fmt.Println("Error reading parameters.")
}
content, err := bundle(entry, true)
if err != nil {
fmt.Println("ERROR: " + err.Error())
return
}
if min {
content, err = minify(content)
if err != nil {
fmt.Println("ERROR: " + err.Error())
return
}
}
err = writeToFile(output, content)
if err != nil {
fmt.Println("ERROR: " + err.Error())
return
}

})


})

commando.Parse(nil)
}

func isShebang(line string) bool {
return strings.HasPrefix(line, "#!")
return strings.HasPrefix(line, "#!")
}

func deleteEmpty(s []string) []string {
Expand All @@ -89,15 +88,14 @@ func deleteEmpty(s []string) []string {

func trimQuotes(s string) string {
if len(s) >= 2 {
if (s[0] == '"' || s[0] == '\'') &&
(s[len(s) -1] == '"' || s[len(s) -1] == '\'' ) {
return s[1 : len(s) - 1]
if (s[0] == '"' || s[0] == '\'') &&
(s[len(s)-1] == '"' || s[len(s)-1] == '\'') {
return s[1 : len(s)-1]
}
}
return s
}


func header(path string, keepSheBang bool) string {
var s string
if keepSheBang {
Expand Down Expand Up @@ -153,15 +151,15 @@ func bundle(path string, keepSheBang bool) (string, error) {
var line string = ""
var internalBuffer = bytes.NewBufferString(line)
printer.Print(internalBuffer, stmt)
temp := strings.Split(internalBuffer.String(), "\n")
temp := strings.Split(internalBuffer.String(), "\n")
for _, s := range temp {
if strings.Contains(s, "source") && !strings.HasPrefix(strings.TrimSpace(s), "#") {
set := strings.Split(strings.TrimSpace(s), " ")
set = deleteEmpty(set)
subPath := directory + "/" + trimQuotes(strings.TrimSpace(set[1]))
fmt.Println("Bundling Source: " + subPath)
sub, err := bundle(subPath, false)
if (err != nil) {
if err != nil {
fmt.Println(err.Error())
return false
}
Expand All @@ -179,4 +177,4 @@ func bundle(path string, keepSheBang bool) (string, error) {
func writeToFile(path string, content string) error {
err := ioutil.WriteFile(path, []byte(content), 0644)
return err
}
}

0 comments on commit 9e3cac6

Please sign in to comment.