Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
takashi committed Apr 22, 2014
0 parents commit fb92a96
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 0 deletions.
46 changes: 46 additions & 0 deletions cmd.go
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
}
23 changes: 23 additions & 0 deletions cmd_init.go
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
},
}
9 changes: 9 additions & 0 deletions cmd_version.go
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
},
}
8 changes: 8 additions & 0 deletions constants.go
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!"
}`
)
28 changes: 28 additions & 0 deletions erk.go
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)
}
11 changes: 11 additions & 0 deletions file_existence.go
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
}
14 changes: 14 additions & 0 deletions flag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import (
// "flag"
// "os"
)

func SetArgs() {

}

func LoadArgs() {
SetArgs()
}
37 changes: 37 additions & 0 deletions log.go
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)
}
3 changes: 3 additions & 0 deletions version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package main

const VERSION = "0.0.1"

0 comments on commit fb92a96

Please sign in to comment.