/
main.go
186 lines (161 loc) · 5.78 KB
/
main.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
This file is part of the Gadget command-line tools.
Copyright (C) 2017 Next Thing Co.
Gadget is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
Gadget is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Gadget. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"errors"
"flag"
"github.com/nextthingco/libgadget"
gadgetFormatter "github.com/nextthingco/logrus-gadget-formatter"
log "gopkg.in/sirupsen/logrus.v1"
"os"
"path/filepath"
"strings"
)
type GadgetCommandFunc func([]string, *libgadget.GadgetContext) error
type GadgetCommand struct {
Name string
Function GadgetCommandFunc
NeedsConfig bool
}
var Commands = []GadgetCommand{
{Name: "init", Function: GadgetInit, NeedsConfig: false},
{Name: "add", Function: GadgetAdd, NeedsConfig: true},
{Name: "build", Function: GadgetBuild, NeedsConfig: true},
{Name: "deploy", Function: GadgetDeploy, NeedsConfig: true},
{Name: "flash", Function: GadgetFlash, NeedsConfig: true},
{Name: "edit", Function: GadgetEdit, NeedsConfig: true},
{Name: "start", Function: GadgetStart, NeedsConfig: true},
{Name: "stop", Function: GadgetStop, NeedsConfig: true},
{Name: "status", Function: GadgetStatus, NeedsConfig: true},
{Name: "delete", Function: GadgetDelete, NeedsConfig: true},
{Name: "purge", Function: GadgetPurge, NeedsConfig: false},
{Name: "shell", Function: GadgetShell, NeedsConfig: true},
{Name: "logs", Function: GadgetLogs, NeedsConfig: true},
{Name: "run", Function: GadgetRun, NeedsConfig: false},
{Name: "tag", Function: GadgetTag, NeedsConfig: true},
{Name: "version", Function: GadgetVersion, NeedsConfig: false},
{Name: "help", Function: GadgetHelp, NeedsConfig: false},
}
func GadgetVersion(args []string, g *libgadget.GadgetContext) error {
log.Infoln(filepath.Base(os.Args[0]))
log.Infof("version: %s", libgadget.Version)
log.Infof("built: %s", libgadget.BuildDate)
log.Infof("commit: %s", libgadget.GitCommit)
return nil
}
func GadgetHelp(args []string, g *libgadget.GadgetContext) error {
flag.Usage()
return nil
}
func FindCommand(name string) (*GadgetCommand, error) {
for _, cmd := range Commands {
if cmd.Name == name {
return &cmd, nil
}
}
return nil, errors.New("Failed to find command")
}
func main() {
// Hey, Listen!
// Everything that outputs needs to come after g.Verbose check!
flag.Usage = func() {
log.Info("")
log.Infof("USAGE: %s [options] COMMAND", filepath.Base(os.Args[0]))
log.Info("")
log.Info("Commands:")
log.Info(" init Initialize gadget project")
log.Info(" add Add new container to config")
log.Info(" edit Edit rootfs configuration")
log.Info(" build Build container[s] and/or rootfs")
log.Info(" deploy Deploy container[s]")
log.Info(" flash Flash rootfs to device running GadgetOS")
log.Info(" start Run container[s]")
log.Info(" stop Stop running container[s]")
log.Info(" status Fetch status of container[s]")
log.Info(" delete Remove container[s] from gadget")
log.Info(" shell Connect to remote device running GadgetOS")
log.Info(" purge Remove container[s], logs, and config from device running GadgetOS")
log.Info(" tag Tag built container[s] for config management")
log.Info(" logs Fetch logs(stdout) of container[s]")
log.Info(" version Print version information")
log.Info(" help Print this message")
log.Info("")
log.Infof("Run '%s COMMAND --help' for more information on the command", filepath.Base(os.Args[0]))
log.Info("")
log.Infof("Options:")
log.Info(" -C <path> ")
log.Info(" Run in directory (default \".\") ")
log.Info(" -v Verbose execution ")
log.Info("")
}
g := libgadget.GadgetContext{}
flag.BoolVar(&g.Verbose, "v", false, "Verbose execution")
flag.StringVar(&g.WorkingDirectory, "C", ".", "Run in directory")
flag.Parse()
var gFormatter *gadgetFormatter.TextFormatter
if g.Verbose {
gFormatter = new(gadgetFormatter.TextFormatter)
gFormatter.DisableColors = true
log.SetLevel(log.DebugLevel)
} else {
gFormatter = new(gadgetFormatter.TextFormatter)
gFormatter.DisableColors = true
gFormatter.DisableTimestamp = true
gFormatter.DisableSorting = true
gFormatter.EntryString.InfoLevelString = "I:"
gFormatter.EntryString.WarnLevelString = "W:"
gFormatter.EntryString.ErrorLevelString = "E:"
log.SetLevel(log.InfoLevel)
}
log.SetFormatter(gFormatter)
// Hey, Listen!
// Everything that outputs needs to come after g.Verbose check!
err := libgadget.RequiredSsh()
if err != nil {
log.Error("Failed to verify ssh requirements")
os.Exit(1)
}
args := flag.Args()
if len(args) < 1 {
flag.Usage()
log.Error("No Command Specified")
os.Exit(1)
}
// file command
cmd, err := FindCommand(args[0])
if err != nil {
flag.Usage()
log.WithFields(log.Fields{
"command": strings.Join(args[0:], " "),
}).Debug("Command is not valid")
log.Errorf("Command %s is not valid", args[0:])
os.Exit(1)
}
// if command needs to use the config file, load it
if cmd.NeedsConfig {
err = g.LoadConfig()
if err != nil {
log.Error("Failed to load config")
log.Warn("Be sure to run gadget in the same directory as 'gadget.yml'")
log.Warn("Or specify a directory e.g. 'gadget -C ../projects/gpio/ [command]'")
os.Exit(1)
}
}
err = cmd.Function(args[1:], &g)
if err != nil {
os.Exit(1)
}
os.Exit(0)
}