diff --git a/cmd.go b/cmd.go new file mode 100644 index 0000000..1218e6d --- /dev/null +++ b/cmd.go @@ -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 +} diff --git a/cmd_init.go b/cmd_init.go new file mode 100644 index 0000000..b5e0ff1 --- /dev/null +++ b/cmd_init.go @@ -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 + }, +} diff --git a/cmd_version.go b/cmd_version.go new file mode 100644 index 0000000..1ff686e --- /dev/null +++ b/cmd_version.go @@ -0,0 +1,9 @@ +package main + +var CmdVersion = &Cmd{ + Name: "version", + Fn: func(args []string) error { + println(VERSION) + return nil + }, +} diff --git a/constants.go b/constants.go new file mode 100644 index 0000000..c56001f --- /dev/null +++ b/constants.go @@ -0,0 +1,8 @@ +package main + +const ( + CONF_FILENAME = "erkconf.json" + INIT_FILE_MESSAGE = `{ + "write_your_configuration": "here!" +}` +) diff --git a/erk.go b/erk.go new file mode 100644 index 0000000..d3a4985 --- /dev/null +++ b/erk.go @@ -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) +} diff --git a/file_existence.go b/file_existence.go new file mode 100644 index 0000000..548721f --- /dev/null +++ b/file_existence.go @@ -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 +} diff --git a/flag.go b/flag.go new file mode 100644 index 0000000..1107c57 --- /dev/null +++ b/flag.go @@ -0,0 +1,14 @@ +package main + +import ( +// "flag" +// "os" +) + +func SetArgs() { + +} + +func LoadArgs() { + SetArgs() +} diff --git a/log.go b/log.go new file mode 100644 index 0000000..4443ff3 --- /dev/null +++ b/log.go @@ -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) +} diff --git a/version.go b/version.go new file mode 100644 index 0000000..365e6cb --- /dev/null +++ b/version.go @@ -0,0 +1,3 @@ +package main + +const VERSION = "0.0.1"