forked from ochinchina/supervisord
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Chang Hua Ou
committed
Jan 16, 2017
1 parent
c1721e4
commit f5a6853
Showing
10 changed files
with
283 additions
and
58 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"unicode" | ||
) | ||
|
||
func findChar( s string, offset int, ch byte ) int { | ||
for i := offset; i < len( s ); i++ { | ||
if s[i] == '\\' { | ||
i++ | ||
} else if s[i] == ch { | ||
return i | ||
} | ||
} | ||
return -1 | ||
} | ||
|
||
func skipSpace( s string, offset int ) int { | ||
for i := offset; i < len( s ); i++ { | ||
if !unicode.IsSpace(rune(s[i]) ) { | ||
return i | ||
} | ||
} | ||
return -1 | ||
} | ||
|
||
func appendArgument( arg string, args []string) []string { | ||
if arg[0] == '"' || arg[0] == '\'' { | ||
return append( args, arg[1:len(arg)-1 ] ) | ||
} | ||
return append( args, arg ) | ||
} | ||
|
||
func parseCommand( command string )( []string, error ) { | ||
args := make([]string, 0) | ||
cmdLen := len( command ) | ||
for i := 0; i < cmdLen; { | ||
j := skipSpace( command, i ) | ||
if j == -1 { | ||
break | ||
} | ||
for ; j < cmdLen; j++ { | ||
if unicode.IsSpace(rune(command[j])) { | ||
args = appendArgument( command[i:j], args ) | ||
i = j + 1 | ||
break | ||
} else if command[j] == '\\' { | ||
j ++ | ||
} else if command[j] == '"' || command[j] == '\'' { | ||
k := findChar( command, j + 1, command[j] ) | ||
if k == -1 { | ||
args = appendArgument( command[i:], args ) | ||
i = cmdLen | ||
} else { | ||
args = appendArgument( command[i:k+1], args) | ||
i = k + 2 | ||
} | ||
break | ||
} | ||
} | ||
if j >= cmdLen { | ||
args = appendArgument( command[i:], args ) | ||
i = cmdLen | ||
} | ||
} | ||
if len( args ) <= 0 { | ||
return nil, fmt.Errorf( "no command from empty string") | ||
} | ||
return args, nil | ||
} | ||
|
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,33 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
|
||
func TestEmptyCommandLine( t* testing.T ) { | ||
args, err := parseCommand( " ") | ||
if err == nil || len( args ) > 0 { | ||
t.Error( "fail to parse the empty command line") | ||
} | ||
} | ||
|
||
func TestNormalCommandLine(t *testing.T ) { | ||
args, err := parseCommand( "program arg1 arg2") | ||
if err != nil { | ||
t.Error( "fail to parse normal command line") | ||
} | ||
if args[0] != "program" || args[1] != "arg1" || args[2] != "arg2" { | ||
t.Error( "fail to parse normal command line" ) | ||
} | ||
} | ||
|
||
func TestCommandLineWithQuotationMarks(t* testing.T ) { | ||
args, err := parseCommand( "program 'this is arg1' args=\"this is arg2\"" ) | ||
if err != nil || len( args) != 3 { | ||
t.Error( "fail to parse command line with quotation marks") | ||
} | ||
if args[0] != "program" || args[1] != "this is arg1" || args[2] != "args=\"this is arg2\"" { | ||
t.Error( "fail to parse command line with quotation marks") | ||
} | ||
} |
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 |
---|---|---|
@@ -1,7 +1,6 @@ | ||
package main | ||
|
||
import( | ||
//"flag" | ||
"os" | ||
"github.com/jessevdk/go-flags" | ||
log "github.com/Sirupsen/logrus" | ||
|
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
Oops, something went wrong.