/
access.go
101 lines (81 loc) · 2.66 KB
/
access.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
package controller
import (
"github.com/kintoproj/kintohub/cli/internal/api"
"github.com/kintoproj/kintohub/cli/internal/config"
"github.com/kintoproj/kintohub/cli/internal/utils"
)
//needs to be run from a local git repo.
//it is the parent function for `kinto access` command. Allows to port-forward all services in an env
//as long as the env has atleast one service belonging to the local git repo.
func (c *Controller) Access() {
utils.StartSpinner()
envDetails := c.GetAvailableEnvNames(true)
utils.StopSpinner()
selectedEnvId := SelectionPrompt(envDetails)
utils.StartSpinner()
blocksToForward := c.GetBlocksToForward(selectedEnvId)
if len(blocksToForward) > 0 {
utils.StopSpinner()
c.api.StartAccess(blocksToForward, selectedEnvId)
} else {
utils.WarningMessage("No service/s found in this environment to teleport into!")
}
}
//sub-function of the `access` command, called when using `kinto env access`.
//allows port-forwarding whole environments.
func (c *Controller) EnvironmentAccess(envId ...string) {
utils.StartSpinner()
var selectedEnvId string
var blocksToForward []api.RemoteConfig
if len(envId) == 0 {
envDetails := c.GetAvailableEnvNames(false)
utils.StopSpinner()
selectedEnvId = SelectionPrompt(envDetails)
utils.StartSpinner()
blocksToForward = c.GetBlocksToForward(selectedEnvId)
} else {
env := c.GetEnvWithId(envId[0])
blocksToForward = c.GetBlocksToForward(env.Id)
selectedEnvId = env.Id
}
if len(blocksToForward) != 0 {
utils.StopSpinner()
c.api.StartAccess(blocksToForward, selectedEnvId)
} else {
utils.WarningMessage("No Accessible service/s found!")
}
}
//sub-function of the `access` command called when using `kinto svs access`.
//allows port-forwarding a particular service.
func (c *Controller) ServiceAccess(envId string, blockId string) {
utils.StartSpinner()
var blocksToForward []api.RemoteConfig
env := c.GetEnvWithId(envId)
blocks, err := c.api.GetBlocks(env.Id)
if err != nil {
utils.TerminateWithError(err)
return
}
for _, block := range blocks {
if block.Id == blockId {
latestRelease := utils.GetLatestSuccessfulRelease(block.Releases)
if utils.CanPortForwardToRelease(latestRelease) &&
utils.CheckIfPortOpened(config.DefaultClientAccessPort, true) {
remote := api.RemoteConfig{
FromHost: "localhost",
FromPort: config.DefaultClientAccessPort,
ToHost: block.Name,
ToPort: utils.GetBlockPort(block.Name),
}
blocksToForward = append(blocksToForward, remote)
}
break
}
}
if len(blocksToForward) != 0 {
utils.StopSpinner()
c.api.StartAccess(blocksToForward, env.Id)
} else {
utils.WarningMessage("No Accessible service/s found!")
}
}