Skip to content

Commit

Permalink
Merge 996972f into 58db4c6
Browse files Browse the repository at this point in the history
  • Loading branch information
smoliji committed Jul 18, 2020
2 parents 58db4c6 + 996972f commit 4c95fe2
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Changed
- History does not contain duplicate items

### Fixed
- Invalid history records with pod name `<none>`

## [1.2.0] - 2020-06-16
### Added
Expand Down
15 changes: 15 additions & 0 deletions internal/history/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ func StoreCloudSQLProxy(projectID string, instance sqlproxy.CloudSQLInstance, lo
store.Append(KeyCommands, record)
}

func deduplicate(commands []string) []string {
uniqueCommand := make(map[string]string)
// Gotta have a separate struct for results to maintain ordering https://blog.golang.org/maps#TOC_7.
uniqueCommands := []string{}
for _, command := range commands {
if uniqueCommand[command] == "" {
uniqueCommands = append(uniqueCommands, command)
uniqueCommand[command] = command
}
}
store.Set(KeyCommands, uniqueCommands)
return uniqueCommands
}

// Browse lets user choose from stored commands.
// Goproxie is executed with given arguments.
func Browse() {
Expand All @@ -43,6 +57,7 @@ func Browse() {
commands = append(commands, fmt.Sprint(item))
}
}
commands = deduplicate(commands)

if len(commands) == 0 {
fmt.Println("History is empty")
Expand Down
8 changes: 7 additions & 1 deletion internal/kubectl/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ func PodsList(namespace string) []*Pod {
ports = append(ports, port)
}
}
pods = append(pods, &Pod{Name: tokens[0], Containers: containers, ContainerPorts: ports, AppLabel: tokens[3]})
name := tokens[0]
appLabel := tokens[3]
// `<none>` is empty serialization value from kubectl
if appLabel == "<none>" {
appLabel = name
}
pods = append(pods, &Pod{Name: name, Containers: containers, ContainerPorts: ports, AppLabel: appLabel})
}
return pods
}
Expand Down
11 changes: 10 additions & 1 deletion internal/kubectl/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "testing"

// Should contain <none> ports and multi-ports
var mockPodsList = `acme-rockets-v0.3.0-74bf544f8b-lzc5b event-exporter,prometheus-to-sd-exporter <none> acme-rockets
acme-finances-0 event-exporter <none> <none>
metrics-server-v0.3.3-6d96fcc55-2qtm8 metrics-server,metrics-server-nanny 443 metrics-server
traefik-ig-7646cb565d-9zxv6 traefik 80,443,8080,8081 traefik-ig
`
Expand Down Expand Up @@ -49,14 +50,22 @@ func TestPodsList(t *testing.T) {
result := PodsList("anynamespace")
expectedItems := []*Pod{
{
Name: "acme-rockets-v0.3.0-74bf544f8b-lzc5b",
Name: "acme-rockets-v0.3.0-74bf544f8b-lzc5b",
ContainerPorts: []int{},
Containers: []string{
"event-exporter",
"prometheus-to-sd-exporter",
},
AppLabel: "acme-rockets",
},
{
Name: "acme-finances-0",
ContainerPorts: []int{},
Containers: []string{
"event-exporter",
},
AppLabel: "acme-finances-0",
},
{
Name: "metrics-server-v0.3.3-6d96fcc55-2qtm8",
ContainerPorts: []int{443},
Expand Down

0 comments on commit 4c95fe2

Please sign in to comment.