-
Notifications
You must be signed in to change notification settings - Fork 1
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
0 parents
commit fb92a96
Showing
9 changed files
with
179 additions
and
0 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,46 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
type Cmd struct { | ||
Name string | ||
Desc string | ||
Args []string | ||
Aliases []string | ||
Fn func(args []string) error | ||
} | ||
|
||
var CmdList []*Cmd | ||
|
||
func init() { | ||
CmdList = []*Cmd{ | ||
CmdVersion, | ||
CmdInit, | ||
} | ||
} | ||
|
||
func CommandDispatch(args []string) error { | ||
var command *Cmd | ||
var commandName string | ||
var commandArgs = make([]string, 0) | ||
|
||
if len(args) < 2 { | ||
commandName = "version" | ||
} else { | ||
commandName = args[1] | ||
} | ||
|
||
for _, cmd := range CmdList { | ||
if cmd.Name == commandName { | ||
command = cmd | ||
break | ||
} | ||
} | ||
|
||
if command == nil { | ||
return fmt.Errorf("Command \"%s\" not found", commandName) | ||
} | ||
|
||
err := command.Fn(commandArgs) | ||
return err | ||
} |
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,23 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
) | ||
|
||
var CmdInit = &Cmd{ | ||
Name: "init", | ||
Fn: func(args []string) error { | ||
// check if configuration file already exists. | ||
if CheckFileExistence(CONF_FILENAME) { | ||
return fmt.Errorf("%s is already exists... ;)", CONF_FILENAME) | ||
} | ||
// creates new configuration file. | ||
err := ioutil.WriteFile(CONF_FILENAME, []byte(INIT_FILE_MESSAGE), 0666) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println("erk initialized!") | ||
return 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,9 @@ | ||
package main | ||
|
||
var CmdVersion = &Cmd{ | ||
Name: "version", | ||
Fn: func(args []string) error { | ||
println(VERSION) | ||
return 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,8 @@ | ||
package main | ||
|
||
const ( | ||
CONF_FILENAME = "erkconf.json" | ||
INIT_FILE_MESSAGE = `{ | ||
"write_your_configuration": "here!" | ||
}` | ||
) |
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,28 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
) | ||
|
||
func LoadConfig() error { | ||
if !CheckFileExistence(CONF_FILENAME) { | ||
return fmt.Errorf("Configuration file: %s is not found. please run \"erk init\" first.", CONF_FILENAME) | ||
} | ||
return nil | ||
} | ||
|
||
func exitIfError(err error) { | ||
if err != nil { | ||
log_error("error %v", err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func main() { | ||
var args = os.Args | ||
// err := LoadConfig() | ||
// exitIfError(err) | ||
err := CommandDispatch(args) | ||
exitIfError(err) | ||
} |
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,11 @@ | ||
package main | ||
|
||
import "os" | ||
|
||
// http://stackoverflow.com/questions/12518876/how-to-check-if-a-file-exists-in-go | ||
func CheckFileExistence(filename string) bool { | ||
if _, err := os.Stat(filename); os.IsNotExist(err) { | ||
return false | ||
} | ||
return true | ||
} |
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,14 @@ | ||
package main | ||
|
||
import ( | ||
// "flag" | ||
// "os" | ||
) | ||
|
||
func SetArgs() { | ||
|
||
} | ||
|
||
func LoadArgs() { | ||
SetArgs() | ||
} |
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,37 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
) | ||
|
||
const ( | ||
defaultLogFormat = "erk: %s" | ||
errorLogFormat = "\033[31merk: %s\033[0m" | ||
) | ||
|
||
var logFormat = "" | ||
|
||
func formatLog(msg string) string { | ||
if logFormat == "" { | ||
logFormat = os.Getenv("DIRENV_LOG_FORMAT") | ||
if logFormat == "" { | ||
logFormat = defaultLogFormat | ||
} | ||
} | ||
return fmt.Sprintf(logFormat, msg) | ||
} | ||
|
||
func log_error(msg string, a ...interface{}) { | ||
msg = fmt.Sprintf(errorLogFormat, msg) | ||
log(msg, a...) | ||
} | ||
|
||
func log_status(msg string, a ...interface{}) { | ||
log(formatLog(msg), a...) | ||
} | ||
|
||
func log(msg string, a ...interface{}) { | ||
msg = fmt.Sprintf(msg, a...) | ||
fmt.Fprintf(os.Stderr, "%s\n", msg) | ||
} |
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,3 @@ | ||
package main | ||
|
||
const VERSION = "0.0.1" |