Add config CLI command and encrypt/decrypt sub-commands (#77)
This commit is contained in:
1 parent
8de5371499
commit
8afcbd4c8e
12 files changed
+509
-49
No files matched your search
+24
-38
@@ -1,17 +1,18 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"github.com/hashicorp/watchtower/internal/cmd/base"
|
||||
"github.com/hashicorp/watchtower/internal/cmd/commands/config"
|
||||
"github.com/hashicorp/watchtower/internal/cmd/commands/controller"
|
||||
"github.com/hashicorp/watchtower/internal/cmd/commands/dev"
|
||||
"github.com/hashicorp/watchtower/internal/cmd/commands/hosts"
|
||||
"github.com/hashicorp/watchtower/internal/cmd/commands/scopes"
|
||||
"github.com/hashicorp/watchtower/internal/cmd/commands/worker"
|
||||
|
||||
"github.com/mitchellh/cli"
|
||||
)
|
||||
|
||||
@@ -19,28 +20,12 @@ import (
|
||||
var Commands map[string]cli.CommandFactory
|
||||
|
||||
func initCommands(ui, serverCmdUi cli.Ui, runOpts *RunOptions) {
|
||||
getBaseCommand := func() *base.Command {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
ret := &base.Command{
|
||||
UI: ui,
|
||||
ShutdownCh: MakeShutdownCh(),
|
||||
Context: ctx,
|
||||
}
|
||||
|
||||
go func() {
|
||||
<-ret.ShutdownCh
|
||||
cancel()
|
||||
}()
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
Commands = map[string]cli.CommandFactory{
|
||||
"controller": func() (cli.Command, error) {
|
||||
return &controller.Command{
|
||||
Server: base.NewServer(&base.Command{
|
||||
UI: serverCmdUi,
|
||||
ShutdownCh: MakeShutdownCh(),
|
||||
ShutdownCh: base.MakeShutdownCh(),
|
||||
}),
|
||||
SighupCh: MakeSighupCh(),
|
||||
SigUSR2Ch: MakeSigUSR2Ch(),
|
||||
@@ -50,7 +35,7 @@ func initCommands(ui, serverCmdUi cli.Ui, runOpts *RunOptions) {
|
||||
return &worker.Command{
|
||||
Server: base.NewServer(&base.Command{
|
||||
UI: serverCmdUi,
|
||||
ShutdownCh: MakeShutdownCh(),
|
||||
ShutdownCh: base.MakeShutdownCh(),
|
||||
}),
|
||||
SighupCh: MakeSighupCh(),
|
||||
SigUSR2Ch: MakeSigUSR2Ch(),
|
||||
@@ -60,7 +45,7 @@ func initCommands(ui, serverCmdUi cli.Ui, runOpts *RunOptions) {
|
||||
return &dev.Command{
|
||||
Server: base.NewServer(&base.Command{
|
||||
UI: serverCmdUi,
|
||||
ShutdownCh: MakeShutdownCh(),
|
||||
ShutdownCh: base.MakeShutdownCh(),
|
||||
}),
|
||||
SighupCh: MakeSighupCh(),
|
||||
SigUSR2Ch: MakeSigUSR2Ch(),
|
||||
@@ -68,37 +53,38 @@ func initCommands(ui, serverCmdUi cli.Ui, runOpts *RunOptions) {
|
||||
},
|
||||
"hosts create": func() (cli.Command, error) {
|
||||
return &hosts.CreateCommand{
|
||||
Command: getBaseCommand(),
|
||||
Command: base.New(ui),
|
||||
}, nil
|
||||
},
|
||||
"projects create": func() (cli.Command, error) {
|
||||
return &scopes.CreateProjectCommand{
|
||||
Command: getBaseCommand(),
|
||||
Command: base.New(ui),
|
||||
}, nil
|
||||
},
|
||||
"projects read": func() (cli.Command, error) {
|
||||
return &scopes.ReadProjectCommand{
|
||||
Command: getBaseCommand(),
|
||||
Command: base.New(ui),
|
||||
}, nil
|
||||
},
|
||||
"config": func() (cli.Command, error) {
|
||||
return &config.Command{
|
||||
Command: base.New(ui),
|
||||
}, nil
|
||||
},
|
||||
"config encrypt": func() (cli.Command, error) {
|
||||
return &config.EncryptDecryptCommand{
|
||||
Command: base.New(ui),
|
||||
Encrypt: true,
|
||||
}, nil
|
||||
},
|
||||
"config decrypt": func() (cli.Command, error) {
|
||||
return &config.EncryptDecryptCommand{
|
||||
Command: base.New(ui),
|
||||
}, nil
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// MakeShutdownCh returns a channel that can be used for shutdown
|
||||
// notifications for commands. This channel will send a message for every
|
||||
// SIGINT or SIGTERM received.
|
||||
func MakeShutdownCh() chan struct{} {
|
||||
resultCh := make(chan struct{})
|
||||
|
||||
shutdownCh := make(chan os.Signal, 4)
|
||||
signal.Notify(shutdownCh, os.Interrupt, syscall.SIGTERM)
|
||||
go func() {
|
||||
<-shutdownCh
|
||||
close(resultCh)
|
||||
}()
|
||||
return resultCh
|
||||
}
|
||||
|
||||
// MakeSighupCh returns a channel that can be used for SIGHUP
|
||||
// reloading. This channel will send a message for every
|
||||
// SIGHUP received.
|
||||
|
||||
Reference in new issue
Block a user