forked from tomtruitt/eris-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eris.go
150 lines (129 loc) · 3.93 KB
/
eris.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package commands
import (
"fmt"
"io"
"os"
"github.com/eris-ltd/eris-cli/config"
"github.com/eris-ltd/eris-cli/definitions"
"github.com/eris-ltd/eris-cli/util"
"github.com/eris-ltd/eris-cli/version"
"github.com/eris-ltd/eris-cli/Godeps/_workspace/src/github.com/eris-ltd/common/go/ipfs"
"github.com/eris-ltd/eris-cli/Godeps/_workspace/src/github.com/eris-ltd/common/go/log"
"github.com/eris-ltd/eris-cli/Godeps/_workspace/src/github.com/spf13/cobra"
)
const VERSION = version.VERSION
// Defining the root command
var ErisCmd = &cobra.Command{
Use: "eris [command] [flags]",
Short: "The Blockchain Application Platform",
Long: `Eris is a platform for building, testing, maintaining, and operating
distributed applications with a blockchain backend. Eris makes it easy
and simple to wrangle the dragons of smart contract blockchains.
Made with <3 by Eris Industries.
Complete documentation is available at https://docs.erisindustries.com
` + "\nVersion:\n " + VERSION,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
var logLevel log.LogLevel
if do.Verbose {
logLevel = 2
} else if do.Debug {
logLevel = 3
}
log.SetLoggers(logLevel, config.GlobalConfig.Writer, config.GlobalConfig.ErrorWriter)
ipfs.IpfsHost = config.GlobalConfig.Config.IpfsHost
util.DockerConnect(do.Verbose, do.MachineName)
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
err := config.SaveGlobalConfig(config.GlobalConfig.Config)
if err != nil {
logger.Errorln(err)
}
log.Flush()
},
}
func Execute() {
InitializeConfig()
AddGlobalFlags()
AddCommands()
ErisCmd.Execute()
}
// Define the commands
func AddCommands() {
buildServicesCommand()
ErisCmd.AddCommand(Services)
buildChainsCommand()
ErisCmd.AddCommand(Chains)
buildContractsCommand()
ErisCmd.AddCommand(Contracts)
buildActionsCommand()
ErisCmd.AddCommand(Actions)
buildDataCommand()
ErisCmd.AddCommand(Data)
buildFilesCommand()
ErisCmd.AddCommand(Files)
// buildProjectsCommand()
// ErisCmd.AddCommand(Projects)
// buildRemotesCommand()
// ErisCmd.AddCommand(Remotes)
ErisCmd.AddCommand(ListKnown)
ErisCmd.AddCommand(ListExisting)
buildConfigCommand()
ErisCmd.AddCommand(Config)
ErisCmd.AddCommand(VerSion)
buildInitCommand()
ErisCmd.AddCommand(Init)
}
// Global Do struct
var do *definitions.Do
// Flags that are to be used by commands are handled by the Do struct
// Define the persistent commands (globals)
func AddGlobalFlags() {
ErisCmd.PersistentFlags().BoolVarP(&do.Verbose, "verbose", "v", false, "verbose output")
ErisCmd.PersistentFlags().BoolVarP(&do.Debug, "debug", "d", false, "debug level output")
ErisCmd.PersistentFlags().IntVarP(&do.Operations.ContainerNumber, "num", "n", 1, "container number")
ErisCmd.PersistentFlags().StringVarP(&do.MachineName, "machine", "", "eris", "machine name for docker-machine that is running VM")
}
func InitializeConfig() {
var err error
var out io.Writer
var erw io.Writer
do = definitions.NowDo()
if os.Getenv("ERIS_CLI_WRITER") != "" {
out, err = os.Open(os.Getenv("ERIS_CLI_WRITER"))
if err != nil {
fmt.Printf("Could not open: %s\n", err)
return
}
} else {
out = os.Stdout
}
if os.Getenv("ERIS_CLI_ERROR_WRITER") != "" {
erw, err = os.Open(os.Getenv("ERIS_CLI_ERROR_WRITER"))
if err != nil {
fmt.Printf("Could not open: %s\n", err)
return
}
} else {
erw = os.Stderr
}
config.GlobalConfig, err = config.SetGlobalObject(out, erw)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func ArgCheck(num int, comp string, cmd *cobra.Command, args []string) error {
switch comp {
case "eq":
if len(args) != num {
cmd.Help()
return fmt.Errorf("\n**Note** you sent our marmots the wrong number of arguments.\nPlease send the marmots %d arguments only.", num)
}
case "ge":
if len(args) < num {
cmd.Help()
return fmt.Errorf("\n**Note** you sent our marmots the wrong number of arguments.\nPlease send the marmots at least %d argument(s).", num)
}
}
return nil
}