Skip to content

Commit 38638a5

Browse files
committed
Add kubeconfig and implement deleting a kubeconfig file
1 parent 7f575c9 commit 38638a5

File tree

8 files changed

+154
-1
lines changed

8 files changed

+154
-1
lines changed

cmd/generate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ var generateCmd = &cobra.Command{
2727
os.Exit(1)
2828
}
2929

30-
fmt.Printf("%+v\n", kubetmuxpCfg)
30+
kubetmuxpCfg.Process()
3131
},
3232
}
3333

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ module github.com/arunvelsriram/kube-tmuxp
22

33
require (
44
github.com/BurntSushi/toml v0.3.1 // indirect
5+
github.com/golang/mock v1.1.1
56
github.com/inconshreveable/mousetrap v1.0.0 // indirect
67
github.com/mitchellh/go-homedir v1.0.0
78
github.com/pmezard/go-difflib v1.0.0 // indirect
89
github.com/spf13/cobra v0.0.3
910
github.com/spf13/viper v1.2.1
1011
github.com/stretchr/testify v1.2.2
12+
golang.org/x/net v0.0.0-20181108082009-03003ca0c849 // indirect
1113
gopkg.in/yaml.v2 v2.2.1
1214
)

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
44
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
55
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
66
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
7+
github.com/golang/mock v1.1.1 h1:G5FRp8JnTd7RQH5kemVNlMeyXQAztQ3mOWV95KxsXH8=
8+
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
79
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
810
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
911
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
@@ -32,6 +34,8 @@ github.com/spf13/viper v1.2.1 h1:bIcUwXqLseLF3BDAZduuNfekWG87ibtFxi59Bq+oI9M=
3234
github.com/spf13/viper v1.2.1/go.mod h1:P4AexN0a+C9tGAnUFNwDMYYZv3pjFuvmeiMyKRaNVlI=
3335
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
3436
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
37+
golang.org/x/net v0.0.0-20181108082009-03003ca0c849 h1:FSqE2GGG7wzsYUsWiQ8MZrvEd1EOyU3NCF0AW3Wtltg=
38+
golang.org/x/net v0.0.0-20181108082009-03003ca0c849/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
3539
golang.org/x/sys v0.0.0-20180906133057-8cf3aee42992 h1:BH3eQWeGbwRU2+wxxuuPOdFBmaiBH81O8BugSjHeTFg=
3640
golang.org/x/sys v0.0.0-20180906133057-8cf3aee42992/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
3741
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=

pkg/filesystem/filesystem.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package filesystem
2+
3+
import "os"
4+
5+
// FileSystem abcd
6+
type FileSystem interface {
7+
Remove(file string) error
8+
}
9+
10+
type defaultFS struct{}
11+
12+
// Remove abcd
13+
func (d *defaultFS) Remove(file string) error {
14+
if err := os.Remove(file); err != nil {
15+
return err
16+
}
17+
18+
return nil
19+
}

pkg/internal/mock/filesystem.go

Lines changed: 45 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/kubeconfig/kubeconfig.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package kubeconfig
2+
3+
import (
4+
"github.com/arunvelsriram/kube-tmuxp/pkg/filesystem"
5+
)
6+
7+
// KubeConfig exposes methods to perform actions on kubeconfig
8+
type KubeConfig struct {
9+
filesystem filesystem.FileSystem
10+
}
11+
12+
// Delete deletes the given kubeconfig file
13+
func (k *KubeConfig) Delete(file string) error {
14+
if err := k.filesystem.Remove(file); err != nil {
15+
return err
16+
}
17+
18+
return nil
19+
}
20+
21+
// New returns a new KubeConfig
22+
func New(fs filesystem.FileSystem) KubeConfig {
23+
return KubeConfig{
24+
filesystem: fs,
25+
}
26+
}

pkg/kubeconfig/kubeconfig_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package kubeconfig_test
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/arunvelsriram/kube-tmuxp/pkg/filesystem"
8+
"github.com/arunvelsriram/kube-tmuxp/pkg/internal/mock"
9+
"github.com/arunvelsriram/kube-tmuxp/pkg/kubeconfig"
10+
"github.com/golang/mock/gomock"
11+
"github.com/stretchr/testify/assert"
12+
)
13+
14+
func TestNew(t *testing.T) {
15+
var fs filesystem.FileSystem
16+
cfg := kubeconfig.New(fs)
17+
18+
assert.NotNil(t, cfg)
19+
}
20+
21+
func TestDelete(t *testing.T) {
22+
t.Run("should delete given kubeconfig file from filesystem", func(t *testing.T) {
23+
ctrl := gomock.NewController(t)
24+
defer ctrl.Finish()
25+
mockFS := mock.NewFileSystem(ctrl)
26+
mockFS.EXPECT().Remove("kubeconfig.yaml").Return(nil)
27+
28+
cfg := kubeconfig.New(mockFS)
29+
err := cfg.Delete("kubeconfig.yaml")
30+
31+
assert.Nil(t, err)
32+
})
33+
34+
t.Run("should return error if kubeconfig file cannot be deleted", func(t *testing.T) {
35+
ctrl := gomock.NewController(t)
36+
defer ctrl.Finish()
37+
mockFS := mock.NewFileSystem(ctrl)
38+
mockFS.EXPECT().Remove("kubeconfig.yaml").Return(fmt.Errorf("some error"))
39+
40+
cfg := kubeconfig.New(mockFS)
41+
err := cfg.Delete("kubeconfig.yaml")
42+
43+
assert.EqualError(t, err, "some error")
44+
})
45+
}

pkg/kubetmuxp/kubetmuxp.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package kubetmuxp
22

33
import (
4+
"fmt"
45
"io"
56
"io/ioutil"
67

@@ -52,6 +53,17 @@ func (c *Config) Load() error {
5253
return nil
5354
}
5455

56+
// Process processes kube-tmuxp configs
57+
func (c *Config) Process() error {
58+
for _, project := range c.Projects {
59+
for _, cluster := range project.Clusters {
60+
fmt.Println(cluster)
61+
}
62+
}
63+
64+
return nil
65+
}
66+
5567
// NewConfig creates a new Config
5668
func NewConfig(reader io.Reader) Config {
5769
return Config{

0 commit comments

Comments
 (0)