func main() { var newline, tab, elseFormat string if *oneliner { newline = " " tab = "" } else { newline = "\n" tab = " " } if *elseCmdIsLiteral { elseFormat = fmt.Sprint(tab, tab, "%[1]s", newline) } else { elseFormat = fmt.Sprint(tab, tab, "%[1]s \"$@\";", newline) } if strings.Contains(flag.Arg(0), " ") && !*withArgs { fmt.Println("Warning: Arguments not included, as --with-args wasn't used.") flag.Args()[0] = strings.Split(flag.Arg(0), " ")[0] } else if flag.NArg() < 2 { fmt.Println("Usage: maulias [-afhls] [-e elseCommand] [-i condition] <ALIAS> <COMMANDS...>") return } if *withArgs { aliasWithArgs(newline, tab, elseFormat) } else if *function { aliasFunction(newline, tab, elseFormat) } else { aliasSimple() } }
func aliasSimple() { fmt.Print("alias '", flag.Arg(0), "'='") // Alias beginning for i, arg := range flag.Args()[1:] { fmt.Print(replace(arg, '¤', '$')) // Commands if i+2 < flag.NArg() { fmt.Print(" && ") } } fmt.Print("'\n") }
func aliasFunction(newline, tab, elseFormat string) { fmt.Print("function ", flag.Arg(0), " {", newline) // Start function if len(*condition) != 0 { fmt.Printf("%[2]sif [ %[1]s ]; ", *condition, tab) fmt.Print(tab, "then", newline) for _, arg := range flag.Args()[1:] { fmt.Printf("%[2]s%[2]s%[1]s;%[3]s", replace(arg, '¤', '$'), tab, newline) // Commands } if len(*elseCmd) != 0 { fmt.Print(tab, "else", newline) // "Else" fmt.Printf(elseFormat, *elseCmd) // Else command } fmt.Print(tab, "fi", "newline") // End if } else { for _, arg := range flag.Args()[1:] { fmt.Printf("%[2]s%[2]s%[1]s;%[3]s", replace(arg, '¤', '$'), tab, newline) // Commands } } fmt.Print("}\n") // End function }
func aliasWithArgs(newline, tab, elseFormat string) { sargs := strings.Split(flag.Arg(0), " ") fmt.Print("function ", sargs[0], " {", newline) // Start function sargs = sargs[1:] fmt.Print(tab, "if [ ") // Start if. Condition bracket opening for i, sarg := range sargs { fmt.Printf("\"$%[1]d\" = \"%[2]s\"", i+1, sarg) // If conditions if i+1 < len(sargs) { fmt.Printf(" -a ") // Bash equivalent to "&&" (logical AND) } } fmt.Print(" ];", newline) // Condition bracket closing fmt.Print(tab, "then", newline) // Then for _, arg := range flag.Args()[1:] { fmt.Printf("%[2]s%[2]s%[1]s;%[3]s", replace(arg, '¤', '$'), tab, newline) // Commands } fmt.Print(tab, "else", newline) // "Else" fmt.Printf(elseFormat, *elseCmd) // Else command fmt.Print(tab, "fi", newline) // End if fmt.Print("}\n") // End function }