-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathroot.go
More file actions
103 lines (83 loc) · 2.69 KB
/
root.go
File metadata and controls
103 lines (83 loc) · 2.69 KB
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
package command
import (
"context"
"fmt"
"github.com/spf13/cobra"
"github.com/basecamp/once/internal/docker"
"github.com/basecamp/once/internal/logging"
"github.com/basecamp/once/internal/ui"
)
type RootCommand struct {
cmd *cobra.Command
namespace string
installImageRef string
}
func NewRootCommand() *RootCommand {
r := &RootCommand{}
r.cmd = &cobra.Command{
Use: "once",
Short: "Manage web applications from Docker images",
SilenceUsage: true,
CompletionOptions: cobra.CompletionOptions{
HiddenDefaultCmd: true,
},
RunE: WithNamespace(func(ctx context.Context, ns *docker.Namespace, cmd *cobra.Command, args []string) error {
return logging.ToLogFile(func() error {
return ui.Run(ns, r.installImageRef)
})
}),
}
r.cmd.PersistentFlags().StringVarP(&r.namespace, "namespace", "n", docker.DefaultNamespace, "namespace for containers")
r.cmd.Flags().StringVar(&r.installImageRef, "install", "", "Path to Docker image to install")
r.cmd.AddCommand(newBackgroundCommand().cmd)
r.cmd.AddCommand(newBackupCommand().cmd)
r.cmd.AddCommand(newDeployCommand().cmd)
r.cmd.AddCommand(newListCommand().cmd)
r.cmd.AddCommand(newRemoveCommand().cmd)
r.cmd.AddCommand(newRestoreCommand().cmd)
r.cmd.AddCommand(newStartCommand().cmd)
r.cmd.AddCommand(newStopCommand().cmd)
r.cmd.AddCommand(newTeardownCommand().cmd)
r.cmd.AddCommand(newUpdateCommand().cmd)
r.cmd.AddCommand(newVersionCommand().cmd)
return r
}
func (r *RootCommand) Execute() error {
return r.cmd.Execute()
}
// Helpers
type NamespaceRunE func(ctx context.Context, ns *docker.Namespace, cmd *cobra.Command, args []string) error
func withApplication(ns *docker.Namespace, name string, action string, fn func(*docker.Application) error) error {
app := ns.Application(name)
if app == nil {
return fmt.Errorf("application %q not found", name)
}
if err := fn(app); err != nil {
return fmt.Errorf("%s application: %w", action, err)
}
return nil
}
func WithNamespace(fn NamespaceRunE) func(*cobra.Command, []string) error {
return func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
ns, err := docker.RestoreNamespace(ctx, namespaceFlag(cmd))
if err != nil {
return fmt.Errorf("restoring namespace: %w", err)
}
return fn(ctx, ns, cmd, args)
}
}
func namespaceFlag(cmd *cobra.Command) string {
namespace, _ := cmd.Root().PersistentFlags().GetString("namespace")
return namespace
}
func printDeployProgress(p docker.DeployProgress) {
switch p.Stage {
case docker.DeployStageDownloading:
fmt.Printf("Downloading: %d%%\n", p.Percentage)
case docker.DeployStageStarting:
fmt.Println("Starting...")
case docker.DeployStageFinished:
fmt.Println("Finished")
}
}