diff --git a/cmd/root.go b/cmd/root.go index 80fd93b..01e78f2 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -16,15 +16,13 @@ package cmd import ( "fmt" - "os" - homedir "github.com/mitchellh/go-homedir" "github.com/spf13/cobra" "github.com/spf13/viper" + "os" + "path/filepath" ) -var cfgFile string - var rootCmd = &cobra.Command{ Use: "nem.sh", Short: "nem.sh is a tool that executes the shell script written in the transaction message.", @@ -59,7 +57,6 @@ var dhash map[string]interface{} func init() { cobra.OnInitialize(initConfig) - rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.nem.sh.json)") viper.SetDefault("node", "san.nem.ninja") viper.SetDefault("port", "7890") viper.SetDefault("protocol", "http") @@ -70,17 +67,29 @@ func init() { } func initConfig() { - if cfgFile != "" { - viper.SetConfigFile(cfgFile) - } else { - home, err := homedir.Dir() - if err != nil { + home, err := homedir.Dir() + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + path := filepath.Join(home, ".nem.sh") + viper.AddConfigPath(path) + viper.SetConfigType("json") + viper.SetConfigName("config") + if err := os.MkdirAll(path, 0777); err != nil { + fmt.Println(err) + } + + configPath := filepath.Join(path, "config.json") + if Exists(configPath) == false { + if err := viper.WriteConfigAs(configPath); err != nil { fmt.Println(err) - os.Exit(1) } - viper.AddConfigPath(home) - viper.SetConfigType("json") - viper.SetConfigName(".nem.sh") + } + cashPath := filepath.Join(path, "cash") + if err := os.MkdirAll(cashPath, 0777); err != nil { + fmt.Println(err) } viper.AutomaticEnv() @@ -89,3 +98,24 @@ func initConfig() { viper.ConfigFileUsed() } } + +func Exists(name string) bool { + if _, err := os.Stat(name); err != nil { + if os.IsNotExist(err) { + return false + } + } + return true +} + +func GetFilePath(name string) string { + home, err := homedir.Dir() + if err != nil { + fmt.Println(err) + os.Exit(1) + } + path := filepath.Join(home, ".nem.sh") + cashPath := filepath.Join(path, "cash") + filePath := filepath.Join(cashPath, name+".sh") + return filePath +} diff --git a/cmd/run.go b/cmd/run.go index 6e3ef9e..aee4a4c 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -15,8 +15,10 @@ package cmd import ( + "bytes" "encoding/hex" "encoding/json" + "errors" "fmt" "github.com/myndshft/nemgo" "github.com/spf13/cobra" @@ -24,6 +26,7 @@ import ( "io/ioutil" "net/http" "os/exec" + "strings" ) var hash string @@ -47,45 +50,53 @@ var runCmd = &cobra.Command{ address := viper.GetString("address") h := "" if alias != "" { - tmp := viper.Get("alias") - if tmp != nil { - a := tmp.(map[string]interface{}) - tmp2 := a[alias] - if tmp2 != nil { - h = a[alias].(string) - } else { - fmt.Println("alias not found.") - return - } - } else { - fmt.Println("alias not found.") + if tmp, err := getAlias(alias); err != nil { + fmt.Println(err) return + } else { + h = tmp } } else { h = hash } - byteArray, _ := request(h) - t := &nemgo.TransactionMetadataPair{} - if err := json.Unmarshal(byteArray, t); err != nil { - fmt.Println("JSON Unmarshal error:", err) - return - } - if t.Meta.Hash.Data == h && t.Transaction.Recipient == address { - p := t.Transaction.Message.Payload - decoded, err := hex.DecodeString(p) - if err != nil { - fmt.Println(err) + + // GetFilePath + filePath := GetFilePath(h) + if Exists(filePath) == false { + byteArray, _ := request(h) + t := &nemgo.TransactionMetadataPair{} + if err := json.Unmarshal(byteArray, t); err != nil { + fmt.Println("JSON Unmarshal error:", err) + return } - if view == true { - fmt.Println(string(decoded)) - } else { - out, err := exec.Command("sh", "-c", string(decoded)).Output() + if t.Meta.Hash.Data == h && t.Transaction.Recipient == address { + p := t.Transaction.Message.Payload + decoded, err := hex.DecodeString(p) if err != nil { fmt.Println(err) } - fmt.Printf("%s", out) + var buf bytes.Buffer + buf.Write(decoded) + if err := ioutil.WriteFile(filePath, buf.Bytes(), 0644); err != nil { + fmt.Println("err:", err) + } } } + + if view == true { + out, err := exec.Command("cat", filePath).Output() + if err != nil { + fmt.Println(err) + } + fmt.Printf("%s\n", out) + } else { + str := strings.Join(args, " ") + out, err := exec.Command("sh", filePath, str).Output() + if err != nil { + fmt.Println(err) + } + fmt.Printf("%s", out) + } }, } @@ -110,3 +121,19 @@ func request(hash string) ([]byte, error) { byteArray, err := ioutil.ReadAll(resp.Body) return byteArray, err } + +func getAlias(key string) (string, error) { + tmp := viper.Get("alias") + if tmp != nil { + a := tmp.(map[string]interface{}) + tmp2 := a[key] + if tmp2 != nil { + h := a[key].(string) + return h, nil + } else { + return "", errors.New("alias not found.") + } + } else { + return "", errors.New("alias not found.") + } +} diff --git a/cmd/setAlias.go b/cmd/setAlias.go index 0a58cdd..5fe4d4a 100644 --- a/cmd/setAlias.go +++ b/cmd/setAlias.go @@ -40,7 +40,7 @@ nem.sh set-alias --hash b37685ca16474b6897550f51f008c11b1e24e93e51b5543d066d9266 h[name] = aHash viper.Set("alias", h) home, _ := homedir.Dir() - err := viper.WriteConfigAs(home + "/.nem.sh.json") + err := viper.WriteConfigAs(home + "/.nem.sh/config.json") if err != nil { fmt.Println(err) }