Skip to content

Commit

Permalink
add cash and args
Browse files Browse the repository at this point in the history
  • Loading branch information
scrpgil committed May 30, 2018
1 parent 1b921f9 commit a1bac0d
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 43 deletions.
58 changes: 44 additions & 14 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down Expand Up @@ -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")
Expand All @@ -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()
Expand All @@ -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
}
83 changes: 55 additions & 28 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@
package cmd

import (
"bytes"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"github.com/myndshft/nemgo"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"io/ioutil"
"net/http"
"os/exec"
"strings"
)

var hash string
Expand All @@ -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)
}
},
}

Expand All @@ -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.")
}
}
2 changes: 1 addition & 1 deletion cmd/setAlias.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit a1bac0d

Please sign in to comment.