Skip to content

Commit

Permalink
Merge pull request #1 from xetys/master
Browse files Browse the repository at this point in the history
sync
  • Loading branch information
gadelkareem authored Nov 16, 2018
2 parents 533c546 + 908dcf6 commit 573a284
Show file tree
Hide file tree
Showing 21 changed files with 268 additions and 118 deletions.
25 changes: 25 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
linters-settings:
govet:
check-shadowing: true
golint:
min-confidence: 0
gocyclo:
min-complexity: 10
maligned:
suggest-new: true
dupl:
threshold: 100
goconst:
min-len: 2
min-occurrences: 2
misspell:
locale: US

linters:
enable-all: true
disable:
- dupl
- errcheck
- gocyclo
- govet
- lll
19 changes: 3 additions & 16 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ go:

before_install:
- go get -u github.com/golang/dep/cmd/dep
- go get -u golang.org/x/lint/golint
- go get -u honnef.co/go/tools/...
- go get -u mvdan.cc/unparam
- go get -u github.com/client9/misspell/cmd/misspell
- dep ensure

after_script:
Expand All @@ -30,29 +26,20 @@ script:

jobs:
include:
- stage: Linting
script:
- golint $(go list ./...)
- unused $(go list ./...)
- gosimple $(go list ./...)
- misspell -error $(git ls-files | grep -v vendor/)
- unparam $(go list ./...)
- staticcheck $(go list ./...)
go: 1.11.x

- stage: E2E testing
script:
- go build
- make preparare
- make test-all
- make cleanup
go: 1.11.x
if: fork = false

- stage: Build
script:
- echo "Build binary & deploying to GitHub releases ..."
- make build
go: 1.10.x
go: 1.11.x
if: tag IS present

deploy:
provider: releases
Expand Down
6 changes: 5 additions & 1 deletion cmd/cluster_add_external_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,11 @@ An external server must meet the following requirements:
FatalOnError(err)
externalNode.Name = hostname

cidrPrefix := clustermanager.PrivateIPPrefix(cluster.NodeCIDR)
cidrPrefix, err := clustermanager.PrivateIPPrefix(cluster.NodeCIDR)
if err != nil {
log.Fatal(err)
}

