-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopilot.go
More file actions
68 lines (56 loc) · 1.4 KB
/
copilot.go
File metadata and controls
68 lines (56 loc) · 1.4 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
package copilot
import (
"github.com/eirwin/copilot/pkg/k8s"
"github.com/eirwin/copilot/pkg/resource"
)
type Service interface {
Run(cmd Command) (string, error)
}
type service struct {
kubernetes k8s.Kubernetes
}
func NewService(kubernetes k8s.Kubernetes) Service {
return service{
kubernetes: kubernetes,
}
}
func (s service) Run(cmd Command) (string, error) {
if cmd.Resource == "" {
return "", NewErrInvalidRunArgument("resource")
}
// the following considers the exceptional case that the
// namespace will not be specified when resource is namespace
if cmd.Namespace == "" && cmd.Resource != "namespaces" {
return "", NewErrInvalidRunArgument("namespace")
}
factory := resource.NewRequestFactory(s.kubernetes)
var result resource.Result
switch cmd.Operation {
case "get":
result = factory.NewRequest(cmd.Resource, cmd.Namespace).Get(nil)
break
case "logs":
result = factory.NewRequest(cmd.Resource, cmd.Namespace).Logs(nil)
break
case "status":
result = factory.NewRequest(cmd.Resource, cmd.Namespace).Status(nil)
break
}
output, err := formatOutput(result, cmd.Output)
if err != nil {
return "", err
}
return output, nil
}
func formatOutput(result resource.Result, format string) (string, error) {
var output string
switch format {
case "json":
return result.JSON()
case "columns":
return result.Columns()
default:
return result.Columns()
}
return output, nil
}