This repository has been archived by the owner on Mar 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
env.go
131 lines (101 loc) · 3.29 KB
/
env.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
// Copyright 2017 The kubecfg authors
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package kubecfg
import (
"fmt"
"io"
"sort"
"strings"
log "github.com/sirupsen/logrus"
"github.com/ksonnet/kubecfg/metadata"
)
type EnvAddCmd struct {
name string
uri string
spec metadata.ClusterSpec
manager metadata.Manager
}
func NewEnvAddCmd(name, uri, specFlag string, manager metadata.Manager) (*EnvAddCmd, error) {
spec, err := metadata.ParseClusterSpec(specFlag)
if err != nil {
return nil, err
}
log.Debugf("Generating ksonnetLib data with spec: %s", specFlag)
return &EnvAddCmd{name: name, uri: uri, spec: spec, manager: manager}, nil
}
func (c *EnvAddCmd) Run() error {
return c.manager.CreateEnvironment(c.name, c.uri, c.spec)
}
// ==================================================================
type EnvRmCmd struct {
name string
manager metadata.Manager
}
func NewEnvRmCmd(name string, manager metadata.Manager) (*EnvRmCmd, error) {
return &EnvRmCmd{name: name, manager: manager}, nil
}
func (c *EnvRmCmd) Run() error {
return c.manager.DeleteEnvironment(c.name)
}
// ==================================================================
type EnvListCmd struct {
manager metadata.Manager
}
func NewEnvListCmd(manager metadata.Manager) (*EnvListCmd, error) {
return &EnvListCmd{manager: manager}, nil
}
func (c *EnvListCmd) Run(out io.Writer) error {
envs, err := c.manager.GetEnvironments()
if err != nil {
return err
}
// Sort environments by ascending alphabetical name
sort.Slice(envs, func(i, j int) bool { return envs[i].Name < envs[j].Name })
// Format each environment information for pretty printing.
// Each environment should be outputted like the following:
//
// us-west/dev localhost:8080
// us-west/staging http://example.com
//
// To accomplish this, need to find the longest env name for proper padding.
maxNameLen := 0
for _, env := range envs {
if l := len(env.Name); l > maxNameLen {
maxNameLen = l
}
}
lines := []string{}
for _, env := range envs {
nameSpacing := strings.Repeat(" ", maxNameLen-len(env.Name)+1)
lines = append(lines, env.Name+nameSpacing+env.URI+"\n")
}
formattedEnvsList := strings.Join(lines, "")
_, err = fmt.Fprint(out, formattedEnvsList)
return err
}
// ==================================================================
type EnvSetCmd struct {
name string
desiredName string
desiredURI string
manager metadata.Manager
}
func NewEnvSetCmd(name, desiredName, desiredURI string, manager metadata.Manager) (*EnvSetCmd, error) {
return &EnvSetCmd{name: name, desiredName: desiredName, desiredURI: desiredURI, manager: manager}, nil
}
func (c *EnvSetCmd) Run() error {
desired := metadata.Environment{Name: c.desiredName, URI: c.desiredURI}
return c.manager.SetEnvironment(c.name, &desired)
}