// render internal IP address
nextNode := 21
outer:
Expand Down
2 changes: 1 addition & 1 deletion cmd/cluster_addon_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var clusterAddonListCmd = &cobra.Command{
tw.Init(os.Stdout, 0, 8, 2, '\t', 0)
fmt.Fprintln(tw, "NAME\tREQUIRES\tDESCRIPTION\tURL")

cluster := &clustermanager.Cluster{Nodes: []clustermanager.Node{clustermanager.Node{IsMaster: true}}}
cluster := &clustermanager.Cluster{Nodes: []clustermanager.Node{{IsMaster: true}}}
provider := hetzner.NewHetznerProvider(AppConf.Context, AppConf.Client, *cluster, AppConf.CurrentContext.Token)
addonService := addons.NewClusterAddonService(provider, AppConf.SSHClient)
for _, addon := range addonService.Addons() {
Expand Down
4 changes: 2 additions & 2 deletions cmd/cluster_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
package cmd

import (
"errors"
"fmt"
"log"

"errors"
"github.com/spf13/cobra"
"log"
)

// clusterDeleteCmd represents the clusterDelete command
Expand Down
1 change: 0 additions & 1 deletion cmd/cluster_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package cmd

import (
"fmt"

"os"
"text/tabwriter"

Expand Down
3 changes: 2 additions & 1 deletion cmd/cluster_remove_external_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ package cmd
import (
"errors"
"fmt"
"log"

"github.com/spf13/cobra"
"github.com/xetys/hetzner-kube/pkg/clustermanager"
"log"
)

// clusterAddWorkerCmd represents the clusterAddWorker command
Expand Down
32 changes: 19 additions & 13 deletions cmd/config_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package cmd
package cmd_test

import (
"testing"

"github.com/xetys/hetzner-kube/cmd"
"github.com/xetys/hetzner-kube/pkg/clustermanager"
)

const (
firstContext string = "first-context"
secondContext string = "second-context"
)

func TestHetznerConfig_FindSSHKeyByName(t *testing.T) {
config := getCloudProvider()
tests := []string{
Expand Down Expand Up @@ -64,15 +70,15 @@ func TestHetznerConfig_DeleteNonExistingSSHKey(t *testing.T) {
func TestAppConfig_FindContextByName(t *testing.T) {
config := getAppConfig()
tests := []string{
"first-context",
firstContext,
"non-existing",
}
for _, test := range tests {
t.Run(test, func(t *testing.T) {
_, err := config.FindContextByName(test)

switch test {
case "first-context", "second-context":
case firstContext, secondContext:
if err != nil {
t.Errorf("unexpected error for context %s", test)
}
Expand All @@ -87,26 +93,26 @@ func TestAppConfig_FindContextByName(t *testing.T) {
func TestAppConfig_SwitchContextByName(t *testing.T) {
config := getAppConfig()

config.SwitchContextByName("second-context")
config.SwitchContextByName(secondContext)

if config.CurrentContext.Name != "second-context" {
if config.CurrentContext.Name != secondContext {
t.Error("could not switch context")
}
}

func getAppConfig() AppConfig {
return AppConfig{
Config: &HetznerConfig{
Contexts: []HetznerContext{
{Name: "first-context"},
{Name: "second-context"},
func getAppConfig() cmd.AppConfig {
return cmd.AppConfig{
Config: &cmd.HetznerConfig{
Contexts: []cmd.HetznerContext{
{Name: firstContext},
{Name: secondContext},
},
},
}
}

func getCloudProvider() HetznerConfig {
return HetznerConfig{
func getCloudProvider() cmd.HetznerConfig {
return cmd.HetznerConfig{
SSHKeys: []clustermanager.SSHKey{
{Name: "test-key1"},
{Name: "test-key2"},
Expand Down
1 change: 1 addition & 0 deletions cmd/context_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

Expand Down
4 changes: 2 additions & 2 deletions cmd/context_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ package cmd

import (
"fmt"

"github.com/spf13/cobra"
"os"
"text/tabwriter"

"github.com/spf13/cobra"
)

// listCmd represents the list command
Expand Down
1 change: 1 addition & 0 deletions cmd/context_use.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

Expand Down
51 changes: 31 additions & 20 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,7 @@ func Execute() {
func init() {
cobra.OnInitialize(initConfig)

// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.hetzner-kube.yaml)")

// Cobra also supports local flags, which will only run
// when this action is called directly.
// rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file to use")
rootCmd.PersistentFlags().BoolP("debug", "d", false, "debug mode")
}

Expand All @@ -74,23 +67,41 @@ func initConfig() {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

// Search config in home directory with name ".hetzner-kube" (without extension).
viper.AddConfigPath(home)
viper.SetConfigName(".hetzner-kube")

setConfigDirectory()
}

viper.AutomaticEnv() // read in environment variables that match
// read in environment variables that match
viper.AutomaticEnv()

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
}

func setConfigDirectory() {
// Find config dir based on XDG Base Directory Specification
// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
xdgConfig := os.Getenv("XDG_CONFIG_HOME")
if xdgConfig != "" {
viper.AddConfigPath(xdgConfig)
}

// Failback to home directory
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
}

if err == nil {
viper.AddConfigPath(home)
}

if xdgConfig == "" && err != nil {
fmt.Println("Unable to detect any config location, please specify it with --config flag")
os.Exit(1)
}

// Search config directory with name ".hetzner-kube" (without extension).
viper.SetConfigName(".hetzner-kube")
}
14 changes: 7 additions & 7 deletions cmd/ssh_key_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@
package cmd

import (
"fmt"

"bytes"
"errors"
"github.com/hetznercloud/hcloud-go/hcloud"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/xetys/hetzner-kube/pkg/clustermanager"
"golang.org/x/crypto/ssh"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"

"github.com/hetznercloud/hcloud-go/hcloud"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/xetys/hetzner-kube/pkg/clustermanager"
"golang.org/x/crypto/ssh"
)

// sshKeyAddCmd represents the sshKeyAdd command
Expand Down
4 changes: 2 additions & 2 deletions cmd/ssh_key_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
package cmd

import (
"errors"
"fmt"
"log"

"errors"
"github.com/spf13/cobra"
"log"
)

// sshKeyDeleteCmd represents the sshKeyDelete command
Expand Down
4 changes: 2 additions & 2 deletions cmd/ssh_key_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ package cmd

import (
"fmt"

"github.com/spf13/cobra"
"os"
"text/tabwriter"

"github.com/spf13/cobra"
)

// sshKeyListCmd represents the sshKeyList command
Expand Down
Loading

0 comments on commit 573a284

Please sign in to comment.