Skip to content

Commit 11a0c93

Browse files
committed
Construct kube-tmuxp config object from config file
1 parent 0c99c1e commit 11a0c93

File tree

4 files changed

+56
-9
lines changed

4 files changed

+56
-9
lines changed

cmd/generate.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package cmd
22

33
import (
44
"fmt"
5+
"os"
56

7+
"github.com/arunvelsriram/kube-tmuxp/pkg/kubetmuxp"
68
"github.com/spf13/cobra"
79
)
810

@@ -11,7 +13,13 @@ var generateCmd = &cobra.Command{
1113
Aliases: []string{"gen"},
1214
Short: "Generates tmuxp configs for various Kubernetes contexts",
1315
Run: func(cmd *cobra.Command, args []string) {
14-
fmt.Println("Generate")
16+
kubetmuxpCfg, err := kubetmuxp.NewConfig(cfgFile)
17+
if err != nil {
18+
fmt.Println(err)
19+
os.Exit(1)
20+
}
21+
22+
fmt.Printf("%+v\n", kubetmuxpCfg)
1523
},
1624
}
1725

config.sample.yaml

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
- project: gcp-project-id
2-
clusters:
3-
- name: gke-cluster-name
4-
zone: zone # for zonal GKE clusters
5-
region: region # for regional GKE clusters
6-
context: name-to-be-used-for-this-context
7-
extra_envs:
8-
ENV_VARIABLE: value
1+
projects:
2+
- name: gcp-project-id
3+
clusters:
4+
- name: gke-cluster-name
5+
zone: zone # for zonal GKE clusters
6+
region: region # for regional GKE clusters
7+
context: name-to-be-used-for-this-context
8+
envs:
9+
ENV_VARIABLE: value
910

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ require (
88
github.com/spf13/cobra v0.0.3
99
github.com/spf13/viper v1.2.1
1010
github.com/stretchr/testify v1.2.2 // indirect
11+
gopkg.in/yaml.v2 v2.2.1
1112
)

pkg/kubetmuxp/kubetmuxp.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package kubetmuxp
2+
3+
import (
4+
"io/ioutil"
5+
6+
yaml "gopkg.in/yaml.v2"
7+
)
8+
9+
// Config represents kube-tmuxp config
10+
type Config struct {
11+
Projects []struct {
12+
Name string `yaml:"name"`
13+
Clusters []struct {
14+
Name string `yaml:"name"`
15+
Zone string `yaml:"zone"`
16+
Region string `yaml:"region"`
17+
Context string `yaml:"context"`
18+
Envs map[string]string `yaml:"envs"`
19+
}
20+
}
21+
}
22+
23+
// NewConfig constructs kube-tmuxp config from given file
24+
func NewConfig(cfgFile string) (Config, error) {
25+
var config Config
26+
data, err := ioutil.ReadFile(cfgFile)
27+
if err != nil {
28+
return config, err
29+
}
30+
31+
err = yaml.Unmarshal(data, &config)
32+
if err != nil {
33+
return config, err
34+
}
35+
36+
return config, nil
37+
}

0 commit comments

Comments
 (0)