-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from rsteube/bash
bash
- Loading branch information
Showing
5 changed files
with
246 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package bash | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
func Callback(uid string) string { | ||
return fmt.Sprintf(`eval $(_callback '%v')`, uid) // TODO update and use ActionExecute for eval? | ||
} | ||
|
||
func ActionExecute(command string) string { | ||
return fmt.Sprintf(`$(%v)`, command) | ||
} | ||
|
||
func ActionBool() string { | ||
return ActionValues("true", "false") | ||
} | ||
|
||
func ActionPathFiles(suffix string) string { | ||
return "" | ||
} | ||
|
||
func ActionFiles(suffix string) string { | ||
return fmt.Sprintf(`compgen -f -o plusdirs -X "!*%v" -- $last`, suffix) | ||
} | ||
|
||
func ActionNetInterfaces() string { | ||
return "" | ||
} | ||
|
||
func ActionUsers() string { | ||
return `compgen -u -- $last` | ||
} | ||
|
||
func ActionGroups() string { | ||
return `compgen -g -- $last` | ||
} | ||
|
||
func ActionHosts() string { | ||
return "" | ||
} | ||
|
||
func ActionOptions() string { | ||
return "" | ||
} | ||
|
||
func ActionValues(values ...string) string { | ||
if len(strings.TrimSpace(strings.Join(values, ""))) == 0 { | ||
return ActionMessage("no values to complete") | ||
} | ||
|
||
vals := make([]string, len(values)) | ||
for index, val := range values { | ||
// TODO escape special characters | ||
//vals[index] = strings.Replace(val, " ", `\ `, -1) | ||
vals[index] = val | ||
} | ||
return fmt.Sprintf(`compgen -W "%v" -- $last`, strings.Join(vals, ` `)) | ||
} | ||
|
||
func ActionValuesDescribed(values ...string) string { | ||
// TODO verify length (description always exists) | ||
vals := make([]string, len(values)) | ||
for index, val := range values { | ||
if index%2 == 0 { | ||
vals[index/2] = val | ||
} | ||
} | ||
return ActionValues(vals...) | ||
} | ||
|
||
func ActionMessage(msg string) string { | ||
return ActionValues("ERR", msg) // TODO escape characters | ||
} | ||
|
||
func ActionMultiParts(separator rune, values ...string) string { | ||
return ActionValues(values...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package bash | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/pflag" | ||
) | ||
|
||
func SnippetFlagList(flags *pflag.FlagSet) string { | ||
flagValues := make([]string, 0) | ||
|
||
flags.VisitAll(func(flag *pflag.Flag) { | ||
if !flag.Hidden { | ||
flagValues = append(flagValues, "--"+flag.Name) | ||
if flag.Shorthand != "" { | ||
flagValues = append(flagValues, "-"+flag.Shorthand) | ||
} | ||
} | ||
}) | ||
return ActionValues(flagValues...) | ||
} | ||
|
||
func SnippetFlagCompletion(flag *pflag.Flag, action string) (snippet string) { | ||
if flag.NoOptDefVal != "" { | ||
return "" | ||
} | ||
|
||
var names string | ||
if flag.Shorthand != "" { | ||
names = fmt.Sprintf("-%v | --%v", flag.Shorthand, flag.Name) | ||
} else { | ||
names = "--" + flag.Name | ||
} | ||
|
||
return fmt.Sprintf(` %v) | ||
COMPREPLY=($(%v)) | ||
;; | ||
`, names, action) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters